It's taken me a few days to get around to writing a new log post, but I haven't been loafing.
I spent some time this past weekend tweaking the Mud-Py models, and wrote up some notes on setting up Mud-Py for development.
I spent more time yesterday twiddling with the hardware - I think I have all the pieces needed to build the nodes, now. That'll get its own write up, when I actually build the darned things.
This evening, I sat down and wrote the MQTT-Mud-Py bridge.
That's about 200 hundred lines of code in two files.
- mud_py_mqtt_client.py - the bridge itself. It connects to the MQTT server, and subscribes to the various messages the nodes can send. It then handles those messages, updating the Mud-Py database and sending responses as needed.
- mud_py_API.py - a collection of functions for putting data into the Mud-Py database. It handles the somewhat squirrely looking task of accessing the Mud-Py Django classes, and adds some functions for automatically creating certain base data as well as automatically creating new nodes and sensors as needed.
I'm not done yet, but most of it is there.
The idea is for each node to connect to the MQTT server, then send an announcement (topic = "node/<nodeID>/battery" value=<battery voltage>.)
The bridge then creates the node if it isn't there, then it inserts the battery voltage in the node data table.
It then puts together a list of sensors that the node should query, and sends it to the node (topic = "node/<nodeID>/sensors", value = <list of sensor IDs>.)
The node then queries all the sensors, and sends sensor messages (topic = "sensor/<sensorID>/<sensor type>", value= <numerical value>) through the MQTT server.
The bridge picks up the sensor messages, and updates the sensor data table, creating base data (sensor types, value types, and value units) as needed.
Once the node has sent all of its sensor data, it sends a "I'm done" message (topic = "node/<nodeID>/done", value = <not used>.) In response to the "I'm done," the bridge sends a "sleep" message (topic = "node/<nodeID>/sleep" value = <time to sleep in seconds>) back.
The "sleep" time is calculated for the node to wake up at the start of the next hour. The nodes don't have a real time clock. They'll just set a wakeup time based on the number of seconds, then go into a deep sleep mode.
When the node wakes up, it goes through the same procedure.
While writing this up, I realized that I forgot to implement the sensor list response. I got so involved in the "automatically creating base data" part that I forgot to generate that response.
I guess that gives me something to do tomorrow, since I plan to leave the nodes to the weekend. I have some cutting and drilling to do to build the nodes, and I'd rather do that on a Saturday afternoon.
To test the bridge, I wrote two little scripts to send simulated node and sensor messages. They are in the "Simulations" folder.
I also whipped up a really simple MQTT snooper. It subscribes to all messages, and prints them out so that I can see what's going on.
No pictures this time around - there's not much interesting to see when a command line program shuffles data.
I went back in and implemented the sensors list response. It aggravated me that I forgot it. It took a little over half an hour.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.