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 00004 #include "mbed.h" 00005 #include "TCPSocket.h" 00006 #include "wifi-ism43362/ISM43362Interface.h" 00007 #define logMessage printf 00008 #define MQTTCLIENT_QOS2 1 00009 #include "MQTTNetwork.h" 00010 #include "MQTTmbed.h" 00011 #include "MQTTClient.h" 00012 #include "HTS221Sensor.h" 00013 //#define MQTT_HOST "demo.thingsboard.io" 00014 #define MQTT_HOST "192.168.43.14" 00015 #define MQTT_PORT 1883 00016 #define MQTT_TOPIC "Pubtest" 00017 #include <string> 00018 #include "VL53L0X.h" 00019 ISM43362Interface net; 00020 // WiFiInterface *wifi; 00021 Serial pc(USBTX,USBRX); 00022 void messageArrived(MQTT::MessageData& md) 00023 { 00024 MQTT::Message &message = md.message; 00025 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); 00026 logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); 00027 //++arrivedcount; 00028 } 00029 static DevI2C devI2c(PB_11,PB_10); 00030 static DigitalOut shutdown_pin(PC_6); 00031 static VL53L0X range(&devI2c, &shutdown_pin, PC_7); 00032 Serial uart(PC_4, PC_5);//TX,RX 00033 00034 int idx=0; 00035 std::string inputdata; 00036 char ch1; 00037 int main(void){ 00038 uart.baud(115200); 00039 range.init_sensor(VL53L0X_DEFAULT_ADDRESS); 00040 int count = 0; 00041 00042 printf("\r\nWiFi+MQTT Example Demo\n"); 00043 00044 // Connect to Wifi 00045 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00046 int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00047 if (ret != 0) { 00048 printf("\nConnection error: %d\n", ret); 00049 return -1; 00050 } 00051 00052 printf("Success\n\n"); 00053 printf("MAC: %s\n", net.get_mac_address()); 00054 printf("IP: %s\n", net.get_ip_address()); 00055 printf("Netmask: %s\n", net.get_netmask()); 00056 printf("Gateway: %s\n", net.get_gateway()); 00057 printf("RSSI: %d\n\n", net.get_rssi()); 00058 00059 printf("\Wifi Example Done,MQTT Example Start\n"); 00060 00061 // MQTT Example Start 00062 float version = 0.6; 00063 char* publishtopic = "publishtest"; 00064 char* subscribetopic = "subscribtest"; 00065 00066 logMessage("HelloMQTT: version is %.2f\r\n", version); 00067 char assess_token[] = "uNYseQgqntIrL7q5F2tL"; 00068 NetworkInterface* network = &net; 00069 if (!network) { 00070 return -1; 00071 } 00072 00073 MQTTNetwork mqttNetwork(network); 00074 00075 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); 00076 00077 const char* hostname = MQTT_HOST; 00078 int port = MQTT_PORT; 00079 logMessage("Connecting to %s:%d\r\n", hostname, port); 00080 int rc = mqttNetwork.connect(hostname, port); 00081 if (rc != 0) 00082 logMessage("rc from TCP connect is %d\r\n", rc); 00083 00084 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00085 //data.MQTTVersion = 3; 00086 data.clientID.cstring = "mbed-sample"; 00087 data.username.cstring = assess_token; 00088 //data.password.cstring = ""; 00089 if ((rc = client.connect(data)) != 0) 00090 logMessage("rc from MQTT connect is %d\r\n", rc); 00091 00092 if ((rc = client.subscribe(publishtopic, MQTT::QOS2, messageArrived)) != 0) 00093 logMessage("rc from MQTT subscribe is %d\r\n", rc); 00094 if ((rc = client.subscribe(subscribetopic, MQTT::QOS2, messageArrived)) != 0) 00095 logMessage("rc from MQTT subscribe is %d\r\n", rc); 00096 00097 00098 00099 printf("successfully connect!\n\n"); 00100 00101 // Initialize sensors -------------------------------------------------- 00102 00103 //uint8_t id; 00104 //DevI2C i2c_2(PB_11, PB_10); 00105 //HTS221Sensor hum_temp(&i2c_2); 00106 00107 // hum_temp.init(NULL); 00108 //hum_temp.enable(); 00109 //hum_temp.read_id(&id); 00110 00111 //printf("HTS221 humidity & temperature sensor = 0x%X\r\n", id); 00112 //std::string inputdata; 00113 //inputdata="{\"PM2.5\":10,\"PM10\":20,\"temperature\":20,\"humidity\":45}"; 00114 while (1) { 00115 00116 //buffer variables 00117 char ch; 00118 //if data is ready in the buffer 00119 while (uart.readable()) { 00120 //read 1 character 00121 00122 ch = uart.getc(); 00123 //pc.printf("datacomming:%c",ch); 00124 /////Pm25/////// 00125 00126 if (ch == 's') { 00127 //so the pointer should be set to the first position 00128 inputdata.clear(); 00129 //pc.printf("start string"); 00130 } 00131 //write buffer character to big buffer string 00132 inputdata += ch; 00133 //pc.printf("input string:%s",inputdata); 00134 //if the character is # than the end of the sentence is 00135 //reached and some stuff has to be done 00136 if (ch == '#') { 00137 //remove start and stop characters 00138 inputdata.erase(0,1); 00139 inputdata.erase(inputdata.length()-1,1); 00140 pc.printf("%s",inputdata); 00141 char msg[inputdata.size()+1]; 00142 inputdata.copy(msg,inputdata.size()+1); 00143 msg[inputdata.size()]='\0'; 00144 int n=strlen(msg); 00145 void *payload = reinterpret_cast<void*>(msg); 00146 size_t payload_len = n; 00147 00148 printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT, MQTT_TOPIC); 00149 00150 if (client.publish(MQTT_TOPIC, payload, n) < 0) { 00151 printf("failed to publish MQTT message"); 00152 } 00153 } 00154 } 00155 00156 } 00157 00158 00159 00160 // net.disconnect(); 00161 00162 }
Generated on Tue Jul 26 2022 08:19:58 by
1.7.2