Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 00002 00003 #include "mbed.h" 00004 #include "EthernetInterface.h" 00005 #include "platform/mbed_thread.h" 00006 #include <MQTTClientMbedOs.h> 00007 00008 // Blinking rate in milliseconds 00009 #define BLINKING_RATE_MS 500 00010 DigitalOut led(LED1); 00011 00012 int arrivedcount = 0; 00013 TCPSocket socket; 00014 MQTTClient client(&socket); 00015 MQTT::Message message; 00016 00017 00018 char* topic_pub = "PMK-server-topic-pub"; 00019 char* topic_sub = "PMK-client-temperature"; 00020 00021 const char *sec2str(nsapi_security_t sec) 00022 { 00023 switch (sec) { 00024 case NSAPI_SECURITY_NONE: 00025 return "None"; 00026 case NSAPI_SECURITY_WEP: 00027 return "WEP"; 00028 case NSAPI_SECURITY_WPA: 00029 return "WPA"; 00030 case NSAPI_SECURITY_WPA2: 00031 return "WPA2"; 00032 case NSAPI_SECURITY_WPA_WPA2: 00033 return "WPA/WPA2"; 00034 case NSAPI_SECURITY_UNKNOWN: 00035 default: 00036 return "Unknown"; 00037 } 00038 } 00039 00040 void messageArrived(MQTT::MessageData& md) 00041 { 00042 MQTT::Message &message = md.message; 00043 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); 00044 printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); 00045 ++arrivedcount; 00046 } 00047 00048 int main() 00049 { 00050 printf("PMK2021 Ethernet MQTT example\r\n"); 00051 00052 const char* hostname = "broker.mqttdashboard.com"; 00053 int port = 1883; 00054 // Network interface 00055 EthernetInterface net; 00056 // Bring up the ethernet interface 00057 net.connect(); 00058 00059 // Show the network address 00060 const char *ip = net.get_ip_address(); 00061 printf("IP address is: %s\r\n", ip ? ip : "No IP"); 00062 00063 //client = MQTTClient::(&socket); 00064 socket.open(&net); 00065 socket.connect(hostname, port); 00066 00067 int rc=0; 00068 00069 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00070 data.MQTTVersion = 3; 00071 data.clientID.cstring = "PMK-server"; 00072 //data.username.cstring = "testuser"; 00073 //data.password.cstring = "testpassword"; 00074 if ((rc = client.connect(data)) != 0) 00075 printf("rc from MQTT connect is %d\r\n", rc); 00076 00077 if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) 00078 printf("rc from MQTT subscribe is %d\r\n", rc); 00079 00080 00081 while (true) { 00082 led = !led; 00083 thread_sleep_for(BLINKING_RATE_MS); 00084 printf("Server Yielding "); 00085 client.yield(1000); 00086 printf(" -> Yielded\r\n"); 00087 00088 } 00089 }
Generated on Sat Jul 16 2022 23:30:26 by
1.7.2