Thing with three properties

Hello everybody!

I am newbie in IOT. It is my first project and I started with installation of Web Things gateway on Raspberry Pi. Everything works fine

and I paired my WiFi and z-wave things with the gateway and played with rules a little bit.

Now I am trying to make the sketch for my Thing on ESP 8266 having three “OnOff” properties. As the base for this sketch I used

LED example with three properties:

https://github.com/mozilla-iot/webthing-arduino/blob/master/examples/RGBLamp/RGBLamp.ino

I paired the example LED Thing with “Level", “OnOff" and “Color" properties to the gateway and saw that graphically it is displayed

in the form of three controls.

But when I tried to make the Thing with three “OnOff" properties it was displayed in form of one on-off switch.

Is it possible to use several same properties for one web thing?

For properties description I used the code:

WebThingAdapter* adapter;

const char* deviceTypes[] = {“OnOffSwitch”, “OnOffSwitch”, “OnOffSwitch”, nullptr};

ThingDevice device(“switch1-switch2-switch3”, “OnOffSwitch OnOffSwitch OnOffSwitch”, deviceTypes);

ThingProperty switch1(“on”, “Whether the pumpone is turned on”, BOOLEAN, “OnOffProperty”);

ThingProperty switch2(“on”, “Whether the pumptwo is turned on”, BOOLEAN, “OnOffProperty”);

ThingProperty switch3(“on”, “Whether the pumpthree is turned on”, BOOLEAN, "OnOffProperty”);

Please advise what’s wrong with this code.

Thank you for support!

That’s close to working! There’s a slightly better way to do it though. A single arduino can serve multiple web things so you can do the following:

WebThingAdapter* adapter;

const char* deviceTypes[] = {"OnOffSwitch", nullptr};

ThingDevice device1("switch1", "Pump One", deviceTypes);
ThingDevice device2("switch1", "Pump One", deviceTypes);
ThingDevice device3("switch1", "Pump One", deviceTypes);

ThingProperty on1("on", "On/Off", BOOLEAN, "OnOffProperty");
ThingProperty on2("on", "On/Off", BOOLEAN, "OnOffProperty");
ThingProperty on3("on", "On/Off", BOOLEAN, "OnOffProperty");

void setup() {
  // ...

  adapter = new WebThingAdapter("pumps", WiFi.localIP());
  device1.addProperty(&on1);
  device2.addProperty(&on2);
  device3.addProperty(&on3);
  adapter->addDevice(&device1);
  adapter->addDevice(&device2);
  adapter->addDevice(&device3);
  adapter->begin();
}

If you’d prefer to stick with your current code the only thing preventing it from working is that switch1, switch2, and switch3 should have their names be “on1”, “on2”, and “on3” so that they don’t overlap

Thank you very much! Will try it on ASAP and let you know the result.

Hello James,

I managed to set up my thing. Thank you very much!

Best regards,

Alex

Hello James,

Sorry for bothering you but please help me some more. Where can I find the chatid and token for Telegram-sender-adapter. I would like to use this add-on in my project. Thank you, Alex

No problem! I’m not the developer of the telegram adapter so I’m not the expert here but the token is from the bot setup process described here: https://github.com/hosein2398/node-telegram-bot-api-tutorial#creating-new-bot-with-botfather. Getting the chat id is a bit more difficult but if you run the following NodeJS script then message your bot you should be able to get it:

const TelegramBot = require('node-telegram-bot-api'); 
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
const bot = new TelegramBot(token, {polling: true});
    
bot.on('message', (msg) => {
  console.log('your chat id', msg.chat.id);
});

Thank you very much! I’ll try.

Hello James,
I am trying to use E-mail sender add-on in my project. I followed your guide on GitHub and got app password. Which e-mail address I have to use for sender configuration - the e-mail address of Google account where I got password, the secureally@gmail.com address or any other gmail address? Thank you, Alex

Hi, thanks for your interest! You should use the email address of the account you were signed into when you got the app password. If you want to switch this around you can click the top right profile bubble on the https://myaccount.google.com/ page to select a different account

Hi James, I set up my E-mail sender configuration and used it in new Rule - send notification when one of my switchers is on. I am also sending notification using browser notification. Messages were not delivered. Please advise what to check.
Thanks, Alex

#define ARDUINOJSON_USE_LONG_LONG 1

#include <Arduino.h>
#include "Thing.h"
#include "WebThingAdapter.h"

const char *ssid = "wifi";
const char *password = "pas";

WebThingAdapter *adapter;

const char *deviceTypes[] = {"OnOffSwitch", nullptr};
ThingDevice device1("switc1", "Led One", deviceTypes);
ThingDevice device2("switch2", "Led Two", deviceTypes);
ThingDevice device3("switch3", "Led Three", deviceTypes);


ThingProperty on1("on1", "On/Off", BOOLEAN, "OnOffProperty");
ThingProperty on2("on2", "On/Off", BOOLEAN, "OnOffProperty");
ThingProperty on3("on3", "On/Off", BOOLEAN, "OnOffProperty");

void setup(void)
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("");
  Serial.print("Connecting to \"");
  Serial.print(ssid);
  Serial.println("\"");
#if defined(ESP8266) || defined(ESP32)
  WiFi.mode(WIFI_STA);
#endif
  WiFi.begin(ssid, password);
  Serial.println("");
  bool blink = true;
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
    digitalWrite(LED_BUILTIN, blink ? LOW : HIGH); // active low led
    blink = !blink;
  }
  adapter = new WebThingAdapter("leds", WiFi.localIP());
  device1.addProperty(&on1);
  device2.addProperty(&on2);
  device3.addProperty(&on3);
  adapter->addDevice(&device1);
  adapter->addDevice(&device2);
  adapter->addDevice(&device3);
  adapter->begin();

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println("HTTP server started");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.print("/things/");
}

void loop()
{
  adapter->update();
  bool one = on1.getValue().boolean;
  bool two = on2.getValue().boolean;
  bool three = on3.getValue().boolean;
}

I did something like this. Only first two things are visible on gateway and only one is controllable. I am new to this. Please help.

I am using Raspberry Pi 3b+ for gateway &
Wemos D1 R2 (ESP8266) for creating things.

Thanks.

Sorry, for to include #define LARGE_JSON_BUFFERS 1. Works fine now.

Thank you very much!