Dependencies:   Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed

Committer:
OHstin
Date:
Thu Mar 23 10:59:50 2017 +0000
Revision:
5:366f17f1ea9b
Parent:
4:c7b0670f96b2
Child:
6:196a63a3378d
PerfBoard

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OHstin 3:7666de697752 1 #ifndef RASPISERIAL_H
OHstin 3:7666de697752 2 #define RASPISERIAL_H
OHstin 3:7666de697752 3
OHstin 3:7666de697752 4 #include "MbedJSONValue.h"
OHstin 3:7666de697752 5 #include "MODSERIAL.h"
OHstin 3:7666de697752 6 #include <string>
OHstin 5:366f17f1ea9b 7 MODSERIAL raspi(p28, p27); // tx, rx
OHstin 3:7666de697752 8
OHstin 3:7666de697752 9 // this function determines wherther serial is active or inactive
OHstin 3:7666de697752 10
OHstin 3:7666de697752 11 // this function manages communication between raspberry pi and mbed
OHstin 3:7666de697752 12
OHstin 3:7666de697752 13 void raspiSerial()
OHstin 3:7666de697752 14 {
OHstin 4:c7b0670f96b2 15 //determines whether serial data should started/stopped
OHstin 4:c7b0670f96b2 16 bool serialStatus;
OHstin 4:c7b0670f96b2 17
OHstin 4:c7b0670f96b2 18 // data to track the status of the pi
OHstin 4:c7b0670f96b2 19 bool piStatus = false; // determines the current serial connection
OHstin 4:c7b0670f96b2 20 bool cameraStatus = false; // determines whether the camera is active
OHstin 4:c7b0670f96b2 21 bool cloudStatus = false; // detemines when the pi is connected to the cloud
OHstin 4:c7b0670f96b2 22 bool detectionStatus =false; // determines whether the object detection system is functional
OHstin 4:c7b0670f96b2 23
OHstin 4:c7b0670f96b2 24
OHstin 3:7666de697752 25 // define variables to store sensor data
OHstin 3:7666de697752 26 float b1Voltage = 0.0;
OHstin 3:7666de697752 27 float b1Current = 0.0;
OHstin 3:7666de697752 28 float b2Voltage = 0.0;
OHstin 3:7666de697752 29 float b2Current = 0.0;
OHstin 3:7666de697752 30 float sVoltage = 0.0;
OHstin 3:7666de697752 31 float sCurrent = 0.0;
OHstin 4:c7b0670f96b2 32
OHstin 4:c7b0670f96b2 33 // define string to store rounded off sensor data
OHstin 4:c7b0670f96b2 34 char b1VoltageString[5];
OHstin 4:c7b0670f96b2 35 char b1CurrentString[5];
OHstin 4:c7b0670f96b2 36 char b2VoltageString[5];
OHstin 4:c7b0670f96b2 37 char b2CurrentString[5];
OHstin 4:c7b0670f96b2 38 char sVoltageString[5];
OHstin 4:c7b0670f96b2 39 char sCurrentString[5];
OHstin 4:c7b0670f96b2 40
OHstin 3:7666de697752 41 // get access to the sensor suite
OHstin 3:7666de697752 42 SensorSuite suite;
OHstin 4:c7b0670f96b2 43
OHstin 3:7666de697752 44
OHstin 4:c7b0670f96b2 45 // power up the raspberry pi
OHstin 4:c7b0670f96b2 46
OHstin 4:c7b0670f96b2 47 // give the pi some time to boot
OHstin 4:c7b0670f96b2 48
OHstin 4:c7b0670f96b2 49 // wait for character x
OHstin 4:c7b0670f96b2 50 int counter = 120; // count down to 2 minutes
OHstin 4:c7b0670f96b2 51 char x = NULL;
OHstin 4:c7b0670f96b2 52 while(counter > 0 && x != 'x') {
OHstin 4:c7b0670f96b2 53 x = raspi.getc();
OHstin 4:c7b0670f96b2 54 Thread::wait(1000);
OHstin 4:c7b0670f96b2 55 counter--;
OHstin 4:c7b0670f96b2 56 }
OHstin 3:7666de697752 57
OHstin 4:c7b0670f96b2 58 if (x == 'x') {
OHstin 4:c7b0670f96b2 59 myled = 1;
OHstin 4:c7b0670f96b2 60 piStatus = true;
OHstin 4:c7b0670f96b2 61 // send message that connection was made
OHstin 4:c7b0670f96b2 62 serial_ui_letter *letter2 = serial_ui_mail.alloc();
OHstin 4:c7b0670f96b2 63 letter2->piStat = piStatus;
OHstin 4:c7b0670f96b2 64 letter2->camStat = cameraStatus;
OHstin 4:c7b0670f96b2 65 letter2->cloudStat = cloudStatus;
OHstin 4:c7b0670f96b2 66 letter2->detectionStat = detectionStatus;
OHstin 4:c7b0670f96b2 67 serial_ui_mail.put(letter2);
OHstin 4:c7b0670f96b2 68 }
OHstin 4:c7b0670f96b2 69
OHstin 4:c7b0670f96b2 70
OHstin 3:7666de697752 71
OHstin 3:7666de697752 72 // open the serial port
OHstin 3:7666de697752 73
OHstin 3:7666de697752 74 // start with an infinite loop
OHstin 3:7666de697752 75 while(true) {
OHstin 4:c7b0670f96b2 76
OHstin 4:c7b0670f96b2 77 /////////////////// Check for any new messages /////////////////////////
OHstin 4:c7b0670f96b2 78
OHstin 4:c7b0670f96b2 79
OHstin 4:c7b0670f96b2 80 osEvent evt = ui_serial_mail.get(0);
OHstin 4:c7b0670f96b2 81 if(evt.status == osEventMail) {
OHstin 4:c7b0670f96b2 82 ui_serial_letter *letter = (ui_serial_letter*)evt.value.p;
OHstin 4:c7b0670f96b2 83 // update the serial status
OHstin 4:c7b0670f96b2 84 serialStatus = letter->activateSerial;
OHstin 4:c7b0670f96b2 85 // delete the message
OHstin 4:c7b0670f96b2 86 ui_serial_mail.free(letter);
OHstin 4:c7b0670f96b2 87 }
OHstin 4:c7b0670f96b2 88
OHstin 4:c7b0670f96b2 89 if(!serialStatus)
OHstin 4:c7b0670f96b2 90 break; // means that the user requested the rpi to be turned off
OHstin 4:c7b0670f96b2 91
OHstin 4:c7b0670f96b2 92 ////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 93
OHstin 3:7666de697752 94 ///////////////// read data from the sensor suite //////////////////////
OHstin 4:c7b0670f96b2 95
OHstin 3:7666de697752 96 // create model to get battery data
OHstin 3:7666de697752 97 BatteryModel bModel = suite.getBatteryData();
OHstin 3:7666de697752 98 // read battery data
OHstin 3:7666de697752 99 b1Voltage = bModel.batteryOneVoltage;
OHstin 3:7666de697752 100 b1Current = bModel.batteryOneCurrent;
OHstin 3:7666de697752 101 b2Voltage = bModel.batteryTwoVoltage;
OHstin 3:7666de697752 102 b2Current = bModel.batteryTwoCurrent;
OHstin 3:7666de697752 103
OHstin 3:7666de697752 104 // create model to get solar data
OHstin 3:7666de697752 105 SolarModel sModel = suite.getSolarData();
OHstin 3:7666de697752 106 // read solar panel data
OHstin 3:7666de697752 107 sVoltage = sModel.solarVoltage;
OHstin 3:7666de697752 108 sCurrent = sModel.solarCurrent;
OHstin 4:c7b0670f96b2 109
OHstin 3:7666de697752 110 ////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 111
OHstin 3:7666de697752 112 /////////////////////// package data into json string //////////////////
OHstin 4:c7b0670f96b2 113
OHstin 3:7666de697752 114 // create JSON object
OHstin 3:7666de697752 115 MbedJSONValue holder;
OHstin 3:7666de697752 116 // create string to store data
OHstin 3:7666de697752 117 std::string jsonString;
OHstin 3:7666de697752 118
OHstin 4:c7b0670f96b2 119 // round off sensor data to 2 decimal places
OHstin 4:c7b0670f96b2 120 sprintf(b1VoltageString,"%0.2f",b1Voltage);
OHstin 4:c7b0670f96b2 121 sprintf(b1CurrentString,"%0.2f",b1Current);
OHstin 4:c7b0670f96b2 122 sprintf(b2VoltageString,"%0.2f",b2Voltage);
OHstin 4:c7b0670f96b2 123 sprintf(b2CurrentString,"%0.2f",b2Current);
OHstin 4:c7b0670f96b2 124 sprintf(sVoltageString,"%0.2f",sVoltage);
OHstin 4:c7b0670f96b2 125 sprintf(sCurrentString,"%0.2f",sCurrent);
OHstin 4:c7b0670f96b2 126
OHstin 3:7666de697752 127 // construct json data
OHstin 4:c7b0670f96b2 128 holder["b1V"] = b1VoltageString;
OHstin 4:c7b0670f96b2 129 holder["b1C"] = b1CurrentString;
OHstin 4:c7b0670f96b2 130 holder["b2V"] = b2VoltageString;
OHstin 4:c7b0670f96b2 131 holder["b2C"] = b2CurrentString;
OHstin 4:c7b0670f96b2 132 holder["sV"] = sVoltageString;
OHstin 4:c7b0670f96b2 133 holder["sC"] = sCurrentString;
OHstin 3:7666de697752 134
OHstin 3:7666de697752 135 // convert json data to string
OHstin 3:7666de697752 136 jsonString = holder.serialize();
OHstin 3:7666de697752 137 ////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 138
OHstin 3:7666de697752 139 //////////////////////// send data to raspberry pi /////////////////////
OHstin 4:c7b0670f96b2 140
OHstin 3:7666de697752 141 // write data onto the serial port
OHstin 3:7666de697752 142 raspi.printf("%s\n", jsonString.c_str());
OHstin 4:c7b0670f96b2 143
OHstin 4:c7b0670f96b2 144 ////////////////////////////////////////////////////////////////////////
OHstin 4:c7b0670f96b2 145 /**
OHstin 4:c7b0670f96b2 146 ////////// receive confirmation from the raspberry pi///////////////////
OHstin 4:c7b0670f96b2 147
OHstin 4:c7b0670f96b2 148 // create json object
OHstin 4:c7b0670f96b2 149 MbedJSONValue demo;
OHstin 4:c7b0670f96b2 150
OHstin 4:c7b0670f96b2 151 // variables to read data
OHstin 4:c7b0670f96b2 152 int i = 0;
OHstin 4:c7b0670f96b2 153 bool completed = false;
OHstin 3:7666de697752 154
OHstin 4:c7b0670f96b2 155 char rxString[80]; // buffer that stores received string
OHstin 4:c7b0670f96b2 156
OHstin 4:c7b0670f96b2 157
OHstin 4:c7b0670f96b2 158 // read the first character
OHstin 4:c7b0670f96b2 159 char receivedChar = raspi.getc();
OHstin 4:c7b0670f96b2 160 while (!completed) {
OHstin 4:c7b0670f96b2 161 // Check if it's a new line character
OHstin 4:c7b0670f96b2 162 if (receivedChar != '\n') {
OHstin 4:c7b0670f96b2 163 // if not store in buffer
OHstin 4:c7b0670f96b2 164 rxString[i] = receivedChar;
OHstin 4:c7b0670f96b2 165 i++;
OHstin 4:c7b0670f96b2 166 // read the next character
OHstin 4:c7b0670f96b2 167 receivedChar = raspi.getc();
OHstin 4:c7b0670f96b2 168 } else {
OHstin 4:c7b0670f96b2 169 // the character was a newline character
OHstin 4:c7b0670f96b2 170 completed = true;
OHstin 4:c7b0670f96b2 171 }
OHstin 4:c7b0670f96b2 172 }
OHstin 4:c7b0670f96b2 173
OHstin 4:c7b0670f96b2 174
OHstin 4:c7b0670f96b2 175
OHstin 4:c7b0670f96b2 176 //convert the buffer data into a json string
OHstin 4:c7b0670f96b2 177 const char *json = rxString;
OHstin 4:c7b0670f96b2 178
OHstin 4:c7b0670f96b2 179 // parse the json string
OHstin 4:c7b0670f96b2 180 parse(demo, json);
OHstin 4:c7b0670f96b2 181
OHstin 4:c7b0670f96b2 182 // retrieve the desired data
OHstin 4:c7b0670f96b2 183 cameraStatus = demo["pS"].get<bool>();
OHstin 4:c7b0670f96b2 184 cloudStatus = demo["caS"].get<bool>();
OHstin 4:c7b0670f96b2 185 detectionStatus = demo["clS"].get<bool>();
OHstin 4:c7b0670f96b2 186
OHstin 4:c7b0670f96b2 187 // empty the buffer here
OHstin 4:c7b0670f96b2 188 raspi.rxBufferFlush();
OHstin 4:c7b0670f96b2 189
OHstin 3:7666de697752 190 ////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 191
OHstin 4:c7b0670f96b2 192 /////////////////// send the data to the ui thread //////////////////////
OHstin 4:c7b0670f96b2 193
OHstin 4:c7b0670f96b2 194 serial_ui_letter *letter2 = serial_ui_mail.alloc();
OHstin 4:c7b0670f96b2 195 letter2->piStat = piStatus;
OHstin 4:c7b0670f96b2 196 letter2->camStat = cameraStatus;
OHstin 4:c7b0670f96b2 197 letter2->cloudStat = cloudStatus;
OHstin 4:c7b0670f96b2 198 letter2->detectionStat = detectionStatus;
OHstin 4:c7b0670f96b2 199 serial_ui_mail.put(letter2);
OHstin 4:c7b0670f96b2 200
OHstin 4:c7b0670f96b2 201 /////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 202
OHstin 4:c7b0670f96b2 203 **/
OHstin 3:7666de697752 204
OHstin 3:7666de697752 205 /////////// wait a certain amount of time before proceeding/////////////
OHstin 4:c7b0670f96b2 206
OHstin 3:7666de697752 207 //**** remember to convert floats to characters to save space on buffer***
OHstin 4:c7b0670f96b2 208 Thread::wait(2000); // one second
OHstin 3:7666de697752 209 }
OHstin 4:c7b0670f96b2 210
OHstin 4:c7b0670f96b2 211 ////////////////// sequence that shuts down the raspberry pi ///////////////
OHstin 4:c7b0670f96b2 212
OHstin 4:c7b0670f96b2 213 raspi.rxBufferFlush(); // empty the receive buffer
OHstin 4:c7b0670f96b2 214
OHstin 4:c7b0670f96b2 215 counter = 120; // reset the counter to 2 minutes
OHstin 4:c7b0670f96b2 216 char p = NULL;
OHstin 4:c7b0670f96b2 217 raspi.putc('x');
OHstin 4:c7b0670f96b2 218 raspi.putc('\n'); // new line character
OHstin 4:c7b0670f96b2 219 while(p != 'x'&& counter != 0) {
OHstin 4:c7b0670f96b2 220 raspi.putc('x');
OHstin 4:c7b0670f96b2 221 raspi.putc('\n'); // new line character
OHstin 4:c7b0670f96b2 222 Thread::wait(1000);
OHstin 4:c7b0670f96b2 223 p = raspi.getc();
OHstin 4:c7b0670f96b2 224 Thread::wait(1000);
OHstin 4:c7b0670f96b2 225 counter--;
OHstin 4:c7b0670f96b2 226 }
OHstin 4:c7b0670f96b2 227
OHstin 4:c7b0670f96b2 228 if (p == 'x') {
OHstin 4:c7b0670f96b2 229 piStatus = false;
OHstin 4:c7b0670f96b2 230 // send message that connection was made
OHstin 4:c7b0670f96b2 231 serial_ui_letter *letter2 = serial_ui_mail.alloc();
OHstin 4:c7b0670f96b2 232 letter2->piStat = piStatus;
OHstin 4:c7b0670f96b2 233 letter2->camStat = cameraStatus;
OHstin 4:c7b0670f96b2 234 letter2->cloudStat = cloudStatus;
OHstin 4:c7b0670f96b2 235 letter2->detectionStat = detectionStatus;
OHstin 4:c7b0670f96b2 236 serial_ui_mail.put(letter2);
OHstin 4:c7b0670f96b2 237 }
OHstin 4:c7b0670f96b2 238
OHstin 4:c7b0670f96b2 239 // remember to give the pi some time to shut down
OHstin 4:c7b0670f96b2 240
OHstin 4:c7b0670f96b2 241 ////////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 242 }
OHstin 3:7666de697752 243
OHstin 3:7666de697752 244 #endif