Control varialbles problem ->Thinger>ESPmodule >Arduino

For a few days I am working with Thinger i’ve got working the setup with Thinger sending data through sliders reciving it with ESP12 module sending it to Arduino through Serial comunication. Than arduino sends data that it gets back all the way to Thinger where i see the response.

The part whitch posts the data back to Thinger is working perfectly, no errors.

But i am not shure why at holding slider in Thinger i got garbage values to Arduino and the same number is shown on Back Info posted to Thinger.

I ve used these kind of comand --thing[“logging”] << inputValue(sdLogging);
and it got the right value when clicked to set slider but with garbage when sliding slider.

When switching to this kind of comand–
thing[“hysteresis”] << [](pson& in){ hysteresis = in;};
I can send values through API window, on the other hand slider recognized input but it refuses to set value - jumps back to 0.

Cant find the bug.

//Code for ESP module

poslji= send
prejmi = recive

#include “SPI.h”
#include “ESP8266WiFi.h”
#include “ThingerWifi.h”

#include “DHT.h”

define DHTTIP DHT22
define DHTPIN 2
DHT dht(DHTPIN, DHTTIP);

ThingerWifi thing(“", "*”, “******”);
int vred;
int UPindex;
String Str;
String Sd;
String Ca;

int casZl=0;
int SvZv=1;
int SvZj=2;
int ventl=3;
int Minut;
int SvetlostZvecer;
int SvetlostZjutraj;
int i=1;
double temp;
unsigned long lastCheck = 0;
unsigned long ZC = 0;
int T = 1000;

int HomeVaria [20];
int UpVaria [20];

void setup() {
Serial.begin(9600);
pinMode(DHTPIN,INPUT);
thing.add_wifi("*****", “*******”);

thing[“DHT”] >> [](pson& out){
out[“Temperatura”] = dht.readTemperature();
out[“Vlaga”] = dht.readHumidity();
};

thing[“Back_Info”] >> [](pson& out){
out[“Minut”] = UpVaria[casZl];
out[“SvetlostZvecer”] = UpVaria[SvZv];
out[“SvetlostZjutraj”] = UpVaria[SvZj];
};

thing[“Minut”] << [](pson& in){Minut=in;};
thing[“SvetlostZvecer”] << [](pson& in){SvetlostZvecer=in;};
thing[“SvetlostZjutraj”] << [](pson& in){SvetlostZjutraj=in;};

}

void loop()
{
if(millis()-ZC>T)
{
ZC=millis();
thing.handle();
poslji(0,Minut);
poslji(1,SvetlostZvecer);
poslji(2,SvetlostZjutraj);
prejmi();

}
if((millis()-lastCheck>=60601000)||i==1)
{
i=2;
lastCheck = millis();
if(dht.readTemperature()>24)
{
thing.call_endpoint(“Temp_23_plus”);
}
}
}

void poslji(int index, int spr)
{
int sp;
if((spr!= HomeVaria[index])&&(0<spr<256))
{
HomeVaria[index]= spr;
sp=0;
if(index<10)
{
Serial.print(“0”+String(index) + String(spr));
}
else
{
Serial.print(String(index) + String(spr));
}
}

}
void prejmi()
{
if(Serial.available())
{
Str = Serial.readString();
Ca = Str.substring(0,2);
Sd = Str.substring(2,Str.length());
vred = Sd.toInt();
UPindex = Ca.toInt();

UpVaria[UPindex]=vred;

}
}

//Code for Arduino

#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27,16,2);
#include "Wire.h"
int casZl=0;
int SvZv=1;
int SvZj=2;
int ventl=3;
int vred;
int po = 1;
int HOMEindex;
String Str;
String Sd;
String Ca;
int HomeVaria [20];
int UpVaria [20];

unsigned long ZC = 0;
int T = 1000;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.init();
lcd.backlight();
}

void loop()
{
if(millis()-ZC>T)
{
ZC=millis();
prejmi();
poslji(0,HomeVaria[casZl]);
poslji(1,HomeVaria[SvZv]);
poslji(2,HomeVaria[SvZj]);
po++;
LCD();
}
}

void poslji(int index, int spr)
{
if((spr!= UpVaria[index])||po<8)
{
UpVaria[index]= spr;
if(index<10)
{
Serial.print(“0”+String(index) + String(spr));
}
else
{
Serial.print(String(index) + String(spr));
}
}

}

void prejmi()
{
if(Serial.available())
{
Str = Serial.readString();
Ca = Str.substring(0,2);
Sd = Str.substring(2,Str.length());
vred = Sd.toInt();
HOMEindex = Ca.toInt();

HomeVaria[HOMEindex]=vred;
}
}
void LCD()
{
lcd.setCursor(0,0);
lcd.print(“Cas:”+String(HomeVaria[casZl]));
lcd.print(" SvZv:"+String(HomeVaria[SvZv]));
lcd.setCursor(0,1);
lcd.print(" SvZj:"+String(HomeVaria[SvZj]));
lcd.print(" Test:");
}

