Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save psibertechsrivats/a2980533815917d49b10998d91fdeebf to your computer and use it in GitHub Desktop.
Save psibertechsrivats/a2980533815917d49b10998d91fdeebf to your computer and use it in GitHub Desktop.
IoT Led Lamp controller
/* Create a WiFi access point and provide a web server on it.
Author: vivek
Last Update: 14 Jul 2017
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Set these to your desired credentials. */
const char *ssid = "********";
const char *password = "********";
ESP8266WebServer server(80);
const int led = 2; //GPIO2
/* Just a little test message. Go to http://192.168.4.1 in a web browser
* connected to this access point to see it.
*/
void handleRoot() {
int size=1000;
char temp[size];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, size,
"<html>\
<head>\
<title>MicroCloud</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h3>You are connected on MicroCloud #2!</h3>\
<p>Uptime: %02d:%02d:%02d</p>\
<p>Status: Light ON</h1></p>\
<p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
<p></p>\
<p><a href=\"http://192.168.4.1/off\"><h1>Turn OFF</h1></a></p>\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send ( 200, "text/html", temp );
}
void setup() {
delay(1000);
Serial.begin(9600);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
pinMode(led, OUTPUT);
digitalWrite ( led, HIGH );
//URLs available to query
server.on("/", handleRoot);
server.on ( "/on", turnON );
server.on ( "/off", turnOFF );
server.begin();
Serial.println("HTTP server started");
}
void turnON(){
digitalWrite ( led, HIGH );
int size=1000;
char temp[size];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, size,
"<html>\
<head>\
<title>MicroCloud</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h3>Request: Light ON</h3>\
<p>Uptime: %02d:%02d:%02d</p>\
<p></p>\
<p>Status: Light ON</h1></p>\
<p><a href=\"http://192.168.4.1/on\"><h1>Turn ON</h1></a></p>\
<p></p>\
<p><a href=\"http://192.168.4.1/off\"><h1>Turn OFF</h1></a></p>\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send ( 200, "text/html", temp);
}
void turnOFF(){
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment