![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
McLab20_esp8266_wifi_mqtt
main.cpp@3:3e3a9fd1a242, 2020-09-29 (annotated)
- Committer:
- petripakkanen
- Date:
- Tue Sep 29 08:14:09 2020 +0000
- Revision:
- 3:3e3a9fd1a242
- Parent:
- 2:6d5dbdb22631
Added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
petripakkanen | 0:0046df9d5380 | 1 | #include "mbed.h" |
petripakkanen | 0:0046df9d5380 | 2 | #include "ESP8266Interface.h" |
petripakkanen | 0:0046df9d5380 | 3 | |
petripakkanen | 1:f6ebd1725aae | 4 | // Library to use https://github.com/ARMmbed/mbed-mqtt |
petripakkanen | 1:f6ebd1725aae | 5 | #include <MQTTClientMbedOs.h> |
petripakkanen | 0:0046df9d5380 | 6 | |
petripakkanen | 0:0046df9d5380 | 7 | int main() |
petripakkanen | 0:0046df9d5380 | 8 | { |
petripakkanen | 1:f6ebd1725aae | 9 | ESP8266Interface esp(MBED_CONF_APP_ESP_TX_PIN, MBED_CONF_APP_ESP_RX_PIN); |
petripakkanen | 1:f6ebd1725aae | 10 | |
petripakkanen | 1:f6ebd1725aae | 11 | //Store device IP |
petripakkanen | 1:f6ebd1725aae | 12 | SocketAddress deviceIP; |
petripakkanen | 1:f6ebd1725aae | 13 | //Store broker IP |
petripakkanen | 1:f6ebd1725aae | 14 | SocketAddress MQTTBroker; |
petripakkanen | 1:f6ebd1725aae | 15 | |
petripakkanen | 1:f6ebd1725aae | 16 | TCPSocket socket; |
petripakkanen | 1:f6ebd1725aae | 17 | MQTTClient client(&socket); |
petripakkanen | 1:f6ebd1725aae | 18 | |
petripakkanen | 1:f6ebd1725aae | 19 | printf("\nConnecting wifi..\n"); |
petripakkanen | 0:0046df9d5380 | 20 | |
petripakkanen | 1:f6ebd1725aae | 21 | int ret = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
petripakkanen | 1:f6ebd1725aae | 22 | |
petripakkanen | 1:f6ebd1725aae | 23 | if(ret != 0) |
petripakkanen | 1:f6ebd1725aae | 24 | { |
petripakkanen | 1:f6ebd1725aae | 25 | printf("\nConnection error\n"); |
petripakkanen | 1:f6ebd1725aae | 26 | } |
petripakkanen | 1:f6ebd1725aae | 27 | else |
petripakkanen | 1:f6ebd1725aae | 28 | { |
petripakkanen | 1:f6ebd1725aae | 29 | printf("\nConnection success\n"); |
petripakkanen | 0:0046df9d5380 | 30 | } |
petripakkanen | 0:0046df9d5380 | 31 | |
petripakkanen | 1:f6ebd1725aae | 32 | esp.get_ip_address(&deviceIP); |
petripakkanen | 1:f6ebd1725aae | 33 | printf("IP via DHCP: %s\n", deviceIP.get_ip_address()); |
petripakkanen | 1:f6ebd1725aae | 34 | |
petripakkanen | 1:f6ebd1725aae | 35 | // Use with IP |
petripakkanen | 1:f6ebd1725aae | 36 | //SocketAddress MQTTBroker(MBED_CONF_APP_MQTT_BROKER_IP, MBED_CONF_APP_MQTT_BROKER_PORT); |
petripakkanen | 1:f6ebd1725aae | 37 | |
petripakkanen | 1:f6ebd1725aae | 38 | // Use with DNS |
petripakkanen | 2:6d5dbdb22631 | 39 | esp.gethostbyname(MBED_CONF_APP_MQTT_BROKER_HOSTNAME, &MQTTBroker, NSAPI_IPv4, "esp"); |
petripakkanen | 1:f6ebd1725aae | 40 | MQTTBroker.set_port(MBED_CONF_APP_MQTT_BROKER_PORT); |
petripakkanen | 1:f6ebd1725aae | 41 | |
petripakkanen | 1:f6ebd1725aae | 42 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
petripakkanen | 1:f6ebd1725aae | 43 | data.MQTTVersion = 3; |
petripakkanen | 1:f6ebd1725aae | 44 | char *id = MBED_CONF_APP_MQTT_ID; |
petripakkanen | 1:f6ebd1725aae | 45 | data.clientID.cstring = id; |
petripakkanen | 1:f6ebd1725aae | 46 | |
petripakkanen | 1:f6ebd1725aae | 47 | char buffer[64]; |
petripakkanen | 1:f6ebd1725aae | 48 | sprintf(buffer, "Hello from Mbed OS %d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION); |
petripakkanen | 0:0046df9d5380 | 49 | |
petripakkanen | 1:f6ebd1725aae | 50 | MQTT::Message msg; |
petripakkanen | 1:f6ebd1725aae | 51 | msg.qos = MQTT::QOS0; |
petripakkanen | 1:f6ebd1725aae | 52 | msg.retained = false; |
petripakkanen | 1:f6ebd1725aae | 53 | msg.dup = false; |
petripakkanen | 1:f6ebd1725aae | 54 | msg.payload = (void*)buffer; |
petripakkanen | 1:f6ebd1725aae | 55 | msg.payloadlen = strlen(buffer); |
petripakkanen | 0:0046df9d5380 | 56 | |
petripakkanen | 1:f6ebd1725aae | 57 | socket.open(&esp); |
petripakkanen | 1:f6ebd1725aae | 58 | socket.connect(MQTTBroker); |
petripakkanen | 1:f6ebd1725aae | 59 | client.connect(data); |
petripakkanen | 2:6d5dbdb22631 | 60 | |
petripakkanen | 2:6d5dbdb22631 | 61 | while(1) { |
petripakkanen | 2:6d5dbdb22631 | 62 | client.publish(MBED_CONF_APP_MQTT_TOPIC, msg); |
petripakkanen | 3:3e3a9fd1a242 | 63 | |
petripakkanen | 3:3e3a9fd1a242 | 64 | // Sleep time must be less than TCP timeout |
petripakkanen | 3:3e3a9fd1a242 | 65 | // TODO: check if socket is usable before publishing |
petripakkanen | 2:6d5dbdb22631 | 66 | ThisThread::sleep_for(30000); |
petripakkanen | 2:6d5dbdb22631 | 67 | } |
petripakkanen | 1:f6ebd1725aae | 68 | //client.yield(100); |
petripakkanen | 1:f6ebd1725aae | 69 | client.disconnect(); |
petripakkanen | 1:f6ebd1725aae | 70 | |
petripakkanen | 0:0046df9d5380 | 71 | } |
petripakkanen | 1:f6ebd1725aae | 72 | |
petripakkanen | 1:f6ebd1725aae | 73 |