I have NodeMCU 1.0 board (ESP-12E). Wifi is working properly but only if the connection is made straight in the beginning of my code.
The first version of my project initializes an OLED screen and then scans for networks and connects to a predefined one. Then takes temperature measurements and updates OLED every 1 minute and send data to cloud every 10 minutes. This works perfectly and Wifi is connected all the time.
Then I made a new version which stores data into RTC memory, updates OLED and goes to ESP.deepSleep(60e6, WAKE_RFCAL);. I want to update the OLED every minute and send data to the cloud every 10 minutes. In this code the same connectWiFi() function never manages to make the connection. It gets stuck in the while (WiFi.status() != WL_CONNECTED) loop. What could be the problem?
Working code:
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
// Network Parameters
const char* ssid = "mynetwork";
const char* password = "mypassword";
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println(F(""));
Serial.println(F(""));
Serial.println("Start");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay(); //tyhjennä näyttö
display.invertDisplay(false);
display.setTextColor(WHITE);
display.setFont(&FreeSans24pt7b);
display.setCursor(0,31);
display.print(" DAN");
display.display();
scanWifi(); //nää tehään loopissa ennenku lähetetään dataa
connectWiFi();
pinMode(lampotilaPin, OUTPUT); //kerrotaan et lämpötilan resistanssiverkon toinen jalka on ulostulossa
}
void loop() {
we get temperature and update screen...
}
int scanWifi(){
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(10);
}
}
Serial.println("");
}
int connectWiFi(){
if(WiFi.status() != WL_CONNECTED){
WiFi.mode(WIFI_OFF); //sammuta wifi
delay(10);
WiFi.mode(WIFI_STA); //ja sitten käynnistä jotta saadaan fresh start
//WiFi.disconnect();
delay(100);
Serial.print("Trying to connect to SSID: ");
Serial.println(ssid);
}
/*display.clearDisplay(); //tyhjennä näyttö
display.invertDisplay(false);
display.setTextColor(WHITE);
display.setFont(&FreeSans9pt7b);
display.setCursor(0,12);
display.println("Yhdistetaan");
display.print("WLAN:");
display.println(ssid);
display.display();*/
//byte kierros = 0;
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin( ssid, password );
delay(2500);
Serial.println("Connecting to WiFi");
//kierros++;
//if(kierros == 9){Serial.println("Failas! Kokeillaan uudestaan syssymmalla"); while(1){}}
}
Serial.println( "Connected" );
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //kerro IP osote
ThingSpeak.begin( client );
}
New not working code:
The new code is basically the same except everything is done in setup(). First we read RTC memory, then take temperature measurement and calculate moving average and then update oled. If we have updated screen 10 times (10 minutes) then I want to connect to wifi and send data. But no connection is established.