What I would like to do is control my garden lights using the ESP8266. For this I use the Arduino approach from here. This works great, I use bootstrap and jquery even :D all is well!
Switching lights on and off using my code is fine but now I'd like to switch some continues routines, like blinking. I can start the blinking from the webpage but this code is a loop so I cannot get back to turn it off again.
I could make it blink for a set amount of loops, so eventually it will stop blinking and show the control webpage again.
Or I can add the blinking to the hardware, maybe use a 555 for the first time?
But there might be a better way of implementing this..
Using Google I found information about Break and Interrupts, not yet what I am looking for.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char *ssid = "mynetwork";
const char *password = "mypassword";
ESP8266WebServer server ( 80 );
const int led = 5;
String webPage =
"<!DOCTYPE html>\
<html lang='en'>\
<head>\
<title>Funny Bunny Control</title>\
<meta charset='utf-8'>\
<meta name='viewport' content='width=device-width, initial-scale=1'>\
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>\
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>\
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>\
</head>\
<body>\
<div class='container-fluid'>\
<div class='page-header'>\
<h1>Funny Bunny v0.1</h1>\
</div>\
<p class='bg-info'>Omdat snuffelen er bij hoort.</p>\
<p class='bg-warning'>Niet voeren aub.</p>\
<div class='row'>\
<div class='col-sm-4'><a href='/aan' class='btn btn-info btn-block' role='button'>aan</a></div>\
<div class='col-sm-4'><a href='/uit' class='btn btn-warning btn-block' role='button'>uit</a></div>\
</div>\
</div>\
</body>\
</html>";
void handleRoot() {
server.send(200, "text/html", webPage);
}
void aan() {
digitalWrite ( led, 1 );
server.send(200, "text/html", webPage);
}
void uit() {
digitalWrite ( led, 0 );
server.send(200, "text/html", webPage);
}
void setup ( void ) {
pinMode ( led, OUTPUT );
digitalWrite ( led, 0 );
Serial.begin ( 115200 );
WiFi.begin ( ssid, password );
Serial.println ( "" );
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
if ( MDNS.begin ( "esp8266" ) ) {
Serial.println ( "MDNS responder started" );
}
server.on ( "/", handleRoot );
server.on ( "/aan", aan );
server.on ( "/uit", uit );
server.onNotFound ( handleRoot );
server.begin();
Serial.println ( "HTTP server started" );
}
void loop ( void ) {
server.handleClient();
}