Dependencies:   Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed

Committer:
OHstin
Date:
Sat Feb 04 20:17:44 2017 +0000
Revision:
3:7666de697752
Child:
4:c7b0670f96b2
This version before adding serial communication and pathways

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 3:7666de697752 7 MODSERIAL raspi(p9, p10); // 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 3:7666de697752 15 // define variables to store sensor data
OHstin 3:7666de697752 16 float b1Voltage = 0.0;
OHstin 3:7666de697752 17 float b1Current = 0.0;
OHstin 3:7666de697752 18 float b2Voltage = 0.0;
OHstin 3:7666de697752 19 float b2Current = 0.0;
OHstin 3:7666de697752 20 float sVoltage = 0.0;
OHstin 3:7666de697752 21 float sCurrent = 0.0;
OHstin 3:7666de697752 22 // get access to the sensor suite
OHstin 3:7666de697752 23 SensorSuite suite;
OHstin 3:7666de697752 24
OHstin 3:7666de697752 25 // show message that raspberry pi is booting
OHstin 3:7666de697752 26
OHstin 3:7666de697752 27 // show message that raspberr pi is launching script
OHstin 3:7666de697752 28
OHstin 3:7666de697752 29 // open the serial port
OHstin 3:7666de697752 30
OHstin 3:7666de697752 31 // start with an infinite loop
OHstin 3:7666de697752 32 while(true) {
OHstin 3:7666de697752 33 // check if there are any messages to terminate
OHstin 3:7666de697752 34
OHstin 3:7666de697752 35 ///////////////// read data from the sensor suite //////////////////////
OHstin 3:7666de697752 36
OHstin 3:7666de697752 37 // create model to get battery data
OHstin 3:7666de697752 38 BatteryModel bModel = suite.getBatteryData();
OHstin 3:7666de697752 39 // read battery data
OHstin 3:7666de697752 40 b1Voltage = bModel.batteryOneVoltage;
OHstin 3:7666de697752 41 b1Current = bModel.batteryOneCurrent;
OHstin 3:7666de697752 42 b2Voltage = bModel.batteryTwoVoltage;
OHstin 3:7666de697752 43 b2Current = bModel.batteryTwoCurrent;
OHstin 3:7666de697752 44
OHstin 3:7666de697752 45 // create model to get solar data
OHstin 3:7666de697752 46 SolarModel sModel = suite.getSolarData();
OHstin 3:7666de697752 47 // read solar panel data
OHstin 3:7666de697752 48 sVoltage = sModel.solarVoltage;
OHstin 3:7666de697752 49 sCurrent = sModel.solarCurrent;
OHstin 3:7666de697752 50
OHstin 3:7666de697752 51 ////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 52
OHstin 3:7666de697752 53 /////////////////////// package data into json string //////////////////
OHstin 3:7666de697752 54
OHstin 3:7666de697752 55 // create JSON object
OHstin 3:7666de697752 56 MbedJSONValue holder;
OHstin 3:7666de697752 57 // create string to store data
OHstin 3:7666de697752 58 std::string jsonString;
OHstin 3:7666de697752 59
OHstin 3:7666de697752 60 // construct json data
OHstin 3:7666de697752 61 holder["b1V"] = static_cast<double>(b1Voltage);
OHstin 3:7666de697752 62 holder["b1C"] = static_cast<double>(b1Current);
OHstin 3:7666de697752 63 holder["b2V"] = static_cast<double>(b2Voltage);
OHstin 3:7666de697752 64 holder["b2C"] = static_cast<double>(b2Current);
OHstin 3:7666de697752 65 holder["sV"] = static_cast<double>(sVoltage);
OHstin 3:7666de697752 66 holder["sC"] = static_cast<double>(sCurrent);
OHstin 3:7666de697752 67
OHstin 3:7666de697752 68 // convert json data to string
OHstin 3:7666de697752 69 jsonString = holder.serialize();
OHstin 3:7666de697752 70 ////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 71
OHstin 3:7666de697752 72 //////////////////////// send data to raspberry pi /////////////////////
OHstin 3:7666de697752 73
OHstin 3:7666de697752 74 // write data onto the serial port
OHstin 3:7666de697752 75 raspi.printf("%s\n", jsonString.c_str());
OHstin 3:7666de697752 76
OHstin 3:7666de697752 77 ////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 78
OHstin 3:7666de697752 79 ////////// receive confirmation from the raspberry pi///////////////////
OHstin 3:7666de697752 80
OHstin 3:7666de697752 81 ////////////////////////////////////////////////////////////////////////
OHstin 3:7666de697752 82
OHstin 3:7666de697752 83 /////////// wait a certain amount of time before proceeding/////////////
OHstin 3:7666de697752 84
OHstin 3:7666de697752 85 //**** remember to convert floats to characters to save space on buffer***
OHstin 3:7666de697752 86 Thread::wait(2000); // one second
OHstin 3:7666de697752 87 }
OHstin 3:7666de697752 88 }
OHstin 3:7666de697752 89
OHstin 3:7666de697752 90 #endif