n/a

Dependencies:   C12832 mbed-http

Fork of HTTP-Python-Demo by Cambridge Hackathon

Files at this revision

API Documentation at this revision

Comitter:
BKasza
Date:
Sun Nov 26 07:24:36 2017 +0000
Parent:
3:8135a6313648
Commit message:
It gets compiled - Let's ship it

Changed in this revision

config.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 8135a6313648 -r 14c774cf7ac2 config.h
--- a/config.h	Sun Nov 26 00:22:09 2017 +0000
+++ b/config.h	Sun Nov 26 07:24:36 2017 +0000
@@ -1,6 +1,17 @@
 #ifndef _CONFIG_H
 #define _CONFIG_H
 
-#define SERVERNAME "Bianca"
+#define SERVERNAME          "Bianca"
+#define GETTIMERINIT        5
+#define REPORTTIMERINIT     10
+#define ALERTLISTSIZE       32
+
+/* These macros are to be set to 0 when the sensor is not used. When the sensor is used, the corresponding macro should be 1 (and not any other number;
+   this is important for calculating array indices, sizes etc... */ 
+#define USING_TEMPERATURE   1
+#define USING_LIGHT         1
+#define USING_AIR           1
+#define USING_LASER         0
+#define USING_ACCEL         1
 
 #endif //_CONFIG_H
\ No newline at end of file
diff -r 8135a6313648 -r 14c774cf7ac2 main.cpp
--- a/main.cpp	Sun Nov 26 00:22:09 2017 +0000
+++ b/main.cpp	Sun Nov 26 07:24:36 2017 +0000
@@ -16,14 +16,53 @@
 #include "C12832.h"
 #include "OdinWiFiInterface.h"
 #include "http_request.h"
+#include <string>
 
 C12832  lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
 OdinWiFiInterface wifiIF;
+NetworkInterface* netIF;
 InterruptIn post_button(PF_2);
 InterruptIn get_put_button(PG_4);
 volatile bool post_clicked = false;
 volatile bool get_clicked = false;
 volatile bool put_clicked = false;
+Timer timeSinceGet;
+float getTimer;
+Timer timeSinceReport;
+float reportTimer;
+
+struct sensorWatcher {
+    char sensor;
+    uint8_t id;
+    uint8_t active;
+    float lowThreshold;
+    float highThreshold;
+};
+sensorWatcher alertList[ALERTLISTSIZE];
+
+struct sensorData {
+#if USING_TEMPERATURE
+    float temperature;
+    float humidity;
+#endif
+#if USING_LIGHT
+    float visible;
+    float infrared;
+#endif
+#if USING_AIR
+    float co;
+    float tvoc;
+#endif
+#if USING_LASER
+    float distance;
+#endif
+#if USING_ACCEL
+    float xAcceleration;
+    float yAcceleration;
+    float zAcceleration;
+#endif  
+};
+sensorData measurements;
 
 void lcd_print(const char* message) {
     lcd.cls();
@@ -39,10 +78,56 @@
     get_clicked = true;
 }
 
+void httpPost(const char* body, int msgLen) {
+    HttpRequest* request = new HttpRequest(netIF, HTTP_POST, "http://10.25.2.152:8080");
+    request->set_header("Content-Type", "application/json");
+    HttpResponse* response = request->send(body, msgLen);
+    delete request;
+}
+
+void addAlert(char* alertData) {
+    int ii;
+    for(ii=0;alertList[ii].id!=0xff && ii<ALERTLISTSIZE;ii++);
+    if(ii<ALERTLISTSIZE){
+        alertList[ii].sensor = alertData[0];
+        alertList[ii].id = (uint8_t)alertData[1];
+        alertList[ii].lowThreshold = *((float*)(&(alertData[2])));
+        alertList[ii].highThreshold = *((float*)(&(alertData[6])));
+    }
+}
+
+void destroyAlert(int alertID) {
+    for(int ii=0;ii<ALERTLISTSIZE;ii++) {
+        if(alertList[ii].id == (uint8_t)alertID) {
+            alertList[ii].id = 0xff;
+            alertList[ii].sensor = 'E';
+        }
+    }
+}
+
+void updateTimers(char* timerData) {
+    float newGetTmr = *((float*)(&(timerData[0])));
+    float newReportTmr = *((float*)(&(timerData[4])));
+    
+    if(newGetTmr > 131072) {
+        getTimer = GETTIMERINIT;
+    } else if(newGetTmr != 0) {
+        getTimer = newGetTmr;
+    }
+    
+    if(newReportTmr > 131072) {
+        newReportTmr = REPORTTIMERINIT;
+    } else if(newReportTmr != 0) {
+        getTimer = newReportTmr;
+    }
+}
+
 int main() {
 
     int ret;
     SocketAddress serverAddr;
+    getTimer = GETTIMERINIT;
+    reportTimer = REPORTTIMERINIT;
     
     lcd_print("Connecting...");
     ret = wifiIF.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
@@ -52,55 +137,259 @@
     }
     lcd_print("Successfully connected!");
     lcd_print(wifiIF.get_mac_address());
