ESP8266 and LED

Hello there !

i can´t find a simple code for makeing a led turn on with ESP8266

i have a some temprature sensors that are connected to a raspbeery on the heating system for my house,

but when furnace stops or get an error, then i want to make a trigger that turns om a led on a esp8266 :slight_smile:

you can use this https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Actuators/LightSwitch/LightSwitch.ino and then set a tigger to temperature sensor but you need to use Sending MQTT messages within notification limit to avoid trigger flood.

i have my raspberry pi 3 with DS18B20 sensor, when this sensor get below a value, then the wireless Wemos D1 mini ( sorry i just mentions this now ) :frowning: has to turn on a led

have you got your wemos connected to cayenne? use cayenne MQTT library and this code https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Connections/ESP8266/ESP8266.ino to add your device to cayenne.
you can then add the actuator code from above link to this code to control the led from a button on the cayenne dashboard.
you can then directly create a new trigger and control the led with change in DS18B20 temperature on raspberry but then you will do trigger flood and it might stop working hence you will have to use cayenne MQTT python library to add the raspberry pi to cayenne. https://github.com/myDevicesIoT/Cayenne-MQTT-Python/blob/master/examples/Example-04-SendDataOnTrigger.py

Thanks ! got the button to work on wemos with led , but i have one more wemos, i can maybe use that one to use for the ds18b20 so i don’t get trigger flood ?

that the best thing you can do, but you will have to use the trigger limit code.

okay got all working now, but is it possible to make it blink, when the button is triggered by the raspberry?

you can code the esp to blink the led whenever you want.

so i have to make ( if ACTUATOR_PIN HIGH ) Then the blink Funktion on the pin i want to use ?

int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);

it just keeps blinking, what is wrong ?

"
#define VIRTUAL_CHANNEL 6
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
int ledPin = 2;

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ACTUATOR_PIN, OUTPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop(){
Cayenne.loop();
if(digitalRead(ACTUATOR_PIN == HIGH))
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}else{
digitalWrite(ledPin, LOW);
}

}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);
}"

something like this could work.

int value = 0;

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;
void loop(){
if (value == 1)
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
 value = getValue.asInt();
}

no i just stays on,

there must be a simple way to do it,

the led just has to blink when triggered :slight_smile:

can you share the entire code including the code i gave above.

sorry deleted it but here is the groundcode
"
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space

#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid =
char wifiPassword =

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username =
char password =
char clientID =

#define VIRTUAL_CHANNEL 6
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
int ledPin = 2;

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ACTUATOR_PIN, OUTPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop()
{
Cayenne.loop();

}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);

}
"

i need with the code i gave.

it comes with multiply errors when checking the code
i can find the correct order

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space

#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid =
char wifiPassword =

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username =
char password =
char clientID =

#define VIRTUAL_CHANNEL 6
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
int ledPin = 2;

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ACTUATOR_PIN, OUTPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

int value = 0;

// constants won’t change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW; // ledState used to set the LED

// Generally, you should use “unsigned long” for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated

// constants won’t change:
const long interval = 1000;
}
void loop(){
if (value == 1)
{
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
  ledState = HIGH;
} else {
  ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);

}
}
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
value = getValue.asInt();

i got it to work Thanks :slight_smile:

2 Likes