First-time configuration

As noted earlier in this chapter, this firmware is designed to be configured and maintained over MQTT. This requires that an MQTT broker is available. An MQTT broker such as Mosquitto (http://mosquitto.org/) is popular. Since it's a lightweight server, it can be installed on a desktop system, a small SBC, inside a virtual machine, and so on.

In addition the broker and the ESP8266 running the firmware, we also need our own client
to interact with the firmware. Since we use binary protocols, our choice there is somewhat
limited, as most common MQTT desktop clients assume text-based messages. One
approach one can use to publish binary messages is to use the MQTT publish client that
comes with Mosquitto and use the
echo command-line tool's hexadecimal input to send
binary data to it as a stream to be published by the client tool

Because of this, the author of this book has developed a new MQTT desktop client (based on C++ and Qt) that is designed around the use and debugging of binary protocols on MQTT: https://github.com/MayaPosch/MQTTCute.

With all three components in place—ESP8266 running the project, the MQTT broker and desktop clientwe can have the entire plant monitoring and watering system assembled and send it the command to enable the plant module.

While monitoring the cc/config topic for messages, we should see the ESP8266 report its presence by publishing its MAC. We can also get this by hooking up a USB to TTL serial adapter to the serial logging output pin (D4 on NodeMCU). By looking at the output on our serial console, we will see both the IP address and the MAC of the system.

When we compose a new topic of the format cc/<MAC>, we can then publish commands to the firmware, for example:

    log;plant001  

This would set the location name of the system to plant001.

When using the MQTTCute client, we can use echo-style binary input, using hexadecimal input, to activate the plant module:

mod;x00x01x00x00  

This would send the mod command to the firmware, along with a bitmask with the value 0 x 100. After this, the plant module should be activated and running. Since we are persisting both the location string and the configuration, we do not have to repeat this step any more unless we do an OTA update, at which point the new firmware will have an empty SPIFFS filesystem, unless we flash the same SPIFFS image on both SPIFFS slots on the ROM.

Here, we could expand the OTA code to also download a SPIFFS ROM image in addition to the firmware one, though this might add the complication of possibly overwriting the existing SPIFFS files.

At this point, we should have a working plant-monitoring and -watering system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.32