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/05 20:01] – pczekalski | en:iot-open:remotelab:sut:generalpurpose2:u4 [2020/09/21 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 26: | Line 26: | ||
| === Steps === | === 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. We also do not present in details on how to organise and print DHT22 sensor data on the LCD screen. Please refer to the scenario | + | 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 |
| == Step 1 == | == Step 1 == | ||
| Include all necessary libraries. We use '' | Include all necessary libraries. We use '' | ||
| <code c> | <code c> | ||
| #include < | #include < | ||
| - | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| Line 51: | Line 51: | ||
| Declare some identifiers, | Declare some identifiers, | ||
| <note important> | <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 n == | + | == Step 3 == |
| - | //Describe activities | + | 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]; | ||
| + | | ||
| + | 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() { | ||
| + | | ||
| + | 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 === | === 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? | ||
| - | // | ||