-    wifiIF.gethostbyname(SERVERNAME, &serverAddr);
+    //wifiIF.gethostbyname(SERVERNAME, &serverAddr);
 
     /* Enable interrupts */
     post_button.rise(&send_post);
     get_put_button.rise(&send_get_put);
     
+    /* Init */
+    for(int ii=0;ii<32;ii++) {
+        alertList[ii].sensor = 'E';    //E for empty
+        alertList[ii].id = 0xff;
+    }
+    
     /* Main loop */
+    netIF = &wifiIF;
+    timeSinceGet.start();
+    timeSinceReport.start();
     while (1) {
-        /* Post data */
-        if (post_clicked) {
-            lcd_print("post");
-            post_clicked = false;
-            NetworkInterface* netIF = &wifiIF;
-            HttpRequest* request = new HttpRequest(netIF, HTTP_POST, "http://10.25.2.152:8080");
-            request->set_header("Content-Type", "application/json");
-            const char body[] = "HelloWorld";
-            HttpResponse* response = request->send(body, strlen(body));
-            lcd_print(response->get_body_as_string().c_str());
-            delete request;
-        }
-        
-        /* Get data */
-        if (get_clicked) {
-            get_clicked = false;
-            put_clicked = true;
-            NetworkInterface* netIF = &wifiIF;
+        /* Ask the server if it has any updates */
+        if(timeSinceGet.read() > getTimer) {
             HttpRequest* request = new HttpRequest(netIF, HTTP_GET, "http://10.25.2.152:8080");
             request->set_header("Content-Type", "application/json");
             const char body[] = "{\"get\":\"request\"}";
             HttpResponse* response = request->send(body, strlen(body));
-            lcd_print(response->get_body_as_string().c_str());
+            char* arrui8Msg = (char*)response->get_body_as_string().c_str();
+            lcd_print(arrui8Msg);
+            char chr = arrui8Msg[0];
+            if(chr == 'T') {
+                updateTimers(&(arrui8Msg[1]));   
+            } else if(chr == 'E') {
+                if(arrui8Msg[1]=='C') {
+                    addAlert(&(arrui8Msg[2]));
+                } else if(arrui8Msg[1]=='D') {
+                    destroyAlert((uint8_t)arrui8Msg[2]);
+                }      
+            }
+            
             delete request;
+            /* Restart timer */
+            timeSinceGet.reset();
         }
-
-        wait_ms(2000);
+        /* Send reports periodically */
+        if(timeSinceReport.read() > reportTimer) {
+            //TODO collect sensor data
+            char reportMsg[(USING_TEMPERATURE*2+USING_LIGHT*2+USING_AIR*2+USING_LASER+USING_ACCEL*3+1)*4+2];//+2 because of the 'R' character and the eof
+            reportMsg[0] = 'R';
+            for(int ii=0;ii<4;ii++){
+#if USING_TEMPERATURE
+                reportMsg[1+ii]=*(((uint8_t*)(&(measurements.temperature)))+ii);
+                reportMsg[5+ii]=*(((uint8_t*)(&(measurements.humidity)))+ii);
+#endif
+#if USING_LIGHT
+                reportMsg[1+8*USING_TEMPERATURE+ii]=*(((uint8_t*)(&(measurements.visible)))+ii);
+                reportMsg[5+8*USING_TEMPERATURE+ii]=*(((uint8_t*)(&(measurements.infrared)))+ii);
+#endif
+#if USING_AIR
+                reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT)+ii]=*(((uint8_t*)(&(measurements.co)))+ii);
+                reportMsg[5+8*(USING_TEMPERATURE+USING_LIGHT)+ii]=*(((uint8_t*)(&(measurements.tvoc)))+ii);
+#endif
+#if USING_LASER
+                reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT+USING_AIR)+ii]=*(((uint8_t)(&(measurements.distance)))+ii);
+#endif
+#if USING_ACCEL
+                reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT+USING_AIR)+4*USING_LASER+ii]=*(((uint8_t*)(&(measurements.xAcceleration)))+ii);
+                reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT+USING_AIR)+4*USING_LASER+ii]=*(((uint8_t*)(&(measurements.yAcceleration)))+ii);
+                reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT+USING_AIR)+4*USING_LASER+ii]=*(((uint8_t*)(&(measurements.zAcceleration)))+ii);
+#endif
+            }
+            /*char testMsg[13] = "Hello World!";
+            testMsg[5] = (char)0x00;
+            httpPost(testMsg, 13);*/
+            httpPost(reportMsg, (USING_TEMPERATURE*2+USING_LIGHT*2+USING_AIR*2+USING_LASER+USING_ACCEL*3+1)*4+2);
+            /* Restart timer */
+            timeSinceReport.reset();
+        }
         
