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 /* mbed Microcontroller Library 00002 * Copyright (c) 2019 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 */ 00005 00006 #include "mbed.h" 00007 //#include "EthernetInterface.h" 00008 #include "platform/mbed_thread.h" 00009 #include <MQTTClientMbedOs.h> 00010 // Blinking rate in milliseconds 00011 #define BLINKING_RATE_MS 500 00012 // Initialise the digital pin LED1 as an output 00013 DigitalOut led(LED1); 00014 00015 00016 InterruptIn button(USER_BUTTON); 00017 00018 int arrivedcount = 0; 00019 TCPSocket socket; 00020 MQTTClient client(&socket); 00021 MQTT::Message message; 00022 00023 int button_pressed=0; 00024 00025 float version = 0.6; 00026 char* topic = "mbed-sample-pub"; 00027 char* topic_sub = "mbed-sample-sub"; 00028 00029 00030 00031 00032 WiFiInterface *wifi; 00033 00034 const char *sec2str(nsapi_security_t sec) 00035 { 00036 switch (sec) { 00037 case NSAPI_SECURITY_NONE: 00038 return "None"; 00039 case NSAPI_SECURITY_WEP: 00040 return "WEP"; 00041 case NSAPI_SECURITY_WPA: 00042 return "WPA"; 00043 case NSAPI_SECURITY_WPA2: 00044 return "WPA2"; 00045 case NSAPI_SECURITY_WPA_WPA2: 00046 return "WPA/WPA2"; 00047 case NSAPI_SECURITY_UNKNOWN: 00048 default: 00049 return "Unknown"; 00050 } 00051 } 00052 00053 int scan_demo(WiFiInterface *wifi) 00054 { 00055 WiFiAccessPoint *ap; 00056 00057 printf("Scan:\n"); 00058 00059 int count = wifi->scan(NULL,0); 00060 00061 if (count <= 0) { 00062 printf("scan() failed with return value: %d\n", count); 00063 return 0; 00064 } 00065 00066 /* Limit number of network arbitrary to 15 */ 00067 count = count < 15 ? count : 15; 00068 00069 ap = new WiFiAccessPoint[count]; 00070 count = wifi->scan(ap, count); 00071 00072 if (count <= 0) { 00073 printf("scan() failed with return value: %d\n", count); 00074 return 0; 00075 } 00076 00077 for (int i = 0; i < count; i++) { 00078 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), 00079 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], 00080 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); 00081 } 00082 printf("%d networks available.\n", count); 00083 00084 delete[] ap; 00085 return count; 00086 } 00087 00088 void messageArrived(MQTT::MessageData& md) 00089 { 00090 MQTT::Message &message = md.message; 00091 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); 00092 printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); 00093 ++arrivedcount; 00094 } 00095 00096 void buttonFunction() { 00097 00098 button_pressed=1; 00099 00100 } 00101 00102 00103 00104 00105 int main() 00106 { 00107 button.rise(&buttonFunction); // attach the address of the flip function to the rising edge 00108 00109 const char* hostname = "broker.mqttdashboard.com"; 00110 int port = 1883; 00111 // Network interface 00112 //EthernetInterface net; 00113 00114 wifi = WiFiInterface::get_default_instance(); 00115 if (!wifi) { 00116 printf("ERROR: No WiFiInterface found.\n"); 00117 return -1; 00118 } 00119 00120 int count = scan_demo(wifi); 00121 if (count == 0) { 00122 printf("No WIFI APs found - can't continue further.\n"); 00123 return -1; 00124 } 00125 00126 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00127 int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00128 if (ret != 0) { 00129 printf("\nConnection error: %d\n", ret); 00130 return -1; 00131 } 00132 00133 printf("Success\n\n"); 00134 printf("MAC: %s\n", wifi->get_mac_address()); 00135 printf("IP: %s\n", wifi->get_ip_address()); 00136 printf("Netmask: %s\n", wifi->get_netmask()); 00137 printf("Gateway: %s\n", wifi->get_gateway()); 00138 printf("RSSI: %d\n\n", wifi->get_rssi()); 00139 00140 00141 00142 00143 //client = MQTTClient::(&socket); 00144 socket.open(wifi); 00145 socket.connect(hostname, port); 00146 00147 int rc=0; 00148 00149 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00150 data.MQTTVersion = 3; 00151 data.clientID.cstring = "mbed-sample"; 00152 //data.username.cstring = "testuser"; 00153 //data.password.cstring = "testpassword"; 00154 if ((rc = client.connect(data)) != 0) 00155 printf("rc from MQTT connect is %d\r\n", rc); 00156 00157 if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) 00158 printf("rc from MQTT subscribe is %d\r\n", rc); 00159 00160 00161 00162 00163 00164 00165 00166 while (true) { 00167 led = !led; 00168 thread_sleep_for(BLINKING_RATE_MS); 00169 if (button_pressed==1) { 00170 button_pressed=0; 00171 printf("sace publish\r\n"); 00172 // QoS 0 00173 char buf[100]; 00174 sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version); 00175 message.qos = MQTT::QOS0; 00176 message.retained = false; 00177 message.dup = false; 00178 message.payload = (void*)buf; 00179 message.payloadlen = strlen(buf)+1; 00180 client.publish(topic, message); 00181 //pc.printf("sace jild\r\n"); 00182 //client.yield(1000); 00183 //pc.printf("izjildovo\r\n"); 00184 } 00185 printf("sace jild\r\n"); 00186 client.yield(1000); 00187 printf("izjildovo\r\n"); 00188 00189 } 00190 }
Generated on Sat Jul 23 2022 21:47:34 by
1.7.2