led module
parent
ed3fafe982
commit
3e642018ab
@ -0,0 +1,87 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <ESP8266HTTPClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
|
||||
const char* ssid = "SSID";
|
||||
const char* password = "PASSWORD";
|
||||
|
||||
ESP8266WebServer server(80);
|
||||
|
||||
const char* id = "led2";
|
||||
bool led_status = false;
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
//set wifi parameters
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
IPAddress ip(10, 10, 10, 11);
|
||||
IPAddress gateway(10, 10, 10, 1);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
WiFi.config(ip, gateway, subnet);
|
||||
|
||||
|
||||
//Wait for connection, print info to serial
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(200);
|
||||
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("/on", HTTP_GET, on_);
|
||||
server.on("/off", HTTP_GET, off_);
|
||||
server.on("/info", HTTP_GET, info_);
|
||||
|
||||
server.begin();
|
||||
|
||||
//say hello to server
|
||||
HTTPClient http;
|
||||
//http://<serverip>.8080/connect/<moduleip>/<moduleid>
|
||||
http.begin("http://10.10.10.10:8080/connect/10.10.10.11/led1");
|
||||
int httpCode = http.GET();
|
||||
if (httpCode > 0) {
|
||||
String payload = http.getString();
|
||||
Serial.println(httpCode);
|
||||
Serial.println(payload);
|
||||
} else Serial.println("Error on HTTP request");
|
||||
http.end();
|
||||
}
|
||||
|
||||
|
||||
void on_() {
|
||||
server.sendHeader("Access-Control-Allow-Origin", "*");
|
||||
server.send(200, "text/plain", "on");
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
led_status = true;
|
||||
}
|
||||
|
||||
void off_() {
|
||||
server.sendHeader("Access-Control-Allow-Origin", "*");
|
||||
server.send(200, "text/plain", "off");
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
led_status = false;
|
||||
}
|
||||
|
||||
void info_() {
|
||||
server.sendHeader("Access-Control-Allow-Origin", "*");
|
||||
if (led_status) server.send(200, "text/plain", "on");
|
||||
else server.send(200, "text/plain", "off");
|
||||
}
|
||||
|
||||
|
||||
void loop(void) {
|
||||
server.handleClient();
|
||||
}
|
Loading…
Reference in New Issue