![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MQTT client to test the ENC28J60-EMAC on NUCLEO-L152RE
main.cpp@6:c123d9b8e6f4, 2021-08-20 (annotated)
- Committer:
- jack1930
- Date:
- Fri Aug 20 11:02:55 2021 +0000
- Revision:
- 6:c123d9b8e6f4
- Parent:
- 5:d9570dbf2f82
Angepasst fuer GSOE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jack1930 | 6:c123d9b8e6f4 | 1 | //based on MQTT-Hello from Zoltan Hudak |
hudakz | 0:238f0d0c0ba3 | 2 | #include "mbed.h" |
hudakz | 0:238f0d0c0ba3 | 3 | |
hudakz | 0:238f0d0c0ba3 | 4 | #include "EthernetInterface.h" |
hudakz | 0:238f0d0c0ba3 | 5 | #include "MQTTNetwork.h" |
hudakz | 0:238f0d0c0ba3 | 6 | #include "MQTTmbed.h" |
hudakz | 0:238f0d0c0ba3 | 7 | #include "MQTTClient.h" |
hudakz | 0:238f0d0c0ba3 | 8 | |
hudakz | 0:238f0d0c0ba3 | 9 | #define IP "192.168.1.35" |
hudakz | 0:238f0d0c0ba3 | 10 | #define GATEWAY "192.168.1.1" |
hudakz | 0:238f0d0c0ba3 | 11 | #define NETMASK "255.255.255.0" |
jack1930 | 6:c123d9b8e6f4 | 12 | #define BROKER_IP "192.168.1.106" // IP address of your MQTT broker. Change to match your case. |
hudakz | 0:238f0d0c0ba3 | 13 | #define MQTT_PORT 1883 // MQTT port |
hudakz | 2:2a16a9f4509c | 14 | |
hudakz | 0:238f0d0c0ba3 | 15 | const uint8_t MAC[6] = { 0, 1, 2, 3, 4, 5 }; |
hudakz | 5:d9570dbf2f82 | 16 | const char payload[] = "Hello, World!"; |
hudakz | 0:238f0d0c0ba3 | 17 | |
hudakz | 0:238f0d0c0ba3 | 18 | // Global variables |
hudakz | 0:238f0d0c0ba3 | 19 | char topic[256]; |
hudakz | 0:238f0d0c0ba3 | 20 | EthernetInterface net; |
hudakz | 0:238f0d0c0ba3 | 21 | MQTTNetwork mqttNetwork(&net); |
hudakz | 0:238f0d0c0ba3 | 22 | MQTT::Client<MQTTNetwork, Countdown> mqttClient(mqttNetwork); |
hudakz | 5:d9570dbf2f82 | 23 | Timer timer; |
jack1930 | 6:c123d9b8e6f4 | 24 | Thread thread; |
hudakz | 0:238f0d0c0ba3 | 25 | |
hudakz | 0:238f0d0c0ba3 | 26 | // Function prototypes |
hudakz | 0:238f0d0c0ba3 | 27 | void onMqttMsgReceived(MQTT::MessageData& md); |
hudakz | 5:d9570dbf2f82 | 28 | void publishMsg(); |
hudakz | 0:238f0d0c0ba3 | 29 | |
hudakz | 0:238f0d0c0ba3 | 30 | /** |
hudakz | 0:238f0d0c0ba3 | 31 | * @brief |
hudakz | 0:238f0d0c0ba3 | 32 | * @note |
hudakz | 0:238f0d0c0ba3 | 33 | * @param |
hudakz | 0:238f0d0c0ba3 | 34 | * @retval |
hudakz | 0:238f0d0c0ba3 | 35 | */ |
hudakz | 0:238f0d0c0ba3 | 36 | FileHandle* mbed::mbed_override_console(int) |
hudakz | 0:238f0d0c0ba3 | 37 | { |
hudakz | 5:d9570dbf2f82 | 38 | static BufferedSerial myConsole(USBTX, USBRX, 115200); |
hudakz | 0:238f0d0c0ba3 | 39 | return &myConsole; |
hudakz | 0:238f0d0c0ba3 | 40 | } |
hudakz | 0:238f0d0c0ba3 | 41 | |
hudakz | 0:238f0d0c0ba3 | 42 | /** |
hudakz | 0:238f0d0c0ba3 | 43 | * @brief |
hudakz | 0:238f0d0c0ba3 | 44 | * @note |
hudakz | 0:238f0d0c0ba3 | 45 | * @param |
hudakz | 0:238f0d0c0ba3 | 46 | * @retval |
hudakz | 0:238f0d0c0ba3 | 47 | */ |
jack1930 | 6:c123d9b8e6f4 | 48 | void check() |
jack1930 | 6:c123d9b8e6f4 | 49 | { |
jack1930 | 6:c123d9b8e6f4 | 50 | while(1) mqttClient.yield(10); |
jack1930 | 6:c123d9b8e6f4 | 51 | } |
jack1930 | 6:c123d9b8e6f4 | 52 | |
hudakz | 0:238f0d0c0ba3 | 53 | int main(void) |
hudakz | 0:238f0d0c0ba3 | 54 | { |
hudakz | 0:238f0d0c0ba3 | 55 | // Bring up the ethernet interface |
hudakz | 0:238f0d0c0ba3 | 56 | net.set_network(IP, NETMASK, GATEWAY); // include this to use static IP address |
hudakz | 0:238f0d0c0ba3 | 57 | net.get_emac().set_hwaddr(MAC); // set MAC address |
hudakz | 0:238f0d0c0ba3 | 58 | printf("MQTT client example\n"); |
hudakz | 0:238f0d0c0ba3 | 59 | if (net.connect() != 0) { |
hudakz | 0:238f0d0c0ba3 | 60 | printf("Error connecting\n"); |
hudakz | 0:238f0d0c0ba3 | 61 | return -1; |
hudakz | 0:238f0d0c0ba3 | 62 | } |
hudakz | 0:238f0d0c0ba3 | 63 | |
hudakz | 0:238f0d0c0ba3 | 64 | // Show network address |
hudakz | 0:238f0d0c0ba3 | 65 | SocketAddress addr; |
hudakz | 0:238f0d0c0ba3 | 66 | net.get_ip_address(&addr); |
hudakz | 0:238f0d0c0ba3 | 67 | printf("IP address: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None"); |
hudakz | 0:238f0d0c0ba3 | 68 | net.get_netmask(&addr); |
hudakz | 0:238f0d0c0ba3 | 69 | printf("Netmask: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None"); |
hudakz | 0:238f0d0c0ba3 | 70 | net.get_gateway(&addr); |
hudakz | 0:238f0d0c0ba3 | 71 | printf("Gateway: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None"); |
hudakz | 0:238f0d0c0ba3 | 72 | |
hudakz | 0:238f0d0c0ba3 | 73 | printf("Connecting to %s:%d\r\n", BROKER_IP, MQTT_PORT); |
hudakz | 0:238f0d0c0ba3 | 74 | nsapi_error_t error = mqttNetwork.connect(BROKER_IP, MQTT_PORT); |
hudakz | 0:238f0d0c0ba3 | 75 | if (error == NSAPI_ERROR_OK) { |
hudakz | 0:238f0d0c0ba3 | 76 | printf("Server connected.\r\n"); |
hudakz | 0:238f0d0c0ba3 | 77 | } |
hudakz | 0:238f0d0c0ba3 | 78 | else { |
hudakz | 0:238f0d0c0ba3 | 79 | printf("Cannot connect server.\r\n"); |
hudakz | 0:238f0d0c0ba3 | 80 | return -1; |
hudakz | 0:238f0d0c0ba3 | 81 | } |
hudakz | 0:238f0d0c0ba3 | 82 | |
hudakz | 0:238f0d0c0ba3 | 83 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
hudakz | 0:238f0d0c0ba3 | 84 | data.MQTTVersion = 3; |
hudakz | 0:238f0d0c0ba3 | 85 | data.clientID.cstring = "MyMqttClient"; |
hudakz | 0:238f0d0c0ba3 | 86 | data.username.cstring = NULL; |
hudakz | 0:238f0d0c0ba3 | 87 | data.password.cstring = NULL; |
hudakz | 0:238f0d0c0ba3 | 88 | |
hudakz | 0:238f0d0c0ba3 | 89 | // Connect MQTT client to MQTT broker |
hudakz | 0:238f0d0c0ba3 | 90 | printf("Connecting MQTT Broker\r\n"); |
hudakz | 0:238f0d0c0ba3 | 91 | if (mqttClient.connect(data) != 0) { |
hudakz | 0:238f0d0c0ba3 | 92 | printf("Cannot connect MQTT broker.\r\n"); |
hudakz | 0:238f0d0c0ba3 | 93 | return -1; |
hudakz | 0:238f0d0c0ba3 | 94 | }; |
hudakz | 0:238f0d0c0ba3 | 95 | printf("MQTT Broker connected.\r\n"); |
hudakz | 0:238f0d0c0ba3 | 96 | |
hudakz | 0:238f0d0c0ba3 | 97 | // Subscribe to topics |
hudakz | 5:d9570dbf2f82 | 98 | mqttClient.subscribe("outdoorTemperature", MQTT::QOS0, onMqttMsgReceived); |
hudakz | 0:238f0d0c0ba3 | 99 | |
hudakz | 5:d9570dbf2f82 | 100 | // Start timer |
hudakz | 5:d9570dbf2f82 | 101 | timer.start(); |
jack1930 | 6:c123d9b8e6f4 | 102 | thread.start(&check); |
hudakz | 0:238f0d0c0ba3 | 103 | // Main thread loop |
hudakz | 0:238f0d0c0ba3 | 104 | while (1) { |
jack1930 | 6:c123d9b8e6f4 | 105 | // mqttClient.yield(10); |
jack1930 | 6:c123d9b8e6f4 | 106 | // if (timer.read_ms() > 1000) { |
jack1930 | 6:c123d9b8e6f4 | 107 | // timer.reset(); |
hudakz | 5:d9570dbf2f82 | 108 | publishMsg(); // once a second publish the MQTT message |
jack1930 | 6:c123d9b8e6f4 | 109 | HAL_Delay(1000); |
jack1930 | 6:c123d9b8e6f4 | 110 | // } |
hudakz | 0:238f0d0c0ba3 | 111 | } |
hudakz | 0:238f0d0c0ba3 | 112 | } |
hudakz | 0:238f0d0c0ba3 | 113 | |
hudakz | 0:238f0d0c0ba3 | 114 | /** |
hudakz | 0:238f0d0c0ba3 | 115 | * @brief |
hudakz | 0:238f0d0c0ba3 | 116 | * @note |
hudakz | 0:238f0d0c0ba3 | 117 | * @param |
hudakz | 0:238f0d0c0ba3 | 118 | * @retval |
hudakz | 0:238f0d0c0ba3 | 119 | */ |
hudakz | 0:238f0d0c0ba3 | 120 | void onMqttMsgReceived(MQTT::MessageData& md) |
hudakz | 0:238f0d0c0ba3 | 121 | { |
hudakz | 0:238f0d0c0ba3 | 122 | memset(topic, 0, sizeof(topic)); |
hudakz | 0:238f0d0c0ba3 | 123 | memcpy(topic, md.topicName.lenstring.data, md.topicName.lenstring.len); |
hudakz | 0:238f0d0c0ba3 | 124 | printf("topic: %s\r\n", topic); |
hudakz | 0:238f0d0c0ba3 | 125 | } |
hudakz | 5:d9570dbf2f82 | 126 | |
hudakz | 5:d9570dbf2f82 | 127 | /** |
hudakz | 5:d9570dbf2f82 | 128 | * @brief |
hudakz | 5:d9570dbf2f82 | 129 | * @note |
hudakz | 5:d9570dbf2f82 | 130 | * @param |
hudakz | 5:d9570dbf2f82 | 131 | * @retval |
hudakz | 5:d9570dbf2f82 | 132 | */ |
hudakz | 5:d9570dbf2f82 | 133 | void publishMsg() |
hudakz | 5:d9570dbf2f82 | 134 | { |
hudakz | 5:d9570dbf2f82 | 135 | MQTT::Message mqttMsg; |
hudakz | 5:d9570dbf2f82 | 136 | |
hudakz | 5:d9570dbf2f82 | 137 | mqttMsg.qos = MQTT::QOS0; |
hudakz | 5:d9570dbf2f82 | 138 | mqttMsg.retained = false; |
hudakz | 5:d9570dbf2f82 | 139 | mqttMsg.dup = false; |
hudakz | 5:d9570dbf2f82 | 140 | mqttMsg.payload = (void*)payload; |
hudakz | 5:d9570dbf2f82 | 141 | mqttMsg.payloadlen = strlen(payload); |
hudakz | 5:d9570dbf2f82 | 142 | mqttClient.publish("topic_hello_world", mqttMsg); |
hudakz | 5:d9570dbf2f82 | 143 | } |