Hi. I’m trying to activate a led using the On/off state widget but when I try to turn it on it it turns off immediately, doing nothing.
Any tip?
Hi. I’m trying to activate a led using the On/off state widget but when I try to turn it on it it turns off immediately, doing nothing.
Any tip?
Hello sr,
It should a problem with your source code. Can you share it with us?
best!
Hi, Maybe this can help you.
#define THINGER_SERVER "your_server_address"
#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>
#include <Ticker.h>
Ticker SecondTick;
#define USERNAME "your_username_on_server"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_credential"
#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_password"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
byte WTDcount=0;
void ISRwatchdog()
{
WTDcount++;
if(WTDcount>4)
{
delay(100);
ESP.reset();
}
}
void setup() {
SecondTick.attach(1,ISRwatchdog);
thing.add_wifi(SSID, SSID_PASSWORD);
thing["millis"] >> outputValue(millis());
pinMode(5, OUTPUT); //RED D1
pinMode(4, OUTPUT); //YELLOW D2
pinMode(14, OUTPUT); //GREEN D5
thing["RED"] << digitalPin(5);
thing["YELLOW"] << digitalPin(4);
thing["GREEN"] << digitalPin(14);
thing["RSSI"] >> outputValue (WiFi.RSSI());
}
void loop() {
WTDcount=0;
thing.handle();
}
I use ESP8266 NodeMCU(ESP-12). Pins 5,4,14 (please refer to the ESP8266 pin reference, I used pins 5,4,14 for some reason because it uses several sensors on other pins) each pin connected to a simple two-stage switch circuit. Remember the BJT on first stage will invert the logic.
#include <Ticker.h>
-----> please see this for reference for arduino library to debounce button switches, detect presses, releases, and long presses.
Este es:
/*
Alarma antiincendios: NodeMCU v3, sensor de llama Ky026 & sensor DHT22 para
adquisición de Tª y %Hr
Monitorización y control manual en la plataforma www.thinger.io
*/
#define _DISABLE_TLS_
#include <ESP8266WiFi.h> //Librería de conexión WiFi del módulo ESP8266
#include <ThingerESP8266.h> //Librería de la plataforma thinger.io
#include "DHT.h" //Librería de los sensores DHT11, DHT22, etc.
// Parámetros de la conexión con Thinger.io
#define usuario "----------"
#define device_Id "----------"
#define device_credentials "----------"
ThingerESP8266 thing(usuario, device_Id, device_credentials);
// Parámetros de conexión WiFi
const char WiFi_ssid[] = "----------"; //Nombre de red
const char WiFi_password[] = "----------"; //Clave de red
// Parámetros del DHT
#define DHTPIN 4 //Pin de conexión - GPIO04 (D2)
#define DHTTYPE DHT22 //Modelo SENSOR
DHT dht(DHTPIN, DHTTYPE);
//activador del rele
bool fuego = true;
//declaración de puertos
const int llama = 5; //Sensor de llama -- Pin de conexión - GPIO05 (D1)
const int rele = 14; //Relé -- Pin de conexión - GPIO14 (D5)
void setup() {
//Iniciación de puertos
pinMode(llama, INPUT); //sensor de llama
pinMode(rele, OUTPUT); //relé
pinMode(2, OUTPUT); //led interno de la placa
//Inicialización del DHT22
dht.begin();
//Serial.begin(9600);
//Inicialización de la WiFi para comunicarse con API
thing.add_wifi(WiFi_ssid, WiFi_password);
thing["boton"] << [](pson & in) {
//interruptor manual
fuego = in && digitalRead(llama);
remoto = in;
};
//Inicialización de la lectura de datos desde la API
thing["dht"] >> [](pson & out) {
//factores de calibración
float cal_temp = -5.8;
float cal_hum = 8.1;
//envío de temperatura y humedad
out["Temperatura"] = dht.readTemperature() + cal_temp;
out["Humedad"] = dht.readHumidity() + cal_hum;
out["Fuego"] = digitalRead(llama);
};
}
void loop() {
thing.handle();
//Serial.print(fuego);
//Serial.print("\n");
digitalWrite(rele, !fuego);
digitalWrite(2, fuego);
}
Sorry, I only understand Portuguese (you speak Spanish) a bit and help with Google Translate. Let’s make it clearer. Do you mean you want to turn on the internal LED on Pin 2 with the widget (like your first question) as an alarm sign that the fire is already on?
Maybe this helps,
#define THINGER_SERVER "-------------"
#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>
#include <DHT.h>
#include <Ticker.h>
Ticker SecondTick;
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define USERNAME "---------------"
#define DEVICE_ID "---------------"
#define DEVICE_CREDENTIAL "---------------"
#define SSID "---------------"
#define SSID_PASSWORD "---------------"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
byte WTDcount=0; //additional: Force MCU restart to attempting reconnect if the wifi is lost
void ISRwatchdog()
{
WTDcount++;
if(WTDcount>4)
{
delay(100);
ESP.reset();
}
}
void setup() {
SecondTick.attach(1,ISRwatchdog);
thing.add_wifi(SSID, SSID_PASSWORD);
thing["millis"] >> outputValue(millis());
pinMode(2, OUTPUT); //LED or Relay to switch Buzzer and light as an ALARM (Manual Switch as you want)
dht.begin();
thing["DHT"] >> [](pson& out){
float cal_temp = -5.8;
float cal_hum = 8.1;
out["Humedad"] = dht.readHumidity();
out["Temperatura"] = dht.readTemperature();
};
thing["Fuego"] >> outputValue(!digitalRead(D1));
thing["ALARM"] << digitalPin(2); //Set your Widget as switch manual ALARM
thing["RSSI"] >> outputValue (WiFi.RSSI()); //additional: if you want to know how strong wifi signal
thing["MAC Address"] >> outputValue (WiFi.macAddress()); //additional: if you have another MCU and separated by MAC Address
}
void loop() {
WTDcount=0;
thing.handle();
}
Actually you can make an LED alarm or an automatic buzzer active by using the tutorial for controlling a digital pin just use the method explained in this resources section.
Really what I want is to turn on the relay (pin D5) with the flame sensor (to warn that there is fire) and with the widget (to use the relay as a remote switch).
Hello all,
I’m not pretty sure about your input resource structure… but please try with this:
thing["boton"] << [](pson & in) {
if(in.is_empty()){
in=(bool)digitalRead(llama);
}else{
digitalWrite(rele, in? HIGH:LOW);
digitalWrite(2, in? LOW:HIGH);
};
in this case, when the Pson has no data, the status will be the referred by the sensor read.
then, in your loop you will have to make a little hierarchy to prevent the status to change in the next execution, something like this:
void loop(){
thing.handle();
if(!remoto){
//Serial.print(fuego);
//Serial.print("\n");
digitalWrite(rele, !fuego);
digitalWrite(2, fuego);
}
}