Multiple things on Arduino

Hi,
I have a basement where I read out several sensor values (about 10) with an Arduino Mega. Until now I published the values via an Ethernet Shield to my network as a web server.
As I found WebThings, I thougt great!
But now I am struggling with multiple things from the same arduino.
Is multiple things implementet in the Arduino? Is there a documentation or better an example?

I tried just to copy the LED example, but did not get far with this. My goal was to see two things on the gateway. The first one is found, but the second (led2) returns 400.
Maybe you could give me a hint…

#define LARGE_JSON_BUFFERS 1
#define ARDUINOJSON_USE_LONG_LONG 1

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

#if defined(LED_BUILTIN)
  const int ledPin = LED_BUILTIN;
#else
  const int ledPin = 13; // manually configure LED pin
#endif

WebThingAdapter *adapter;
WebThingAdapter *adapter2;

const char *ledTypes[] = {"OnOffSwitch", "Light", nullptr};
ThingDevice led("led", "Built-in LED", ledTypes);
ThingDevice led2("led2", "Built-out LED", ledTypes);
ThingProperty ledOn("on", "", BOOLEAN, "OnOffProperty");

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFB, 0x80 };
IPAddress ip(192, 168, 0, 100);
//EthernetServer server(80);

bool lastOn = false;

void setup(void) {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  Serial.begin(9600);
  Serial.println("");
  bool blink = true;
  Ethernet.begin(mac, ip);
  //  server.begin();
  Serial.print("Server gestartet. IP: ");
  Serial.println(Ethernet.localIP());
  // Wait for connection

  //  while (WiFi.status() != WL_CONNECTED) {
  //    delay(500);
  //    Serial.print(".");
  //    digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
  //    blink = !blink;
  //  }
  digitalWrite(ledPin, HIGH); // active low led

  adapter = new WebThingAdapter("w25", Ethernet.localIP());
  adapter2 = new WebThingAdapter("w26", Ethernet.localIP());

  led.addProperty(&ledOn);
  led2.addProperty(&ledOn);
  adapter->addDevice(&led);
  adapter2->addDevice(&led2);
  adapter->begin();
  adapter2->begin();
  Serial.println("HTTP server started");
  Serial.print("http://");
  Serial.print(Ethernet.localIP());
  Serial.print("/things/");
  Serial.println(led.id);

  
  Serial.println("HTTP server started");
  Serial.print("http://");
  Serial.print(Ethernet.localIP());
  Serial.print("/things/");
  Serial.println(led2.id);
}

void loop(void) {
  adapter->update();
  bool on = ledOn.getValue().boolean;
  digitalWrite(ledPin, on ? LOW : HIGH); // active low led
  if (on != lastOn) {
    Serial.print(led.id);
    Serial.print(": ");
    Serial.println(on);
  }
  lastOn = on;
}

This seems to work:

#define LARGE_JSON_BUFFERS 1
#define ARDUINOJSON_USE_LONG_LONG 1

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

#if defined(LED_BUILTIN)
  const int ledPin = LED_BUILTIN;
#else
  const int ledPin = 13; // manually configure LED pin
#endif

WebThingAdapter *adapter;

const char *ledTypes[] = {"Light", nullptr};
ThingDevice led1("led1", "Built-in LED 1", ledTypes);
ThingDevice led2("led2", "Built-in LED 2", ledTypes);
ThingProperty ledOn1("on", "", BOOLEAN, "OnOffProperty");
ThingProperty ledOn2("on", "", BOOLEAN, "OnOffProperty");

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFB, 0x80 };
IPAddress ip(192, 168, 0, 100);

bool lastOn = false;

void setup(void) {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

  Serial.begin(9600);
  Serial.println("");
  Ethernet.begin(mac, ip);

  Serial.print("Server started. IP: ");
  Serial.println(Ethernet.localIP());

  adapter = new WebThingAdapter("w25", Ethernet.localIP());

  led1.addProperty(&ledOn1);
  led2.addProperty(&ledOn2);
  adapter->addDevice(&led1);
  adapter->addDevice(&led2);
  adapter->begin();

  Serial.println("HTTP server started");
  Serial.print("http://");
  Serial.print(Ethernet.localIP());
  Serial.print("/things/");
  Serial.println(led1.id);
  Serial.print("http://");
  Serial.print(Ethernet.localIP());
  Serial.print("/things/");
  Serial.println(led2.id);
}

void loop(void) {
  adapter->update();
  bool on = ledOn1.getValue().boolean;
  digitalWrite(ledPin, on ? LOW : HIGH); // active low led
  if (on != lastOn) {
    Serial.print(led1.id);
    Serial.print(": ");
    Serial.println(on);
  }
  lastOn = on;
}

Notably:

  • You only one to create a single adapter, and attach all devices to it.
  • Do not reuse properties for multiple devices.
1 Like

Sweet! I was there using a single adapter at some point, but must have made another mistake.

Great! Thank you :slight_smile: