Created
July 19, 2018 07:14
-
-
Save rhaseven7h/30ee13759189d5591d76ec4353702533 to your computer and use it in GitHub Desktop.
Arduino WiFi Lamp Controller
This file contains 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
#include <SerialESP8266wifi.h> | |
#define PIN_WIFI_RESET 8 | |
#define PIN_RELAY_SIGNAL 9 | |
SerialESP8266wifi wifi(Serial1, Serial1, PIN_WIFI_RESET, Serial); | |
char vBuff[64] = ""; | |
String ipAddress = "0.0.0.0"; | |
void handleWiFiIncoming() { | |
while (Serial1.available()) { | |
char inByte = Serial1.read(); | |
Serial.write(inByte); | |
} | |
} | |
void handleSerialComms() { | |
while (Serial1.available()) { | |
char inByte = Serial1.read(); | |
Serial.write(inByte); | |
} | |
while (Serial.available()) { | |
int inByte = Serial.read(); | |
Serial1.write(inByte); | |
} | |
} | |
void sendCmd(String s) { | |
Serial1.println(s); | |
Serial1.flush(); | |
Serial.println("Executing [" + s + "]"); | |
} | |
void sendCmdExpect(String s, String x) { | |
sendCmd(s); | |
if (Serial1.find(const_cast<char*>(x.c_str())), "link is not valid", "ERROR") { | |
Serial.println("Successfully executed [" + s + "]"); | |
} else { | |
Serial.println("Timeout executing [" + s + "]"); | |
} | |
handleSerialComms(); | |
} | |
void setup() { | |
pinMode(PIN_WIFI_RESET, OUTPUT); | |
digitalWrite(PIN_WIFI_RESET, HIGH); | |
pinMode(PIN_RELAY_SIGNAL, OUTPUT); | |
digitalWrite(PIN_RELAY_SIGNAL, LOW); | |
Serial.begin(115200); | |
Serial.println("Booting up..."); | |
Serial1.setTimeout(5000); | |
Serial1.begin(115200); | |
if (!wifi.begin()) { | |
Serial.println("\n\nWiFi module could not be set up."); | |
} else { | |
Serial.println("\n\nWiFi Module succesfully set up."); | |
} | |
if (wifi.connectToAP("AFRODITA", "A38sJjf3X7ByyFJ3")) { | |
Serial.println("\n\nWiFi was connected successfully."); | |
} else { | |
Serial.println("\n\nError connecting to WiFi."); | |
} | |
strcpy(vBuff, wifi.getIP()); | |
ipAddress = String(vBuff); | |
Serial.println("\n\nWiFi IP: " + String(ipAddress)); | |
wifi.startLocalServer("80"); | |
Serial.println("\n\nWiFi connection completed."); | |
handleSerialComms(); | |
} | |
void changeLampStatus(bool lampState) { | |
if (lampState) { | |
digitalWrite(PIN_RELAY_SIGNAL, HIGH); | |
} else { | |
digitalWrite(PIN_RELAY_SIGNAL, LOW); | |
} | |
} | |
String inMessage = ""; | |
void processMessage() { | |
Serial.println("========================"); | |
Serial.println(inMessage); | |
Serial.println("------------------------"); | |
String respMessage="none"; | |
String respStatus="unknown"; | |
if (inMessage.startsWith("+IPD")) { | |
String connID = inMessage.substring(5,6); | |
String cmdLine = inMessage.substring(0, inMessage.indexOf("\x0D\x0A")); | |
String cmdString = cmdLine.substring(inMessage.indexOf(":")+1, cmdLine.lastIndexOf(" ")); | |
Serial.println("ConnID: "+connID); | |
Serial.println("CmdLine: "+cmdLine); | |
Serial.println("CmdString: "+cmdString); | |
if (cmdString == "GET /lamp/on") { | |
Serial.println("Turning lamp on."); | |
respMessage="lamp turned on"; | |
respStatus="ok"; | |
changeLampStatus(true); | |
} else if (cmdString == "GET /lamp/off") { | |
Serial.println("Turning lamp off."); | |
respMessage="lamp turned off"; | |
respStatus="ok"; | |
changeLampStatus(false); | |
} else { | |
Serial.println("Unknown command."); | |
respMessage="unknown action"; | |
respStatus="error"; | |
} | |
String doneMessage="HTTP/1.1 200 OK\nContent-Type: application/json\n\n{\"status\":\""+respStatus+"\", \"message\":\""+respMessage+"\"}"; | |
sendCmd("AT+CIPSEND="+connID+","+String(doneMessage.length())); | |
handleWiFiIncoming(); | |
Serial.println(); | |
sendCmdExpect(doneMessage, "SEND OK"); | |
sendCmdExpect("AT+CIPCLOSE="+connID, "OK"); | |
} | |
} | |
void loop() { | |
while (Serial1.available()) { | |
char inByte = Serial1.read(); | |
inMessage += inByte; | |
if (inByte == '\x0A' or inByte == '\x0D') { | |
if (inMessage.endsWith("\x0D\x0A\x0D\x0A")) { | |
processMessage(); | |
inMessage = ""; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment