diff --git a/ESP8266.cpp b/ESP8266.cpp index 38d2c60..c3605d7 100644 --- a/ESP8266.cpp +++ b/ESP8266.cpp @@ -49,7 +49,7 @@ ESP8266::ESP8266(SoftwareSerial &uart, uint32_t baud): m_puart(&uart) } ESP8266::ESP8266(uint32_t baud) { - SoftwareSerial *s = new SoftwareSerial(7, 6); + SoftwareSerial *s = new SoftwareSerial(TX_PIN, RX_PIN); m_puart = s; m_puart->begin(baud); rx_empty(); @@ -701,7 +701,8 @@ bool ESP8266::eATCIPCLOSESingle(void) { rx_empty(); m_puart->println("AT+CIPCLOSE"); - return recvFind("OK", 5000); + //return recvFind("OK", 5000); + return true; } bool ESP8266::eATCIFSR(String &list) { @@ -753,54 +754,67 @@ bool ESP8266::sATCIPSTO(uint32_t timeout) bool ESP8266::getPuzzleStatus(char *puzzle) { char request[8]; strcpy(request, puzzle); - strcat(request, " GET"); - request[8] = '\0'; - char resp[8]; + strcat(request, " GET\0"); + //request[8] = '\0'; + char resp[8] = {0}; int i=0; - for (; i<3; i++) if (sendRequest(request, resp)) break; + //for (; i<3; i++) if (sendRequest(request, resp)) break; + for (; i<3; i++) { + if (sendRequest(request, resp)) { + if (resp[0] != '\0' && strcmp(resp, "INVALID")) break; + } + } // Return false if we send 3 requests with invalid responses if (i == 3) return false; char doneResp[8]; strcpy(doneResp, puzzle); - strcat(request, " DNE"); + strcat(doneResp, " DNE"); doneResp[7] = '\0'; - if (strcmp(resp, doneResp)) return true; + if (!strcmp(resp, doneResp)) return true; else return false; } bool ESP8266::markPuzzleDone(char *puzzle) { char request[8]; strcpy(request, puzzle); - strcat(request, " DNE"); - request[8] = '\0'; - char resp[8]; + strcat(request, " DNE\0"); + char resp[8] = {0}; int i=0; - for (; i<3; i++) if (sendRequest(request, resp)) break; + //for (; i<3; i++) if (sendRequest(request, resp)) break; + for (; i<3; i++) { + if (sendRequest(request, resp)) { + if (resp[0] != '\0' && strcmp(resp, "INVALID")) break; + } + } // Return false if we send 3 requests with invalid responses if (i == 3) return false; - if (strcmp(resp, "INVALID")) return false; return true; } bool ESP8266::markPuzzleIncomplete(char *puzzle) { char request[8]; strcpy(request, puzzle); - strcat(request, " RST"); - request[8] = '\0'; - char resp[8]; + strcat(request, " RST\0"); + char resp[8] = {0}; int i=0; - for (; i<3; i++) if (sendRequest(request, resp)) break; + for (; i<3; i++) { + if (sendRequest(request, resp)) { + if (resp[0] != '\0' && strcmp(resp, "INVALID")) break; + } + } // Return false if we send 3 requests with invalid responses if (i == 3) return false; - if (strcmp(resp, "INVALID")) return false; return true; } -bool ESP8266::sendRequest(char *request, char *buffer) { - if (!createTCP(HOST_NAME, HOST_PORT)) return false; +bool ESP8266::sendRequest(char *request, char *resp) { + if (!createTCP(HOST_NAME, HOST_PORT)) { + releaseTCP(); + return false; + } send((const uint8_t*)request, strlen(request)); - uint32_t len = recv(buffer, sizeof(buffer), 10000); + uint32_t len = recv(resp, 8); + releaseTCP(); if (len > 0) return true; else return false; } - diff --git a/ESP8266.h b/ESP8266.h index 4411a6a..a3eb930 100644 --- a/ESP8266.h +++ b/ESP8266.h @@ -21,7 +21,9 @@ #ifndef __ESP8266_H__ #define __ESP8266_H__ -#define HOST_NAME "172.27.109.43" +#define RX_PIN 6 +#define TX_PIN 7 +#define HOST_NAME "172.27.109.239" #define HOST_PORT (1337) #include "Arduino.h" @@ -46,11 +48,11 @@ class ESP8266 { * Constuctor. * * @param uart - an reference of SoftwareSerial object. - * @param baud - the buad rate to communicate with ESP8266(default:9600). + * @param - the buad rate to communicate with ESP8266(default:9600). * * @warning parameter baud depends on the AT firmware. 9600 is an common value. */ - ESP8266(uint32_t baud = 9600); + ESP8266(uint32_t baud = 115200); ESP8266(SoftwareSerial &uart, uint32_t baud = 9600); #else /* HardwareSerial */ /* @@ -472,6 +474,8 @@ class ESP8266 { bool sATCIPSERVER(uint8_t mode, uint32_t port = 333); bool sATCIPSTO(uint32_t timeout); bool sendRequest(char *request, char *buffer); + void ESP8266::mySend(const uint8_t *buffer, uint32_t len); + /* * +IPD,len:data @@ -486,4 +490,3 @@ class ESP8266 { }; #endif /* #ifndef __ESP8266_H__ */ - diff --git a/ESP8266EscapeRoom.ino b/ESP8266EscapeRoom.ino index 0abe848..696e084 100644 --- a/ESP8266EscapeRoom.ino +++ b/ESP8266EscapeRoom.ino @@ -18,14 +18,16 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -//#include "ESP8266EscapeRoom.h" #include "ESP8266.h" #include - +// Connect to wahoo #define SSID "wahoo" #define PASSWORD "" +// Setup ESP +// Pin 7 is TX on adapter; 6 is RX on adapter +// Adapter takes 5V in VCC SoftwareSerial esp(7,6); ESP8266 wifi(esp); @@ -33,31 +35,9 @@ void setup(void) { Serial.begin(115200); Serial.print("setup begin\r\n"); - - Serial.print("FW Version:"); - Serial.println(wifi.getVersion().c_str()); - - if (wifi.setOprToStationSoftAP()) { - Serial.print("to station + softap ok\r\n"); - } else { - Serial.print("to station + softap err\r\n"); - } Serial.println("Connecting to WiFi"); + // Connect to WiFi and wait till it's connected while (!wifi.joinAP(SSID, PASSWORD)) Serial.println("Retrying..."); - /*if (wifi.joinAP(SSID, PASSWORD)) { - Serial.print("Join AP success\r\n"); - Serial.print("IP:"); - Serial.println( wifi.getLocalIP().c_str()); - } else { - Serial.print("Join AP failure\r\n"); - }*/ - - if (wifi.disableMUX()) { - Serial.print("single ok\r\n"); - } else { - Serial.print("single err\r\n"); - } - Serial.print("setup end\r\n"); } @@ -70,10 +50,13 @@ void loop(void) // GRS = Gears Serial.println("Mark books done:"); Serial.println(wifi.markPuzzleDone("BKS")); + delay(50); Serial.println("Books status:"); Serial.println(wifi.getPuzzleStatus("BKS")); + delay(50); Serial.println("Mark books incomplete:"); Serial.println(wifi.markPuzzleIncomplete("BKS")); + delay(50); Serial.println("Books status:"); Serial.println(wifi.getPuzzleStatus("BKS")); delay(5000); diff --git a/escape-room-server.py b/escape-room-server.py index 37f4e8a..4e6807b 100644 --- a/escape-room-server.py +++ b/escape-room-server.py @@ -32,30 +32,31 @@ # Invalid commands are responded to with INVALID import socket +from Adafruit_IO import * + +aio = Client('ogreatfox', '8341795c01774027901eae1c9f822f2d') PUZZLE_FEEDS = { - "MRS": "morse_feed", - "TIM": "time_feed", - "BKS": "books_feed", - "GRS": "gears_feed" + "MRS": "morse-feed", + "TIM": "time-feed", + "BKS": "books-feed", + "GRS": "gears-feed" } +status = {} def puzzle_done(puzzle): print("done", puzzle) data = aio.receive(PUZZLE_FEEDS[puzzle]) data = data.value.split() aio.send(PUZZLE_FEEDS[puzzle], str(data[0]) + " DNE") + status[puzzle] = "DNE" return puzzle + " DNE" def puzzle_get(puzzle): print("get", puzzle) - data = aio.receive(PUZZLE_FEEDS[puzzle]) - data = data.value.split() - if data[1] == "DNE": - return puzzle + " DNE" - return puzzle + " RST" + return puzzle + " " + status[puzzle] def puzzle_reset(puzzle): @@ -63,6 +64,7 @@ def puzzle_reset(puzzle): data = aio.receive(PUZZLE_FEEDS[puzzle]) data = data.value.split() aio.send(PUZZLE_FEEDS[puzzle], str(data[0]) + " RST") + status[puzzle] = "RST" return puzzle + " RST" @@ -74,6 +76,12 @@ def puzzle_reset(puzzle): "RST": puzzle_reset } +for feed in list(PUZZLE_FEEDS.keys()): + data = aio.receive(PUZZLE_FEEDS[feed]).value.split() + status[feed] = data[1] + +print("Puzzle statuses: ", status) + # Setup socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("0.0.0.0", PORT))