Created
December 1, 2018 13:50
-
-
Save gamelaster/a0790481c01ff76a71cc848a6e61003c to your computer and use it in GitHub Desktop.
Arduino sketch for ESP8266-01 relay baord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2018 Marek gamelaster Kraus, license: GNU GPLv3 | |
| #include <ESP8266WiFi.h> | |
| const char* ssid = ""; // Write WiFi name | |
| const char* password = ""; // Write here WiFi password | |
| WiFiServer server(80); | |
| int relayState = LOW; | |
| String header; | |
| void setup() { | |
| Serial.begin(9600); | |
| //Serial.print("Connecting to "); | |
| //Serial.println(ssid); | |
| WiFi.begin(ssid, password); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| //Serial.print("."); | |
| } | |
| //Serial.println(""); | |
| //Serial.println("WiFi connected."); | |
| //Serial.println("IP address: "); | |
| //Serial.println(WiFi.localIP()); | |
| server.begin(); | |
| } | |
| void loop(){ | |
| WiFiClient client = server.available(); | |
| if (client) { | |
| //Serial.println("New Client."); | |
| String currentLine = ""; | |
| while (client.connected()) { | |
| if (client.available()) { | |
| char c = client.read(); | |
| //Serial.write(c); | |
| header += c; | |
| if (c == '\n') { | |
| if (currentLine.length() == 0) { | |
| client.println("HTTP/1.1 200 OK"); | |
| client.println("Content-type:text/html"); | |
| client.println("Connection: close"); | |
| client.println(); | |
| if (header.indexOf("GET /on") >= 0) { | |
| ////Serial.println("GPIO 0 on"); | |
| relayState = HIGH; | |
| client.println("OK"); | |
| const byte miBufferON[] = {0xA0, 0x01, 0x01, 0xA2}; | |
| Serial.write(miBufferON, sizeof(miBufferON)); | |
| } else if (header.indexOf("GET /off") >= 0) { | |
| //Serial.println("GPIO 0 off"); | |
| relayState = LOW; | |
| const byte miBufferOFF[] = {0xA0, 0x01, 0x00, 0xA1}; | |
| Serial.write(miBufferOFF, sizeof(miBufferOFF)); | |
| client.println("OK"); | |
| } else if (header.indexOf("GET /status") >= 0) { | |
| client.println(relayState == HIGH ? "HIGH" : "LOW"); | |
| } | |
| client.println(); | |
| break; | |
| } else { | |
| currentLine = ""; | |
| } | |
| } else if (c != '\r') { | |
| currentLine += c; | |
| } | |
| } | |
| } | |
| header = ""; | |
| client.stop(); | |
| //Serial.println("Client disconnected."); | |
| //Serial.println(""); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment