V3 of the fire detector project

Dependencies:   BSP_B-L475E-IOT01

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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