From 3167f6fb8b959b3eb3d00b360af6c22aedc7cccc Mon Sep 17 00:00:00 2001 From: Michael JIN Date: Tue, 21 Apr 2015 08:45:20 +0800 Subject: [PATCH 1/3] mv exsample to correct folder of Arduino IDE --- tester.ino => exsamples/tester/tester.ino | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tester.ino => exsamples/tester/tester.ino (100%) diff --git a/tester.ino b/exsamples/tester/tester.ino similarity index 100% rename from tester.ino rename to exsamples/tester/tester.ino From 8e2b95e133cce6ea7dfb6b5ff8c4b59cb24bf7f0 Mon Sep 17 00:00:00 2001 From: Michael JIN Date: Tue, 21 Apr 2015 08:47:15 +0800 Subject: [PATCH 2/3] fix search string of 'ready', works in firmware of 'Vendor:www.ai-thinker.com Version:0.9.2.4' --- ESP8266.cpp | 126 ++++++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/ESP8266.cpp b/ESP8266.cpp index ae90bb6..367f5ed 100644 --- a/ESP8266.cpp +++ b/ESP8266.cpp @@ -54,55 +54,55 @@ ESP8266::ESP8266(int mode, long baudrate, int debugLevel) int ESP8266::initializeWifi(DataCallback dcb, ConnectCallback ccb) { - + if (dcb) { _dcb = dcb; } - + if (ccb) { - _ccb = ccb; + _ccb = ccb; } - + wifi.begin(_baudrate); - wifi.setTimeout(5000); - + wifi.setTimeout(5000); + //delay(500); clearResults(); - + // check for presence of wifi module wifi.println(F("AT")); delay(500); if(!searchResults("OK", 1000, _debugLevel)) { return WIFI_ERR_AT; - } - + } + //delay(500); - + // reset WiFi module wifi.println(F("AT+RST")); delay(500); - if(!searchResults("Ready", 5000, _debugLevel)) { + if(!searchResults("ready", 5000, _debugLevel)) { return WIFI_ERR_RESET; } - + delay(500); - + // set the connectivity mode 1=sta, 2=ap, 3=sta+ap wifi.print(F("AT+CWMODE=")); wifi.println(_mode); //delay(500); - + clearResults(); - + return WIFI_ERR_NONE; } int ESP8266::connectWifi(char *ssid, char *password) { - + strcpy(_ssid, ssid); strcpy(_password, password); - + // set the access point value and connect wifi.print(F("AT+CWJAP=\"")); wifi.print(ssid); @@ -113,24 +113,24 @@ int ESP8266::connectWifi(char *ssid, char *password) if(!searchResults("OK", 30000, _debugLevel)) { return WIFI_ERR_CONNECT; } - + // enable multi-connection mode if (!setLinkMode(1)) { return WIFI_ERR_LINK; } - + // get the IP assigned by DHCP getIP(); - + // get the broadcast address (assumes class c w/ .255) getBroadcast(); - + return WIFI_ERR_NONE; } bool ESP8266::disconnectWifi() { - + } bool ESP8266::enableBeacon(char *device) @@ -138,7 +138,7 @@ bool ESP8266::enableBeacon(char *device) // you can only beacon if you're a server if (_connectMode != CONNECT_MODE_SERVER) return false; - + bool ret; strcpy(_device, device); ret = startUDPChannel(BCN_CHAN, _broadcast, BEACON_PORT); @@ -169,9 +169,9 @@ void ESP8266::run() int v; char _data[255]; unsigned long currentMillis = millis(); - + if (currentMillis - _previousMillis < _beaconInterval) { - + // process wifi messages while(wifi.available() > 0) { v = wifi.read(); @@ -186,22 +186,22 @@ void ESP8266::run() _wctr++; } } - - + + } else { _previousMillis = currentMillis; if (_beacon == false) return; - + // create message text char *line1 = "{\"event\": \"beacon\", \"ip\": \""; char *line3 = "\", \"port\": "; char *line5 = ", \"device\": \""; char *line7 = "\"}\r\n"; - + // convert port to a string char p[6]; itoa(_port, p, 10); - + // get lenthg of message text memset(_data, 0, 255); strcat(_data, line1); @@ -211,17 +211,17 @@ void ESP8266::run() strcat(_data, line5); strcat(_data, _device); strcat(_data, line7); - + sendData(BCN_CHAN, _data); } } bool ESP8266::startServer(int port, long timeout) { - + // cache the port number for the beacon _port = port; - + wifi.print(F("AT+CIPSERVER=")); wifi.print(SVR_CHAN); wifi.print(F(",")); @@ -229,14 +229,14 @@ bool ESP8266::startServer(int port, long timeout) if(!searchResults("OK", 500, _debugLevel)){ return false; } - + // send AT command wifi.print(F("AT+CIPSTO=")); wifi.println(timeout); if(!searchResults("OK", 500, _debugLevel)) { return false; } - + _connectMode = CONNECT_MODE_SERVER; return true; } @@ -253,7 +253,7 @@ bool ESP8266::startClient(char *ip, int port, long timeout) if(!searchResults("OK", timeout, _debugLevel)) { return false; } - + _connectMode = CONNECT_MODE_CLIENT; return true; } @@ -268,7 +268,7 @@ int ESP8266::scan(char *out, int max) int timeout = 10000; int count = 0; int c = 0; - + if (_debugLevel > 0) { char num[6]; itoa(max, num, 10); @@ -298,45 +298,45 @@ int ESP8266::scan(char *out, int max) void ESP8266::processWifiMessage() { int packet_len; int channel; - char *pb; - + char *pb; + // if the message is simply "Link", then we have a live connection if(strncmp(_wb, "Link", 5) == 0) { - + // reduce the beacon frequency by increasing the interval _beaconInterval = 30000; //false; - + // flag the connection as active _connected = true; - + // if a connection callback is set, call it if (_ccb) _ccb(); } else - + // message packet received from the server if(strncmp(_wb, "+IPD,", 5)==0) { - + // get the channel and length of the packet sscanf(_wb+5, "%d,%d", &channel, &packet_len); - + // cache the channel ID - this is used to reply _replyChan = channel; - + // if the packet contained data, move the pointer past the header if (packet_len > 0) { pb = _wb+5; while(*pb!=':') pb++; pb++; - + // execute the callback passing a pointer to the message if (_dcb) { _dcb(pb); } - + // DANGER WILL ROBINSON - there is no ring buffer or other safety net here. // the application should either use the data immediately or make a copy of it! // do NOT block in the callback or bad things may happen - + } } else { // other messages might wind up here - some useful, some not. @@ -351,15 +351,15 @@ bool ESP8266::sendData(int chan, char *data) { wifi.print(chan); wifi.print(","); wifi.println(strlen(data)); - + // send the data wifi.println(data); - + delay(50); - + // to debug only searchResults("OK", 500, _debugLevel); - + return true; } @@ -388,7 +388,7 @@ bool ESP8266::startUDPChannel(int chan, char *address, int port) { // private convenience functions bool ESP8266::getIP() { - + char c; char buf[15]; int dots, ptr = 0; @@ -400,7 +400,7 @@ bool ESP8266::getIP() { delay(500); while (wifi.available() > 0) { c = wifi.read(); - + // increment the dot counter if we have a "." if ((int)c == 46) { dots++; @@ -430,13 +430,13 @@ bool ESP8266::getIP() { bool ESP8266::getBroadcast() { - + int i, c, dots = 0; - + if (strlen(_ipaddress) < 7) { return false; } - + memset(_broadcast, 0, 15); for (i = 0; i < strlen(_ipaddress); i++) { c = _ipaddress[i]; @@ -448,14 +448,14 @@ bool ESP8266::getBroadcast() { _broadcast[i++] = 50; _broadcast[i++] = 53; _broadcast[i++] = 53; - + return true; } void ESP8266::debug(char *msg) { if (_dcb && (_debugLevel > 0)) { _dcb(msg); - } + } } bool ESP8266::searchResults(char *target, long timeout, int dbg) @@ -465,13 +465,13 @@ bool ESP8266::searchResults(char *target, long timeout, int dbg) int targetLength = strlen(target); int count = 0; char _data[255]; - + memset(_data, 0, 255); long _startMillis = millis(); do { c = wifi.read(); - + if (c >= 0) { if (dbg > 0) { @@ -486,7 +486,7 @@ bool ESP8266::searchResults(char *target, long timeout, int dbg) if (c != target[index]) index = 0; - + if (c == target[index]){ if(++index >= targetLength){ if (dbg > 1) From 14d62315780e7c61fea9106fa103d6128cbe2299 Mon Sep 17 00:00:00 2001 From: Michael JIN Date: Tue, 21 Apr 2015 09:16:41 +0800 Subject: [PATCH 3/3] set debug_level default to 2, set baud to 9600 which is default of '[Vendor:www.ai-thinker.com Version:0.9.2.4]' --- exsamples/tester/tester.ino | 60 ++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/exsamples/tester/tester.ino b/exsamples/tester/tester.ino index 5fbae6f..5fc8099 100644 --- a/exsamples/tester/tester.ino +++ b/exsamples/tester/tester.ino @@ -5,14 +5,14 @@ #include "ESP8266.h" // CHANGE THIS TO 2 TO SEE MOST OF THE OUTPUT FROM THE LIBRARY -#define DEBUG_LEVEL 0 +#define DEBUG_LEVEL 2 // DO NOT try to use the default SoftSerial it WILL NOT work AltSoftSerial softSerial; // instantiate the wifi object with STA mode and server on port 80 and the baud rate // for communicating with the module -ESP8266 wifi(WIFI_MODE_STA, 38400, DEBUG_LEVEL); +ESP8266 wifi(WIFI_MODE_STA, 9600, DEBUG_LEVEL); #define BUFFER_SIZE 255 #define rs232 softSerial // alias the software port as 'rs232' @@ -33,25 +33,25 @@ void connectCallback() { void setup() { int ret; - + // Start the AltSoftSerial port at 9600 - can theoretically go as high as 56K rs232.begin(9600); rs232.println("Starting up WiFi to RS232 Bridge"); - + // THE FOLLOWING CODE READS VALUES FOR ssid, password, etc FROM THE EEPROM // ON AN Arudino Pro Mini. IF YOU DON'T WANT TO MESS WITH THIS, REPLACE IT // WITH SIMPLE STRING LITERALS - + // read the SSID from eeprom (up to 48 bytes: 0 to 47) char ssid[48]; memset(ssid, 0, 48); eepromReadString(0, 48, ssid); - + // read the password from the eeprom (up to 24 bytes: 48 to 71) char password[24]; memset(password, 0, 24); eepromReadString(48, 24, password); - + // read the port number from the eeprom (2 bytes: 72-73) int port = eepromReadInt(72); @@ -79,18 +79,18 @@ void setup() { rs232.println(F("\tAT+DEVICE=device-name")); strcpy(device, "wifi-serial"); } - + // start up the wifi connection // returns true if the module responds / resets properly ret = wifi.initializeWifi(&dataCallback, &connectCallback); - if (ret != WIFI_ERR_NONE) { + if (ret != WIFI_ERR_NONE) { rs232.println(F("Wifi initialization failed.")); rs232.println(ret); return; } - + rs232.println(F("Wifi initialized")); - + // connect the module to the provided SSID ret = wifi.connectWifi(ssid, password); if (ret != WIFI_ERR_NONE) { @@ -98,10 +98,10 @@ void setup() { rs232.println(ret); return; } - + rs232.println(F("Wifi connected")); rs232.println(wifi.ip()); - + // start the server bool svr = wifi.startServer(80); if (svr) { @@ -111,19 +111,19 @@ void setup() { /* // demo of client mode - connects to 4040 on 10.0.1.3 and sends a message - + if (wifi.startClient("10.0.1.3", 4040, 1000)) { rs232.println("client connected"); - wifi.send("bazinga!"); + wifi.send("bazinga!"); } */ } void loop() { - + int v; - + // make the wifi library run wifi.run(); @@ -142,19 +142,19 @@ void loop() { // process a complete message from the Serial side // and send the payload to the WiFi connection void processSerialMessage(int len) { - + // look for command messages - start with "AT" // really should require a pin high to put in programming // mode if ((strncmp(sb, "AT", 2) == 0) && (connected == false)) { if (processSerialCommand()) { - rs232.println("OK"); + rs232.println("OK"); } } else { // data message - no command - just pass it on to the WiFi wifi.send(sb); } - + // reset the character counter sctr = 0; } @@ -162,10 +162,10 @@ void processSerialMessage(int len) { // process a serial command bool processSerialCommand() { - + // command message - AT+something or AT+something=XXX if (strncmp(sb+2, "+", 1) == 0) { - + // eliminate any \r and \n at the end for (int i = 0; i < strlen(sb); i++) { if ((int)sb[i] == 13) { @@ -173,14 +173,14 @@ bool processSerialCommand() { } if ((int)sb[i] == 10) { sb[i] = 0; - } + } } - + char command[32]; memset(command, 0, 32); char value[48]; memset(value, 0, 48); - + char *split = strchr(sb+3, '='); if (split) { int index = (split - (sb+3)); @@ -192,7 +192,7 @@ bool processSerialCommand() { rs232.print("Command: "); rs232.println(command); - + rs232.print("Value: "); rs232.println(value); @@ -208,22 +208,22 @@ bool processSerialCommand() { eepromWriteString(48, 24, value); return true; } - + if (strncmp(command, "PORT", 4) == 0) { int port = atoi(value); eepromWriteInt(72, port); return true; } - + // device name if (strncmp(command, "DEVICE", 4) == 0) { eepromWriteString(74, 48, value); return true; } - + return false; } - + return true; }