Device still working without internet


#1

Hi folks.

I have an embedded device. It has two sensors. In my code I did it works in two modes. Like automate device and control with thinger server. But if any circumstance the device lost the internet connection it automatically have to change in automate mode.

I want to change the code in tghe libraries of ESP8266WiFi…h ThingerESP8266.h to made that condition. Did anyone do it?


#2

You may want to paste sth. like the code below to get the info if the ESP is connected to WiFi

if(WiFi.status() != WL_CONNECTED)
{
// here you insert the code to trigger the auto mode
}
else
{
\and here the code to turn auto off
}
I see no reason why you need to put it into ESP library it doesnt take much space.

If you want to experiment messing with library to insert this function make a backup copy so you dont need to dowload new one ifit goes messy and not working.

I ve been making and repairing libraries for a while and until the function isnt tested better not to have it in library it is harder to find bugs.

Still respect your thaughts and not shure if i spoted your intend?
Hope that helps
Cherss Matej


#3

HI, thanks for ur answer.

But thinger.handle() is still trying to reconnect to networks, so my automatic function is not executing in real time, it have to wait the time in those lebraries.
I’m using ThingerWebConfig, i reduce the time to make the call to network to reconnect.
But I dont know how to make the loop execute my autmatic function and thinger.handle works at the same time.

#define _DEBUG_
#include <ESP8266WiFi.h>
#include <ThingerWebConfig.h>
#include <EEPROM.h>
#define LedStrip D1
//#define LedStrip 4

unsigned long interval1 = 1000;
unsigned long interval2 = 2000;
unsigned long previousMillis1;
unsigned long previousMillis2;
/*
 * Define a value for initial flash prog
 */
byte stPointLDR = 40;
int TimeFade = 30;
int TimeMax = 30;
float TimeMul = 8.5;

/*
 * Initial states for our variables
 */

int ledState = LOW; // Initial state from led mode
int SensorState, LDRVal;
bool SelectModo = false;
bool WifiChange = false;
int brightness = 0;
int SPLDR;

ThingerWebConfig thing;
void setup() {

  EEPROM.begin(512);
  Serial.begin(115200);
  stPointLDR = EEPROM.read(0);
  TimeFade = EEPROM.read(1);
  TimeMul = EEPROM.read(2);
  TimeMax = EEPROM.read(3);
  
  previousMillis1 = millis();
  previousMillis2 = millis();


  pinMode (LedStrip, OUTPUT);
  pinMode (LedModoAuto, OUTPUT);
  pinMode(PirSensor, INPUT);
  pinMode (A0, INPUT);
  pinMode (LedStrip, INPUT);
  analogWriteFreq(500);
  analogWriteRange(100);

  digitalWrite(LedStrip, HIGH);

  //analogWrite(LedStrip, 0);    // Set analogWrite to begin PWM

  thing["Intesidad"] << inputValue(brightness);
  thing["modo"] << inputValue(SelectModo);
  thing["wifi"] << inputValue(WifiChange);
  thing["StPointLDR"] << inputValue(stPointLDR, {
    EEPROM.write(0, stPointLDR);
    EEPROM.commit();
  });
  thing["TimeFade"] << inputValue(TimeFade, {
    EEPROM.write(1, TimeFade);
    EEPROM.commit();
  });
  thing["TimeMul"] << inputValue(TimeMul, {
    EEPROM.write(2, TimeMul);
    EEPROM.commit();
  });
  thing["TimeMax"] << inputValue(TimeMax, {
    EEPROM.write(3, TimeMax);
    EEPROM.commit();
  });

  
  thing["Estado"] >> [] (pson & out) {
    out = SelectModo;
  };
  //Reading value in memory and in value
  thing["SP"] >> [] (pson & out) {
    out = stPointLDR;
  };
  thing["SET"] >> [] (pson & out) {
    out = SPLDR;
  };
  
  //Reading the LDR value
  const int analogInPin = A0;
  thing["ReadLDR"] >> [] (pson & out) {
    int sensorValue = analogRead(analogInPin);
    out = sensorValue;
  };
  
}

void loop() {


  thing.handle();
  unsigned long currentMillis = millis();
  if ((unsigned long)(currentMillis - previousMillis1) >= interval1)
  {
    StatusWifi();
    WebServer();
    intensidad();
    previousMillis1 = millis();
  }
  if ((unsigned long)(currentMillis - previousMillis2) >= interval2)
  {

    ledEstadoAuto();
    previousMillis2 = millis();
  }
}

void WebServer() {
  switch (WifiChange) {
    case false:
      break;
    case true:
      WifiChange = false;
      WiFi.disconnect();
      
      break;
  }
}
void intensidad() {
  switch (SelectModo)
  {
    case false:
      automatico();
      break;
    case true:
      digitalWrite(LedModoAuto, HIGH);
      analogWrite(LedStrip, 100 - (brightness * 1));

      switch (brightness)
      {
        case 0:
          analogWrite(LedStrip, 100);
          break;
        case 1:
          analogWrite(LedStrip , 0);
          break;
        case 2:
          break;
      }
      break;
    case 2:
      break;
  }
}

void ledEstadoAuto() {
  if (ledState == LOW) {
    ledState = HIGH;
  } else {
    ledState = LOW;
  }
  digitalWrite(LedModoAuto, ledState);
}
int contador;
void automatico() {

 //Mode auto
}

void setFocoLed(float porcentaje) {
 
}

void StatusWifi() {
  if (WiFi.status() != WL_CONNECTED)
  {
    automatico();
  }

}

#4

All you need to do is
if (WiFi.status() == WL_CONNECTED)
{
thinger. handle() ;
}

TO stop thinger blocking when you try to do other things when offline.

Wherever you want to connect back you than call
Wifi.begin(ssid, pwd) ;

And thinger starts on its own because it is again online.

Hope that helps
Cheers


#5

I tried, but I’m defining the SSID and PWD on my sketch coz I’m using <ThingerWebConfig.h> and it’s work with wifimanager. Actually I’m looking for the way to call it from it.
Instead I just just WiFi.begin(); and not works.


#6

Oh, my mistake, you may need to call thing.add_wifi(SSID, PWD); instead.
Cheers


#7

Thnx u for ur time. I do it my way using “Threads”.