#include "mbed.h" #include "EthernetNetIf.h" #include "PachubeClient.h" #include "string.h" #include using namespace std; Ticker PGEN; //setup ticker to send power to PACHUBE every 100 seconds // LEDs used to indicate code activity and reset source DigitalOut myled1(LED1); //in main loop part 1 DigitalOut myled2(LED2); //in main loop part 2 (where fault occurs) DigitalOut myled3(LED3); //The pushbutton or power on caused a reset DigitalOut myled4(LED4); //The watchdog timer caused a reset // Simon's Watchdog code from // http://mbed.org/forum/mbed/topic/508/ class Watchdog { public: // Load timeout value in watchdog timer and enable void kick(float s) { LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 LPC_WDT->WDTC = s * (float)clk; LPC_WDT->WDMOD = 0x3; // Enabled and Reset kick(); } // "kick" or "feed" the dog - reset the watchdog timer // by writing this required bit pattern void kick() { LPC_WDT->WDFEED = 0xAA; LPC_WDT->WDFEED = 0x55; } }; // Setup the watchdog timer Watchdog wdt; // ethernet network interface for ip stack EthernetNetIf eth; Serial pc(USBTX, USBRX); // tx, rx Serial xbee(p28, p27); // ";pinput from xbee int data_1; float data_2; char AHID[40]; char AHDATA[200]; char CID[40]; char CDATA[200]; int b0 = 0; int b1 = 0; float AHVOLTS; float AHAMPS; float CVOLTS; float CAMPS; float CFAMPS; float AHWATTS; float CWATTS; float CFWATTS; float AHWH; float CAH; float CWH; float CFAH; float CFWH; void AH_DATA() //send Air Handler data to PACHUBE { PachubeClient pachubeAH("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); pachubeAH.PutCsv("14671", AHID , AHDATA); printf("Pachube result / response : %d / %d\r", pachubeAH.Result(), pachubeAH.Response()); } void C_DATA() //send Compressor data to PACHUBE { PachubeClient pachubeC("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); pachubeC .PutCsv("14616", CID, CDATA ); printf("Pachube result / response : %d / %d\r", pachubeC.Result(), pachubeC.Response()); } void POWER() //calculate POWER and send to PACHUBE { AHWATTS = AHVOLTS * AHAMPS; //WATTS at x time printf("AHVOLTS = %.4f \r", AHVOLTS); printf("AHAMPS = %.4f \r", AHAMPS); printf("AHWATTS = %.4f \r", AHWATTS); AHWH = AHWH + AHWATTS * .0278; //WATTHOURS for TICKER time =100 seconds LPC_RTC->GPREG0 = AHWH; printf("AH WATTHOURS = %.1f \r",AHWH); sprintf(AHID, "55"); sprintf(AHDATA , "%.1f" , AHWH); AH_DATA(); wait (5); CWATTS = CVOLTS * CAMPS; printf("CVOLTS = %.4f \r", CVOLTS); printf("CAMPS = %.4f \r", CAMPS); printf("CWATTS = %.4f \r", CWATTS); CWH = CWH + CWATTS *.0278; LPC_RTC->GPREG1 = CWH; printf("C WATTHOURS = %.1f \r",CWH); sprintf(CID, "105"); sprintf(CDATA , "%.1f" , CWH); C_DATA(); wait(5); CFWATTS = CVOLTS * CFAMPS; printf("CVOLTS = %.4f \r", CVOLTS); printf("CFAMPS = %.4f \r", CFAMPS); printf("CFWATTS = %.4f \r", CFWATTS); CFWH = CFWH + CFWATTS * .0278; LPC_RTC->GPREG2 = CFWH; printf("CF WATTHOURS = %.1f \r",CFWH); sprintf(CID, "106"); sprintf(CDATA , "%.1f" , CFWH); C_DATA(); wait(5); } int main() { AHWH = LPC_RTC->GPREG0; //update Air Handler cumulative POWER CWH = LPC_RTC->GPREG1; //update Compressor cumulative POWER CFWH = LPC_RTC->GPREG2; //updat Compressor Fan cumulative POWER PGEN.attach(&POWER, 100); //ticker interface sends data to PACHUBE every 100 seconds int count = 0; // On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3 if ((LPC_WDT->WDMOD >> 2) & 1) myled4 = 1; else myled3 = 1; // setup a 10 second timeout on watchdog timer hardware // needs to be longer than worst case main loop exection time wdt.kick(200.0); // setup the network interface (by dhcp) eth.setup(); printf("made it to eth \r"); // Main program loop - resets watchdog once each loop iteration // Would typically have a lot of code in loop with many calls while(1) { myled1 = 1; //Flash LEDs 1 & 2 to indicate normal loop activity wait(.05); myled1 = 0; myled2 = 1; wait(.05); // Simulate a fault with an infinite loop, but only after 5000 loop iterations if (count == 5000) while (1) {}; // LED 2 will stay on during the fault myled2 = 0; count ++; if(xbee.readable()) { data_1 = xbee.getc(); b0 = xbee.getc(); b1 = xbee.getc(); if( data_1 == 10) { printf( "DATID IS %d \r",data_1); data_2 = (b1 * 256) + b0; data_2 = ((data_2/100) * 9/5) + 32; printf("Here is %f \r" , data_2); sprintf(AHID , "%d" , data_1); sprintf(AHDATA , "%.2f" , data_2); AH_DATA(); } // End of main loop so "kick" to reset watchdog timer and avoid a reset wdt.kick(); } }