First publish Luci
Dependencies: Adafruit_RTCLib SoftI2C SoftSerial mbed
main.cpp@2:9d4438557eb7, 2017-11-12 (annotated)
- Committer:
- arsenalist
- Date:
- Sun Nov 12 23:52:50 2017 +0000
- Revision:
- 2:9d4438557eb7
- Parent:
- 1:6cbf32f31179
Latest Luci commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arsenalist | 0:99eede875063 | 1 | #include "mbed.h" |
arsenalist | 2:9d4438557eb7 | 2 | //#include "SoftSerial.h" |
arsenalist | 1:6cbf32f31179 | 3 | //#include "SoftI2C.h" |
arsenalist | 0:99eede875063 | 4 | //#include "DS1307.h" |
arsenalist | 0:99eede875063 | 5 | |
arsenalist | 1:6cbf32f31179 | 6 | #define typeIP 0 |
arsenalist | 1:6cbf32f31179 | 7 | #define typeIL 1 |
arsenalist | 1:6cbf32f31179 | 8 | #define typeIB 2 |
arsenalist | 1:6cbf32f31179 | 9 | |
arsenalist | 0:99eede875063 | 10 | //Init debug port |
arsenalist | 0:99eede875063 | 11 | Serial DBG(PA_9, PA_10); //tx, rx |
arsenalist | 0:99eede875063 | 12 | |
arsenalist | 0:99eede875063 | 13 | //Init xbee |
arsenalist | 2:9d4438557eb7 | 14 | Serial XB(PC_10, PC_11); //tx, rx |
arsenalist | 0:99eede875063 | 15 | |
arsenalist | 0:99eede875063 | 16 | //Init rtc |
arsenalist | 0:99eede875063 | 17 | //I2C luciI2C (PB_7,PB_6); //sda, scl |
arsenalist | 1:6cbf32f31179 | 18 | //SoftI2C luciI2C (PB_7,PB_6); //sda, scl |
arsenalist | 0:99eede875063 | 19 | //RtcDs1307 luciRTC(luciI2C); |
arsenalist | 0:99eede875063 | 20 | |
arsenalist | 0:99eede875063 | 21 | //Init adc |
arsenalist | 2:9d4438557eb7 | 22 | AnalogIn anVP(PA_0); |
arsenalist | 2:9d4438557eb7 | 23 | AnalogIn anIP(PA_3); |
arsenalist | 0:99eede875063 | 24 | AnalogIn anVL(PA_2); |
arsenalist | 2:9d4438557eb7 | 25 | AnalogIn anIL(PA_1); |
arsenalist | 0:99eede875063 | 26 | AnalogIn anVB(PB_1); |
arsenalist | 0:99eede875063 | 27 | AnalogIn anIB(PB_0); |
arsenalist | 0:99eede875063 | 28 | AnalogIn anTE(PC_2); |
arsenalist | 0:99eede875063 | 29 | |
arsenalist | 2:9d4438557eb7 | 30 | //Init switch control |
arsenalist | 2:9d4438557eb7 | 31 | DigitalOut PB12(PB_12); |
arsenalist | 2:9d4438557eb7 | 32 | |
arsenalist | 1:6cbf32f31179 | 33 | //Declared unused adc as digital out |
arsenalist | 1:6cbf32f31179 | 34 | //PA0 used //PA1 used |
arsenalist | 1:6cbf32f31179 | 35 | //PA2 used //PA3 used |
arsenalist | 1:6cbf32f31179 | 36 | //PA4 UN //PA5 SPI UN |
arsenalist | 1:6cbf32f31179 | 37 | //PA6 SPI UN //PA7 SPI UN |
arsenalist | 1:6cbf32f31179 | 38 | //PB0 used //PB1 used |
arsenalist | 2:9d4438557eb7 | 39 | //PC0 UN //PC1 UN |
arsenalist | 2:9d4438557eb7 | 40 | //PC2 Temp UN //PC3 UN |
arsenalist | 1:6cbf32f31179 | 41 | //PC4 UN //PC5 UN |
arsenalist | 1:6cbf32f31179 | 42 | DigitalOut PA4(PA_4); |
arsenalist | 1:6cbf32f31179 | 43 | DigitalOut PA5(PA_5); |
arsenalist | 1:6cbf32f31179 | 44 | DigitalOut PA6(PA_6); |
arsenalist | 1:6cbf32f31179 | 45 | DigitalOut PA7(PA_7); |
arsenalist | 2:9d4438557eb7 | 46 | DigitalOut PC0(PC_0); |
arsenalist | 2:9d4438557eb7 | 47 | DigitalOut PC1(PC_1); |
arsenalist | 1:6cbf32f31179 | 48 | DigitalOut PC2(PC_2); |
arsenalist | 1:6cbf32f31179 | 49 | DigitalOut PC3(PC_3); |
arsenalist | 1:6cbf32f31179 | 50 | DigitalOut PC4(PC_4); |
arsenalist | 1:6cbf32f31179 | 51 | DigitalOut PC5(PC_5); |
arsenalist | 1:6cbf32f31179 | 52 | |
arsenalist | 0:99eede875063 | 53 | //Sensor value |
arsenalist | 0:99eede875063 | 54 | float valVP,valIP; |
arsenalist | 0:99eede875063 | 55 | float valVL,valIL; |
arsenalist | 0:99eede875063 | 56 | float valVB,valIB; |
arsenalist | 0:99eede875063 | 57 | float valTE; |
arsenalist | 0:99eede875063 | 58 | |
arsenalist | 0:99eede875063 | 59 | //Date and Time |
arsenalist | 0:99eede875063 | 60 | int year,month,date; |
arsenalist | 0:99eede875063 | 61 | int day,hr,min,sec; |
arsenalist | 0:99eede875063 | 62 | //DateTime dt; |
arsenalist | 0:99eede875063 | 63 | |
arsenalist | 0:99eede875063 | 64 | //Variables |
arsenalist | 0:99eede875063 | 65 | int i; |
arsenalist | 0:99eede875063 | 66 | |
arsenalist | 0:99eede875063 | 67 | float adcRead(AnalogIn an) { |
arsenalist | 0:99eede875063 | 68 | float s = 0; |
arsenalist | 0:99eede875063 | 69 | float sTotal = 0; |
arsenalist | 1:6cbf32f31179 | 70 | int adcMax = 10000; |
arsenalist | 0:99eede875063 | 71 | |
arsenalist | 0:99eede875063 | 72 | for(int i=0; i<adcMax; i++) { //Do adcMax readings |
arsenalist | 0:99eede875063 | 73 | s = an.read(); |
arsenalist | 0:99eede875063 | 74 | sTotal = sTotal + s; |
arsenalist | 0:99eede875063 | 75 | wait_us(10); |
arsenalist | 0:99eede875063 | 76 | } |
arsenalist | 0:99eede875063 | 77 | return sTotal/adcMax; |
arsenalist | 0:99eede875063 | 78 | } |
arsenalist | 0:99eede875063 | 79 | |
arsenalist | 1:6cbf32f31179 | 80 | float currentCalculate(float adcVal, int type) { |
arsenalist | 0:99eede875063 | 81 | float res = 0; |
arsenalist | 0:99eede875063 | 82 | |
arsenalist | 2:9d4438557eb7 | 83 | if(type == typeIP || type == typeIB) { |
arsenalist | 2:9d4438557eb7 | 84 | adcVal = adcVal - 0.510980; //Remove offset |
arsenalist | 2:9d4438557eb7 | 85 | } else { |
arsenalist | 2:9d4438557eb7 | 86 | adcVal = adcVal - 0.491730; //Remove offset for IL |
arsenalist | 2:9d4438557eb7 | 87 | } |
arsenalist | 1:6cbf32f31179 | 88 | if(-0.0025 < adcVal && adcVal < 0.0025) { //No current |
arsenalist | 0:99eede875063 | 89 | res = 0; |
arsenalist | 0:99eede875063 | 90 | } else { |
arsenalist | 0:99eede875063 | 91 | res = (adcVal/13.2)*1000; |
arsenalist | 0:99eede875063 | 92 | } |
arsenalist | 1:6cbf32f31179 | 93 | |
arsenalist | 1:6cbf32f31179 | 94 | if(type == typeIP) { |
arsenalist | 1:6cbf32f31179 | 95 | if(res < 0) res = 0; |
arsenalist | 1:6cbf32f31179 | 96 | } |
arsenalist | 0:99eede875063 | 97 | return res; |
arsenalist | 0:99eede875063 | 98 | } |
arsenalist | 0:99eede875063 | 99 | |
arsenalist | 0:99eede875063 | 100 | /* |
arsenalist | 0:99eede875063 | 101 | bool rtcUpdate(RtcDs1307 &rtc, int32_t bias) // this must be signed |
arsenalist | 0:99eede875063 | 102 | { bool bUpdated = false; |
arsenalist | 0:99eede875063 | 103 | |
arsenalist | 0:99eede875063 | 104 | // Use the compiled date/time as a basis for setting the clock. |
arsenalist | 0:99eede875063 | 105 | // We assign it to a signed integer so that negative biases work correctly |
arsenalist | 0:99eede875063 | 106 | int64_t compiledTime = DateTime(__DATE__,__TIME__).unixtime(); |
arsenalist | 0:99eede875063 | 107 | |
arsenalist | 0:99eede875063 | 108 | // This assumes that the program is run VERY soon after the initial compile. |
arsenalist | 0:99eede875063 | 109 | time_t localt = DateTime(compiledTime + bias).unixtime(); // offset by bias |
arsenalist | 0:99eede875063 | 110 | |
arsenalist | 0:99eede875063 | 111 | // If the stored static time stamp does not equal the compiled time stamp, |
arsenalist | 0:99eede875063 | 112 | // then we need to update the RTC clock and the stored time stamp |
arsenalist | 0:99eede875063 | 113 | if(*((time_t *)&rtc[0]) != localt) |
arsenalist | 0:99eede875063 | 114 | { |
arsenalist | 0:99eede875063 | 115 | // Update the RTC time as local time, not GMT/UTC |
arsenalist | 0:99eede875063 | 116 | rtc.adjust(localt); |
arsenalist | 0:99eede875063 | 117 | // Store the new compiled time statically in the object ram image |
arsenalist | 0:99eede875063 | 118 | *((time_t *)&rtc[0]) = localt; |
arsenalist | 0:99eede875063 | 119 | // Push the object ram image to the RTC ram image |
arsenalist | 0:99eede875063 | 120 | bUpdated = rtc.commit(); |
arsenalist | 0:99eede875063 | 121 | } |
arsenalist | 0:99eede875063 | 122 | return bUpdated; |
arsenalist | 0:99eede875063 | 123 | } |
arsenalist | 0:99eede875063 | 124 | */ |
arsenalist | 0:99eede875063 | 125 | int main() { |
arsenalist | 0:99eede875063 | 126 | DBG.baud(115200); |
arsenalist | 0:99eede875063 | 127 | XB.baud(9600); |
arsenalist | 0:99eede875063 | 128 | |
arsenalist | 0:99eede875063 | 129 | // Uncomment to update time |
arsenalist | 1:6cbf32f31179 | 130 | //DBG.printf("update time\r\n"); |
arsenalist | 0:99eede875063 | 131 | //if(rtcUpdate(luciRTC, (7*60*60) )) // GMT+7 |
arsenalist | 0:99eede875063 | 132 | // DBG.printf("Updated RTC to compiled time\r\n"); |
arsenalist | 0:99eede875063 | 133 | //DBG.printf("compiled %s %s\r\n",__DATE__,__TIME__); |
arsenalist | 0:99eede875063 | 134 | //DBG.printf("rtc clock is %s\r\n", (luciRTC.isRunning() ? "running" : "halted")); |
arsenalist | 0:99eede875063 | 135 | |
arsenalist | 2:9d4438557eb7 | 136 | //Set switch control always on |
arsenalist | 2:9d4438557eb7 | 137 | PB12.write(1); |
arsenalist | 2:9d4438557eb7 | 138 | |
arsenalist | 1:6cbf32f31179 | 139 | PA4.write(0); |
arsenalist | 1:6cbf32f31179 | 140 | PA5.write(0); |
arsenalist | 1:6cbf32f31179 | 141 | PA6.write(0); |
arsenalist | 1:6cbf32f31179 | 142 | PA7.write(0); |
arsenalist | 2:9d4438557eb7 | 143 | PC0.write(0); |
arsenalist | 2:9d4438557eb7 | 144 | PC1.write(0); |
arsenalist | 1:6cbf32f31179 | 145 | PC2.write(0); |
arsenalist | 1:6cbf32f31179 | 146 | PC3.write(0); |
arsenalist | 1:6cbf32f31179 | 147 | PC4.write(0); |
arsenalist | 1:6cbf32f31179 | 148 | PC5.write(0); |
arsenalist | 2:9d4438557eb7 | 149 | |
arsenalist | 0:99eede875063 | 150 | while(1){ |
arsenalist | 0:99eede875063 | 151 | //Read sensor |
arsenalist | 0:99eede875063 | 152 | valVP = 0; |
arsenalist | 0:99eede875063 | 153 | valVL = 0; |
arsenalist | 0:99eede875063 | 154 | valVB = 0; |
arsenalist | 0:99eede875063 | 155 | valIP = 0; |
arsenalist | 0:99eede875063 | 156 | valIL = 0; |
arsenalist | 0:99eede875063 | 157 | valIB = 0; |
arsenalist | 0:99eede875063 | 158 | valTE = 0; |
arsenalist | 0:99eede875063 | 159 | |
arsenalist | 0:99eede875063 | 160 | //Read sensor |
arsenalist | 2:9d4438557eb7 | 161 | valVP = adcRead(anVP)*48.21; |
arsenalist | 2:9d4438557eb7 | 162 | valVL = adcRead(anVL)*48.21; |
arsenalist | 2:9d4438557eb7 | 163 | valVB = adcRead(anVB)*48.21; |
arsenalist | 1:6cbf32f31179 | 164 | valIP = currentCalculate(adcRead(anIP),typeIP); |
arsenalist | 1:6cbf32f31179 | 165 | valIL = currentCalculate(adcRead(anIL),typeIL); |
arsenalist | 1:6cbf32f31179 | 166 | valIB = currentCalculate(adcRead(anIB),typeIB); |
arsenalist | 0:99eede875063 | 167 | valTE = adcRead(anTE); |
arsenalist | 0:99eede875063 | 168 | |
arsenalist | 2:9d4438557eb7 | 169 | //valVP = adcRead(anVP)*52.8; |
arsenalist | 2:9d4438557eb7 | 170 | //valVL = adcRead(anVL)*52.8; |
arsenalist | 2:9d4438557eb7 | 171 | //valVB = adcRead(anVB)*52.8; |
arsenalist | 2:9d4438557eb7 | 172 | //valIP = currentCalculate(adcRead(anIP),typeIP); |
arsenalist | 2:9d4438557eb7 | 173 | //valIL = currentCalculate(adcRead(anIL),typeIL); |
arsenalist | 2:9d4438557eb7 | 174 | //valIB = currentCalculate(adcRead(anIB),typeIB); |
arsenalist | 2:9d4438557eb7 | 175 | //valTE = adcRead(anTE); |
arsenalist | 2:9d4438557eb7 | 176 | |
arsenalist | 0:99eede875063 | 177 | //Read time |
arsenalist | 0:99eede875063 | 178 | //dt = luciRTC.now(); |
arsenalist | 0:99eede875063 | 179 | //DBG.printf("%02u%02u%02u %02u:%02u:%02u\r\n" |
arsenalist | 0:99eede875063 | 180 | //,dt.year(),dt.month(),dt.day(),dt.hour(),dt.minute(),dt.second()); |
arsenalist | 0:99eede875063 | 181 | |
arsenalist | 0:99eede875063 | 182 | //Contruct and senddata |
arsenalist | 0:99eede875063 | 183 | //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" |
arsenalist | 0:99eede875063 | 184 | //,i,dt.year(),dt.month(),dt.day(),dt.hour(),dt.minute(),dt.second(),valVP,valIP,valVL,valIL,valVB,valIB,valTE); |
arsenalist | 0:99eede875063 | 185 | //XB.printf("{DT=\"%02u%02u%02u %02u:%02u:%02u\",VP=%.1f,IP=%.1f,VL=%.1f,IL=%.1f,VB=%.1f,IB=%.1f,TE=%.1f}" |
arsenalist | 0:99eede875063 | 186 | //,dt.year(),dt.month(),dt.day(),dt.hour(),dt.minute(),dt.second(),valVP,valIP,valVL,valIL,valVB,valIB,valTE); |
arsenalist | 2:9d4438557eb7 | 187 | DBG.printf("[%d]{DT=\"YYYYMMDD HH:MM:SS\",VP=%.5f,IP=%.5f,VL=%.5f,IL=%.5f,VB=%.5f,IB=%.5f,TE=%.5f}\r\n" |
arsenalist | 0:99eede875063 | 188 | ,i,valVP,valIP,valVL,valIL,valVB,valIB,valTE); |
arsenalist | 0:99eede875063 | 189 | XB.printf("{DT=\"YYYYMMDD HH:MM:SS\",VP=%.1f,IP=%.1f,VL=%.1f,IL=%.1f,VB=%.1f,IB=%.1f,TE=%.1f}" |
arsenalist | 0:99eede875063 | 190 | ,valVP,valIP,valVL,valIL,valVB,valIB,valTE); |
arsenalist | 2:9d4438557eb7 | 191 | wait(30); |
arsenalist | 0:99eede875063 | 192 | i++; |
arsenalist | 0:99eede875063 | 193 | } |
arsenalist | 0:99eede875063 | 194 | } |