Made a Blynk project using sonoff basic (just wanted a nicer esp8266-12 package for relay switching) using code below, and it worked great for about 20 days. Yesterday, two out of three machines just lost connection to blynk/ wifi router. I have not changed the code at all, but since then, no matter if I upload the code again, this still does not respond. Not even the hardware button (tactile switch which does not require wifi to work) works. I am pretty confused - nothing seems to work.
Details:
• Sonoff basic, which is a esp8266-12. Using on board wifi
• Android oreo (Blynk version 2.18.2)
• Cloud server
• Blynk version (0.5.1)
• Sketch code:
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <DHT.h>
#define VPIN V1 //Use a unique virtual pin for each device using the same token / dashboard
#define VPIN_T V2 // For temperature data
#define VPIN_H V3 // For humidity data
#define VPIN_F V4 // For temperature in farenheit
#define VPIN_tag V20 // For tag
#define DHTPIN 14
char auth[] = "XXXXXXXXXXXXXXX"; //Get token from Blynk
//on/off callbacks
void lightOn();
void lightOff();
boolean LampState = 0;
boolean SwitchReset = true; //Flag indicating that the hardware button has been released
const int TacSwitch = 0; //Pin for hardware momentary switch. On when grounded
const int RelayPin = 12; //Relay switching pin. Relay is pin 12
const int LED = 13; //On / Off indicator LED. Onboard LED is 13 on Sonoff
#define DHTTYPE DHT22
BlynkTimer timer;
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, LOW);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH); //LED on sonoff is pulled up
pinMode(TacSwitch, INPUT_PULLUP);
Serial.begin(9600);
WiFiManager wifi; //WiFiManager intialization.
wifi.autoConnect("XXXXXXXXXXXX", "XXXXXXXXXXX"); //Auto connect, otherwise ESP will make a hotspot
delay(100);
Blynk.config(auth);
ArduinoOTA.setHostname("esp8266_4_outside");
ArduinoOTA.setPassword("XXXXXXXXXXXXXXXX");
ArduinoOTA.begin();
timer.setInterval(100, ButtonCheck);
timer.setInterval(300000, temp_humidity);
lightOff();
dht.begin();
}
void loop()
{
Blynk.run();
ArduinoOTA.handle();
timer.run();
}
// Toggle the relay on
void lightOn() {
Serial.println("Outside switch ON!...");
digitalWrite(RelayPin, HIGH);
digitalWrite(LED, LOW);
LampState = 1;
Blynk.virtualWrite(VPIN, HIGH); // Sync the Blynk button widget state
}
// Toggle the relay off
void lightOff() {
Serial.println("Outside switch OFF ...");
digitalWrite(RelayPin, LOW);
digitalWrite(LED, HIGH);
LampState = 0;
Blynk.virtualWrite(VPIN, LOW); // Sync the Blynk button widget state
}
// Handle switch changes originating on the Blynk app
BLYNK_WRITE(VPIN){
int SwitchStatus = param.asInt();
Serial.println("Blynk switch for Outside switch activated");
// For use with IFTTT, toggle the relay by sending a "2"
if (SwitchStatus == 2){
ToggleRelay();
}
else if (SwitchStatus == 1){
lightOn();
}
else lightOff();
}
BLYNK_WRITE(VPIN_tag){
int SwitchStatus = param.asInt();
Serial.println("Blynk switch for Outside switch activated");
// For use with IFTTT, toggle the relay by sending a "2"
if (SwitchStatus == 2){
ToggleRelay();
}
else if (SwitchStatus == 1){
lightOn();
}
else lightOff();
}
void temp_humidity(){
//Read the sensor for temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();
float t_f = dht.readTemperature(true);
Blynk.virtualWrite(VPIN_T, t);
Blynk.virtualWrite(VPIN_H, h);
Blynk.virtualWrite(VPIN_F, t_f);
}
// Handle hardware switch activation
void ButtonCheck(){
// look for new button press
boolean SwitchState = (digitalRead(TacSwitch));
// toggle the switch if there's a new button press
if (!SwitchState && SwitchReset == true){
Serial.println("Hardware switch for coffee activated");
if (LampState){
lightOff();
}
else{
lightOn();
}
// Flag that indicates the physical button hasn't been released
SwitchReset = false;
delay(50); //debounce
}
else if (SwitchState){
// reset flag the physical button release
SwitchReset = true;
}
}
void ToggleRelay(){
LampState = !LampState;
if (LampState){
lightOn();
}
else lightOff();
}
Also, I tried just uploading a simple code without Blynk/ wifi requirements but just a tactile switch to toggle relay. Unfortunately, this did not work either. Confused, I added in loop to just print ON in serial port.
Code below (same setup)
void lightOn();
void lightOff();
boolean LampState = 0;
boolean SwitchReset = true; //Flag indicating that the hardware button has been released
const int TacSwitch = 0; //Pin for hardware momentary switch. On when grounded
const int RelayPin = 12; //Relay switching pin. Relay is pin 12
const int LED = 13; //On / Off indicator LED. Onboard LED is 13 on Sonoff
void setup()
{
pinMode(RelayPin, OUTPUT);
// digitalWrite(RelayPin, LOW);
pinMode(LED, OUTPUT);
// digitalWrite(LED, HIGH); //LED on sonoff is pulled up
pinMode(TacSwitch, INPUT_PULLUP);
Serial.begin(115200);
}
void loop()
{
Serial.println("ON");
delay(3000);
}
// Toggle the relay on
void lightOn() {
Serial.println("Outside switch ON!...");
digitalWrite(RelayPin, HIGH);
digitalWrite(LED, LOW);
LampState = 1;
}
// Toggle the relay off
void lightOff() {
Serial.println("Outside switch OFF ...");
digitalWrite(RelayPin, LOW);
digitalWrite(LED, HIGH);
LampState = 0;
}
// Handle hardware switch activation
void ButtonCheck(){
// look for new button press
boolean SwitchState = (digitalRead(TacSwitch));
// toggle the switch if there's a new button press
if (!SwitchState && SwitchReset == true){
Serial.println("Hardware switch for coffee activated");
if (LampState){
lightOff();
}
else{
lightOn();
}
// Flag that indicates the physical button hasn't been released
SwitchReset = false;
delay(50); //debounce
}
else if (SwitchState){
// reset flag the physical button release
SwitchReset = true;
}
}
void ToggleRelay(){
LampState = !LampState;
if (LampState){
lightOn();
}
else lightOff();
}
Yes - I use the following settings for the flash process - which completes fine. Thanks for taking interest - this was an awesome project and it has been pretty hard to troubleshoot.
I always use 74880 as the serial baud rate so that you can see the ESP boot/crash messages as well as the Serial.print data without changing the baud rate of the serial monitor.
Turns out i was uploading the sketch with Crystal frequency of 40hz, instead of 26hz. So serial works. I uploaded the second sketch, I get “ON” every three seconds.
BUT, if I use the main sketch for controlling sonoff, I still don’t get any connections. Usually, on serial port, after a connection establishes you get in address and all - but I don’t get anything.
You’re obviously using your original code for this, not the simple no-Blynk code.
Because this is using WiFi Manager, it’s setting up your Sonoff as a Wi-Fi access point (hotspot) and serving up a web page and DNS server on 192.168.4.1
Have you tried using a mobile device or laptop to connect to to the WiFi Manager hotspot?
This line of code should prevent WiFi Manager going into Hotspot mode, if the corret Wi-Fi Credentials are supplied:
I’m guessing that you haven’t replaced the “XXXXXXXXXXXX”, “XXXXXXXXXXX” with your home Wi-Fi SSID and password, or if you have you’ve mis-typed them in some way (remember that BOTH SSID and password are case sensitive)
@Pete, worth checking (the credentials) but @randomwalker pointed, the setup has been working for 20 days. If 2 lost connection… Perhaps some IP conflict in network??
To check (not restricted to):
@PeteKnight: This indeed was from the original code. I had the right SSID and password in my original code (but masked it for posting here). If I connect to the SSID when esp makes a hotspot, I do not get a captive portal - I think it is going in the error loop before it can complete that. Looking at the issue (https://github.com/esp8266/Arduino/issues/2922) I notice that this was blamed on board quality.
@marvin7: That is a god thought. I have a DCHP server setup on my router which should give a unique IP. When my sonoffs were connecting to server, I checked all the mac address and made them static. There should be no conflict.
General question: I did a quick search on STA disconnect: 201 or Wifi evt 1/7 etc.; I could not find much. Can anybody comment on that? Is there something specific these codes mean? More importantly, is this the ESP debugging, or wii manager debugging? The only sight going forward I have is to upload a generic code from wifimanager examples, and check the code. They are all brand new - I hope they are not all already fried at the same time. I should say that there is one ESP, which is still running under same code minus the DHT parts.
Sorry, I just meant that is how I actually got their individual mac address, when they were connected for the first time. Then, I look at my router settings, find the mac address of individual ones and bind it with a static IP. I will go home and get a screenshot.
Are there any example codes that will be useful to use as a troubleshooting aid? I am talking in the wifimanager library - which I am new to.
When I first stated using ESPs and Sonoffs, I played around with Wi-Fi Manager, but soon realised that it wasn’t for me. My devices are used in one or two different locations and would run a different set of code in each location, so would need re-flashing anyway.
For me, using OTA updates made much more sense, as it meant that I didn’t have to take my Sonoffs apart to reflash them.
Taking the Wi-Fi Manager stuff out of the code makes it much simpler and easier to work with.
I think that Blynk Provisioning has probably replaced Wi-Fi Manager in most situations anyway, although once again it’s not something I use as I have no need for it.
Yes, here I’m with @PeteKnight. Do you REALLY need WiFi Manager? Those serial outputs ( although not 100% sure) are coming just from WiFi Manager. You can look for these searching source code to shed more light.
Interesting, so you mean just connect as Blynk(auth, wifi ssid, password) instead of doing the whole wifi manager thing?
I used it for the ease of reconfiguration if I change my wifi password - which I never have. So I will try this, and see if it works! Thanks for the suggestion.