draft2
Dependencies: ARCH_GPRS_V2_HW Blinker GPRSInterface HTTPClient_GPRS RTC_WorkingLibrary SDFileSystem USBDevice mbed
Revision 0:77d82c39b97c, committed 2015-05-05
- Comitter:
- mbotkinl
- Date:
- Tue May 05 19:07:02 2015 +0000
- Commit message:
- second draft of code
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ARCH_GPRS_V2_HW.lib Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/labishrestha/code/ARCH_GPRS_V2_HW/#d3d48514a2aa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Blinker.lib Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/Blinker/#ea5bb72717cc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT/DHT.cpp Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,251 @@ +/* + * DHT Library for Digital-output Humidity and Temperature sensors + * + * Works with DHT11, DHT22 + * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio) + * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio) + * AM2302 , temperature-humidity sensor + * HM2303 , Digital-output humidity and temperature sensor + * + * Copyright (C) Wim De Roeve + * based on DHT22 sensor library by HO WING KIT + * Arduino DHT11 library + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documnetation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "DHT.h" +#define DHT_DATA_BIT_COUNT 41 +DigitalOut myled1(LED2); + + +DHT::DHT(PinName pin,int DHTtype) { + _pin = pin; + _DHTtype = DHTtype; + _firsttime=true; +} + +DHT::~DHT() { +} + +int DHT::readData() { + int i, j, retryCount,b; + unsigned int bitTimes[DHT_DATA_BIT_COUNT]; + + + //myled1=0; + eError err = ERROR_NONE; + time_t currentTime = time(NULL); + + DigitalInOut DHT_io(_pin); + + for (i = 0; i < DHT_DATA_BIT_COUNT; i++) { + bitTimes[i] = 0; + } + +// if (!_firsttime) { +// if (int(currentTime - _lastReadTime) < 2) { +// err = ERROR_NO_PATIENCE; +// return err; +// } +// } else { +// _firsttime=false; +// _lastReadTime=currentTime; +// } + retryCount = 0; + + do { + if (retryCount > 125) { + err = BUS_BUSY; + return err; + } + retryCount ++; + wait_us(2); + } while ((DHT_io==0)); + + + DHT_io.output(); + DHT_io = 0; + + + +// wait(.1); +// DHT_io=1; +// wait(.1); +// DHT_io=0; +// return 0; + + + + + + + + wait_ms(18); + DHT_io = 1; + wait_us(40); + DHT_io.input(); + + retryCount = 0; + do { + if (retryCount > 40) { + err = ERROR_NOT_PRESENT; + return err; + } + retryCount++; + wait_us(1); + } while ((DHT_io==1)); + + if (err != ERROR_NONE) { + return err; + } + + wait_us(80); + + for (i = 0; i < 5; i++) { + for (j = 0; j < 8; j++) { + + retryCount = 0; + do { + if (retryCount > 75) { + err = ERROR_DATA_TIMEOUT; + return err; + } + retryCount++; + wait_us(1); + } while (DHT_io == 0); + wait_us(40); + bitTimes[i*8+j]=DHT_io; + + int count = 0; + while (DHT_io == 1 && count < 100) { + wait_us(1); + count++; + } + } + } + DHT_io.output(); + DHT_io = 1; + for (i = 0; i < 5; i++) { + b=0; + for (j=0; j<8; j++) { + if (bitTimes[i*8+j+1] > 0) { + b |= ( 1 << (7-j)); + } + } + DHT_data[i]=b; + } + + //if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { + _lastReadTime = currentTime; + _lastTemperature=CalcTemperature(); + _lastHumidity=CalcHumidity(); + + //} else { + // err = ERROR_CHECKSUM; + //} + + return err; + +} + +float DHT::CalcTemperature() { + int v; + + switch (_DHTtype) { + case DHT11: + v = DHT_data[2]; + return float(v); + case DHT22: + v = DHT_data[2] & 0x7F; + v *= 256; + v += DHT_data[3]; + v /= 10; + if (DHT_data[2] & 0x80) + v *= -1; + return float(v); + } + return 0; +} + +float DHT::ReadHumidity() { + return _lastHumidity; +} + +float DHT::ConvertCelciustoFarenheit(float celsius) { + return celsius * 9 / 5 + 32; +} + +float DHT::ConvertCelciustoKelvin(float celsius) { + return celsius + 273.15; +} + +// dewPoint function NOAA +// reference: http://wahiduddin.net/calc/density_algorithms.htm +float DHT::CalcdewPoint(float celsius, float humidity) { + float A0= 373.15/(273.15 + celsius); + float SUM = -7.90298 * (A0-1); + SUM += 5.02808 * log10(A0); + SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; + SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; + SUM += log10(1013.246); + float VP = pow(10, SUM-3) * humidity; + float T = log(VP/0.61078); // temp var + return (241.88 * T) / (17.558-T); +} + +// delta max = 0.6544 wrt dewPoint() +// 5x faster than dewPoint() +// reference: http://en.wikipedia.org/wiki/Dew_point +float DHT::CalcdewPointFast(float celsius, float humidity) +{ + float a = 17.271; + float b = 237.7; + float temp = (a * celsius) / (b + celsius) + log(humidity/100); + float Td = (b * temp) / (a - temp); + return Td; +} + +float DHT::ReadTemperature(eScale Scale) { + if (Scale == FARENHEIT) + return ConvertCelciustoFarenheit(_lastTemperature); + else if (Scale == KELVIN) + return ConvertCelciustoKelvin(_lastTemperature); + else + return _lastTemperature; +} + +float DHT::CalcHumidity() { + int v; + + switch (_DHTtype) { + case DHT11: + v = DHT_data[0]; + return float(v); + case DHT22: + v = DHT_data[0]; + v *= 256; + v += DHT_data[1]; + v /= 10; + return float(v); + } + return 0; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT/DHT.h Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,94 @@ +/* + * DHT Library for Digital-output Humidity and Temperature sensors + * + * Works with DHT11, DHT21, DHT22 + * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio) + * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio) + * AM2302 , temperature-humidity sensor + * RHT01,RHT02, RHT03 , Humidity and Temperature Sensor (Sparkfun) + * + * Copyright (C) Wim De Roeve + * based on DHT22 sensor library by HO WING KIT + * Arduino DHT11 library + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documnetation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MBED_DHT_H +#define MBED_DHT_H + +#include "mbed.h" + +enum eType{ + DHT11 = 11, + SEN11301P = 11, + RHT01 = 11, + DHT22 = 22, + AM2302 = 22, + SEN51035P = 22, + RHT02 = 22, + RHT03 = 22 + } ; + +enum eError { + ERROR_NONE = 0, + BUS_BUSY =1, + ERROR_NOT_PRESENT =2 , + ERROR_ACK_TOO_LONG =3 , + ERROR_SYNC_TIMEOUT = 4, + ERROR_DATA_TIMEOUT =5 , + ERROR_CHECKSUM = 6, + ERROR_NO_PATIENCE =7 +} ; + +typedef enum { + CELCIUS =0 , + FARENHEIT =1, + KELVIN=2 +} eScale; + + +class DHT { + +public: + + DHT(PinName pin,int DHTtype); + ~DHT(); + int readData(void); + float ReadHumidity(void); + float ReadTemperature(eScale Scale); + float CalcdewPoint(float celsius, float humidity); + float CalcdewPointFast(float celsius, float humidity); + +private: + time_t _lastReadTime; + float _lastTemperature; + float _lastHumidity; + PinName _pin; + bool _firsttime; + int _DHTtype; + int DHT_data[6]; + float CalcTemperature(); + float CalcHumidity(); + float ConvertCelciustoFarenheit(float); + float ConvertCelciustoKelvin(float); + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GPRSInterface.lib Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbotkinl/code/GPRSInterface/#180feb3ebe62
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient_GPRS.lib Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbotkinl/code/HTTPClient_GPRS/#aaab2081c1c0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2C_UART/i2c_uart.cpp Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,23 @@ +#include "mbed.h" +#include "i2c_uart.h" +#include "ARCH_GPRS_V2_HW_DFS.h" + +#define ADDRESS 8 + +I2C i2c_debug(GROVE_I2C_SDA, GROVE_I2C_SCL); + + +void debug_i2c(char *dta) +{ + int len = strlen(dta); + i2c_debug.write(ADDRESS, dta, len); + //wait_ms(10); +} + +void debug_i2c(char dta) +{ + char dta1[5]; + sprintf(dta1, "%c", dta); + debug_i2c(dta1); +} +//
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2C_UART/i2c_uart.h Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,17 @@ +#ifndef __I2C_UART_H__ +#define __I2C_UART_H__ + +#define __Debug 1 + +#if __Debug +#define DBG(X) debug_i2c(X) +#else +#define DBG(X) +#endif + + +void debug_i2c(char *dta); + + +void debug_i2c(char dta); +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RTC_WorkingLibrary.lib Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/Cellular-building-monitoring/code/RTC_WorkingLibrary/#9702e2e4aef9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbotkinl/code/SDFileSystem/#7b35d1709458
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SLEEP/ARCH_GPRS_Sleep.cpp Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,90 @@ +#include "mbed.h" +#include "ARCH_GPRS_Sleep.h" +#include "ARCH_GPRS_V2_HW.h" + +void Stalker3_0_sleep::gotoSleep() // goto sleep mode, untill wdt interrupt +{ + LPC_PMU->PCON |= 0x01; /* ????????? */ + LPC_SYSCON->PDSLEEPCFG |= (1UL << 3); /* ??BOD??????? */ + SCB->SCR &= ~(1UL << 2); /* ?????? */ + __wfi(); +} + +void Stalker3_0_sleep::wdtClkSetup(unsigned long clksrc) +{ + /* Freq = 0.5Mhz, div_sel is 0x1F, divided by 64. WDT_OSC should be 7.8125khz */ + LPC_SYSCON->WDTOSCCTRL = (0x1<<5)|0x1F; + LPC_SYSCON->PDRUNCFG &= ~(0x1<<6); /* Let WDT clock run */ + + /* Enables clock for WDT */ + LPC_SYSCON->SYSAHBCLKCTRL |= (1<<15); + LPC_WWDT->CLKSEL = clksrc; /* Select clock source */ + +} + +void Stalker3_0_sleep::wdtInit(long tc) // init wdt +{ + uint32_t regVal; + + LPC_WWDT->TC = tc; + + regVal = WDEN; + LPC_WWDT->MOD = regVal; + + LPC_WWDT->FEED = 0xAA; /* Feeding sequence */ + LPC_WWDT->FEED = 0x55; + + NVIC_EnableIRQ(WDT_IRQn); + NVIC_SetPriority(WDT_IRQn, 2); + return; + +} + +void Stalker3_0_sleep::init() +{ + +} + +void Stalker3_0_sleep::sleep(long ts) // sleep for ts (s) +{ + + workMode = MODE_SLEEP; + wdtInit(0x2dc6c0); + + for(int i=0; i<ts; i++) + { + gotoSleep(); + } + + workMode = MODE_WORKING; + feed(); +} + +void Stalker3_0_sleep::wakeUp() // wake up from sleep +{ + +} + +void Stalker3_0_sleep::feed() // watch dog feed +{ + LPC_WWDT->FEED = 0xAA; /* Feeding sequence */ + LPC_WWDT->FEED = 0x55; + return; +} + +Stalker3_0_sleep wdt_sleep; + +extern "C"{ + + void WDT_IRQHandler(void) + { + + if(wdt_sleep.workMode == MODE_WORKING) // WORKING MODE, AND NO FEET WDT, RESET!!! + { + NVIC_SystemReset(); + } + LPC_WWDT->MOD &= ~WDTOF; /* clear the time-out flag and interrupt flag */ + LPC_WWDT->MOD &= ~WDINT; /* clear the time-out flag and interrupt flag */ + wdt_sleep.wdtInit(0x2dc6c0); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SLEEP/ARCH_GPRS_Sleep.h Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,48 @@ +#ifndef __ARCH_GPRS_SLEEP_H__ +#define __ARCH_GPRS_SLEEP_H__ + + +#define WDTCLK_SRC_IRC_OSC 0 +#define WDTCLK_SRC_WDT_OSC 1 + +#define WDEN (0x1<<0) +#define WDRESET (0x1<<1) +#define WDTOF (0x1<<2) +#define WDINT (0x1<<3) +#define WDPROTECT (0x1<<4) +#define WDLOCKCLK (0x1<<5) + +#define WDT_FEED_VALUE 0x003FFFFF + +#define WINDOW_MODE 0 +#define PROTECT_MODE 0 +#define WATCHDOG_RESET 1 +#define WDLOCK_MODE 0 +#define LOCKCLK_MODE 0 + +#define MODE_SLEEP 0 +#define MODE_WORKING 1 + +class Stalker3_0_sleep{ + + public: + + int workMode; // working mode, sleep or working + + public: + + void gotoSleep(); // goto sleep mode, untill wdt interrupt + void wdtClkSetup(unsigned long clksrc); + + public: + + void init(); + + void sleep(long ts); // sleep for ts (s) + void wakeUp(); // wake up from sleep + void feed(); // watch dog feed + void wdtInit(long tc); // init wdt +}; + +extern Stalker3_0_sleep wdt_sleep; +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBDevice.lib Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbotkinl/code/USBDevice/#0f216c4e75e5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/finalCodev2.cpp Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,433 @@ +//SEED Team 20 Final Code draft 1 4/16/15 + +#include "mbed.h" +#include "SDFileSystem.h" +#include "ARCH_GPRS_V2_HW.h" +#include "HTTPClient.h" +#include "GPRSInterface.h" +#include "Blinker.h" +#include "DHT.h" +#include "i2c_uart.h" +#include "ARCH_GPRS_Sleep.h" +#include "ds1307.h" +#include "USBSerial.h" +#include <string> + +#define PIN_TX P1_27 +#define PIN_RX P1_26 + +#define SDA P0_5 +#define SCL P0_4 + + +#define BROADCAST_TIME 300 + +USBSerial pc; + +//LED Blink +DigitalOut line11(P0_9); +DigitalOut line13(P0_2); +DigitalOut line14(P1_28); + +Blinker yellowLED(LED1), redLED(LED2), greenLED(LED3), blueLED(LED4); + +//USBSerial pc; +SDFileSystem sd(P1_22, P1_21, P1_20, P1_23, "sd"); // the pinout on the /Arch GPRS v2 mbed board. +char filename[60]; + +//RTC +DS1307 rtc(SDA,SCL); +int sec, minute, hours, day, date, month, year; +char timestamp[17]; + +//variables for reading data from SD card +char APIKey[17]; +char sensors[3][5]; +char sensorBuff[5]; +int tempField, humField, lightField; +int field1, field2, field3; +int numSensors; +bool tempSensorflag=false, lightSensorflag=false, SomeotherSensorflag=false; + +//GPRS setup +GPRSInterface gprs(PIN_TX,PIN_RX,115200,"internetd.gdsp",NULL,NULL); +HTTPClient http; +char* thingSpeakUrl = "http://api.thingspeak.com/update"; +char urlBuffer[256]; +char str[1024]; +//char errorR[1024]; +string errorR; + +//Sensors +//DHT sensor(P1_14,SEN51035P); +DHT sensor(P1_14,SEN11301P); +AnalogIn lightSensor(P0_12); + + +int tempData,humData, lightData; + + +void connectGPRS(void) { + iot_hw.init(); // power on SIM900 + + int count = 0; + while(false == gprs.connect() && count < 5) { + wait(2); + count += 1; + } +} + +void getTempHumid(int* tempData,int* humData){ + int err = 1; + int count = 0; + iot_hw.grovePwrOn(); + wait(1); // wait 1 second for device stable status + while (err != 0 && count < 4) { + err = sensor.readData(); + count += 1; + *tempData = sensor.ReadTemperature(FARENHEIT); + *humData = sensor.ReadHumidity(); + + wait(1); + } + iot_hw.grovePwrOff(); +} + +void getLightReading(int* lightData) +{ + *lightData = lightSensor*1000; +} + + +int ReadFile (void) { + mkdir("/sd", 0777); // All other times open file in append mode + FILE *fp = fopen("/sd/config.txt","r"); + if (fp==NULL) { + fclose(fp); + return 0; + + } else if (fp) { + //fscanf(fp,"%16c %d %s %s", APIKey, &numSensors, sensors[0],sensors[1]); + fscanf(fp,"%16c",APIKey); + +// pc.printf("APIKEY= %s\r\n",APIKey); +// + fscanf(fp,"%d",&numSensors); +// pc.printf("number of sensors = %d \r\n",numSensors); +// + for (int i = 0; i<numSensors;i=i+1) { + fscanf(fp,"%s",sensors[i]); + //fscanf(fp,"%s",sensorBuff); + strcpy(sensors[i],sensorBuff); + //pc.printf("sensor %d = %s\r\n",i,sensors[i]); + } + + + fclose(fp); + return 1; + + } else { + fclose(fp); + return 0; + } + +} + +bool stringComparison (const char *string1, const char *string2) { + int count = 0; + while (string1[count] != 0) { + if (string1[count] != string2[count]) + return false; + count++; + } + return true; +} + + +void sendData1(int sensor1Data, int field1) { + connectGPRS(); + urlBuffer[0] = 0; + sprintf(urlBuffer, "%s?key=%s&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data); + HTTPResult res = http.get(urlBuffer, str,128); + if (res != HTTP_OK) { + redLED.blink(5); + } else { + greenLED.blink(5); + } + + + iot_hw.init_io(); //power down SIM900 +} + +void sendData2(int sensor1Data, int field1, int sensor2Data, int field2) { + connectGPRS(); + urlBuffer[0] = 0; + sprintf(urlBuffer, "%s?key=%s&field%d=%d&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data, field2, sensor2Data); + HTTPResult res = http.get(urlBuffer, str,128); + if (res != HTTP_OK) { + redLED.blink(5); + } else { + greenLED.blink(5); + } + iot_hw.init_io(); //power down SIM900 +} + +void sendData3(int sensor1Data, int field1, int sensor2Data, int field2, int sensor3Data, int field3) { + connectGPRS(); + urlBuffer[0] = 0; + sprintf(urlBuffer, "%s?key=%s&field%d=%d&field%d=%d&field%d=%d", thingSpeakUrl, APIKey, field1, sensor1Data, field2, sensor2Data, field3, sensor3Data); + HTTPResult res = http.get(urlBuffer, str,128); + if (res != HTTP_OK) { + redLED.blink(5); + } else { + greenLED.blink(5); + } + iot_hw.init_io(); //power down SIM900 +} + +//void errorWrite(const char* errorReport) { +void errorWrite(string errorReport) { + rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); + sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); + + mkdir("/sd", 0777); + FILE *fp = fopen("/sd/ErrorLog.txt","a"); + if (fp == NULL) { + //no sd card + redLED.blink(10); + } else { + pc.printf("%s: %s \r\n",timestamp,errorReport); + fprintf(fp,"%s: %s \r\n",timestamp,errorReport); + fclose(fp); + blueLED.blink(5); + } +} + +void sdWrite1(int sensor1Data) +{ + rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); + sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); + + mkdir("/sd", 0777); + FILE *fp = fopen("/sd/node1.csv","a"); + if (fp == NULL) { + //no sd card + redLED.blink(10); + } else { + + fprintf(fp,"%s, %d \r\n",timestamp,sensor1Data); + fclose(fp); + blueLED.blink(5); + } +} + +void sdWrite2(int sensor1Data, int sensor2Data) +{ + rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); + sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); + + mkdir("/sd", 0777); + FILE *fp = fopen("/sd/node1.csv","a"); + if (fp == NULL) { + //no sd card + redLED.blink(10); + } else { + + fprintf(fp,"%s, %d, %d\r\n",timestamp, sensor1Data, sensor2Data); + fclose(fp); + blueLED.blink(5); + } +} + +void sdWrite3(int sensor1Data, int sensor2Data,int sensor3Data) +{ + rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); + sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); + + mkdir("/sd", 0777); + FILE *fp = fopen("/sd/node1.csv","a"); + if (fp == NULL) { + //no sd card + redLED.blink(10); + } else { + + fprintf(fp,"%s, %d, %d, %d\r\n",timestamp, sensor1Data, sensor2Data, sensor3Data); + fclose(fp); + blueLED.blink(5); + } +} + + + + +int main() { + wdt_sleep.wdtClkSetup(WDTCLK_SRC_IRC_OSC); //set up sleep + + + + //test of breadboard +// while (1) { +// //rtc test +// errorR="Test rtc"; +// rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); +// sprintf(timestamp,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d", year, month, date, hours, minute, sec); +// pc.printf("%s: %s\r\n",timestamp,errorR); +// +// //rgb test +// int n=5; +// int t=1; +// pc.printf("line11 blink\r\n"); +// for (int i = 0; i < n; i++) { +// line11 = 1; +// wait(t); +// line11 = 0; +// wait(t); +// } +// +// pc.printf("line13 blink\r\n"); +// +// for (int i = 0; i < n; i++) { +// line13 = 1; +// wait(t); +// line13 = 0; +// wait(t); +// } +// +// pc.printf("line11 blink\r\n"); +// +// for (int i = 0; i < n; i++) { +// line14 = 1; +// wait(t); +// line14 = 0; +// wait(t); +// } +// //light sensor test +// getLightReading(&lightData); +// pc.printf("light reading: %d \r\n",lightData); +// +// wait(15); +// } + + + //read SD card + while (ReadFile()==0) { + wait(5); + redLED.blink(10); + } + greenLED.blink(10); + + + //identify sensors and find order of fields + int field=1; + for (int i=0;i<numSensors;i=i+1) { + if (stringComparison(sensors[i],"temp")==true) { + tempSensorflag=true; + tempField=field; + humField=field+1; + field=field+1; + } else if (stringComparison(sensors[i],"light")==true) { + lightSensorflag=true; + lightField=field; + } else if (stringComparison(sensors[i],"some other sensor")==true) { + SomeotherSensorflag=true; + } + field=field+1; + } + + //set up CSV file + mkdir("/sd", 0777); + rtc.gettime( &sec, &minute, &hours, &day, &date, &month, &year); + sprintf(filename,"/sd/DataLog_20%.2d.%.2d.%.2d_%.2d.%.2d.csv",year, month, date, hours, minute); + FILE *fp = fopen(filename,"a"); + if (fp == NULL) { + //no sd card + redLED.blink(10); + } else { + + //print header + fprintf(fp,"Timestamp, sensor1, sensor2 \r\n"); + fclose(fp); + blueLED.blink(5); + } + + //Debug Mode + for (int dd=0;dd<30;dd=dd+1) { + + if (numSensors == 3) { + + } else if (numSensors ==2) { + if (tempSensorflag==true) { + if (lightSensorflag==true) { + getLightReading(&lightData); + getTempHumid(&tempData,&humData); + sendData3(tempData,tempField,humData,humField,lightData,lightField); + sdWrite3(tempData,humData,lightData); + } else if (SomeotherSensorflag==true) { + + } + } else if (lightSensorflag==true) { + if (SomeotherSensorflag==true) { + getLightReading(&lightData); + + + } + } + } else { + if (tempSensorflag==true) { + getTempHumid(&tempData,&humData); + sendData2(tempData,tempField,humData,humField); + sdWrite2(tempData,humData); + } else if (lightSensorflag==true) { + getLightReading(&lightData); + sendData1(lightData,lightField); + sdWrite1(lightData); + } else if (SomeotherSensorflag==true) { + + } + } + wait(30); + } + + + + + + //normal mode + while (1) { + + if (numSensors == 3) { + + } else if (numSensors ==2) { + if (tempSensorflag==true) { + if (lightSensorflag==true) { + getLightReading(&lightData); + getTempHumid(&tempData,&humData); + sendData3(tempData,tempField,humData,humField,lightData,lightField); + sdWrite3(tempData,humData,lightData); + } else if (SomeotherSensorflag==true) { + + } + } else if (lightSensorflag==true) { + if (SomeotherSensorflag==true) { + getLightReading(&lightData); + + + } + } + } else { + if (tempSensorflag==true) { + getTempHumid(&tempData,&humData); + sendData2(tempData,tempField,humData,humField); + sdWrite2(tempData,humData); + } else if (lightSensorflag==true) { + getLightReading(&lightData); + sendData1(lightData,lightField); + sdWrite1(lightData); + } else if (SomeotherSensorflag==true) { + + } + } + wdt_sleep.sleep(BROADCAST_TIME); + + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue May 05 19:07:02 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e188a91d3eaa \ No newline at end of file