This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open:practical:hardware:sut:stm32:iot_4 [2024/04/27 09:08] – created ktokarz | en:iot-open:practical:hardware:sut:stm32:iot_4 [2024/04/27 09:41] (current) – ktokarz | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ===== Prerequisites ===== | ===== Prerequisites ===== | ||
| - | To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first: | + | To implement this scenario, it is necessary to get familiar with the LED controlling scenario: |
| + | * [[en: | ||
| + | |||
| + | and at least one of the following scenarios first: | ||
| * [[en: | * [[en: | ||
| * [[en: | * [[en: | ||
| Line 87: | Line 90: | ||
| In our case, the command can look like this: | In our case, the command can look like this: | ||
| < | < | ||
| - | AT+MQTTUSERCFG=0, | + | AT+MQTTUSERCFG=0, |
| </ | </ | ||
| Line 102: | Line 105: | ||
| The command can look like this: | The command can look like this: | ||
| < | < | ||
| - | AT+MQTTCONN=0, | + | AT+MQTTCONN=0, |
| </ | </ | ||
| Line 114: | Line 117: | ||
| * " | * " | ||
| * qos - mode of the quality of service, 0, 1 or 2, default 0. | * qos - mode of the quality of service, 0, 1 or 2, default 0. | ||
| + | The command can look like this in the example below: | ||
| + | < | ||
| + | AT+MQTTSUB=0, | ||
| + | </ | ||
| After a successful subscription, | After a successful subscription, | ||
| Line 128: | Line 135: | ||
| === Step 4 === | === Step 4 === | ||
| - | Implement handling of the MQTT messages. Here we present how to control LED with messages sent with the topic " | + | Implement handling of the MQTT messages. Here we present how to control LED with messages sent with the topic " |
| - | Declare variables to store the strings for comparison. | + | Declare variables to store the strings for comparison |
| - | < | + | < |
| + | #define LED_GREEN D3 | ||
| String compend0, compend1; | String compend0, compend1; | ||
| + | String response; | ||
| </ | </ | ||
| + | |||
| Prepare string variables and set the mode of the LED pin. | Prepare string variables and set the mode of the LED pin. | ||
| - | < | + | < |
| compend0 = " | compend0 = " | ||
| compend1 = " | compend1 = " | ||
| - | pinMode(LED_BUILTIN,OUTPUT); | + | pinMode(LED_GREEN,OUTPUT); |
| </ | </ | ||
| + | In the " | ||
| + | <code c> | ||
| + | response = WiFiSerial.readStringUntil(0x0A); | ||
| + | if (response.startsWith(compend0)) | ||
| + | { | ||
| + | digitalWrite(LED_GREEN, | ||
| + | }; | ||
| + | if (response.startsWith(compend1)) | ||
| + | { | ||
| + | digitalWrite(LED_GREEN, | ||
| + | }; | ||
| + | </ | ||
| ==== Result validation ==== | ==== Result validation ==== | ||
| - | You should be able to connect to the WiFi and MQTT broker (verified by the status present on the selected display) and then publish | + | You should be able to connect to the WiFi and MQTT broker (verified by the status present on the selected display) and then subscribe to the topic. After a successful subscription, |
| <note info> | <note info> | ||
| Because LCD can't properly display some non-visible characters the presented code sometimes shows additional, non-letter characters. It is out of the scope of this scenario to filter these characters out. We leave the task of making visual improvements to your invention. | Because LCD can't properly display some non-visible characters the presented code sometimes shows additional, non-letter characters. It is out of the scope of this scenario to filter these characters out. We leave the task of making visual improvements to your invention. | ||
| Line 148: | Line 170: | ||
| ===== FAQ ===== | ===== FAQ ===== | ||
| - | **Can I change the IP address?**: Normally IP addresses are assigned by the server known as DHCP. In some situations, you can use static IP assigned manually | + | **My MQTT client disconnects randomly**: The most common reason is you're using a non-unique MQTT client name. Please change it to some other (even random generated) and give it another try.\\ |
| + | **Can I subscribe to more than one topic?**: Yes, you can. For every incoming message you would receive | ||
| + | **How do I send messages to which I am subscribed? | ||
| + | **Do I need to authorise to publish and subscribe? | ||
| <WRAP noprint> | <WRAP noprint> | ||