Web Thing Step-By Step

Struggling to understand how to use this addon !

If I click add new thing, I can then click ‘add by url’ but any url I enter I get a 404 not found error message, even if I know for sure that this is a valid url.

If I enter the url of the gateway http://192.168.0.20/ I also get a 404 not found.

But let’s say I add a url, then what do I do ???

OK, lets say I have a nodejs web server running and listening on port 3000, it simply accepts requests and sends a simple echo response.

When I enter the url in ‘add thing’ I get a 404 even though I can access vi a web browser !

So how exactly does this work ?? - assuming that I can customise my response to url requests on this web server and perform i/o actions and reply to the iot-gateway, what is needed for the most basic request/response so I can at least get something working.

Just to try and figure this out, I had the server respond with a JSON array of things when doing a GET to /, but I have no idea if this is correct:

[
  "thing-a",
  "thing-b",
  "thing-c"
]

Not having much success today :frowning:

OK, this may be obvious to everyone else except me.

Settings/addons/webthing > Configure thing-url-adapter - this adds the server url, and mozilla-iot will poll this url (root) to get a list of devices: GET http://myhost/

The server then responds with a list of things:

[{
    "@context": "https://iot.mozilla.org/schemas/",
    "@type": ["Light", "OnOffSwitch"],
    "name":"LED",
    "description": "LED",
    "properties": {
    
      "on": {
        "@type": "OnOffProperty",
        "type": "boolean",
        "href": "/things/led/properties/on",
        "value": false
      }
    }
  }
]

the gateway will then query the webserver for the thing status:

GET http://myhost//things/led/properties/on

The server then responds with the status:

{"led":true/false}

If you toggle the thing on/off, then the iot-gateway sends a POST instead of a GET, the web server can then perform it’s I/O and reply with the same status as for the query.

I don’t know if this is the correct usage, but it seems to work, now I understand this I can also create web server devices on other platforms such as micropython.

This is what I was trying to understand.

When you create your own web things that you want to associate with the gateway, if they are native web things you should be able to see their thing description at their URL (IP address), like the attached image shows. If that doesn’t load correctly in your browser then there’s likely a syntax error in the JSON thing description.

Why is it that when I switch a web-thing LED on and off, the request is PUT properties and not POST actions ???

This is what is sent to the things server when I toggle the LED:

PUT /things/led/properties/on

Interesting question. I had to search it and i found a very good explanation: https://www.keycdn.com/support/put-vs-post

@Novski - Thanks so much for helping, I looked at the link, but why is the gateway sending a PUT instead of a POST when I toggle the on/off switch ? - surely it should send a POST action and not a PUT property when changing the value of a property ?

Because the Staus (on/off) is known by the gateway it can replace it with the opposite.

If the State of the thing would not be known, then a Post is necessary to tell the thing to first check and then change it.

But the gateway should always know the state of a thing…

Hi @diginfo,

The gateway expects web things to follow the Web Thing API which means that the URL you enter should resolve to a Web Thing Description in JSON format (with a Content-Type: application/json header) which describes the web thing’s properties, actions and events.

The Web Thing REST API describes the RESTful API for communicating with a web thing. You can GET a property to read it and PUT a property to write it, or invoke an action with a POST for example. Setting a property is a PUT because it is overwriting the resource at the property URL with the payload of the request, whereas invoking an action is a POST because it’s create a new (action request) resource.

The specification says that “Actions are used when the setting of a property alone is not sufficient to affect a required change in state.”

I hope this helps.