![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Program to control UPAS with MicroChip BLE chip + iPhone App
Dependencies: ADS1115 BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 MicroBLE NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed
Diff: main.cpp
- Revision:
- 2:88fcbfadec6a
- Parent:
- 1:9fbb5b665068
- Child:
- 3:122bfc998c4c
--- a/main.cpp Thu Jan 28 23:05:25 2016 +0000 +++ b/main.cpp Wed Feb 03 01:29:18 2016 +0000 @@ -1,29 +1,39 @@ #include "mbed.h" - +#include "SDFileSystem.h" +#include "Adafruit_ADS1015.h" +#include "MCP40D17.h" +#include "STC3100.h" +#include "LSM303.h" +#include "BME280.h" +#include "SI1145.h" +#include "NCP5623BMUTBG.h" +#include "CronoDot.h" +#include "EEPROM.h" +#include "Calibration.h" Serial pc(USBTX, USBRX); -//Serial microChannel(PA_9, PA_10); // tx, rx Serial microChannel(D8, D2); // tx, rx Timer t; struct tm tt; -// manual set RTC values -int minute =00; // 0-59 -int hour =21; // 2-23 -int dayofmonth =6; // 1-31 -int month =1; // 1-12 -int year =16; // last 2 digits - -int count,ended,timeout; -char timebuf[30]; -char buf[2024]; -char snd[1024]; - -char ssid[32] = "AdHoc"; // enter WiFi router ssid inside the quotes -char pwd [32] = "password"; // enter WiFi router password inside the quotes -void gettime(),setRTC(); -void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(), YoYo(); +I2C i2c(D14, D15); +Adafruit_ADS1115 ads(&i2c); +MCP40D17 DigPot(&i2c); +BME280 bmesensor(D14, D15); +STC3100 gasG(D14, D15); +DigitalOut blower(D9, 0); +DigitalOut pbKill(D7, 1); +LSM303 movementsensor(D14, D15); +SI1145 lightsensor(D14, D15); +NCP5623BMUTBG RGB_LED(D14, D15); +//CronoDot RTC(D14, D15); +EEPROM E2PROM(D14, D15); +//DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications +Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option - +void sendData(); + +int timeout = 2; + void pc_recv(void){ while(pc.readable()){ pc.getc(); @@ -34,38 +44,119 @@ static uint8_t rx_len=0; static int haltBLE = 1; static int transmissionValue = 0; +uint8_t writeData[20] = {0,}; +static uint8_t dataLength = 0; void uartMicro(void){ - haltBLE = 2; - + haltBLE = 2; + //if(transmissionValue==4)pc.printf("DEBUG"); while(microChannel.readable()){ rx_buf[rx_len++] = microChannel.getc(); - if(rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\0'){ - wait(0.1); - haltBLE = 1; - break; + + //Code block to verify what is being transmitted. To function correctly, all data must terminate with \0 or \n + if(transmissionValue==0){ + + if (rx_buf[0] == 0x01)transmissionValue = 1; //rtc + else if(rx_buf[0] == 0x02)transmissionValue = 2; //sample start and end times + else if(rx_buf[0] == 0x03)transmissionValue = 3; //sample name + else if(rx_buf[0] == 0x04)transmissionValue = 4; //Run Check + + else if(rx_buf[0] == 0x05)transmissionValue = 5; //log interval + else if(rx_buf[0] == 0x06)transmissionValue = 6; //Flow Rate + else if(rx_buf[0] == 0x07)transmissionValue = 7; //Serial Number + else transmissionValue = 100; //Not useful data + } + + if(rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n' || rx_buf[rx_len-1] == 0xff){ + if((transmissionValue == 1 || transmissionValue == 2 || transmissionValue == 3 || transmissionValue == 4 || transmissionValue == 5 || + transmissionValue == 6 || transmissionValue == 7) && rx_buf[rx_len-1] != 0xff) + {}else{ + if(transmissionValue == 4)sendData(); + haltBLE = 1; + transmissionValue = 0; + dataLength = 0; + } } } if(haltBLE!=1){ - for(int i=0; i<rx_len; i++){ - pc.putc(rx_buf[0]); + + if((transmissionValue!=100) && (dataLength!= 0)) writeData[dataLength-1] = rx_buf[0]; + + if(transmissionValue ==100){ + pc.putc(rx_buf[0]); + + }else if(transmissionValue ==1){ //process and store RTC values + + // if(dataLength==6)RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]); + + }else if(transmissionValue ==2){ //process and store sample start/end + if(dataLength ==12)E2PROM.write(0x00015, writeData, 12); + + }else if(transmissionValue ==3){ //process and store sample name + if(dataLength ==8)E2PROM.write(0x00001,writeData,8); + }else if(transmissionValue ==4){ //process and store Run Check + //if(dataLength==1)E2PROM.write(0x00033,writeData,1); + + + + }else if(transmissionValue ==5){ //process and store Log Interval + if(dataLength ==1)E2PROM.write(0x00014,writeData,1); + + }else if(transmissionValue ==6){ //process and store Flow Rate + if(dataLength ==4)E2PROM.write(0x00010,writeData,4); + + }else if(transmissionValue ==7){ //process and store Serial Number + if(dataLength ==2)E2PROM.write(0x00034,writeData,2); } + dataLength++; } + // rx_len = 0; + +} +void sendData(){ + + uint8_t sampleTimePassValues[13] = {0x01,}; + uint8_t subjectLabelOriginal[9] = {0x02,}; + uint8_t dataLogOriginal[2] = {0x03,}; + uint8_t flowRateOriginal[5] = {0x04,}; + //uint8_t presetRunModeCheck[1] = {0,}; Commented and currently unused to prevent mem issues + E2PROM.read(0x00015, sampleTimePassValues+1, 12); + E2PROM.read(0x00001, subjectLabelOriginal+1,8); + E2PROM.read(0x00014,dataLogOriginal+1,1); + E2PROM.read(0x00010,flowRateOriginal+1,4); + + for(int i=0; i<13; i++){ + microChannel.putc(sampleTimePassValues[i]); + } + wait(.25); + + for(int i=0; i<9; i++){ + microChannel.putc(subjectLabelOriginal[i]); + } + wait(.25); + + for(int i=0; i<2; i++){ + microChannel.putc(dataLogOriginal[i]); + } + wait(.25); + + for(int i=0; i<5; i++){ + microChannel.putc(flowRateOriginal[i]); + } + //pc.printf("End of data"); -} - + +} int main(){ - - // reset=0; //hardware reset for 8266 + pc.baud(115200); // set what you want here depending on your terminal program speed pc.printf("\f\n\r-------------Startup-------------\n\r"); wait(0.5); - //reset=1; timeout=2; - + pc.attach(pc_recv); microChannel.attach(uartMicro,microChannel.RxIrq); microChannel.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. @@ -73,20 +164,18 @@ //uint8_t tempBuf[20] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13}; microChannel.printf("$$$"); wait(1); - microChannel.printf("SN, JakeMicro\r"); + microChannel.printf("SN, JakeMicroX\r"); wait(1); microChannel.printf("A\r"); wait(1); - microChannel.printf("---"); - wait(1); - microChannel.printf("Here is 21 character\n"); - // continuosly get AP list and IP + microChannel.printf("---\r"); + wait(2); + + + + + RGB_LED.set_led(1,1,1); while(1) { - wait(25); - uint8_t zero[1] = {0,}; - microChannel.printf("Here is 20 characr\n"); - microChannel.putc(zero[0]); - //pc.printf("Alive\n"); - //microChannel.printf("ALIVE"); + for(int i=0;i<10000000;i++); } }