Augustine Kizito / Mbed 2 deprecated Solar_Powered_Smart_Camera

Dependencies:   Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RaspiSerial.h Source File

RaspiSerial.h

00001 #ifndef RASPISERIAL_H
00002 #define RASPISERIAL_H
00003 #include "MbedJSONValue.h"
00004 #include "MODSERIAL.h"
00005 #include <string>
00006 MODSERIAL raspi(p28, p27); // tx, rx
00007 DigitalOut piPin(p7);
00008 
00009 // this function determines wherther serial is active or inactive
00010 
00011 // this function manages communication between raspberry pi and mbed
00012 
00013 void raspiSerial()
00014 {
00015     //determines whether serial data should started/stopped
00016     bool serialStatus;
00017 
00018     // data to track the status of the pi
00019     bool piStatus = false;              // determines the current serial connection
00020     bool cameraStatus = false;         // determines whether the camera is active
00021     bool cloudStatus = false;         // detemines when the pi is connected to the cloud
00022     bool detectionStatus =false;     // determines whether the object detection system is functional
00023 
00024 
00025     // define variables to store sensor data
00026     float b1Voltage = 0.0;
00027     float b1Current = 0.0;
00028     float b2Voltage = 0.0;
00029     float b2Current = 0.0;
00030     float sVoltage = 0.0;
00031     float sCurrent = 0.0;
00032     float oVoltage = 0.0;
00033     float oCurrent = 0.0;
00034 
00035     // define string to store rounded off sensor data
00036     char b1VoltageString[5];
00037     char b1CurrentString[5];
00038     char b2VoltageString[5];
00039     char b2CurrentString[5];
00040     char sVoltageString[5];
00041     char sCurrentString[5];
00042     char oVoltageString[5];
00043     char oCurrentString[5];
00044 
00045     // get access to the sensor suite
00046     SensorSuite suite;
00047  
00048 
00049     // power up the raspberry pi
00050     piPin = 1;
00051 
00052     // give the pi some time to boot
00053     //Thread::wait(1000*10);
00054     
00055     // wait for character x
00056     int counter = 180;  // count down to 3 minutes
00057     char x = NULL;
00058     while(counter > 0 && x != 'x') {
00059         x = raspi.getc();
00060         Thread::wait(1000);
00061         counter--;
00062     }
00063 
00064     if (x == 'x') {
00065         myled = 1;
00066         piStatus = true;
00067         // send message that connection was made
00068         serial_ui_letter *letter2 = serial_ui_mail.alloc();
00069         letter2->piStat = piStatus;
00070         letter2->camStat = cameraStatus;
00071         letter2->cloudStat = cloudStatus;
00072         letter2->detectionStat = detectionStatus;
00073         serial_ui_mail.put(letter2);
00074     }
00075 
00076 
00077 
00078     // open the serial port
00079 
00080     // start with an infinite loop
00081     while(true) {
00082 
00083         /////////////////// Check for any new messages /////////////////////////
00084 
00085 
00086         osEvent evt = ui_serial_mail.get(0);
00087         if(evt.status == osEventMail) {
00088             ui_serial_letter *letter = (ui_serial_letter*)evt.value.p;
00089             // update the serial status
00090             serialStatus = letter->activateSerial;
00091             // delete the message
00092             ui_serial_mail.free(letter);
00093         }
00094 
00095         if(!serialStatus)
00096             break;          // means that the user requested the rpi to be turned off
00097 
00098         ////////////////////////////////////////////////////////////////////////
00099 
00100         ///////////////// read data from the sensor suite //////////////////////
00101 
00102         // create model to get battery data
00103         BatteryModel bModel = suite.getBatteryData();
00104         // read battery data
00105         b1Voltage = bModel.batteryOneVoltage;
00106         b1Current = bModel.batteryOneCurrent;
00107         b2Voltage = bModel.batteryTwoVoltage;
00108         b2Current = bModel.batteryTwoCurrent;
00109 
00110         // create model to get solar data
00111         SolarModel sModel = suite.getSolarData();
00112         // read solar panel data
00113         sVoltage = sModel.solarVoltage;
00114         sCurrent = sModel.solarCurrent;
00115         
00116         
00117         
00118         // create model to get consumption data
00119         ConsumptionModel oModel = suite.getConsumptionData();
00120         // read the consumption data
00121         oVoltage = oModel.consumptionVoltage;
00122         oCurrent = oModel.consumptionCurrent;
00123         
00124         
00125 
00126         ////////////////////////////////////////////////////////////////////////
00127 
00128         /////////////////////// package data into json string //////////////////
00129 
00130         // create JSON object
00131         MbedJSONValue holder;
00132         // create string to store data
00133         std::string jsonString;
00134 
00135         // round off sensor data to 2 decimal places
00136         sprintf(b1VoltageString,"%0.2f",b1Voltage);
00137         sprintf(b1CurrentString,"%0.2f",b1Current);
00138         sprintf(b2VoltageString,"%0.2f",b2Voltage);
00139         sprintf(b2CurrentString,"%0.2f",b2Current);
00140         sprintf(sVoltageString,"%0.2f",sVoltage);
00141         sprintf(sCurrentString,"%0.2f",sCurrent);
00142         sprintf(oVoltageString,"%0.2f",oVoltage);
00143         sprintf(oCurrentString,"%0.2f",oCurrent);
00144 
00145         // construct json data
00146         holder["b1V"] = b1VoltageString;
00147         holder["b1C"] = b1CurrentString;
00148         holder["b2V"] = b2VoltageString;
00149         holder["b2C"] = b2CurrentString;
00150         holder["sV"]  = sVoltageString;
00151         holder["sC"]  = sCurrentString;
00152         holder["oV"]  = oVoltageString;
00153         holder["oC"]  = oCurrentString;
00154 
00155         // convert json data to string
00156         jsonString = holder.serialize();
00157         ////////////////////////////////////////////////////////////////////////
00158 
00159         //////////////////////// send data to raspberry pi /////////////////////
00160 
00161         // write data onto the serial port
00162         raspi.printf("%s\n", jsonString.c_str());
00163 
00164         ////////////////////////////////////////////////////////////////////////
00165         /**
00166         ////////// receive confirmation from the raspberry pi///////////////////
00167 
00168         // create json object
00169         MbedJSONValue demo;
00170 
00171         // variables to read data
00172         int i = 0;
00173         bool completed = false;
00174         
00175         char rxString[80]; // buffer that stores received string
00176 
00177 
00178         // read the first character
00179         char receivedChar = raspi.getc();
00180         while (!completed) {
00181             // Check if it's a new line character
00182             if (receivedChar != '\n') {
00183                 // if not store in buffer
00184                 rxString[i] = receivedChar;
00185                 i++;
00186                 // read the next character
00187                 receivedChar = raspi.getc();
00188             } else {
00189                 // the character was a newline character
00190                 completed = true;
00191             }
00192         }
00193 
00194 
00195 
00196         //convert the buffer data into a json string
00197         const char *json = rxString;
00198 
00199         // parse the json string
00200         parse(demo, json);
00201 
00202         // retrieve the desired data
00203         cameraStatus = demo["pS"].get<bool>();
00204         cloudStatus = demo["caS"].get<bool>();
00205         detectionStatus = demo["clS"].get<bool>();
00206 
00207         // empty the buffer here
00208         raspi.rxBufferFlush();
00209 
00210         ////////////////////////////////////////////////////////////////////////
00211 
00212         /////////////////// send the data to the ui thread //////////////////////
00213 
00214         serial_ui_letter *letter2 = serial_ui_mail.alloc();
00215         letter2->piStat = piStatus;
00216         letter2->camStat = cameraStatus;
00217         letter2->cloudStat = cloudStatus;
00218         letter2->detectionStat = detectionStatus;
00219         serial_ui_mail.put(letter2);
00220 
00221         /////////////////////////////////////////////////////////////////////////
00222         
00223         **/
00224 
00225         /////////// wait a certain amount of time before proceeding/////////////
00226 
00227         //**** remember  to convert floats to characters to save space on buffer***
00228         Thread::wait(2000); // one second
00229     }
00230 
00231     ////////////////// sequence that shuts down the raspberry pi ///////////////
00232     
00233     raspi.rxBufferFlush();  // empty the receive buffer
00234 
00235     counter = 120; // reset the counter to 2 minutes
00236     char p = NULL;
00237     raspi.putc('x');
00238     raspi.putc('\n'); // new line character
00239     while(p != 'x'&& counter != 0) {
00240         raspi.putc('x');
00241         raspi.putc('\n'); // new line character
00242         Thread::wait(1000);
00243         p = raspi.getc();
00244         Thread::wait(1000);
00245         counter--;
00246     }
00247     
00248     if (p == 'x') {
00249         Thread::wait(1000*12);// wait 12 seconds
00250         piPin = 0;
00251         myled = 0;
00252         piStatus = false;
00253         // send message that connection was made
00254         serial_ui_letter *letter2 = serial_ui_mail.alloc();
00255         letter2->piStat = piStatus;
00256         letter2->camStat = cameraStatus;
00257         letter2->cloudStat = cloudStatus;
00258         letter2->detectionStat = detectionStatus;
00259         serial_ui_mail.put(letter2);
00260     }
00261 
00262     // remember to give the pi some time to shut down
00263 
00264     ////////////////////////////////////////////////////////////////////////////
00265 }
00266 
00267 #endif