Webthing-arduino ThingProperty Unit

Hi,

I’m wondering if there is a way to set a ThingProperty Unit when using the MultiLevelSensor capabilty with the LevelProperty.

This is how I have it setup right now and I’m able to get my value as a Number but without a Unit.

const char* sensorTypes[] = {"MultiLevelSensor", nullptr};
ThingDevice soilMoistureSensor("soilMoistureSensor", "Analog Soil Moisture Sensor", sensorTypes);
ThingProperty moistureLevel("moistureLevel", "Percent of moisture in the soil", NUMBER, "LevelProperty");

LevelProperty mentions an optional Unit but I’m not sure how to set it using webthing-arduino.

You should be able to do this:

moistureLevel.unit = "percent";

I tried adding that line directly below the existing code

const char* sensorTypes[] = {"MultiLevelSensor", nullptr};
ThingDevice soilMoistureSensor("soilMoistureSensor", "Analog Soil Moisture Sensor", sensorTypes);
ThingProperty moistureLevel("moistureLevel", "Percent of moisture in the soil", NUMBER, "LevelProperty");
moistureLevel.unit = "percent";

The Arduino IDE says

'moistureLevel' does not name a type

What version of webthing-arduino are you using? Are you using the published v0.4.1 version, or the latest in GitHub?

That line should be placed in the setup function, not the top level

I’m using v0.4.1 installed through the Library manager in the Arduino IDE

I moved the line to the setup function

const char* sensorTypes[] = {"MultiLevelSensor", nullptr};
ThingDevice soilMoistureSensor("soilMoistureSensor", "Analog Soil Moisture Sensor", sensorTypes);
ThingProperty moistureLevel("moistureLevel", "Percent of moisture in the soil", NUMBER, "LevelProperty");


void setup() {
  moistureLevel.unit = "percent";

I’m now getting a new error but seem to be getting closer

'class ThingProperty' has no member named 'unit'

You’ll need to switch to a newer version of the webthing-arduino library, from GitHub.

I’m sure @hobinjk can provide assistance with that, if needed.

Yes, I should have just updated the version of the library available in the Arduino IDE library manager but while that’s propagating outwards you can also download the zip from https://github.com/mozilla-iot/webthing-arduino/releases/download/v0.9.0/webthing-arduino.zip

Many thanks! Cloned the git repo and the error is gone, flashing now :slight_smile:

Confirmed, is working. I also managed to add readOnly and it works as intended :slight_smile:

I noticed that these were not working

moistureLevel.title = "Moisture";
moistureLevel.minimum = 0;
moistureLevel.maximum = 100;

So I tried adding them to Thing.h and ESPWebThingAdapter.h in the same way that unit and readOnly are added and it seems to work!

Thing.h

class ThingItem {
public:
  String id;
  String description;
  ThingPropertyType type;
  String atType;
  ThingItem* next = nullptr;

  bool readOnly = false;
  String unit = "";
  // this is what I added
  String title = "";
  double minimum;
  double maximum;

ESPWebThingAdapter.h

      if (item->readOnly) {
        prop["readOnly"] = true;
      }

      if (item->unit != "") {
        prop["unit"] = item->unit;
      }
      // this is what I added
      if (item->title != "") {
        prop["title"] = item->title;
      }

      if (item->minimum < item->maximum) {
        prop["minimum"] = item->minimum;
      }

      if (item->maximum > item->minimum) {
        prop["maximum"] = item->maximum;
      }

Would you want to open a PR with those changes?

Absolutely! I haven’t done it before but I read through this: https://help.github.com/en/articles/creating-a-pull-request

Which suggests that I need write access to create a branch

Anyone with read permissions to a repository can create a pull request, but you must have write permissions to create a branch.

I did so and tried to push the changes but I got this error:

remote: Permission to mozilla-iot/webthing-arduino.git denied to lundsholm.
fatal: unable to access 'https://github.com/mozilla-iot/webthing-arduino.git/': The requested URL returned error: 403

EDIT: @mrstegeman Let me know if I should contact you any other way to keep this thread clean and on topic :stuck_out_tongue:

You’ll actually want to fork the repo first, then create a branch and submit a PR from that. Here’s a nice tutorial: https://gist.github.com/Chaser324/ce0505fbed06b947d962

Thank you! PR is submitted :nerd_face: