mqtt esp8266
Dependencies: X_NUCLEO_IKS01A3
main.cpp
00001 00002 #include "mbed.h" 00003 #include "platform/mbed_thread.h" 00004 #include <MQTTClientMbedOs.h> 00005 #include "XNucleoIKS01A3.h" 00006 00007 // Blinking rate in milliseconds 00008 #define BLINKING_RATE_MS 500 00009 // Initialise the digital pin LED1 as an output 00010 DigitalOut led(LED1); 00011 00012 /* Instantiate the expansion board */ 00013 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4); 00014 00015 /* Retrieve the composing elements of the expansion board */ 00016 static STTS751Sensor *t_sensor = mems_expansion_board->t_sensor; 00017 00018 00019 volatile int mems_event = 0; 00020 uint32_t previous_tick = 0; 00021 uint32_t current_tick = 0; 00022 uint8_t high = 0, low = 0; 00023 float temperature = 0.0f; 00024 char buffer[32]; 00025 00026 void INT_cb(); 00027 00028 /* Helper function for printing floats & doubles */ 00029 static char *print_double(char *str, double v, int decimalDigits = 2) 00030 { 00031 int i = 1; 00032 int intPart, fractPart; 00033 int len; 00034 char *ptr; 00035 00036 /* prepare decimal digits multiplicator */ 00037 for (; decimalDigits != 0; i *= 10, decimalDigits--); 00038 00039 /* calculate integer & fractinal parts */ 00040 intPart = (int)v; 00041 fractPart = (int)((v - (double)(int)v) * i); 00042 00043 /* fill in integer part */ 00044 sprintf(str, "%i.", intPart); 00045 00046 /* prepare fill in of fractional part */ 00047 len = strlen(str); 00048 ptr = &str[len]; 00049 00050 /* fill in leading fractional zeros */ 00051 for (i /= 10; i > 1; i /= 10, ptr++) { 00052 if (fractPart >= i) { 00053 break; 00054 } 00055 *ptr = '0'; 00056 } 00057 00058 /* fill in (rest of) fractional part */ 00059 sprintf(ptr, "%i", fractPart); 00060 00061 return str; 00062 } 00063 00064 InterruptIn button(USER_BUTTON); 00065 int arrivedcount = 0; 00066 TCPSocket socket; 00067 MQTTClient client(&socket); 00068 MQTT::Message message; 00069 int button_pressed=0; 00070 00071 char* topic_pub = "PMK-client-temperature"; 00072 char* topic_sub = "PMK-client-topic_sub"; 00073 00074 WiFiInterface *wifi; 00075 00076 const char *sec2str(nsapi_security_t sec) 00077 { 00078 switch (sec) { 00079 case NSAPI_SECURITY_NONE: 00080 return "None"; 00081 case NSAPI_SECURITY_WEP: 00082 return "WEP"; 00083 case NSAPI_SECURITY_WPA: 00084 return "WPA"; 00085 case NSAPI_SECURITY_WPA2: 00086 return "WPA2"; 00087 case NSAPI_SECURITY_WPA_WPA2: 00088 return "WPA/WPA2"; 00089 case NSAPI_SECURITY_UNKNOWN: 00090 default: 00091 return "Unknown"; 00092 } 00093 } 00094 00095 int scan_demo(WiFiInterface *wifi) 00096 { 00097 WiFiAccessPoint *ap; 00098 printf("Scan:\n"); 00099 int count = wifi->scan(NULL,0); 00100 if (count <= 0) { 00101 printf("scan() failed with return value: %d\n", count); 00102 return 0; 00103 } 00104 /* Limit number of network arbitrary to 15 */ 00105 count = count < 15 ? count : 15; 00106 ap = new WiFiAccessPoint[count]; 00107 count = wifi->scan(ap, count); 00108 if (count <= 0) { 00109 printf("scan() failed with return value: %d\n", count); 00110 return 0; 00111 } 00112 for (int i = 0; i < count; i++) { 00113 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), 00114 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], 00115 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); 00116 } 00117 printf("%d networks available.\n", count); 00118 delete[] ap; 00119 return count; 00120 } 00121 00122 void messageArrived(MQTT::MessageData& md) 00123 { 00124 MQTT::Message &message = md.message; 00125 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); 00126 printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); 00127 ++arrivedcount; 00128 } 00129 00130 void buttonFunction() { 00131 button_pressed=1; 00132 } 00133 00134 00135 00136 00137 int main() 00138 { 00139 /* Attach callback to STTS751 INT */ 00140 t_sensor->attach_int_irq(&INT_cb); 00141 /* Enable STTS751 temperature sensor */ 00142 t_sensor->enable(); 00143 /* Set ODR to 4Hz */ 00144 t_sensor->set_odr(4.0f); 00145 /* Set Low Temperature Threshold */ 00146 t_sensor->set_low_temp_thr(22.0f); 00147 /* Set High Temperature Threshold */ 00148 t_sensor->set_high_temp_thr(28.0f); 00149 /* Enable Event pin */ 00150 t_sensor->set_event_pin(1); 00151 /* Get beginning status */ 00152 t_sensor->get_temp_limit_status(NULL, NULL, NULL); 00153 00154 button.rise(&buttonFunction); // attach the address of the flip function to the rising edge 00155 00156 const char* hostname = "broker.mqttdashboard.com"; 00157 int port = 1883; 00158 00159 wifi = WiFiInterface::get_default_instance(); 00160 if (!wifi) { 00161 printf("ERROR: No WiFiInterface found.\n"); 00162 return -1; 00163 } 00164 00165 int count = scan_demo(wifi); 00166 if (count == 0) { 00167 printf("No WIFI APs found - can't continue further.\n"); 00168 return -1; 00169 } 00170 00171 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00172 int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00173 if (ret != 0) { 00174 printf("\nConnection error: %d\n", ret); 00175 return -1; 00176 } 00177 00178 printf("Success\n\n"); 00179 printf("MAC: %s\n", wifi->get_mac_address()); 00180 printf("IP: %s\n", wifi->get_ip_address()); 00181 printf("Netmask: %s\n", wifi->get_netmask()); 00182 printf("Gateway: %s\n", wifi->get_gateway()); 00183 printf("RSSI: %d\n\n", wifi->get_rssi()); 00184 00185 socket.open(wifi); 00186 socket.connect(hostname, port); 00187 00188 int rc=0; 00189 00190 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00191 data.MQTTVersion = 3; 00192 data.clientID.cstring = "PMK-client"; 00193 //data.username.cstring = "testuser"; 00194 //data.password.cstring = "testpassword"; 00195 if ((rc = client.connect(data)) != 0) 00196 printf("rc from MQTT connect is %d\r\n", rc); 00197 00198 if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0) 00199 printf("rc from MQTT subscribe is %d\r\n", rc); 00200 00201 while (true) { 00202 t_sensor->get_temperature(&temperature); 00203 printf("Temp[C]: %7s C\r\n", print_double(buffer, temperature)); 00204 led = !led; 00205 thread_sleep_for(BLINKING_RATE_MS); 00206 if (button_pressed==1) { 00207 button_pressed=0; 00208 printf("Publishing data\r\n"); 00209 // QoS 0 00210 char buf[100]; 00211 sprintf(buf, "Temp[C]: %7s C\r\n", print_double(buffer, temperature)); 00212 message.qos = MQTT::QOS0; 00213 message.retained = false; 00214 message.dup = false; 00215 message.payload = (void*)buf; 00216 message.payloadlen = strlen(buf)+1; 00217 client.publish(topic_pub, message); 00218 } 00219 printf("Yielding"); 00220 client.yield(1000); 00221 printf(" -> Yielded\r\n"); 00222 00223 } 00224 } 00225 00226 void INT_cb() 00227 { 00228 mems_event = 1; 00229 }
Generated on Tue Jul 12 2022 18:16:58 by
1.7.2