Bonjour,
Oui, et c'est aussi la version 3.1.1 de MQTT qui est utilisée par défaut par la bibliothèque PubSubClient.
J'ai retrouvé mon ESP32 et j'ai réussi à me connecter à Thingspeak au channel public https://thingspeak.com/channels/357142 avec le code suivant à adapter :
arduino_secrets.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 /* MqttClient - WiFi Simple Receive This example connects to a MQTT broker and subscribes to a single topic. When a message is received it prints the message to the Serial Monitor. This example code is in the public domain. */ #include <PubSubClient.h> #include <WiFi.h> #include "arduino_secrets.h" void WIFISetUp(void); void mqttCallback(char *topic, byte *payload, unsigned int length); ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) WiFiClient wifiClient; PubSubClient mqttClient(wifiClient); const char mqttBroker[] = "mqtt3.thingspeak.com"; int mqttPort = 1883; const char mqttTopic[] = "channels/357142/subscribe"; const char mqttClientID[] = "IywXBgxxxxxxxxxxxxxxxxxxx"; const char mqttUsername[] = "IywXBgxxxxxxxxxxxxxxxxxxx"; const char mqttPassword[] = "B7V0ujgxxxxxxxxxxxxxxxxxx"; void setup() { Serial.begin(115200); while (!Serial) { ; } // attempt to connect to WiFi network: Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); WIFISetUp(); Serial.println("You're connected to the network"); Serial.println(); mqttClient.setServer(mqttBroker, mqttPort); mqttClient.setCallback(mqttCallback); mqttClient.setBufferSize(512); while (!mqttClient.connected()) { Serial.print("Attempting to connect to the MQTT broker: "); Serial.println(mqttBroker); if (mqttClient.connect(mqttClientID, mqttUsername, mqttPassword)) { Serial.println("You're connected to the MQTT broker!"); if (mqttClient.subscribe(mqttTopic)) { Serial.print("Subscribing to topic "); Serial.print(mqttTopic); Serial.println(": success"); Serial.println("Waiting for messages on topic..."); } else { Serial.println("Subscribing to topic: failed"); } } else { Serial.print("Failed, state="); Serial.print(mqttClient.state()); Serial.println(" New try in 5s..."); delay(5000); } } } void loop() { mqttClient.loop(); } void mqttCallback(char *topic, byte *payload, unsigned int length) { Serial.print("Message arrived in topic: "); Serial.println(topic); Serial.print("Message:"); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); Serial.println("-----------------------"); } void WIFISetUp(void) { // Set WiFi to station mode and disconnect from an AP if it was previously connected WiFi.disconnect(true); delay(100); WiFi.mode(WIFI_STA); WiFi.setAutoReconnect(true); WiFi.begin(ssid, pass); //fill in "Your WiFi SSID","Your Password" delay(100); byte count = 0; while (WiFi.status() != WL_CONNECTED && count < 15) { count++; delay(2000); Serial.println("Connecting..."); } if (WiFi.status() == WL_CONNECTED) { Serial.println("Connecting...OK."); delay(500); } else { Serial.println("Connecting...Failed"); while (1) ; } Serial.println("WIFI Setup done"); delay(500); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 #define SECRET_SSID "your SSID" #define SECRET_PASS "your password"
Dans le terminal série, j'ai bien les messages qui arrivent :
La ligne mqttClient.setBufferSize(512); est importante, par défaut la taille du buffer est de 256 octets et comme le message est plus long, je ne voyais aucun message arriver
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 Connecting... Connecting...OK. WIFI Setup done You're connected to the network Attempting to connect to the MQTT broker: mqtt3.thingspeak.com You're connected to the MQTT broker! Subscribing to topic channels/357142/subscribe: success Waiting for messages on topic... Message arrived in topic: channels/357142/subscribe Message:{"channel_id":357142,"created_at":"2024-08-06T13:52:20+02:00","entry_id":290735,"field1":"3.3","field2":"4.0","field3":"32.360","field4":"978.435","field5":"34.546","field6":null,"field7":"43.715","field8":"5.543","latitude":null,"longitude":null,"elevation":null,"status":"13:52:20: new data"} ----------------------- Message arrived in topic: channels/357142/subscribe Message:{"channel_id":357142,"created_at":"2024-08-06T14:04:26+02:00","entry_id":290736,"field1":"2.8","field2":"3.1","field3":"33.383","field4":"978.354","field5":"33.946","field6":null,"field7":"44.298","field8":"5.615","latitude":null,"longitude":null,"elevation":null,"status":"14:04:26: new data"} -----------------------
Partager