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.
Dependencies: HTS221 VL53L0X BSP_B-L475E-IOT01 MQTT
main.cpp
00001 /* WiFi+MQTT Example*/ 00002 00003 #include "mbed.h" 00004 #include "TCPSocket.h" 00005 #include "wifi-ism43362/ISM43362Interface.h" 00006 #define logMessage printf 00007 #define MQTTCLIENT_QOS2 1 00008 #include "MQTTNetwork.h" 00009 #include "MQTTmbed.h" 00010 #include "MQTTClient.h" 00011 #include "HTS221Sensor.h" 00012 //#define MQTT_HOST "demo.thingsboard.io" 00013 #define MQTT_HOST "172.20.10.6" 00014 #define MQTT_PORT 1883 00015 #define MQTT_TOPIC "Voltage" 00016 #include <string> 00017 //#include "VL53L0X.h" 00018 ISM43362Interface net; 00019 // WiFiInterface *wifi; 00020 Serial pc(USBTX,USBRX); 00021 void messageArrived(MQTT::MessageData& md) 00022 { 00023 MQTT::Message &message = md.message; 00024 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); 00025 logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); 00026 //++arrivedcount; 00027 } 00028 static DevI2C devI2c(PB_11,PB_10); 00029 static DigitalOut shutdown_pin(PC_6); 00030 //static VL53L0X range(&devI2c, &shutdown_pin, PC_7); 00031 Serial uart(PA_0, PA_1);//TX,RX 00032 00033 int idx=0; 00034 std::string inputdata; 00035 char ch1; 00036 int main(void){ 00037 uart.baud(115200); 00038 //range.init_sensor(VL53L0X_DEFAULT_ADDRESS); 00039 int count = 0; 00040 00041 printf("\r\nWiFi+MQTT Example Demo\n"); 00042 00043 // Connect to Wifi 00044 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00045 int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00046 if (ret != 0) { 00047 printf("\nConnection error: %d\n", ret); 00048 return -1; 00049 } 00050 00051 printf("Success\n\n"); 00052 printf("MAC: %s\n", net.get_mac_address()); 00053 printf("IP: %s\n", net.get_ip_address()); 00054 printf("Netmask: %s\n", net.get_netmask()); 00055 printf("Gateway: %s\n", net.get_gateway()); 00056 printf("RSSI: %d\n\n", net.get_rssi()); 00057 00058 printf("\Wifi Example Done,MQTT Example Start\n"); 00059 00060 // MQTT Example Start 00061 //float version = 0.6; 00062 char* publishtopic = "publishtest"; 00063 char* subscribetopic = "subscribtest"; 00064 00065 //logMessage("HelloMQTT: version is %.2f\r\n", version); 00066 //char assess_token[] = "uNYseQgqntIrL7q5F2tL"; 00067 NetworkInterface* network = &net; 00068 if (!network) { 00069 return -1; 00070 } 00071 00072 MQTTNetwork mqttNetwork(network); 00073 00074 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); 00075 00076 const char* hostname = MQTT_HOST; 00077 int port = MQTT_PORT; 00078 logMessage("Connecting to %s:%d\r\n", hostname, port); 00079 int rc = mqttNetwork.connect(hostname, port); 00080 if (rc != 0) 00081 logMessage("rc from TCP connect is %d\r\n", rc); 00082 00083 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00084 //data.MQTTVersion = 3; 00085 data.clientID.cstring = "mbed-sample"; 00086 //data.username.cstring = assess_token; 00087 //data.password.cstring = ""; 00088 if ((rc = client.connect(data)) != 0) 00089 logMessage("rc from MQTT connect is %d\r\n", rc); 00090 00091 if ((rc = client.subscribe(publishtopic, MQTT::QOS2, messageArrived)) != 0) 00092 logMessage("rc from MQTT subscribe is %d\r\n", rc); 00093 if ((rc = client.subscribe(subscribetopic, MQTT::QOS2, messageArrived)) != 0) 00094 logMessage("rc from MQTT subscribe is %d\r\n", rc); 00095 00096 00097 00098 printf("successfully connect!\n\n"); 00099 00100 // Initialize sensors -------------------------------------------------- 00101 00102 std::string inputdata; 00103 while (1) { 00104 char ch; 00105 while (uart.readable()) { 00106 ch = uart.getc(); 00107 if (ch == 's') { 00108 inputdata.clear(); 00109 } 00110 inputdata += ch; 00111 if (ch == '#') { 00112 inputdata.erase(0,1); 00113 inputdata.erase(inputdata.length()-1,1); 00114 pc.printf("%s\r\n",inputdata); 00115 char msg[inputdata.size()+1]; 00116 inputdata.copy(msg,inputdata.size()+1); 00117 msg[inputdata.size()]='\0'; 00118 int n=strlen(msg); 00119 void *payload = reinterpret_cast<void*>(msg); 00120 size_t payload_len = n; 00121 //printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT,MQTT_TOPIC); 00122 00123 if(client.publish(MQTT_TOPIC, payload, n) < 0) { 00124 printf("failed to publish MQTT message"); 00125 } 00126 } 00127 } 00128 } 00129 00130 00131 00132 // net.disconnect(); 00133 00134 }
Generated on Sun Jul 24 2022 01:10:38 by
1.7.2