0

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();
}

1 Answer 1

1

Take a look at the SimpleTimer [http://playground.arduino.cc/Code/SimpleTimer] library in the Arduino IDE environment.

You can have timers to:

  • call a function at a consistent interval,

  • call a function once after a set delay,

  • call a function a set number of times with a consistent interval between.

2
  • Looks great! It works fine but I cannot get the timer to stop, I can name the time and disable it I guess but the documentation does not show how to type the variable that holds the timer. something like this? int blinky = timer.setInterval(1000, RepeatTask); Commented Nov 2, 2016 at 21:11
  • The type of the timerId is int. You could call timer.disable(blinky); to turn off one repeating timer. Commented Nov 3, 2016 at 0:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.