First publish Luci
Dependencies: Adafruit_RTCLib SoftI2C SoftSerial mbed
Diff: main.cpp
- Revision:
- 0:99eede875063
- Child:
- 1:6cbf32f31179
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue May 23 11:15:38 2017 +0000 @@ -0,0 +1,135 @@ +#include "mbed.h" +#include "SoftSerial.h" +//#include "DS1307.h" + +//Init debug port +Serial DBG(PA_9, PA_10); //tx, rx + +//Init xbee +SoftSerial XB(PC_0, PC_1); //tx, rx + +//Init rtc +//I2C luciI2C (PB_7,PB_6); //sda, scl +//RtcDs1307 luciRTC(luciI2C); + +//Init adc +AnalogIn anVP(PA_1); +AnalogIn anIP(PA_0); +AnalogIn anVL(PA_2); +AnalogIn anIL(PA_3); +AnalogIn anVB(PB_1); +AnalogIn anIB(PB_0); +AnalogIn anTE(PC_2); + +//Sensor value +float valVP,valIP; +float valVL,valIL; +float valVB,valIB; +float valTE; + +//Date and Time +int year,month,date; +int day,hr,min,sec; +//DateTime dt; + +//Variables +int i; + +float adcRead(AnalogIn an) { + float s = 0; + float sTotal = 0; + int adcMax = 10; + + for(int i=0; i<adcMax; i++) { //Do adcMax readings + s = an.read(); + sTotal = sTotal + s; + wait_us(10); + } + return sTotal/adcMax; +} + +float currentCalculate(float adcVal) { + float res = 0; + + adcVal = adcVal - 0.510980; //Remove offset + if(-0.001 < adcVal && adcVal < 0.001) { //No current + res = 0; + } else { + res = (adcVal/13.2)*1000; + } + return res; +} + +/* +bool rtcUpdate(RtcDs1307 &rtc, int32_t bias) // this must be signed +{ bool bUpdated = false; + + // Use the compiled date/time as a basis for setting the clock. + // We assign it to a signed integer so that negative biases work correctly + int64_t compiledTime = DateTime(__DATE__,__TIME__).unixtime(); + + // This assumes that the program is run VERY soon after the initial compile. + time_t localt = DateTime(compiledTime + bias).unixtime(); // offset by bias + + // If the stored static time stamp does not equal the compiled time stamp, + // then we need to update the RTC clock and the stored time stamp + if(*((time_t *)&rtc[0]) != localt) + { + // Update the RTC time as local time, not GMT/UTC + rtc.adjust(localt); + // Store the new compiled time statically in the object ram image + *((time_t *)&rtc[0]) = localt; + // Push the object ram image to the RTC ram image + bUpdated = rtc.commit(); + } + return bUpdated; +} +*/ + +int main() { + DBG.baud(115200); + XB.baud(9600); + + // Uncomment to update time + //if(rtcUpdate(luciRTC, (7*60*60) )) // GMT+7 + // DBG.printf("Updated RTC to compiled time\r\n"); + //DBG.printf("compiled %s %s\r\n",__DATE__,__TIME__); + //DBG.printf("rtc clock is %s\r\n", (luciRTC.isRunning() ? "running" : "halted")); + + while(1){ + //Read sensor + valVP = 0; + valVL = 0; + valVB = 0; + valIP = 0; + valIL = 0; + valIB = 0; + valTE = 0; + + //Read sensor + valVP = adcRead(anVP)*52.8; + valVL = adcRead(anVL)*52.8; + valVB = adcRead(anVB)*52.8; + valIP = currentCalculate(adcRead(anIP)); + valIL = currentCalculate(adcRead(anIL)); + valIB = currentCalculate(adcRead(anIB)); + valTE = adcRead(anTE); + + //Read time + //dt = luciRTC.now(); + //DBG.printf("%02u%02u%02u %02u:%02u:%02u\r\n" + //,dt.year(),dt.month(),dt.day(),dt.hour(),dt.minute(),dt.second()); + + //Contruct and senddata + //DBG.printf("[%d]{DT=\"%02u%02u%02u %02u:%02u:%02u\",VP=%.1f,IP=%.1f,VL=%.1f,IL=%.1f,VB=%.1f,IB=%.1f,TE=%.1f}\r\n" + //,i,dt.year(),dt.month(),dt.day(),dt.hour(),dt.minute(),dt.second(),valVP,valIP,valVL,valIL,valVB,valIB,valTE); + //XB.printf("{DT=\"%02u%02u%02u %02u:%02u:%02u\",VP=%.1f,IP=%.1f,VL=%.1f,IL=%.1f,VB=%.1f,IB=%.1f,TE=%.1f}" + //,dt.year(),dt.month(),dt.day(),dt.hour(),dt.minute(),dt.second(),valVP,valIP,valVL,valIL,valVB,valIB,valTE); + DBG.printf("[%d]{DT=\"YYYYMMDD HH:MM:SS\",VP=%.1f,IP=%.1f,VL=%.1f,IL=%.1f,VB=%.1f,IB=%.1f,TE=%.1f}\r\n" + ,i,valVP,valIP,valVL,valIL,valVB,valIB,valTE); + XB.printf("{DT=\"YYYYMMDD HH:MM:SS\",VP=%.1f,IP=%.1f,VL=%.1f,IL=%.1f,VB=%.1f,IB=%.1f,TE=%.1f}" + ,valVP,valIP,valVL,valIL,valVB,valIB,valTE); + wait(60); + i++; + } +} \ No newline at end of file