Thanks in anvance, Matej.

Hi, you can use thing["logging"] << inputValue(sdLogging); with any other variable you want, like an integer holding the slider value, a float, and so on, just put the variable you want to modify (except a string). This should fix some problems with zeroes or garbage.

If you want to change to the other complex notation (i.e. for sending the value to serial just right away when you receive it), you should slightly modify the code to avoid values being reset, something like this:

thing["SvetlostZjutraj"] << [](pson& in){
    if(in.is_empty(){
        in = SvetlostZjutraj;
    }else{
        SvetlostZjutraj=in;
        // your variable has been modified here
    } 
};

This code is exactly what the inputValue macro does. It provides the current value when you are querying the resource, and update the current value if there is content to set. Hope that helps!

Thanks, it helped. Nice feature -> in.is_empty()
The comunication works fine now,

But i’m having problems with ESP 12 module being disconected every few days and than it dosn’t connect untill i reset it.

May i ask you why is it due to the long distance to the router ( 7 meters and wall of the house in between) - outside in greenhouse (with phone i get 25% signal strenght )

Or it can’t connect itself back after possibly IP confilct.

Thanks for helping me.

Hi, not sure if the signal is the problem. I have seen NodeMCU connected several months (more than 7 meters away). Sometimes they get disconnected, but always seems to connect back again. What is the power supply of your ESP12? It would be useful to enable the debug feature to see what is happening, or try to keep another ESP12 closer to the router. I don’t think it is a problem with the IP, as the reconnection mechanism trieas to reconnect to the access point, at it will eventually get a new IP address.

http://docs.thinger.io/arduino/#coding-enabling-debug-output

Hi, the module in greenhouse now works fine. It was an underpowered power supply.

But i’ve got a problem with serial comunication between arduino and ESP module with the code that i’m having on experimental setup .

I want to just send some readings from arduino inputs to the Thinger,

you may say to me i ve already done this at the greenhouse module but even there it refuses to send any other readings than just Back_Info ???

It acts the same as this code down there:

for Arduino

String Str,Ca,Sdd;
int vred;
unsigned long ZC;

void setup() 
{
   Serial.begin(9600);
}

void loop() 
{
  if(Serial.available())
  {
    Str = Serial.readString();
    Ca = Str.substring(0,3);
    Sdd = Str.substring(3,Str.length());
    vred = Sdd.toInt();
   
    if((vred<256)&&(vred>=0)){
     if(Ca.compareTo("led")==0) analogWrite(5,vred);
     if(Ca.compareTo("pwm")==0) analogWrite(6,vred);
   }
  }
  
  if(millis()-ZC>1000)
{
  ZC=millis();
  int Pot = analogRead(0);
  Serial.print("pot"+String(Pot));
}
}

for ESP

 #include "ESP8266WiFi.h"
 #include "ThingerWifi.h"

ThingerWifi thing("Matej", "ESP_201", "**********");


String Str,Sdd,Ca;
int PWM,LED,Pot,vred;
unsigned long ZC;

 // int UpVaria [20];

void setup() {
  Serial.begin(9600);

  thing.add_wifi("*****", "*************");
  
  thing["Pot"] >> [](pson& out){
      out = Pot; 
};


 thing["Motor_PWM"] << inputValue(PWM);
 thing["Led"] << inputValue(LED);
}

void loop() 
{
 
    thing.handle();
    if(millis()-ZC>1000)
    {
      ZC=millis();
Serial.println("pwm"+String(PWM));
Serial.println("led"+String(LED));
    }

 if(Serial.available())
  {
    Str = Serial.readString();
    Ca = Str.substring(0,3);
    Sdd = Str.substring(3,Str.length());
    vred = Sdd.toInt();
   
if((vred<256)&&(vred>=0))
  {

     if(Ca.compareTo("pot")==0) Pot=vred;
   } 
    
}
}

and if I connect the arduino to serial monitor i can read all data as it should be sent
And if i write the same data on serial monitor connected to ESP it gets to the thinger

Why it dosen’t when Aarduino and ESP are connected together

if i Spy the connection when they are connected together i can still see data sent and rx tx lights are blinking but data doesn’t come through.

Thanks for help in advance,
Matej.

Hi @Matej, I have been checking your code and everything seems to be fine. I don’t know why you cannot share between Arduino and ESP! :confused:

Not sure if it matters in the Arduino IDE, but I just recommend you to initialize ZC to zero, as it could be initialized with garbage, and fail to check in your evaluation and then send data…