![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
zzz
Dependencies: C12832_lcd ConfigFile LM75B WiflyInterface2 mbed-rtos mbed
Fork of Xbee_Hello_World_B by
Diff: main.cpp
- Revision:
- 3:623e2b67ebaa
- Parent:
- 2:ae1a5862504b
--- a/main.cpp Fri Sep 16 15:36:31 2016 +0000 +++ b/main.cpp Fri Dec 16 02:56:13 2016 +0000 @@ -1,86 +1,125 @@ #include "mbed.h" -#include "xbee.h" #include "C12832_lcd.h" +#include "LM75B.h" #include <string> -#include "EthernetInterface.h" +#include "WiflyInterface.h" +#include "rtos.h" +#include "LM75B.h" + +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; -xbee xbee1(p9,p10,p30); //Initalise xbee_lib +WiflyInterface wifly(p9, p10, p30, p29, "SSID", "PASSWORD", WPA); +AnalogIn ain(p17); C12832_LCD lcd; -DigitalOut LanLED(LED1); -DigitalOut XbeeLED(LED2); -DigitalOut LanDataLED(LED3); -EthernetInterface eth; +DigitalOut led [] ={(LED1),(LED2),(LED3),(LED4)}; +LM75B tempSensor(p28,p27); +const char *server_ip="192.168.10.2"; +const int server_port=40000; +char status_data[100]; + +const char* tempFormat="0;TechRoomB_Monitor;BlockB_Temperature;%f"; +void sendTempReading () { + led[2]=1; + TCPSocketConnection conn; + + for (int i=0;i<100;i++) status_data[i]=0; + snprintf(status_data,100,tempFormat,(((float)tempSensor)+55)/175); -bool setupEthernet() { + led[3]=1; + if (conn.connect(server_ip,server_port)==0 && conn.send_all(status_data,100)>=0); wdt.kick(); + led[3]=0; + + wait(0.1); + conn.close(); + led[2]=0; +} + +const char* currentFormat="0;TechRoomB_Monitor;BlockB_AirCondCurrent;%f"; +void sendCurrentReading () { + led[0]=1; + TCPSocketConnection conn; + + float max=0.0; + time_t start=time(NULL); + while (time(NULL)-start<2) { + if (ain>0 && ain<0.3 && ain>max) max=ain; + } + lcd.cls(); lcd.locate(0,0); - lcd.printf("Start Ethernet Setup\n"); + lcd.printf("Current : %f",max); + + for (int i=0;i<100;i++) status_data[i]=0; + snprintf(status_data,100,currentFormat,max*3); + + led[1]=1; + if (conn.connect(server_ip,server_port)==0 && conn.send_all(status_data,100)>=0); wdt.kick(); + led[1]=0; - eth.init("172.16.0.2","255.255.0.0","172.16.0.1"); - if(eth.connect()!=0) { - lcd.printf("Not connected!\n"); - return false; - } else { - lcd.printf("IP Address : %s\n", eth.getIPAddress()); - return true; + wait(0.1); + conn.close(); + led[0]=0; +} + +void sendReading() { + sendCurrentReading(); + wait(3.0); + sendTempReading(); +} + +void setupWifly() { + lcd.cls(); + lcd.locate(0,0); + lcd.printf("Start WiFi Setup\n"); + + wifly.init(); +} + +void checkWiflyConnection() { + if (!wifly.is_associated()) { + lcd.cls(); + lcd.locate(0,0); + lcd.printf("Reconnecting..."); + while (!wifly.connect()); + lcd.cls(); + lcd.locate(0,0); + lcd.printf("IP Address : %s\n", wifly.getIPAddress()); } } -void setupXbee() { - lcd.cls(); - lcd.locate(0,0); - lcd.printf("Start Xbee Setup\n"); - xbee1.Reset(); - xbee1.ConfigMode(); - xbee1.ResetConfig(); - xbee1.SetPanId(123); - xbee1.ExitConfigMode(); - lcd.printf("Done Setup!\n"); +void getCommand() { + checkWiflyConnection(); + wdt.kick(120.0); + while (1) { + sendReading(); + wait(60.0); + } } int main() { - bool flag=true; - flag=setupEthernet(); - LanLED=flag; - wait(0.5); - setupXbee(); + set_time(0); + mbed_interface_disconnect(); //Disable debugging to improve ADC precision. | Comment this if you need debugging via serial. - if (flag) { - UDPSocket socket; - socket.init(); - Endpoint server; - server.set_address("172.16.0.1",40000); - - int count=0; - char read_data[202]; //Xbee buffer size is 202 bytes - - while(1) { - char temp[202]; - XbeeLED=1; - xbee1.RecieveData(temp,10); //Read data from the XBee - XbeeLED=0; - for (int i=0;i<10 && temp[i]!='\0';i++) { - if (temp[i]=='|') { - count=0; - } else if (temp[i]=='&') { - string real_data=""; - for (int i2=0;i2<count;i2++) { - real_data+=read_data[i2]; - } - lcd.cls(); - lcd.locate(0,3); - lcd.printf("%s",real_data); - LanDataLED=1; - socket.sendTo(server,read_data,real_data.length()); - LanDataLED=0; - count=0; - for (int i2=0;i2<202;i2++) { - read_data[i2]='\0'; - } - } else { - read_data[count++]=temp[i]; - } - } - } - } + setupWifly(); + + getCommand(); } \ No newline at end of file