Have 5 sonoff S20, with the same code, but they all reboots periodicly.
read here something about pinMode and Blynk.syncAll. the solution was updating to 2.4.0
But I’m using 2.4.1 and still having the problem.
could it be wrong timed timers?
timer.setInterval(100000L, sendWifi);
timer.setInterval(60000L, syncRelay);
timer.setInterval(1000,showCurrentTime);
timer.setInterval(1000,ledStatus);
timer.setInterval(1000,pushButton);
timer.setInterval(10500L, sendSensor1);
here is the complete code, a mix of sketches, and a mess…
/*************************************************************
ITEAD Sonoff S31 and S20 for the Blynk app.
*************************************************************/
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 1 // (nodemcu D4=2 & TX=1 & RX=3) This is the ESP8266 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int buttonPin = 0; // Sonoff On/Off button
int relayPin = 12; // Sonoff relay
int ledPin = 13; // Sonoff green LED - always on
int relayStatus = 0;
int buttonState = LOW; // variable for reading the pushbutton status
int lastButtonState = HIGH; // previous state of the button
int relayState = LOW;
int ledState = LOW;
long lastTime = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
DeviceAddress tempSensor1 = { 0x28, 0xFF, 0x2F, 0x3E, 0x24, 0x17, 0x03, 0xE8 }; // Temperature probe #1
float temperature1; // Variables for storing temperatures
SimpleTimer timer;
WidgetRTC rtc;
char Date[16];
char Time[16];
int manual=0;
long startsecondswd; // weekday start time in seconds
long stopsecondswd; // weekday stop time in seconds
long nowseconds; // time now in seconds
// V1 is virtualbutton
// v2 test button for timer
// V8 is virtualLed-green
// V9 is greenstandyled
// V10 is virtualLed-blue
// V11 is Dateprint
// V12 is wifi signal
// v13 auto-manual
// V14 is virtual time-date picker
// V15 is DS18B20 sensor
// Your WiFi credentials.
// Set password to "" for open networks.
const char* ssid = "";
const char* password = "";
//char auth[] = "blynk authorization"; // Sonoff S31 "4M(3M SPIFFS)"
char auth[] = ""; // Sonoff S20 "1M(512k SPIFFS)"
WidgetLED led1(V10); // Widget Relay LED
WidgetLED led2(V8); // Widget Relay LED
BLYNK_WRITE(V1) { // Widget relay button
relayStatus = param.asInt();
if (relayStatus == 1) {
relayState = HIGH;
}
else {
relayState = LOW;
}
} // end - BLYNK_WRITE(V1)
void setup() {
WiFi.mode(WIFI_STA);
//Serial.begin(115200);
delay(10);
Blynk.begin(auth, ssid, password);
while (Blynk.connect() == false) {
//Wait until connected
Serial.print("trying to connect waiting");
}
rtc.begin();
pinMode(buttonPin, INPUT); // on/off button
pinMode(relayPin, OUTPUT); // relay
pinMode(ledPin, OUTPUT); // greenled
digitalWrite(ledPin, LOW); // always on
//Setting automaticmode to on(1)
Blynk.virtualWrite(V13, 0);
timer.setInterval(100000L, sendWifi);
timer.setInterval(60000L, syncRelay);
timer.setInterval(1000,showCurrentTime);
timer.setInterval(1000,ledStatus);
timer.setInterval(1000,pushButton);
//Setting tempSensor1
sensors.begin();
sensors.setResolution(tempSensor1, 12);
timer.setInterval(10500L, sendSensor1);
} // end - setup()
BLYNK_WRITE(V9)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 0) { // If value is 1 run this command
digitalWrite(ledPin, HIGH); //D4 output from Wemos D1 mini
led2.off();
}
if (pinValue == 1) { // If value is 1 run this command
digitalWrite(ledPin, millis()>>10 &1); //D4 output from Wemos D1 mini
}
if (pinValue == 2) { // If value is 0 run this command
digitalWrite(ledPin, LOW);
led2.on();
}
}
BLYNK_WRITE(V13)
{
if (param.asInt()==1)
{
manual=1;
}
else
{
manual=0;
}
}
BLYNK_WRITE(V14)//datepicker
{
if (manual==1)
{
sprintf(Date, "%02d/%02d/%04d", day(), month(), year());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
TimeInputParam t(param);
Serial.println(Time);
Serial.println(Date);
int dayadjustment = -1;
if(weekday() == 1){
dayadjustment = 6; // needed for Sunday, Time library is day 1 and Blynk is day 7
}
if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
Serial.println(startsecondswd); // used for debugging
if(nowseconds >= startsecondswd){
if(nowseconds <= startsecondswd + 90){ // 90s on 60s timer ensures 1 trigger command is sent
Blynk.virtualWrite(V1, 1);
// code here to switch the relay ON
}
}
else
{
}
stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
Serial.println(stopsecondswd); // used for debugging
if(nowseconds >= stopsecondswd){
Blynk.virtualWrite(V1, 0);
if(nowseconds <= stopsecondswd + 90){ // 90s on 60s timer ensures 1 trigger command is sent
Blynk.virtualWrite(V1, 0);
// code here to switch the relay OFF
}
}
else{
if(nowseconds >= startsecondswd){
Blynk.virtualWrite(V1, 1);
}
}
}
else
{
// nothing to do today, check again in 30 SECONDS time
}
}
}
void pushButton() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastButtonState == LOW && millis() - lastTime > debounce) {
if (relayState == HIGH) {
relayState = LOW;
Blynk.virtualWrite(V1, 0);
}
else {
relayState = HIGH;
Blynk.virtualWrite(V1, 1);
}
lastTime = millis();
}
digitalWrite(relayPin, relayState); // Relay open/close - digital pin 12
lastButtonState = buttonState;
} //end - pushButton()
void ledStatus() { // Widget LED - relay status
ledState = digitalRead(relayPin);
if (ledState == HIGH)
led1.on();
else
led1.off();
} // end - ledStatus()
void syncRelay()
{
Blynk.syncAll();
Serial.println("sync relay");
}
void showCurrentTime()
{
String CurrentDate = String(day()) + '-' + monthShortStr(month()) + '-' + year();
String CurrentTime = String(hour()) + ':' + minute() + ':' + second();
String formattedDate = CurrentDate + String(" | ") + CurrentTime;
Blynk.virtualWrite(V11,formattedDate);
}
void sendWifi()
{
Blynk.virtualWrite(12, map(WiFi.RSSI(), -105, -40, 0, 100) );
}
void sendSensor1()
{
sensors.requestTemperatures(); // Polls the sensors
temperature1 = sensors.getTempC(tempSensor1); // Stores temp in F. Change getTempF to getTempC for celcius.
Blynk.virtualWrite(15, temperature1); // Send temp to Blynk virtual pin 1
}
void loop() {
Blynk.run();
// pushButton();
timer.run();
}