Skip to content

Weird behaviour with more than two things #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lennart-k opened this issue Mar 30, 2020 · 2 comments
Closed

Weird behaviour with more than two things #106

lennart-k opened this issue Mar 30, 2020 · 2 comments

Comments

@lennart-k
Copy link

So I'd like to use expose five things with my ESP32:
The status LED and four relays.
This is my horrible code:

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

const char *ssid = "SeInternetzOfSings";
const char *password = "DieWolke";

WebThingAdapter *adapter;

const int statusLedPin = 2;
const int relay1Pin = 15;
const int relay2Pin = 4;
const int relay3Pin = 22;
const int relay4Pin = 23;

const char *ledTypes[] = {"OnOffSwitch", "Light", nullptr};
const char *relayTypes[] = {"OnOffSwitch", "SmartPlug", nullptr};

ThingDevice statusLed("led", "Built-in LED", ledTypes);
ThingProperty statusLedOn("on", "", BOOLEAN, "OnOffProperty");

ThingDevice relay1("relay1", "Relay 1", relayTypes);
ThingProperty relay1On("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relay2("relay2", "Relay 2", relayTypes);
ThingProperty relay2On("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relay3("relay3", "Relay 3", relayTypes);
ThingProperty relay3On("on", "", BOOLEAN, "OnOffProperty");
ThingDevice relay4("relay4", "Relay 4", relayTypes);
ThingProperty relay4On("on", "", BOOLEAN, "OnOffProperty");


void setup(void) {
  pinMode(statusLedPin, OUTPUT);
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(relay3Pin, OUTPUT);
  pinMode(relay4Pin, OUTPUT);
  
  Serial.begin(115200);
  Serial.print("\nConnecting to \"");
  Serial.print(ssid);
  Serial.println("\"");

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  bool blink = true;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    digitalWrite(statusLedPin, blink);
    blink = !blink;
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  adapter = new WebThingAdapter("w25", WiFi.localIP());

  statusLed.addProperty(&statusLedOn);
  adapter->addDevice(&statusLed);

  relay1.addProperty(&relay1On);
  adapter->addDevice(&relay1);
  
  relay2.addProperty(&relay2On);
  adapter->addDevice(&relay2);
  
  relay3.addProperty(&relay3On);
  adapter->addDevice(&relay3);

  relay4.addProperty(&relay4On);
  adapter->addDevice(&relay4);
  
  adapter->begin();
  Serial.println("HTTP server started");
  Serial.print("http://");
  Serial.println(WiFi.localIP());

}

void loop(void) {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connection lost, reconnecting");
    WiFi.reconnect();
    bool blink = true;
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
      Serial.println("");
      digitalWrite(statusLedPin, blink ? LOW : HIGH);
      blink = !blink;
    }
    Serial.println("Reconnected");
  }

  adapter->update();

  digitalWrite(statusLedPin, statusLedOn.getValue().boolean);
  digitalWrite(relay1Pin, relay1On.getValue().boolean);
  digitalWrite(relay2Pin, relay2On.getValue().boolean);
  digitalWrite(relay3Pin, relay3On.getValue().boolean);
  digitalWrite(relay4Pin, relay4On.getValue().boolean);
}

The weird thing is that I only get the led and relay1.
Relay 2-4 are nowhere displayed and cannot be accessed using the REST API.

Now when I comment out

statusLed.addProperty(&statusLedOn);
adapter->addDevice(&statusLed);

then only relay 1 and 2 get listed:

[
  {
    "id": "relay1",
    "title": "Relay 1",
    "@context": "https://iot.mozilla.org/schemas",
    "base": "http://192.168.3.103/",
    "securityDefinitions": {
      "nosec_sc": {
        "scheme": "nosec"
      }
    },
    "security": "nosec_sc",
    "@type": [],
    "links": [
      {
        "rel": "properties",
        "href": "/things/relay1/properties"
      },
      {
        "rel": "actions",
        "href": "/things/relay1/actions"
      },
      {
        "rel": "events",
        "href": "/things/relay1/events"
      },
      {
        "rel": "alternate",
        "href": "ws://192.168.3.103/things/relay1"
      }
    ],
    "properties": {
      "on": {
        "type": "boolean",
        "@type": "OnOffProperty",
        "links": [
          {
            "href": "/things/relay1/properties/on"
          }
        ]
      }
    },
    "href": "/things/relay1"
  },
  {
    "id": "relay2",
    "title": "Relay 2",
    "@context": "https://iot.mozilla.org/schemas",
    "base": "http://192.168.3.103/",
    "securityDefinitions": {
      "nosec_sc": {
        "scheme": "nosec"
      }
    },
    "security": "nosec_sc",
    "@type": [],
    "links": [
      {
        "rel": "properties",
        "href": "/things/relay2/properties"
      },
      {
        "rel": "actions"
      }
    ]
  }
]

But now I can access all relays with /things/relay[1-4].

I'm using [email protected] with PlatformIO.

@lennart-k
Copy link
Author

Oh, I don't why but now the only error is that not every relay gets listed and I bet its a duplicate of #10

@lennart-k
Copy link
Author

Yeah, just had to set LARGE_JSON_BUFFERS for anybody wondering in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant