Switching Led on and off


#1

Hello,
I want to add Led(s) in this program. Can you help me? I should be able to turn it on and off on thinger platform.

Regards.


#2

Hello @Navisen

You’ll be able to add a button to your dashboard with this code :

// Change LED_BUILTIN with the pin of your led.
pinMode(LED_BUILTIN, OUTPUT);

// Short version : "map" the resource "led" to the digital pin
thing["led"] << digitalPin(LED_BUILTIN);

// Long version that allow you to add logic.
thing["led"] << [](pson& in){
    if(in.is_empty()){
        // We send back the pin value to thinger platform
        in = (bool) digitalRead(LED_BUILTIN);
    }
    else{
        // This code is called whenever the "led" resource change
        digitalWrite(LED_BUILTIN, in ? HIGH : LOW);
    }
}

You’ll then have a new resource (led) available from your device and will be able to add an On/Off State widget.

And you’ll see a nice button like this on your dashboard :

2018-06-20_20-03-05

Also note that there may be actually a problem on the mobile app (at least Android) as I don’t see the toggle button for boolean resource.

You should also take a look at the input ressource section of the doc. Plentiful of explanation and examples.


Tips for sharing your code

```
code here
```


#3

I recomend you use the forum in the right way, is so uncomfortable to read your sketch from a printscreen (I dont even did it), there is a way to paste your code here in order to make easy to somebody else helps you

The way is paste here your code, select it, all and press “Ctrl+Shift+C” or pressing the “</>” button on forum toolbar, even is easier for you instead inserting an image.

Regards


#4

Hello,
The led stays on. The button on thinger is not doing anything. Can you help?

Regards.
#include <SPI.h>
#include <Ethernet.h>
#include <ThingerEthernet.h>
#include “DHT.h”

// dht config
#define DHTPIN 2
#define VENTPIN 3
#define DHTTYPE DHT11
#define led 10
DHT dht(DHTPIN, DHTTYPE);

// thinger.io config
ThingerEthernet thing(“", “Arduino”, "*”);

void setup() {
dht.begin();

thing[“dht11”] >> [](pson& out){
out[“humidity”] = dht.readHumidity();
out[“celsius”] = dht.readTemperature();
out[“fahrenheit”] = dht.readTemperature(true);
};
thing[“led”] << [](pson& in){
digitalWrite(10, in ? HIGH : LOW);
};
};
void loop() {
thing.handle();

if (dht.readTemperature() >= 28){
digitalWrite(VENTPIN, HIGH);
}
else {
digitalWrite(VENTPIN, LOW);
}
}


#5

Hello,
I have succeeded in implementing the led. Thank you for your help @f3rland.
Regards.