Skip to content

Commit

Permalink
Make wifi work
Browse files Browse the repository at this point in the history
  • Loading branch information
evantypanski committed Mar 25, 2019
1 parent 00c579d commit 7a4d056
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 60 deletions.
58 changes: 36 additions & 22 deletions ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

11 changes: 7 additions & 4 deletions ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 */
/*
Expand Down Expand Up @@ -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
Expand All @@ -486,4 +490,3 @@ class ESP8266 {
};

#endif /* #ifndef __ESP8266_H__ */

33 changes: 8 additions & 25 deletions ESP8266EscapeRoom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,26 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
//#include "ESP8266EscapeRoom.h"
#include "ESP8266.h"
#include <SoftwareSerial.h>


// 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);

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");
}

Expand All @@ -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);
Expand Down
26 changes: 17 additions & 9 deletions escape-room-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,39 @@
# 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):
print("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"


Expand All @@ -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))
Expand Down

0 comments on commit 7a4d056

Please sign in to comment.