Simple demo with GPIO MQTT protocol test on STM32 broker tests.mosquitto.org WIFI interface ESP8266 Issue of topic0 by pressing the button If reception of ', switching of the led If received from 'q' end of program
main.cpp@25:bfdd064b9d99, 2020-06-12 (annotated)
- Committer:
- cdupaty
- Date:
- Fri Jun 12 14:42:35 2020 +0000
- Revision:
- 25:bfdd064b9d99
- Parent:
- 24:cc01ff2c2603
terminal avec couleurs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 1:a1d5c7a6acbc | 1 | /******************************************************************************* |
icraggs | 17:0811bdbdd78a | 2 | * Copyright (c) 2014, 2015 IBM Corp. |
icraggs | 1:a1d5c7a6acbc | 3 | * |
icraggs | 1:a1d5c7a6acbc | 4 | * All rights reserved. This program and the accompanying materials |
icraggs | 1:a1d5c7a6acbc | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
icraggs | 1:a1d5c7a6acbc | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
icraggs | 1:a1d5c7a6acbc | 7 | * |
icraggs | 1:a1d5c7a6acbc | 8 | * The Eclipse Public License is available at |
icraggs | 1:a1d5c7a6acbc | 9 | * http://www.eclipse.org/legal/epl-v10.html |
icraggs | 1:a1d5c7a6acbc | 10 | * and the Eclipse Distribution License is available at |
icraggs | 1:a1d5c7a6acbc | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
icraggs | 1:a1d5c7a6acbc | 12 | * |
icraggs | 1:a1d5c7a6acbc | 13 | * Contributors: |
icraggs | 1:a1d5c7a6acbc | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
icraggs | 17:0811bdbdd78a | 15 | * Ian Craggs - make sure QoS2 processing works, and add device headers |
cdupaty | 24:cc01ff2c2603 | 16 | * |
cdupaty | 24:cc01ff2c2603 | 17 | * Adaptation STM32 NUCLEO and mosquitto.org : C.Dupaty |
cdupaty | 24:cc01ff2c2603 | 18 | * 06/2020 |
cdupaty | 24:cc01ff2c2603 | 19 | * |
cdupaty | 24:cc01ff2c2603 | 20 | This demo works on NUCLEO STM32. |
cdupaty | 24:cc01ff2c2603 | 21 | WIFI link with ESP8266 connected |
cdupaty | 24:cc01ff2c2603 | 22 | Configuration in mbed_app.json |
cdupaty | 24:cc01ff2c2603 | 23 | target_overrides for the UART connection of the ESP8266 and the WIFI connection (SSID / PASS) |
cdupaty | 24:cc01ff2c2603 | 24 | MQTT parameters are configured in #define below |
cdupaty | 24:cc01ff2c2603 | 25 | if receive payload with first q -> exit |
cdupaty | 24:cc01ff2c2603 | 26 | if receive payload with first l -> toggle LED1 on NUCLEO |
cdupaty | 24:cc01ff2c2603 | 27 | * |
icraggs | 1:a1d5c7a6acbc | 28 | *******************************************************************************/ |
Jan Jongboom |
20:49c9daf2b0ff | 29 | |
Jan Jongboom |
20:49c9daf2b0ff | 30 | #include "easy-connect.h" |
Jan Jongboom |
20:49c9daf2b0ff | 31 | #include "MQTTNetwork.h" |
Jan Jongboom |
20:49c9daf2b0ff | 32 | #include "MQTTmbed.h" |
icraggs | 2:638c854c0695 | 33 | #include "MQTTClient.h" |
icraggs | 2:638c854c0695 | 34 | |
cdupaty | 25:bfdd064b9d99 | 35 | DigitalOut led(LED1); // LED verte (user) |
cdupaty | 24:cc01ff2c2603 | 36 | Serial pc(USBTX, USBRX); |
cdupaty | 25:bfdd064b9d99 | 37 | //DigitalIn btn(USER_BUTTON); //PC13 sur F411 |
cdupaty | 25:bfdd064b9d99 | 38 | DigitalIn btn(PA_8,PullUp); // button connected with internal pullup |
icraggs | 2:638c854c0695 | 39 | |
cdupaty | 24:cc01ff2c2603 | 40 | #define board "NUCLEO_F411RE" |
cdupaty | 24:cc01ff2c2603 | 41 | /* |
cdupaty | 24:cc01ff2c2603 | 42 | MQTT QOS, There are 3 QoS levels in MQTT |
cdupaty | 24:cc01ff2c2603 | 43 | At most once (MQTT::QOS0) |
cdupaty | 24:cc01ff2c2603 | 44 | At least once (MQTT::QOS1) |
cdupaty | 24:cc01ff2c2603 | 45 | Exactly once (MQTT::QOS2). |
cdupaty | 24:cc01ff2c2603 | 46 | */ |
cdupaty | 24:cc01ff2c2603 | 47 | #define quality MQTT::QOS0 |
cdupaty | 24:cc01ff2c2603 | 48 | /* |
cdupaty | 24:cc01ff2c2603 | 49 | retained flag : The broker stores the last retained message and the corresponding QoS for that topic. |
cdupaty | 24:cc01ff2c2603 | 50 | Each client that subscribes to a topic pattern that matches the topic of the retained |
cdupaty | 24:cc01ff2c2603 | 51 | message receives the retained message immediately after they subscribe. |
cdupaty | 24:cc01ff2c2603 | 52 | The broker stores only one retained message per topic. |
cdupaty | 24:cc01ff2c2603 | 53 | */ |
cdupaty | 24:cc01ff2c2603 | 54 | #define ret false |
cdupaty | 24:cc01ff2c2603 | 55 | /* |
cdupaty | 24:cc01ff2c2603 | 56 | dup flag : The flag indicates that the message is a duplicate |
cdupaty | 24:cc01ff2c2603 | 57 | and was resent because the intended recipient (client or broker) did not acknowledge the original message. |
cdupaty | 24:cc01ff2c2603 | 58 | */ |
cdupaty | 24:cc01ff2c2603 | 59 | #define dupli false |
cdupaty | 24:cc01ff2c2603 | 60 | |
cdupaty | 24:cc01ff2c2603 | 61 | const char* hostname = "test.mosquitto.org"; |
cdupaty | 24:cc01ff2c2603 | 62 | const int port = 1883; |
cdupaty | 24:cc01ff2c2603 | 63 | char* ID ="mbedSTM32Fourcade"; |
cdupaty | 24:cc01ff2c2603 | 64 | char* user =NULL; // user & pass = NULL for broker with no credential. |
cdupaty | 24:cc01ff2c2603 | 65 | char* pass =NULL; |
cdupaty | 24:cc01ff2c2603 | 66 | const char* topic = "topic0"; |
cdupaty | 24:cc01ff2c2603 | 67 | int arrivedcount=0; |
cdupaty | 24:cc01ff2c2603 | 68 | bool quit=false; |
cdupaty | 24:cc01ff2c2603 | 69 | |
cdupaty | 24:cc01ff2c2603 | 70 | NetworkInterface* network = easy_connect(true); |
cdupaty | 24:cc01ff2c2603 | 71 | MQTTNetwork mqttNetwork(network); |
cdupaty | 24:cc01ff2c2603 | 72 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
cdupaty | 24:cc01ff2c2603 | 73 | MQTT::Message message; |
cdupaty | 24:cc01ff2c2603 | 74 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 8:a3e3113054a1 | 75 | |
icraggs | 9:5beb8609e9f7 | 76 | void messageArrived(MQTT::MessageData& md) |
icraggs | 2:638c854c0695 | 77 | { |
icraggs | 9:5beb8609e9f7 | 78 | MQTT::Message &message = md.message; |
cdupaty | 24:cc01ff2c2603 | 79 | pc.printf("\x1B[33m"); // yellow |
cdupaty | 24:cc01ff2c2603 | 80 | pc.printf("Message arrived -> qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
cdupaty | 24:cc01ff2c2603 | 81 | pc.printf("Payload -> %.*s\r\n", message.payloadlen, (char*)message.payload); |
cdupaty | 24:cc01ff2c2603 | 82 | ++arrivedcount; |
cdupaty | 24:cc01ff2c2603 | 83 | if (*(char*)message.payload=='q') |
cdupaty | 24:cc01ff2c2603 | 84 | { |
cdupaty | 24:cc01ff2c2603 | 85 | pc.printf("\x1B[31m"); // red |
cdupaty | 24:cc01ff2c2603 | 86 | pc.printf("Exit command received\n\r"); |
cdupaty | 24:cc01ff2c2603 | 87 | quit=true; |
cdupaty | 24:cc01ff2c2603 | 88 | } |
cdupaty | 24:cc01ff2c2603 | 89 | if (*(char*)message.payload=='l') |
cdupaty | 24:cc01ff2c2603 | 90 | { |
cdupaty | 24:cc01ff2c2603 | 91 | led=!led; |
cdupaty | 24:cc01ff2c2603 | 92 | pc.printf("\x1B[31m"); // red |
cdupaty | 24:cc01ff2c2603 | 93 | pc.printf("toggle LED\n\r"); |
cdupaty | 24:cc01ff2c2603 | 94 | } |
cdupaty | 24:cc01ff2c2603 | 95 | pc.printf("\x1B[0m"); // raz |
icraggs | 2:638c854c0695 | 96 | } |
icraggs | 0:0cae29831d01 | 97 | |
cdupaty | 24:cc01ff2c2603 | 98 | void connection(void) |
cdupaty | 24:cc01ff2c2603 | 99 | { |
cdupaty | 24:cc01ff2c2603 | 100 | int rc; |
cdupaty | 24:cc01ff2c2603 | 101 | pc.printf("\x1B[32m"); // green |
cdupaty | 24:cc01ff2c2603 | 102 | pc.printf("Connecting to MQTT broker %s:%d\r\n", hostname, port); |
cdupaty | 24:cc01ff2c2603 | 103 | |
cdupaty | 24:cc01ff2c2603 | 104 | if ((rc = client.connect(data)) != 0) |
cdupaty | 24:cc01ff2c2603 | 105 | pc.printf("rc from MQTT connect is %d\r\n", rc); |
cdupaty | 24:cc01ff2c2603 | 106 | else pc.printf("connection MQTT OK ID:%s USER:%s PASS:%s\r\n",ID,user,pass); |
cdupaty | 24:cc01ff2c2603 | 107 | |
cdupaty | 24:cc01ff2c2603 | 108 | if ((rc = client.subscribe(topic, quality, messageArrived)) != 0) |
cdupaty | 24:cc01ff2c2603 | 109 | pc.printf("rc from MQTT subscribe is %d\r\n", rc); |
cdupaty | 24:cc01ff2c2603 | 110 | else pc.printf("Subscribe MQTT OK topic:%s quality:%d\r\n",topic,quality); |
cdupaty | 24:cc01ff2c2603 | 111 | pc.printf("\x1B[0m"); // raz |
cdupaty | 24:cc01ff2c2603 | 112 | } |
cdupaty | 24:cc01ff2c2603 | 113 | |
cdupaty | 24:cc01ff2c2603 | 114 | void deconnection(void) |
cdupaty | 24:cc01ff2c2603 | 115 | { |
cdupaty | 24:cc01ff2c2603 | 116 | int rc; |
cdupaty | 24:cc01ff2c2603 | 117 | pc.printf("\x1B[36m"); // cyan |
cdupaty | 24:cc01ff2c2603 | 118 | if ((rc = client.unsubscribe(topic)) != 0) pc.printf("rc from unsubscribe was %d\r\n", rc); |
cdupaty | 24:cc01ff2c2603 | 119 | else pc.printf("unsubscribe OK \r\n"); |
cdupaty | 24:cc01ff2c2603 | 120 | if ((rc = client.disconnect()) != 0) pc.printf("rc from disconnect was %d\r\n", rc); |
cdupaty | 24:cc01ff2c2603 | 121 | else pc.printf("disconnect OK\r\n"); |
cdupaty | 24:cc01ff2c2603 | 122 | mqttNetwork.disconnect(); |
cdupaty | 24:cc01ff2c2603 | 123 | pc.printf("\x1B[0m"); // raz |
cdupaty | 24:cc01ff2c2603 | 124 | } |
icraggs | 2:638c854c0695 | 125 | |
icraggs | 2:638c854c0695 | 126 | int main(int argc, char* argv[]) |
Jan Jongboom |
20:49c9daf2b0ff | 127 | { |
cdupaty | 24:cc01ff2c2603 | 128 | int rc; |
cdupaty | 24:cc01ff2c2603 | 129 | int cpt=0; |
cdupaty | 24:cc01ff2c2603 | 130 | char buf[100]; |
cdupaty | 24:cc01ff2c2603 | 131 | |
cdupaty | 24:cc01ff2c2603 | 132 | //pc.printf("\x1B[2J"); //efface ecran |
cdupaty | 24:cc01ff2c2603 | 133 | //pc.printf("\x1B[0;0H"); // curseur en 0,0 |
cdupaty | 24:cc01ff2c2603 | 134 | pc.printf("\x1B[1m"); // brillant |
cdupaty | 24:cc01ff2c2603 | 135 | pc.printf("\r\nMQTT test on %s\r\n",board); |
cdupaty | 24:cc01ff2c2603 | 136 | pc.printf("--------------------------\r\n\r\n"); |
cdupaty | 24:cc01ff2c2603 | 137 | pc.printf("\x1B[33m"); // yellow |
cdupaty | 24:cc01ff2c2603 | 138 | wait(1); |
cdupaty | 24:cc01ff2c2603 | 139 | |
cdupaty | 24:cc01ff2c2603 | 140 | // TCP connection |
cdupaty | 24:cc01ff2c2603 | 141 | mqttNetwork.connect(hostname, port); |
cdupaty | 24:cc01ff2c2603 | 142 | if (rc != 0) pc.printf("rc from TCP connect is %d\r\n", rc); |
cdupaty | 24:cc01ff2c2603 | 143 | else pc.printf("WIFI network connection OK\r\n"); |
cdupaty | 24:cc01ff2c2603 | 144 | |
cdupaty | 24:cc01ff2c2603 | 145 | // MQTT data |
cdupaty | 24:cc01ff2c2603 | 146 | data.MQTTVersion = 3; |
cdupaty | 24:cc01ff2c2603 | 147 | data.clientID.cstring = ID; |
cdupaty | 24:cc01ff2c2603 | 148 | data.username.cstring = user; |
cdupaty | 24:cc01ff2c2603 | 149 | data.password.cstring = pass; |
cdupaty | 24:cc01ff2c2603 | 150 | message.qos = quality; |
cdupaty | 24:cc01ff2c2603 | 151 | message.retained = ret; |
cdupaty | 24:cc01ff2c2603 | 152 | message.dup = dupli; |
cdupaty | 24:cc01ff2c2603 | 153 | message.payload = (void*)buf; |
cdupaty | 24:cc01ff2c2603 | 154 | connection(); |
cdupaty | 24:cc01ff2c2603 | 155 | while(!quit) |
cdupaty | 24:cc01ff2c2603 | 156 | { |
cdupaty | 24:cc01ff2c2603 | 157 | pc.printf("---> Press button to send : %s<--- \n\r",topic); |
cdupaty | 24:cc01ff2c2603 | 158 | while(btn); |
cdupaty | 24:cc01ff2c2603 | 159 | while(!btn); |
cdupaty | 24:cc01ff2c2603 | 160 | //connection(); |
cdupaty | 24:cc01ff2c2603 | 161 | sprintf(buf, "Message from %s number -> %d \r\n", board,++cpt); |
cdupaty | 24:cc01ff2c2603 | 162 | pc.printf("Send message -> %s\n\r",buf); |
cdupaty | 24:cc01ff2c2603 | 163 | message.payloadlen = strlen(buf)+1; |
cdupaty | 24:cc01ff2c2603 | 164 | if ((rc = client.publish(topic, message)) !=0) pc.printf("rc publication error %d\r\n", rc); |
cdupaty | 24:cc01ff2c2603 | 165 | client.yield(100); |
cdupaty | 24:cc01ff2c2603 | 166 | //deconnection(); |
Jan Jongboom |
20:49c9daf2b0ff | 167 | } |
Jan Jongboom |
20:49c9daf2b0ff | 168 | |
cdupaty | 24:cc01ff2c2603 | 169 | deconnection(); |
cdupaty | 24:cc01ff2c2603 | 170 | pc.printf("Disconnect from MQTT network. End of program, %d messages arrived\r\n",arrivedcount); |
Jan Jongboom |
20:49c9daf2b0ff | 171 | |
icraggs | 0:0cae29831d01 | 172 | return 0; |
icraggs | 0:0cae29831d01 | 173 | } |