I need WiFi for a project, and I planned to use 8266. I bought six WeMos for this.
This code will get me online with the 32, but not the 8266. Can anybody tell me why?
#include <Arduino.h>
#include <WiFi.h>
const char *ssid = "Fishing24";
const char *password = "Fishing$602";
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
WiFi.begin(ssid, password);
while (WiFi.status() == WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
}
// put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
}
This is an infinite looop and youâre not feeding the watchdog here. Also this code will not turn off the LED when WiFi is disconnected - it just exists the loop and does nothing. No explicit digitalWrite
with LOW
is done.
Just put it in loop
as a check.
void loop()
{
//most builtin LEDs on ESP8266 boards I've seen seen are *inverted*.
//writing LOW to them will turn the LED on.
//switch LOW and HIGH if needed else..
digitalWrite(LED_BUILTIN, (WiFi.status() == WL_CONNECTED) ? LOW : HIGH);
}
Activate debug log level as show in docs by using
build_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI
then post log of the ESP8266 when it connects.
If Wifi status changes, doesnât WL_CONNECTED change and thus break the while loop?
I added that to .ini, and it still doesnât connect, per serial.But it only goes five cycles before it quits.
Executing task in folder Wifi_WeMos8266: C:\Users\joema.platformio\penv\Scripts\pio.exe device monitor <
â Available filters and text transformations: colorize, debug, default, direct, esp8266_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
â More details at Redirecting...
â Miniterm on COM8 9600,8,N,1 â
â Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H â
ďż˝HpYďż˝bfr{ďż˝â:lďż˝âďż˝âŚâďż˝l��H2ďż˝$âyOďż˝3ďż˝âŚ
Okay wait I read the code youâre running again and⌠youâre connecting twice to the same network?
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
WiFi.begin(ssid, password);
while (WiFi.status() == WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
}
can you give a high-level description of what the firmware is supposed to do?
In this case, the second begin is artifact.
I want to light the LED when it connects with the second while loop.
looks cool- red and blue.
Added an if to turn the LED off if it disconnects.
It looks like it inverts high/low.
1 Like
8266 doesnât have the red. Boo.
ESP32 uses the library you included:
#include <WiFi.h>
ESP8266 DOES NOT. It is uses
#include <ESP8266WiFi.h>
Meaning if you want to use the same code on both the ESP32 and ESP8266 you will need add some conditional include logic i.e.
#if defined(ESP8266)
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library
#elif defined(ESP32)
#include <WiFi.h> //ESP32 Core WiFi Library
#else
#error Stop right now, and choose a valid board architecture!
#endif
Additionally, code like
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
WiFi.begin(ssid, password);
while (WiFi.status() == WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
}
is just plain bad⌠as it will
- try to connect twice for not valid reason
- prevent
loop()
being reached unless the wifi disconnects
- unless the wifi disconnects within a couple of seconds the watchdog timer will run out (it needs to be reset periodically so that the underlying operating system knows the code hasnât locked up) and the board will reset.
If you want to do that sort of test, do it in the main loop like Max showed or alternaltely, this way (does the same thing, just a different way):
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
Can you see how putting it in loop()
will result in the main loop being able to keep running without being locked up on in your code⌠every time the loop runs, (hundreds of times a second, depending on what else the ESP is doing in the main loop) it will check the wifi status, set the LED accordingly, and carry onâŚ
Apparently, that second connect attempt was a copy/paste error and isnât in the code.
Cheers,
Norm.
I actually read it the other way, that it was there (especially as itâs the same in sets of code, which are otherwise different), but shouldnât have been and was the result of a bad copy-paste⌠who knows!
Either way, Joe needs as much positive reinforcement of proper programming as he can get to help him along in successfully writing working programs! 
1 Like
Once again, thank you very much.
I really do appreciate the positive reinforcement.
I stopped freaking out, because I have stuff working. But I have too much working. I thrive on chaos, but then I get too many question marks. Iâm going to stop all the learning until I get the ISS tracker working.
I will move the LED to the loop- I just like to look at it. The WeMos has a red and a blue. Itâs pretty.
I understand why I need to move it- current information.
That ifdef stuff will have to wait.
When I was making web pages, lots of stuff didnât work cross-platform. I hated checking for browser, so I just kept it all straight HTML. Alta Vista.
1 Like