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:u3 [2019/08/05 14:36] – pczekalski | en:iot-open:remotelab:sut:generalpurpose2:u3 [2021/02/27 10:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 18: | Line 18: | ||
| === Result === | === Result === | ||
| - | You should be able to see connection status, temperature and humidity on the screen while your MQTT subscriber should be able to present you readings delivered via MQTT. It should be equal to the data presented in the LCD screen you observe locally, via video stream | + | You should be able to see connection status, temperature and humidity on the screen while your MQTT subscriber should be able to present you readings delivered via MQTT. It should be equal to the data presented in the LCD screen you observe locally, via video stream. |
| === Start === | === 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. | + | 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 |
| === 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 | + | 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 how to organise and print DHT11 sensor data on the LCD screen. Please refer to scenario B2 if you need a recall. |
| == 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 35: | Line 34: | ||
| #include < | #include < | ||
| ... | ... | ||
| + | </ | ||
| + | Declare some identifiers to let you easier handle necessary modifications and keep code clear: | ||
| + | <code c> | ||
| #define wifi_ssid " | #define wifi_ssid " | ||
| #define wifi_password " | #define wifi_password " | ||
| Line 47: | Line 49: | ||
| <code c> | <code c> | ||
| #define MQTTClientName ...<your client name>... | #define MQTTClientName ...<your client name>... | ||
| - | #define tempTopic | + | #define tempTopic |
| + | // i.e. including your name | ||
| #define humTopic | #define humTopic | ||
| //MQTT last will | //MQTT last will | ||
| - | #define lastWillTopic ...<some topic for exposing state and last will> | + | #define lastWillTopic ...<some topic for exposing state and last will> |
| + | | ||
| + | // i.e. including your name | ||
| #define lastWillMessage " | #define lastWillMessage " | ||
| #define mqttWelcomeMessage " | #define mqttWelcomeMessage " | ||
| Line 70: | Line 75: | ||
| == Step 4 == | == Step 4 == | ||
| - | If your WiFi client is working, your MQTT client is configurd | + | If your WiFi client is working, your MQTT client is configured |
| <code c> | <code c> | ||
| void reconnect() { | void reconnect() { | ||
| // Loop until we're reconnected | // Loop until we're reconnected | ||
| while (!client.connected()) { | while (!client.connected()) { | ||
| - | if (client.connect(MQTTClientName, | + | if (client.connect(MQTTClientName, |
| + | lastWillTopic, | ||
| + | | ||
| client.publish(lastWillTopic, | client.publish(lastWillTopic, | ||
| - | } else { | + | } else |
| + | | ||
| // Wait 5 seconds before retrying | // Wait 5 seconds before retrying | ||
| delay(5000); | delay(5000); | ||
| Line 108: | Line 116: | ||
| if (!(isnan(temp)||isnan(hum))) | if (!(isnan(temp)||isnan(hum))) | ||
| { | { | ||
| - | client.publish(tempTopic, | + | client.publish(tempTopic, |
| + | // | ||
| client.publish(humTopic, | client.publish(humTopic, | ||
| } | } | ||
| Line 140: | Line 149: | ||
| === FAQ === | === FAQ === | ||
| - | **What topic should I choose?**: Up to you. Anyway, we suggest using non-trivial ones just not to overlap with other users. | + | **What topic should I choose?**: Up to you. Anyway, we suggest using non-trivial ones just not to overlap with other users.\\ |
| **Why MQTT broker uses two IP addresses? | **Why MQTT broker uses two IP addresses? | ||