This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:remotelab:sut:generalpurpose2:u4 [2019/08/01 12:22] – gdrabik | en:iot-open:remotelab:sut:generalpurpose2:u4 [2020/09/21 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== U4: Receiving and handling MQTT messages ==== | ||
| + | In this scenario, you will subscribe to the MQTT broker for MQTT messages and handle them. Most of the code yo will implement here is similar to the scenario U3, including LCD handling and connecting to the MQTT broker. | ||
| + | === Target group === | ||
| + | Undergraduate / Bachelor / Engineering Students | ||
| + | |||
| + | === Prerequisites === | ||
| + | We assume you already know how to: | ||
| + | * handle DHT sensor to read temperature and humidity, | ||
| + | * handle LCD screen to present information, | ||
| + | * connect to the existing WiFi network: '' | ||
| + | * additionally we will ask you to install and use an MQTT client of your choice. We suggest using [[https:// | ||
| + | |||
| + | MQTT broker present in the '' | ||
| + | === Scenario === | ||
| + | In this scenario, you will connect to the infrastructure as a client (STA) and use the MQTT server to subscribe to the messages sent by some publisher. You will present received MQTT message on the LCD screen. We will implement a " | ||
| + | |||
| + | |||
| + | === Result === | ||
| + | You should be able to visualise payload of the subscribed messages on the LCD screen. Note, you handle only a limited number of messages that you subscribe to. When subscribing, | ||
| + | <note important> | ||
| + | |||
| + | === Start === | ||
| + | Define some identifiers to separate and update AP's SSID and passphrase easily. To format lines for the LCD, we suggest using a char buffer of 20 characters (one full line) and some 2-3 integers for iterators. Remember to declare the LCD control class in your code. You do not need to instantiate WiFi communication class - as you have only one interface here, it is singleton class you can refer directly using WiFi. Note - in this scenario, you connect only once. If your connection breaks, you will have no information about it here. To handle such situation, there are multiple solutions: you can check in the loop() if the connection is OK and in case it is down, you can call your re-connecting function, or you can use one of the asynchronous handlers, triggered automatically via the WiFi manager. To use the latter approach, refer to the ESP8266 WiFi implementation for Arduino documentation. | ||
| + | |||
| + | To handle incoming MQTT messages, you will need to implement a callback function that will be triggered, whenever a new message comes. You will receive only those messages that you've subscribed to. Note you need to check and decode message topics yourself. | ||
| + | |||
| + | === Steps === | ||
| + | Following steps do not present full code - you need to supply missing parts on your own! We do not present here how to connect to the WiFi AP. If you're in doubt, rever to the U1 scenario. Please refer to scenario B1, if you need a recall on how to handle LCD screen. | ||
| + | |||
| + | == Step 1 == | ||
| + | Include all necessary libraries. We use '' | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | ... | ||
| + | </ | ||
| + | Declare some identifiers to let you easier handle necessary modifications and keep code clear: | ||
| + | <code c> | ||
| + | #define wifi_ssid " | ||
| + | #define wifi_password " | ||
| + | #define mqtt_server " | ||
| + | #define mqtt_user " | ||
| + | #define mqtt_password " | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | == Step 2 == | ||
| + | Declare some identifiers, | ||
| + | <note important> | ||
| + | Use topics that their last character is a line number (0 till 3) to easily suit '' | ||
| + | <code c> | ||
| + | // MQTT messages | ||
| + | #define MQTTClientName " | ||
| + | #define lcdTopicWithWildcard "/ | ||
| + | </ | ||
| + | Valid MQTT messages are: | ||
| + | * ''/ | ||
| + | * ''/ | ||
| + | |||
| + | == Step 3 == | ||
| + | We will declare an MQTT handler callback function that is called whenever new MQTT message comes. Mind - only those messages that you've subscribed to are triggering this function, so you do not need to check the topic other than decoding its part to obtain, which LCD line number comes (this is the last character of the MQTT topic): | ||
| + | <code c> | ||
| + | void mqttCallback(char* topic, byte* payload, unsigned int length) { | ||
| + | char nLine = topic[strlen(topic)-1]; | ||
| + | n = atoi(& | ||
| + | if (n>=0 && n<=3) | ||
| + | { | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | for (int i = 0; i < length; i++) | ||
| + | { | ||
| + | lcd.setCursor(i, | ||
| + | lcd.write((char)payload[i]); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | Note - header of the function is interface declared by the mqtt PubSubClient library. '' | ||
| + | |||
| + | == Step 4 == | ||
| + | By the regular variables related to your WiFi ESP network client and so on, here you need to configure an object additionally to handle communication with MQTT broker. As you may use many brokers in your code, you need to instantiate it yourself, there is no singleton class as in case of the network interface. The constructor accepts '' | ||
| + | <code c> | ||
| + | // WiFi & MQTT | ||
| + | WiFiClient espClient; | ||
| + | PubSubClient client(espClient); | ||
| + | </ | ||
| + | Configure your network client, remember to set '' | ||
| + | <code c> | ||
| + | client.setServer(mqtt_server, | ||
| + | client.setCallback(mqttCallback); | ||
| + | </ | ||
| + | You can call it i.e. in the '' | ||
| + | |||
| + | == Step 5 == | ||
| + | If your WiFi client is working, your MQTT client is configured it is time to connect to the MQTT broker. It is a good idea to have this procedure separated to call as needed if your connection with MQTT broker goes down for any reason. Here we encapsulate it in the '' | ||
| + | <code c> | ||
| + | void reconnect() { | ||
| + | // Loop until we're reconnected | ||
| + | while (!client.connected()) { | ||
| + | if (client.connect(MQTTClientName, | ||
| + | client.subscribe(lcdTopicWithWildcard); | ||
| + | } else { | ||
| + | // Wait 5 seconds before retrying | ||
| + | delay(5000); | ||
| + | } | ||
| + | } | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | } | ||
| + | </ | ||
| + | == Step 6 == | ||
| + | Finally your '' | ||
| + | <code c> | ||
| + | void loop() | ||
| + | { | ||
| + | if (!client.connected()) { | ||
| + | reconnect(); | ||
| + | } | ||
| + | client.loop(); | ||
| + | } | ||
| + | </ | ||
| + | The '' | ||
| + | |||
| + | === Result validation === | ||
| + | Compile and run your code then wait till it connects to the network and MQTT broker. Once it is connected, use your favourite MQTT client to connect to the MQTT broker and issue a number of MQTT messages that their topics are: | ||
| + | * ''/ | ||
| + | * ''/ | ||
| + | * ''/ | ||
| + | * ''/ | ||
| + | and the payload is a text you want to display on your LCD. Note, your display is 20 characters each line only so keep your payload within those limits. | ||
| + | |||