This project has been started already and well, haven't documented anything about it except for what's been posted here.
My first steps involved having a look at what's already available for IoT from a low level embedded developer point of view. Since I have no commercial background in IoT and wanted to build something for my home.
First round of researching found an already popular MQTT protocol which is primarily used over TCP. This protocol is a simple Subscribe/Publish messaging protocol with a few added features like passwords and (I think) forwarding messages onto nodes not directly accessible by the subscriber. It's clean, though, not feature rich.
For some reason I was interested in UDP IoT protocols (possibly for ease of jumping into Low Power mode and not worrying about TCP states). I Quickly found CoAP which was designed to follow in the footsteps of HTTP; As it's designed for low resource environments, includes URI schemes: `coap://` and `coaps://` (using DTLS), oobserve (subscribe) model and a pile of other features.
Okay Cool. Decided on CoAP, Now What?
I read through CoAP's RFC (yeah, all of it... Several times...) and learnt a few extra things:
- CoAP is transport agnostic (meaning it doesn't care if you're using BLE, nRF24, infrared, smoke signals, etc...) and wanted to keep that in mind while working on my implementation.
- It supports resrouce discovery using multicast (similar to how your phone knows there's chromecast on the network).
- Each resource on the device (leds, sensors, status, configuration) can provide a scheme and a data type. For example, LEDs can return data in human readable (text/plain) or in JSON (applicaiton/json).
The ESP32 has a nice framework for quickly getting started with it, called ESP-IDF. This framework includes a scrap bin full of useful components already configured to go. there is libCoAP included in there too. but when poking around with it. it's got a built in transport layer that's not easy to override. and ESP32's IP stack Light Weight IP (LWIP) doesn't support multicast properly (Will go into detail on request).
I found a neat little library called Lobaro-CoAP, no way near polished at libCoAP but it exposed the transport mechanism allowing me the choice of how and when I send the data from the device. So went with that!
Hello World~!
I set out to create a basic Hello World project. The requirements were:
- My phone must discover the device on the network
- The phone must list the resources on the device and allow me to GET the resource I selected.
- The ESP32 must connect to the WiFI and respond to CoAP discovery requests
- Have a simple /hello resource that can be found and provide a GET method.
Right. A phone app...
This is the part where I learn Xamarin.Forms and figure out how to build an app that works on both my phone and my computer. This took some time. but the results can be found at https://github.com/NZSmartie/CoapTest
The second challenge was getting the ESP32 to work as intended. I implemented lobaro-coap on my IoTNode project, hitting some limitations pretty quickly but after a few pull requests later. my ESP32 was responding to multicast messages and my app was finding the device with no problem!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.