Fire Detector IoT
Dependencies: BSP_B-L475E-IOT01
main.cpp
00001 /*THIS PROGRAM USES PARTS OF THE FOLLOWING SOFTWARE 00002 * WiFi Example 00003 * Copyright (c) 2018 ARM Limited 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 // This version of Fire Detector IoT is created by 00019 // Fouad BRAX 00020 // Achille CADIX 00021 // Guilherme LUBK DO PRADO 00022 00023 #include "mbed.h" 00024 #include "TCPSocket.h" 00025 #include "stm32l475e_iot01_tsensor.h" 00026 00027 #define FIRE_DETECTED (1UL << 0) 00028 00029 #define WIFI_IDW0XX1 2 00030 #define TS_DEVICE "stmWifi" 00031 #define thingspeak_APIkey_write "SCS2JFDTW4EZC0NP" 00032 #define thingspeak_APIkey_read "O3ZVNRCX5J95EB4G" 00033 00034 #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH)) 00035 #include "ISM43362Interface.h" 00036 ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, MBED_CONF_APP_WIFI_SPI_MISO, MBED_CONF_APP_WIFI_SPI_SCLK, MBED_CONF_APP_WIFI_SPI_NSS, MBED_CONF_APP_WIFI_RESET, MBED_CONF_APP_WIFI_DATAREADY, MBED_CONF_APP_WIFI_WAKEUP, false); 00037 00038 #else // External WiFi modules 00039 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 00040 #include "SpwfSAInterface.h" 00041 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); 00042 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 00043 #endif 00044 00045 //LEDS used for debugging 00046 DigitalOut led1(LED1, 1); 00047 DigitalOut led2(LED2, 1); 00048 00049 //Buzzer 00050 PwmOut buzzer(D9); 00051 00052 //Interruptions 00053 InterruptIn button(D3); 00054 InterruptIn IR(D7); 00055 00056 //Thread 00057 Thread thread; 00058 00059 //Event flag to control thread start 00060 EventFlags event_flags; 00061 00062 00063 00064 void button_ISP() 00065 { 00066 led1 = 0; 00067 NVIC_SystemReset(); 00068 } 00069 00070 void IR_ISP() 00071 { 00072 led1 = 1; 00073 event_flags.set(FIRE_DETECTED); 00074 IR.rise(NULL); 00075 } 00076 00077 const char *sec2str(nsapi_security_t sec) 00078 { 00079 switch (sec) { 00080 case NSAPI_SECURITY_NONE: 00081 return "None"; 00082 case NSAPI_SECURITY_WEP: 00083 return "WEP"; 00084 case NSAPI_SECURITY_WPA: 00085 return "WPA"; 00086 case NSAPI_SECURITY_WPA2: 00087 return "WPA2"; 00088 case NSAPI_SECURITY_WPA_WPA2: 00089 return "WPA/WPA2"; 00090 case NSAPI_SECURITY_UNKNOWN: 00091 default: 00092 return "Unknown"; 00093 } 00094 } 00095 00096 void main_routine() 00097 { 00098 //Temperature reading 00099 BSP_TSENSOR_Init(); 00100 float temperature_value = 0; 00101 int fire_state=1; 00102 //Check fire variable 00103 int numReadings = 0; 00104 00105 while(true) { 00106 //wait for a fire reading 00107 event_flags.wait_any(FIRE_DETECTED); 00108 00109 //check against false readings 00110 for(int n = 0; n < 5; n++) { 00111 numReadings += IR.read(); 00112 led1 = !led1; 00113 wait(0.2); 00114 } 00115 00116 printf("\nNum of positive readings: %d\n\r", numReadings); 00117 00118 if(numReadings >= 5) { 00119 buzzer.write(1.0f); 00120 printf("\nFIRE DETECTED - SENDING DATA\n\r"); 00121 IR.rise(NULL); 00122 led1 = 1; 00123 led2 = 1; 00124 00125 //Connect to ThingSpeak and starting sending data 00126 int ret = 1; 00127 do { 00128 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00129 ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00130 } while(ret != 0); 00131 00132 printf("Success\n\n"); 00133 printf("MAC: %s\n", wifi.get_mac_address()); 00134 printf("IP: %s\n", wifi.get_ip_address()); 00135 printf("Netmask: %s\n", wifi.get_netmask()); 00136 printf("Gateway: %s\n", wifi.get_gateway()); 00137 printf("RSSI: %d\n\n", wifi.get_rssi()); 00138 00139 NetworkInterface *net = &wifi; 00140 00141 TCPSocket socket; 00142 nsapi_error_t response; 00143 char sbuffer[256]; 00144 char message[64]; 00145 00146 while(true) { 00147 00148 buzzer.write(1.0f); 00149 // Open a socket on the network interface, and create a TCP connection to thingspeaks.com 00150 socket.open(net); 00151 response = socket.connect("api.thingspeak.com", 80); 00152 if(0 != response) { 00153 printf("Error connecting: %d\n", response); 00154 socket.close(); 00155 return; 00156 } 00157 00158 printf("Connected to the Server\n"); 00159 00160 //lecture des données des capteurs et actualisation des variables à transmettre 00161 temperature_value = BSP_TSENSOR_ReadTemp() - 8; 00162 led1 = !led1; 00163 00164 00165 /* Construct content of HTTP command */ 00166 //message à transmettre (données des capteurs) 00167 sprintf(message, "{\"field1\": %0.2f, \"field2\": %d}", temperature_value,fire_state); 00168 printf("Content Length = %d\r\n", (int)strlen(message)); 00169 00170 /* Construct HTTP command to send */ 00171 // Phase de transmission des données à ThingSpeaks.com 00172 sprintf(sbuffer, "GET /update?api_key=%s HTTP/1.1\r\nHost: api.thingspeak.com\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", thingspeak_APIkey_write, 00173 (int)strlen(message),message); 00174 printf("HTTP command %s\r\n", sbuffer); 00175 wait(1.0); // temporisation avant la nouvelle transmission 00176 00177 // Send a simple http request 00178 printf("Sending HTTP request to thingspeak.com...\n");// 00179 nsapi_size_t size = strlen(sbuffer); 00180 response = 0; 00181 while(size) { 00182 response = socket.send(sbuffer+response, size); 00183 if (response < 0) { 00184 printf("Error sending data: %d\n", response); 00185 socket.close(); 00186 return; 00187 } else { 00188 size -= response; 00189 // Check if entire message was sent or not 00190 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); 00191 } 00192 } 00193 buzzer.write(0.8f); 00194 00195 // Receive a simple http response and print out the response line 00196 char rbuffer[64]; 00197 response = socket.recv(rbuffer, sizeof rbuffer); 00198 if (response < 0) { 00199 printf("Error receiving data: %d\n", response); 00200 } else { 00201 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); 00202 } 00203 // Close the socket to return its memory and bring down the network interface 00204 socket.close(); 00205 fire_state=0; 00206 } 00207 00208 00209 } else { 00210 numReadings = 0; 00211 if(IR.read() == 0) { 00212 event_flags.set(0); 00213 IR.rise(&IR_ISP); 00214 printf("\nNo fire detected, returning IDLE\n\r"); 00215 } else { 00216 event_flags.set(FIRE_DETECTED); 00217 printf("\nSensor still high, re-checking\n\r"); 00218 } 00219 } 00220 00221 00222 } 00223 } 00224 00225 00226 00227 int main() 00228 { 00229 buzzer.period(0.5f); // 4 second period 00230 buzzer.write(0.0f); 00231 led1 = 0; 00232 led2 = 0; 00233 thread.start(mbed::callback(main_routine)); 00234 button.fall(&button_ISP); 00235 IR.rise(&IR_ISP); 00236 //main function will sleep 00237 while(true) { 00238 wait(1); 00239 } 00240 00241 }
Generated on Tue Aug 23 2022 11:04:37 by
1.7.2