-->
Page 1 of 10

Toggle LED On / Off from Web -- Example?

PostPosted: Sat Jun 27, 2015 5:09 pm
by Stephen
Do we have a Arduino based sketch available for ESP8266 like the video below? I found this Arduinno + Ethernet tutorial we can probably convert.

Arduino + Ethernet Sketch
https://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/


Re: Toggle LED On / Off from Web -- Example?

PostPosted: Sun Jun 28, 2015 4:01 pm
by SwiCago
Sure, look at my captive portal example. It acts as AP, but is easy to change into a client of another AP

viewtopic.php?p=21907#p21907

Re: Toggle LED On / Off from Web -- Example?

PostPosted: Sun Jun 28, 2015 9:54 pm
by Stephen
Thanks SwiCago -- I ended up using the Webserver "HelloServer" example with modifications.

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const int RELAY_PIN = 2; //RELAY
 
const char* ssid = "...";
const char* password = "...";
MDNSResponder mdns;
ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/plain", "hello from esp8266!");
}

void handleNotFound(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}
 
void setup(void){
  pinMode(RELAY_PIN, OUTPUT);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    digitalWrite(RELAY_PIN, 1);
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  digitalWrite(RELAY_PIN, 0);
 
  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }
 
  server.on("/", handleRoot);
 
  server.on("/lightsON", [](){
  server.send(200, "text/plain", "Okay -- Light is ON!");
  digitalWrite(RELAY_PIN, 1);
  });
 
  server.on("/lightsOFF", [](){
  server.send(200, "text/plain", "Okay -- Light is OFF!");
  digitalWrite(RELAY_PIN, 0);
  });

  server.onNotFound(handleNotFound);
 
  server.begin();
  Serial.println("HTTP server started");
}
 
void loop(void){
  server.handleClient();
}

Re: Toggle LED On / Off from Web -- Example?

PostPosted: Mon Jul 20, 2015 6:32 pm
by moun1234
Does anybody know how to add button on html that can trigger /lightOff and /lightOn without manually type it in url ?