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 09:22] – gdrabik | en:iot-open:remotelab:sut:generalpurpose2:u4 [2020/09/21 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ==== U4: ==== | + | ==== U4: Receiving and handling MQTT messages |
| - | //Give few words about this scenario. | + | 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 |
| === Target group === | === Target group === | ||
| - | //This hands-on lab guide is intended for the Beginners/ | + | Undergraduate |
| === Prerequisites === | === Prerequisites === | ||
| - | //Provide prerequisite readings/ | + | 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 | ||
| + | MQTT broker present in the '' | ||
| === Scenario === | === Scenario === | ||
| - | // | + | In this scenario, you will connect |
| === Result === | === Result === | ||
| - | //Describe expected result when scenario | + | 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 === | === Start === | ||
| - | //Write starting conditions, i.e. what to do at the beginning, what to pay attention before beginning, how the mechanical part should look like, etc.// | + | 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 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 === | === Steps === | ||
| - | // Write some extra information if i.e. some steps are optional otherwise cancel this paragraph (but do not remove header).// | + | Following |
| - | == Step 1 == | + | |
| - | //Describe activities done in Step 1.// | + | |
| + | == 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 n == | + | == Step 5 == |
| - | //Describe activities done in Step n.// | + | 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() { | ||
| + | | ||
| + | while (!client.connected()) { | ||
| + | if (client.connect(MQTTClientName, | ||
| + | client.subscribe(lcdTopicWithWildcard); | ||
| + | } else { | ||
| + | | ||
| + | delay(5000); | ||
| + | } | ||
| + | } | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | } | ||
| + | </ | ||
| + | == Step 6 == | ||
| + | Finally your '' | ||
| + | <code c> | ||
| + | void loop() | ||
| + | { | ||
| + | if (!client.connected()) { | ||
| + | reconnect(); | ||
| + | } | ||
| + | client.loop(); | ||
| + | } | ||
| + | </ | ||
| + | The '' | ||
| === Result validation === | === Result validation === | ||
| - | //Provide some result validation methods, for self assesment.// | + | 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. | ||
| - | === FAQ === | ||
| - | This section is to be extended as new questions appear. \\ | ||
| - | When using the printed version of this manual please refer to the latest online version of this document to obtain the valid and up-to-date list of the FAQ. | ||
| - | //Provide some FAQs in the following form:\\ | ||
| - | **Question? | ||
| - | // | ||