![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
McLab20_esp8266_wifi_mqtt
main.cpp
- Committer:
- petripakkanen
- Date:
- 2020-09-16
- Revision:
- 1:f6ebd1725aae
- Parent:
- 0:0046df9d5380
- Child:
- 2:6d5dbdb22631
File content as of revision 1:f6ebd1725aae:
#include "mbed.h" #include "ESP8266Interface.h" // Library to use https://github.com/ARMmbed/mbed-mqtt #include <MQTTClientMbedOs.h> int main() { ESP8266Interface esp(MBED_CONF_APP_ESP_TX_PIN, MBED_CONF_APP_ESP_RX_PIN); //Store device IP SocketAddress deviceIP; //Store broker IP SocketAddress MQTTBroker; TCPSocket socket; MQTTClient client(&socket); printf("\nConnecting wifi..\n"); int ret = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); if(ret != 0) { printf("\nConnection error\n"); } else { printf("\nConnection success\n"); } esp.get_ip_address(&deviceIP); printf("IP via DHCP: %s\n", deviceIP.get_ip_address()); // Use with IP //SocketAddress MQTTBroker(MBED_CONF_APP_MQTT_BROKER_IP, MBED_CONF_APP_MQTT_BROKER_PORT); // Use with DNS esp.gethostbyname(MBED_CONF_APP_MQTT_BROKER_HOSTNAME, &MQTTBroker); MQTTBroker.set_port(MBED_CONF_APP_MQTT_BROKER_PORT); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.MQTTVersion = 3; char *id = MBED_CONF_APP_MQTT_ID; data.clientID.cstring = id; char buffer[64]; sprintf(buffer, "Hello from Mbed OS %d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION); MQTT::Message msg; msg.qos = MQTT::QOS0; msg.retained = false; msg.dup = false; msg.payload = (void*)buffer; msg.payloadlen = strlen(buffer); socket.open(&esp); socket.connect(MQTTBroker); client.connect(data); client.publish(MBED_CONF_APP_MQTT_TOPIC, msg); //client.yield(100); client.disconnect(); }