n/a

Dependencies:   C12832 mbed-http

Fork of HTTP-Python-Demo by Cambridge Hackathon

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //----------------------------------------------------------------------------
00002 // The confidential and proprietary information contained in this file may
00003 // only be used by a person authorised under and to the extent permitted
00004 // by a subsisting licensing agreement from ARM Limited or its affiliates.
00005 //
00006 // (C) COPYRIGHT 2016 ARM Limited or its affiliates.
00007 // ALL RIGHTS RESERVED
00008 //
00009 // This entire notice must be reproduced on all copies of this file
00010 // and copies of this file may only be made by a person if such person is
00011 // permitted to do so under the terms of a subsisting license agreement
00012 // from ARM Limited or its affiliates.
00013 //----------------------------------------------------------------------------
00014 #include "mbed.h"
00015 #include "config.h"
00016 #include "C12832.h"
00017 #include "OdinWiFiInterface.h"
00018 #include "http_request.h"
00019 #include <string>
00020 
00021 C12832  lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
00022 OdinWiFiInterface wifiIF;
00023 NetworkInterface* netIF;
00024 InterruptIn post_button(PF_2);
00025 InterruptIn get_put_button(PG_4);
00026 volatile bool post_clicked = false;
00027 volatile bool get_clicked = false;
00028 volatile bool put_clicked = false;
00029 Timer timeSinceGet;
00030 float getTimer;
00031 Timer timeSinceReport;
00032 float reportTimer;
00033 
00034 struct sensorWatcher {
00035     char sensor;
00036     uint8_t id;
00037     uint8_t active;
00038     float lowThreshold;
00039     float highThreshold;
00040 };
00041 sensorWatcher alertList[ALERTLISTSIZE];
00042 
00043 struct sensorData {
00044 #if USING_TEMPERATURE
00045     float temperature;
00046     float humidity;
00047 #endif
00048 #if USING_LIGHT
00049     float visible;
00050     float infrared;
00051 #endif
00052 #if USING_AIR
00053     float co;
00054     float tvoc;
00055 #endif
00056 #if USING_LASER
00057     float distance;
00058 #endif
00059 #if USING_ACCEL
00060     float xAcceleration;
00061     float yAcceleration;
00062     float zAcceleration;
00063 #endif  
00064 };
00065 sensorData measurements;
00066 
00067 void lcd_print(const char* message) {
00068     lcd.cls();
00069     lcd.locate(0, 3);
00070     lcd.printf(message);
00071 }
00072 
00073 void send_post() {
00074     post_clicked = true;
00075 }
00076  
00077 void send_get_put() {
00078     get_clicked = true;
00079 }
00080 
00081 void httpPost(const char* body, int msgLen) {
00082     HttpRequest* request = new HttpRequest(netIF, HTTP_POST, "http://10.25.2.152:8080");
00083     request->set_header("Content-Type", "application/json");
00084     HttpResponse* response = request->send(body, msgLen);
00085     delete request;
00086 }
00087 
00088 void addAlert(char* alertData) {
00089     int ii;
00090     for(ii=0;alertList[ii].id!=0xff && ii<ALERTLISTSIZE;ii++);
00091     if(ii<ALERTLISTSIZE){
00092         alertList[ii].sensor = alertData[0];
00093         alertList[ii].id = (uint8_t)alertData[1];
00094         alertList[ii].lowThreshold = *((float*)(&(alertData[2])));
00095         alertList[ii].highThreshold = *((float*)(&(alertData[6])));
00096     }
00097 }
00098 
00099 void destroyAlert(int alertID) {
00100     for(int ii=0;ii<ALERTLISTSIZE;ii++) {
00101         if(alertList[ii].id == (uint8_t)alertID) {
00102             alertList[ii].id = 0xff;
00103             alertList[ii].sensor = 'E';
00104         }
00105     }
00106 }
00107 
00108 void updateTimers(char* timerData) {
00109     float newGetTmr = *((float*)(&(timerData[0])));
00110     float newReportTmr = *((float*)(&(timerData[4])));
00111     
00112     if(newGetTmr > 131072) {
00113         getTimer = GETTIMERINIT;
00114     } else if(newGetTmr != 0) {
00115         getTimer = newGetTmr;
00116     }
00117     
00118     if(newReportTmr > 131072) {
00119         newReportTmr = REPORTTIMERINIT;
00120     } else if(newReportTmr != 0) {
00121         getTimer = newReportTmr;
00122     }
00123 }
00124 
00125 int main() {
00126 
00127     int ret;
00128     SocketAddress serverAddr;
00129     getTimer = GETTIMERINIT;
00130     reportTimer = REPORTTIMERINIT;
00131     
00132     lcd_print("Connecting...");
00133     ret = wifiIF.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00134     if (ret != 0) {
00135         lcd_print("Connection error.");
00136         return -1;
00137     }
00138     lcd_print("Successfully connected!");
00139     lcd_print(wifiIF.get_mac_address());
00140     //wifiIF.gethostbyname(SERVERNAME, &serverAddr);
00141 
00142     /* Enable interrupts */
00143     post_button.rise(&send_post);
00144     get_put_button.rise(&send_get_put);
00145     
00146     /* Init */
00147     for(int ii=0;ii<32;ii++) {
00148         alertList[ii].sensor = 'E';    //E for empty
00149         alertList[ii].id = 0xff;
00150     }
00151     
00152     /* Main loop */
00153     netIF = &wifiIF;
00154     timeSinceGet.start();
00155     timeSinceReport.start();
00156     while (1) {
00157         /* Ask the server if it has any updates */
00158         if(timeSinceGet.read() > getTimer) {
00159             HttpRequest* request = new HttpRequest(netIF, HTTP_GET, "http://10.25.2.152:8080");
00160             request->set_header("Content-Type", "application/json");
00161             const char body[] = "{\"get\":\"request\"}";
00162             HttpResponse* response = request->send(body, strlen(body));
00163             char* arrui8Msg = (char*)response->get_body_as_string().c_str();
00164             lcd_print(arrui8Msg);
00165             char chr = arrui8Msg[0];
00166             if(chr == 'T') {
00167                 updateTimers(&(arrui8Msg[1]));   
00168             } else if(chr == 'E') {
00169                 if(arrui8Msg[1]=='C') {
00170                     addAlert(&(arrui8Msg[2]));
00171                 } else if(arrui8Msg[1]=='D') {
00172                     destroyAlert((uint8_t)arrui8Msg[2]);
00173                 }      
00174             }
00175             
00176             delete request;
00177             /* Restart timer */
00178             timeSinceGet.reset();
00179         }
00180         /* Send reports periodically */
00181         if(timeSinceReport.read() > reportTimer) {
00182             //TODO collect sensor data
00183             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
00184             reportMsg[0] = 'R';
00185             for(int ii=0;ii<4;ii++){
00186 #if USING_TEMPERATURE
00187                 reportMsg[1+ii]=*(((uint8_t*)(&(measurements.temperature)))+ii);
00188                 reportMsg[5+ii]=*(((uint8_t*)(&(measurements.humidity)))+ii);
00189 #endif
00190 #if USING_LIGHT
00191                 reportMsg[1+8*USING_TEMPERATURE+ii]=*(((uint8_t*)(&(measurements.visible)))+ii);
00192                 reportMsg[5+8*USING_TEMPERATURE+ii]=*(((uint8_t*)(&(measurements.infrared)))+ii);
00193 #endif
00194 #if USING_AIR
00195                 reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT)+ii]=*(((uint8_t*)(&(measurements.co)))+ii);
00196                 reportMsg[5+8*(USING_TEMPERATURE+USING_LIGHT)+ii]=*(((uint8_t*)(&(measurements.tvoc)))+ii);
00197 #endif
00198 #if USING_LASER
00199                 reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT+USING_AIR)+ii]=*(((uint8_t)(&(measurements.distance)))+ii);
00200 #endif
00201 #if USING_ACCEL
00202                 reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT+USING_AIR)+4*USING_LASER+ii]=*(((uint8_t*)(&(measurements.xAcceleration)))+ii);
00203                 reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT+USING_AIR)+4*USING_LASER+ii]=*(((uint8_t*)(&(measurements.yAcceleration)))+ii);
00204                 reportMsg[1+8*(USING_TEMPERATURE+USING_LIGHT+USING_AIR)+4*USING_LASER+ii]=*(((uint8_t*)(&(measurements.zAcceleration)))+ii);
00205 #endif
00206             }
00207             /*char testMsg[13] = "Hello World!";
00208             testMsg[5] = (char)0x00;
00209             httpPost(testMsg, 13);*/
00210             httpPost(reportMsg, (USING_TEMPERATURE*2+USING_LIGHT*2+USING_AIR*2+USING_LASER+USING_ACCEL*3+1)*4+2);
00211             /* Restart timer */
00212             timeSinceReport.reset();
00213         }
00214         
00215         /* Check whether or not to send alert */
00216         uint8_t breach; //0x0f if below lower boundary, 0xf0 if above higher boundary
00217         uint8_t alertReset;
00218         for(int ii=0;ii<ALERTLISTSIZE;ii++){
00219             breach = 0x00;
00220             alertReset = 0;
00221             switch(alertList[ii].sensor) {
00222 #if USING_TEMPERATURE
00223                 case 'T':
00224                     if(!alertList[ii].active) {
00225                         if(measurements.temperature < alertList[ii].lowThreshold) {
00226                             alertList[ii].active = 1;
00227                             breach = 0x0f;
00228                         } else if(measurements.temperature > alertList[ii].highThreshold) {
00229                             alertList[ii].active = 1;
00230                             breach = 0xf0;
00231                         }
00232                     } else if( measurements.temperature > alertList[ii].lowThreshold
00233                                 && measurements.temperature < alertList[ii].highThreshold ) {
00234                         alertList[ii].active = 0;
00235                         alertReset = 1;        
00236                     }
00237                     break;
00238                 case 'H':
00239                      if(!alertList[ii].active) {
00240                         if(measurements.humidity < alertList[ii].lowThreshold) {
00241                             alertList[ii].active = 1;
00242                             breach = 0x0f;
00243                         } else if(measurements.humidity > alertList[ii].highThreshold) {
00244                             alertList[ii].active = 1;
00245                             breach = 0xf0;
00246                         }
00247                     } else if( measurements.humidity > alertList[ii].lowThreshold
00248                                 && measurements.humidity < alertList[ii].highThreshold ) {
00249                         alertList[ii].active = 0;
00250                         alertReset = 1;        
00251                     }
00252                    break;
00253 #endif
00254 #if USING_LIGHT
00255                 case 'V':
00256                     if(!alertList[ii].active) {
00257                         if(measurements.visible < alertList[ii].lowThreshold) {
00258                             alertList[ii].active = 1;
00259                             breach = 0x0f;
00260                         } else if(measurements.visible > alertList[ii].highThreshold) {
00261                             alertList[ii].active = 1;
00262                             breach = 0xf0;
00263                         }
00264                     } else if( measurements.visible > alertList[ii].lowThreshold
00265                                 && measurements.visible < alertList[ii].highThreshold ) {
00266                         alertList[ii].active = 0;
00267                         alertReset = 1;        
00268                     }
00269                     break;
00270                 case 'I':
00271                      if(!alertList[ii].active) {
00272                         if(measurements.infrared < alertList[ii].lowThreshold) {
00273                             alertList[ii].active = 1;
00274                             breach = 0x0f;
00275                         } else if(measurements.infrared > alertList[ii].highThreshold) {
00276                             alertList[ii].active = 1;
00277                             breach = 0xf0;
00278                         }
00279                     } else if( measurements.infrared > alertList[ii].lowThreshold
00280                                 && measurements.infrared < alertList[ii].highThreshold ) {
00281                         alertList[ii].active = 0;
00282                         alertReset = 1;        
00283                     }
00284                    break;
00285 #endif
00286 #if USING_AIR
00287                 case 'C':
00288                     if(!alertList[ii].active) {
00289                         if(measurements.co < alertList[ii].lowThreshold) {
00290                             alertList[ii].active = 1;
00291                             breach = 0x0f;
00292                         } else if(measurements.co > alertList[ii].highThreshold) {
00293                             alertList[ii].active = 1;
00294                             breach = 0xf0;
00295                         }
00296                     } else if( measurements.co > alertList[ii].lowThreshold
00297                                 && measurements.co < alertList[ii].highThreshold ) {
00298                         alertList[ii].active = 0;
00299                         alertReset = 1;        
00300                     }
00301                     break;
00302                 case 'O':
00303                     if(!alertList[ii].active) {
00304                         if(measurements.tvoc < alertList[ii].lowThreshold) {
00305                             alertList[ii].active = 1;
00306                             breach = 0x0f;
00307                         } else if(measurements.tvoc > alertList[ii].highThreshold) {
00308                             alertList[ii].active = 1;
00309                             breach = 0xf0;
00310                         }
00311                     } else if( measurements.tvoc > alertList[ii].lowThreshold
00312                                 && measurements.tvoc < alertList[ii].highThreshold ) {
00313                         alertList[ii].active = 0;
00314                         alertReset = 1;        
00315                     }
00316                     break;
00317 #endif
00318 #if USING_LASER
00319                 case 'D':
00320                     if(!alertList[ii].active) {
00321                         if(measurements.distance < alertList[ii].lowThreshold) {
00322                             alertList[ii].active = 1;
00323                             breach = 0x0f;
00324                         } else if(measurements.distance > alertList[ii].highThreshold) {
00325                             alertList[ii].active = 1;
00326                             breach = 0xf0;
00327                         }
00328                     } else if( measurements.distance > alertList[ii].lowThreshold
00329                                 && measurements.distance < alertList[ii].highThreshold ) {
00330                         alertList[ii].active = 0;
00331                         alertReset = 1;        
00332                     }
00333                     break;
00334 #endif
00335 #if USING_ACCEL
00336                 case 'X':
00337                     if(!alertList[ii].active) {
00338                         if(measurements.xAcceleration < alertList[ii].lowThreshold) {
00339                             alertList[ii].active = 1;
00340                             breach = 0x0f;
00341                         } else if(measurements.xAcceleration > alertList[ii].highThreshold) {
00342                             alertList[ii].active = 1;
00343                             breach = 0xf0;
00344                         }
00345                     } else if( measurements.xAcceleration > alertList[ii].lowThreshold
00346                                 && measurements.xAcceleration < alertList[ii].highThreshold ) {
00347                         alertList[ii].active = 0;
00348                         alertReset = 1;        
00349                     }
00350                     break;
00351                 case 'Y':
00352                     if(!alertList[ii].active) {
00353                         if(measurements.yAcceleration < alertList[ii].lowThreshold) {
00354                             alertList[ii].active = 1;
00355                             breach = 0x0f;
00356                         } else if(measurements.yAcceleration > alertList[ii].highThreshold) {
00357                             alertList[ii].active = 1;
00358                             breach = 0xf0;
00359                         }
00360                     } else if( measurements.yAcceleration > alertList[ii].lowThreshold
00361                                 && measurements.yAcceleration < alertList[ii].highThreshold ) {
00362                         alertList[ii].active = 0;
00363                         alertReset = 1;        
00364                     }
00365                     break;
00366                 case 'Z':
00367                     if(!alertList[ii].active) {
00368                         if(measurements.zAcceleration < alertList[ii].lowThreshold) {
00369                             alertList[ii].active = 1;
00370                             breach = 0x0f;
00371                         } else if(measurements.zAcceleration > alertList[ii].highThreshold) {
00372                             alertList[ii].active = 1;
00373                             breach = 0xf0;
00374                         }
00375                     } else if( measurements.zAcceleration > alertList[ii].lowThreshold
00376                                 && measurements.zAcceleration < alertList[ii].highThreshold ) {
00377                         alertList[ii].active = 0;
00378                         alertReset = 1;        
00379                     }
00380                     break;
00381 #endif
00382                 default:
00383                     break;
00384             }
00385             if(breach != 0) {
00386                 char msgBuff[5] = {'A', 'S', (char)alertList[ii].id, (char)breach};
00387                 httpPost(msgBuff, 5);
00388             } else if(alertReset) {
00389                 char msgBuff[4] = {'A', 'R', (char)alertList[ii].id};
00390                 httpPost(msgBuff, 4);
00391             }
00392         }
00393     }
00394 
00395 }