Go Down

Topic: WiFi101WebThingAdapter (Read 340 times) previous topic - next topic

dr_esi

Hi,

Sorry to disturb you on a Sunday afternoon  ;)

I stumbled over some .h-files when trying to run the following example code:

Code: [Select]
#define LARGE_JSON_BUFFERS 1
#include <Arduino.h>
#include <Thing.h>
#include <WebThingAdapter.h>

#ifdef ESP32
#include <analogWrite.h>
#endif

const char *ssid = "....";
const char *password = "..........";

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

ThingActionObject *action_generator(DynamicJsonDocument *);

WebThingAdapter *adapter;

const char *lampTypes[] = {"OnOffSwitch", "Light", nullptr};
ThingDevice lamp("urn:dev:ops:my-lamp-1234", "My Lamp", lampTypes);

ThingProperty lampOn("on", "Whether the lamp is turned on", BOOLEAN,
                     "OnOffProperty");
ThingProperty lampLevel("brightness", "The level of light from 0-100", INTEGER,
                        "BrightnessProperty");

StaticJsonDocument<256> fadeInput;
JsonObject fadeInputObj = fadeInput.to<JsonObject>();
ThingAction fade("fade", "Fade", "Fade the lamp to a given level",
                 "FadeAction", &fadeInputObj, action_generator);
ThingEvent overheated("overheated",
                      "The lamp has exceeded its safe operating temperature",
                      NUMBER, "OverheatedEvent");

bool lastOn = true;

void setup(void) {
  pinMode(lampPin, OUTPUT);
  digitalWrite(lampPin, HIGH);
  Serial.begin(115200);
  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("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

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

  lamp.description = "A web connected lamp";

  lampOn.title = "On/Off";
  lamp.addProperty(&lampOn);

  lampLevel.title = "Brightness";
  lampLevel.minimum = 0;
  lampLevel.maximum = 100;
  lampLevel.unit = "percent";
  lamp.addProperty(&lampLevel);

  fadeInputObj["type"] = "object";
  JsonObject fadeInputProperties =
    fadeInputObj.createNestedObject("properties");
  JsonObject brightnessInput =
    fadeInputProperties.createNestedObject("brightness");
  brightnessInput["type"] = "integer";
  brightnessInput["minimum"] = 0;
  brightnessInput["maximum"] = 100;
  brightnessInput["unit"] = "percent";
  JsonObject durationInput =
    fadeInputProperties.createNestedObject("duration");
  durationInput["type"] = "integer";
  durationInput["minimum"] = 1;
  durationInput["unit"] = "milliseconds";
  lamp.addAction(&fade);

  overheated.unit = "degree celsius";
  lamp.addEvent(&overheated);

  adapter->addDevice(&lamp);
  adapter->begin();

  Serial.println("HTTP server started");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.print("/things/");
  Serial.println(lamp.id);

#ifdef analogWriteRange
  analogWriteRange(255);
#endif

  // set initial values
  ThingPropertyValue initialOn = {.boolean = true};
  lampOn.setValue(initialOn);
  (void)lampOn.changedValueOrNull();

  ThingPropertyValue initialLevel = {.integer = 50};
  lampLevel.setValue(initialLevel);
  (void)lampLevel.changedValueOrNull();

  analogWrite(lampPin, 128);

  randomSeed(analogRead(0));
}

void loop(void) {
  adapter->update();
  bool on = lampOn.getValue().boolean;
  if (on) {
    int level = map(lampLevel.getValue().number, 0, 100, 255, 0);
    analogWrite(lampPin, level);
  } else {
    analogWrite(lampPin, 255);
  }

  if (lastOn != on) {
    lastOn = on;
  }
}


It throws the following error:

libraries\webthing-arduino/WiFi101WebThingAdapter.h:27:10: fatal error: ArduinoMDNS.h: No such file or directory

 #include <ArduinoMDNS.h>


I tried to mend it following the suggestion of Jurai: https://forum.arduino.cc/index.php?topic=603001.0

this results then in the error:
'MDNS' does not name a type

   MDNS mdns;


So I guess ArduinoMDNS.h is needed: Dead end.
But I m not sure where to dig now.


Any hints or indication where my mistake lies would be appreciated.

Cheers


dr_esi

Some progress here:

I re-installed the ArduinoMDNS.h and this brought me one step further.

But the amount of error messages just exploded, over some JSON problem. Here just an extract with the effective error messages:

\Arduino\libraries\ArduinoJson\src/ArduinoJson/Variant/VariantFunctions.hpp:135:3: error: static assertion failed: To use 64-bit integers with ArduinoJson, you must set ARDUINOJSON_USE_LONG_LONG to 1. See https://arduinojson.org/v6/api/config/use_long_long/

   ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);

   ^


\ArduinoJson\src/ArduinoJson/Variant/VariantAs.hpp:58:3: error: static assertion failed: To use 64-bit integers with ArduinoJson, you must set ARDUINOJSON_USE_LONG_LONG to 1. See https://arduinojson.org/v6/api/config/use_long_long/

   ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);

   ^

exit status 1
Error compiling for board Arduino Uno.


what I also found out is that I installed the webthing-arduino library instead of the webthing library, as described on mozilla's homepage: https://iot.mozilla.org/framework/

I did not find a webthing library though on https://www.arduinolibraries.info/libraries


Still digging

dr_esi

Ok, figured out something more, according to https://arduinojson.org/v6/api/config/use_long_long/ I had to introduce

#define ARDUINOJSON_USE_LONG_LONG 1
just above
#include <ArduinoJson.h>
 in file Thing.h, which is located in the "webthing-arduino"-folder.

But it doesn't won't to start because of this, next error:

\AppData\Local\Temp\ccX8IYT4.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_WebthingsTes.ino.cpp.o.6136':

<artificial>:(.text.startup+0x37a): undefined reference to `action_generator(ArduinoJson6152_1000010::BasicJsonDocument<ArduinoJson6152_1000010::DefaultAllocator>*)'

<artificial>:(.text.startup+0x37c): undefined reference to `action_generator(ArduinoJson6152_1000010::BasicJsonDocument<ArduinoJson6152_1000010::DefaultAllocator>*)'

collect2.exe: error: ld returned 1 exit status


I run
webthing version 0.11.5
Arduino JSON version 6.15.2 (eventhough this video shows it with version 5 and it's mentioned that version 6 wouldn't work, I tried both and version 5 throws the error that some JSON objects are only within version 6)

So, looking for the next solution.

Cheers

dr_esi

Ok, I adressed the same topic at Mozillas WebThings Forum.

You can find the answer there, the example was incomplete...

Btw the readme example works.

Go Up