After hundreds of attempts, I identified the problem: in order for the control switch, on the dashboard, to open at the last value, the resource name must be written in block capitals.
thing[“led”] << [](pson& in){ …etc WORKS
thing[“LED”] << [](pson& in){ …etc DOESN’T WORK
I’ve made many many test…that is what I’ve found…
Could you make a test on this and give me a feedback?
THIS WORKS: (control switch in dashboard linked to resource “LED”)
#include <ESP8266WiFi.h> //ESP8266 WiFi connection library
#include <ThingerESP8266.h> //THINGER.IO library
// Thinger.io connection parameters
#define USERNAME "davidealessandrini"
#define DEVICE_ID "esp8266"
#define DEVICE_CREDENTIAL "WRLbbOtuaYb&"
#define SSID "******"
#define SSID_PASSWORD "********"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
// Global variable
int led = 2; // GPIO02 - D4
int led_status = 0;
void setup() {
// Initialization of the WiFi connection with THINGER.IO
thing.add_wifi(SSID, SSID_PASSWORD);
// Initialization of the LED
pinMode(led, OUTPUT);
// Resource for changing LED status from THINGER.IO
thing["LED"] << [](pson& in){
if(in.is_empty()){
in = (bool)led_status;//(bool) led_status;//digitalRead(led);
}
else{
//digitalWrite(led, in ? HIGH : LOW);
led_status = in;
}
};
}
void loop() {
thing.handle();
digitalWrite(led, led_status);
}
THIS DOESN’T WORK: (control switch in dashboard linked to resource “led”)
#include <ESP8266WiFi.h> //ESP8266 WiFi connection library
#include <ThingerESP8266.h> //THINGER.IO library
// Thinger.io connection parameters
#define USERNAME "davidealessandrini"
#define DEVICE_ID "esp8266"
#define DEVICE_CREDENTIAL "WRLbbOtuaYb&"
#define SSID "******"
#define SSID_PASSWORD "********"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
// Global variable
int led = 2; // GPIO02 - D4
int led_status = 0;
void setup() {
// Initialization of the WiFi connection with THINGER.IO
thing.add_wifi(SSID, SSID_PASSWORD);
// Initialization of the LED
pinMode(led, OUTPUT);
// Resource for changing LED status from THINGER.IO
thing["led"] << [](pson& in){
if(in.is_empty()){
in = (bool)led_status;//(bool) led_status;//digitalRead(led);
}
else{
//digitalWrite(led, in ? HIGH : LOW);
led_status = in;
}
};
}
void loop() {
thing.handle();
digitalWrite(led, led_status);
}
This is not enough, casting to bool is required, even if I use “LED”, casting to bool is mandatory for dashbord to work correctly.