-        /* Put */
-        if (put_clicked) {
-            put_clicked = false;
-            lcd_print("put");
-            NetworkInterface* net = &wifiIF;
-            HttpRequest* request = new HttpRequest(net, HTTP_PUT, "http://10.25.2.152:8080");
-            request->set_header("Content-Type", "application/json");
-            const char body[] = "{\"put\":\"request\"}";
-            HttpResponse* response = request->send(body, strlen(body));
-            lcd_print(response->get_body_as_string().c_str());
-            delete request;
+        /* Check whether or not to send alert */
+        uint8_t breach; //0x0f if below lower boundary, 0xf0 if above higher boundary
+        uint8_t alertReset;
+        for(int ii=0;ii<ALERTLISTSIZE;ii++){
+            breach = 0x00;
+            alertReset = 0;
+            switch(alertList[ii].sensor) {
+#if USING_TEMPERATURE
+                case 'T':
+                    if(!alertList[ii].active) {
+                        if(measurements.temperature < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.temperature > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.temperature > alertList[ii].lowThreshold
+                                && measurements.temperature < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                    break;
+                case 'H':
+                     if(!alertList[ii].active) {
+                        if(measurements.humidity < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.humidity > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.humidity > alertList[ii].lowThreshold
+                                && measurements.humidity < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                   break;
+#endif
+#if USING_LIGHT
+                case 'V':
+                    if(!alertList[ii].active) {
+                        if(measurements.visible < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.visible > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.visible > alertList[ii].lowThreshold
+                                && measurements.visible < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                    break;
+                case 'I':
+                     if(!alertList[ii].active) {
+                        if(measurements.infrared < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.infrared > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.infrared > alertList[ii].lowThreshold
+                                && measurements.infrared < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                   break;
+#endif
+#if USING_AIR
+                case 'C':
+                    if(!alertList[ii].active) {
+                        if(measurements.co < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.co > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.co > alertList[ii].lowThreshold
+                                && measurements.co < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                    break;
+                case 'O':
+                    if(!alertList[ii].active) {
+                        if(measurements.tvoc < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.tvoc > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.tvoc > alertList[ii].lowThreshold
+                                && measurements.tvoc < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                    break;
+#endif
+#if USING_LASER
+                case 'D':
+                    if(!alertList[ii].active) {
+                        if(measurements.distance < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.distance > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.distance > alertList[ii].lowThreshold
+                                && measurements.distance < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                    break;
+#endif
+#if USING_ACCEL
+                case 'X':
+                    if(!alertList[ii].active) {
+                        if(measurements.xAcceleration < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.xAcceleration > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.xAcceleration > alertList[ii].lowThreshold
+                                && measurements.xAcceleration < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                    break;
+                case 'Y':
+                    if(!alertList[ii].active) {
+                        if(measurements.yAcceleration < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.yAcceleration > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.yAcceleration > alertList[ii].lowThreshold
+                                && measurements.yAcceleration < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                    break;
+                case 'Z':
+                    if(!alertList[ii].active) {
+                        if(measurements.zAcceleration < alertList[ii].lowThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0x0f;
+                        } else if(measurements.zAcceleration > alertList[ii].highThreshold) {
+                            alertList[ii].active = 1;
+                            breach = 0xf0;
+                        }
+                    } else if( measurements.zAcceleration > alertList[ii].lowThreshold
+                                && measurements.zAcceleration < alertList[ii].highThreshold ) {
+                        alertList[ii].active = 0;
+                        alertReset = 1;        
+                    }
+                    break;
+#endif
+                default:
+                    break;
+            }
+            if(breach != 0) {
+                char msgBuff[5] = {'A', 'S', (char)alertList[ii].id, (char)breach};
+                httpPost(msgBuff, 5);
+            } else if(alertReset) {
+                char msgBuff[4] = {'A', 'R', (char)alertList[ii].id};
+                httpPost(msgBuff, 4);
+            }
         }
-   
     }
 
 }