I have a sketch that talks to an HTTP web page via a esp8266 module.
Needless to say, this module has almost been more headache that it is worth - and I feel my solution lies in a better WiFi chip or ethernet shield.
Regardless, let's take a look at some code:
#include <EEPROM.h>
#include <SoftwareSerial.h>
//#include <LiquidCrystal.h>
int esp8266_CH_PD = 2;
int esp8266_cmd_timeout = 3000;
String server_response_buffer;
String current_time;
// Config variables in eeprom
char server_ip[15];
char server_port[5];
char server_vhost[50];
char wifi_ssid[50];
char wifi_pass[50];
char input_line[50];
bool input_complete = false;
char input_mode[20];
char tmp_var_1[50];
char tmp_var_2[50];
char tmp_var_3[50];
void setup() {
get_eeprom_value("server_ip",server_ip);
get_eeprom_value("server_port",server_port);
get_eeprom_value("server_vhost",server_vhost);
get_eeprom_value("wifi_ssid",wifi_ssid);
get_eeprom_value("wifi_pass",wifi_pass);
strcpy(input_line,"");
strcpy(input_mode,"none");
current_time.reserve(16);
server_response_buffer.reserve(500);
unsigned long start = millis();
String cmd_response;
cmd_response = "";
Serial.begin(9600);
Serial3.begin(9600);
Serial.println("Booting...");
pinMode(esp8266_CH_PD, OUTPUT);
digitalWrite(esp8266_CH_PD, 1);
Serial.println("First loop");
while(1) {
if(Serial3.available()) {
cmd_response += (char) Serial3.read();
}
if(cmd_response.endsWith("[System Ready, Vendor:www.ai-thinker.com]\r\n")) {
Serial.println("esp8266 seems ready");
break;
}
if( (millis()-start) >= 10000) {
Serial.println("esp8266 is not ready");
break;
}
}
Serial.println("Second loop");
cmd_response = "";
Serial3.println("AT+CWMODE=1");
while(1) {
if(Serial3.available()) {
cmd_response += (char) Serial3.read();
}
if(cmd_response.endsWith("OK\r\n") || cmd_response.endsWith("no change\r\n")) {
Serial.println("Got ok status from esp8266");
break;
}
if( (millis()-start) >= 10000) {
Serial.println("esp8266 timed out at_cwmode=1");
break;
}
}
Serial.println("Third loop");
cmd_response = "";
Serial3.print("AT+CWJAP=\"");
Serial3.print(wifi_ssid);
Serial3.print("\",\"");
Serial3.print(wifi_pass);
Serial3.print("\"\r\n");
while(1) {
if(Serial3.available()) {
cmd_response += (char) Serial3.read();
}
if(cmd_response.endsWith("OK\r\n")) {
Serial.println("esp8266 join AP success");
break;
}
if( (millis()-start) >= 10000) {
Serial.println("esp8266 timed out w/ join ap cmd");
break;
}
}
Serial.println("ITL TimeclockOS is up and running!");
/*
lcd.setCursor(0,1);
lcd.print("System ready ");
*/
//fetch_data("/");
//Serial.println(server_response_buffer);
}
I have left the loop() and a couple other functions out - not that I imagine that make a difference.
Now here is the wonderful part that is pi**ing me off.... When this setup() routine runs, here is what happens at the serial console:
Booting...
First loop
pause for 10 seconds
esp8266 is not ready
Second loop
esp8266 timed out at at_cwmode=1
Third loop
esp8266 timed out w/ join ap cmd
ITL TimeclockOS is up and running!
Now pay close attention: there is only ONE 10 second pause! Take a look at the code - there should have been a total of THREE 10-second pauses... no? It looks as though the next TWO wile() loops are running in parallel with the first! I can imagine no way possible that the other TWO while() loops execute and hit the 10 second timeouts (in a total of 10 seconds for all 3 loops) unless the other two are running at the same time.
Let me stress that point: A total of 10 seconds is taking place from boot start, to finish.
But this gets even stranger. You see.. when I take a BLANK sketch (without anything in loop() and without my additional 4 custom functions).. the entire block of setup() code works!!!!!!
I cannot imagine how 3 or 4 other defined functions OUTSIDE the setup() function would affect the functionality within the setup() loop itself. Seriously.. what am I missing?
I am soooooooo close to getting a Rasberry Pi and writing some Perl - needless to say, I am NOT a C guy. But the inconsistencies I have faced with this Arduino and C has got me pulling my hair out.
Another point to mention that adds to my frustration: THIS CODE WORKED LAST NIGHT!!!! All I did was power the Arduino down, went to bed and woke back up. NO CODE WAS ALTERED! Yet, it was working (more than several dozen cycles) last night!
Please, somebody...
- Dean
This code, when executed in my main sketch