first commit
Dependencies: C12832_lcd ConfigFile LM75B WiflyInterface mbed-rtos mbed
Fork of Xbee_Hello_World_B by
main.cpp@3:12afbbb4b29d, 2016-12-16 (annotated)
- Committer:
- kingkingyyk
- Date:
- Fri Dec 16 02:52:50 2016 +0000
- Revision:
- 3:12afbbb4b29d
- Parent:
- 2:ae1a5862504b
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tristanjph | 0:122702c9168a | 1 | #include "mbed.h" |
kingkingyyk | 1:15d917484a8d | 2 | #include "C12832_lcd.h" |
kingkingyyk | 3:12afbbb4b29d | 3 | #include "LM75B.h" |
kingkingyyk | 1:15d917484a8d | 4 | #include <string> |
kingkingyyk | 3:12afbbb4b29d | 5 | #include "WiflyInterface.h" |
kingkingyyk | 3:12afbbb4b29d | 6 | #include "rtos.h" |
tristanjph | 0:122702c9168a | 7 | |
kingkingyyk | 3:12afbbb4b29d | 8 | class Watchdog { |
kingkingyyk | 3:12afbbb4b29d | 9 | public: |
kingkingyyk | 3:12afbbb4b29d | 10 | // Load timeout value in watchdog timer and enable |
kingkingyyk | 3:12afbbb4b29d | 11 | void kick(float s) { |
kingkingyyk | 3:12afbbb4b29d | 12 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
kingkingyyk | 3:12afbbb4b29d | 13 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
kingkingyyk | 3:12afbbb4b29d | 14 | LPC_WDT->WDTC = s * (float)clk; |
kingkingyyk | 3:12afbbb4b29d | 15 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
kingkingyyk | 3:12afbbb4b29d | 16 | kick(); |
kingkingyyk | 3:12afbbb4b29d | 17 | } |
kingkingyyk | 3:12afbbb4b29d | 18 | // "kick" or "feed" the dog - reset the watchdog timer |
kingkingyyk | 3:12afbbb4b29d | 19 | // by writing this required bit pattern |
kingkingyyk | 3:12afbbb4b29d | 20 | void kick() { |
kingkingyyk | 3:12afbbb4b29d | 21 | LPC_WDT->WDFEED = 0xAA; |
kingkingyyk | 3:12afbbb4b29d | 22 | LPC_WDT->WDFEED = 0x55; |
kingkingyyk | 3:12afbbb4b29d | 23 | } |
kingkingyyk | 3:12afbbb4b29d | 24 | }; |
kingkingyyk | 3:12afbbb4b29d | 25 | |
kingkingyyk | 3:12afbbb4b29d | 26 | WiflyInterface wifly(p9, p10, p30, p29, "SSID", "PASSWORD", WPA); |
kingkingyyk | 3:12afbbb4b29d | 27 | AnalogIn ain(p17); |
kingkingyyk | 1:15d917484a8d | 28 | C12832_LCD lcd; |
kingkingyyk | 3:12afbbb4b29d | 29 | DigitalOut fan(p13); |
kingkingyyk | 3:12afbbb4b29d | 30 | DigitalOut led [] ={(LED1),(LED2)}; |
kingkingyyk | 3:12afbbb4b29d | 31 | const char *server_ip="192.168.10.2"; |
kingkingyyk | 3:12afbbb4b29d | 32 | const int server_port=40000; |
kingkingyyk | 3:12afbbb4b29d | 33 | const char *field_delimiter=";"; |
kingkingyyk | 3:12afbbb4b29d | 34 | const char *controller_name="TechRoomB_Control"; |
kingkingyyk | 3:12afbbb4b29d | 35 | const char *procCommand_ON="ON"; |
kingkingyyk | 3:12afbbb4b29d | 36 | const char *procCommand_OFF="OFF"; |
kingkingyyk | 3:12afbbb4b29d | 37 | char read_data[100]; |
kingkingyyk | 3:12afbbb4b29d | 38 | char send_data[100]; |
kingkingyyk | 3:12afbbb4b29d | 39 | char status_data[50]; |
kingkingyyk | 3:12afbbb4b29d | 40 | |
kingkingyyk | 1:15d917484a8d | 41 | |
kingkingyyk | 3:12afbbb4b29d | 42 | void sendActuatorStatusFunc () { |
kingkingyyk | 3:12afbbb4b29d | 43 | TCPSocketConnection conn; |
kingkingyyk | 3:12afbbb4b29d | 44 | |
kingkingyyk | 3:12afbbb4b29d | 45 | for (int i=0;i<100;i++) status_data[i]=0; |
kingkingyyk | 3:12afbbb4b29d | 46 | |
kingkingyyk | 3:12afbbb4b29d | 47 | if (fan==1) snprintf(status_data,50,"2;%s;AirCond;ON",controller_name); |
kingkingyyk | 3:12afbbb4b29d | 48 | else snprintf(status_data,50,"2;%s;AirCond;OFF",controller_name); |
kingkingyyk | 3:12afbbb4b29d | 49 | |
kingkingyyk | 3:12afbbb4b29d | 50 | if (conn.connect(server_ip,server_port)==0) conn.send_all(status_data,50); |
kingkingyyk | 3:12afbbb4b29d | 51 | |
kingkingyyk | 3:12afbbb4b29d | 52 | wait(0.1); |
kingkingyyk | 3:12afbbb4b29d | 53 | conn.close(); |
kingkingyyk | 3:12afbbb4b29d | 54 | } |
kingkingyyk | 3:12afbbb4b29d | 55 | |
kingkingyyk | 3:12afbbb4b29d | 56 | void setupWifly() { |
kingkingyyk | 1:15d917484a8d | 57 | lcd.cls(); |
kingkingyyk | 1:15d917484a8d | 58 | lcd.locate(0,0); |
kingkingyyk | 3:12afbbb4b29d | 59 | lcd.printf("Start WiFi Setup\n"); |
kingkingyyk | 1:15d917484a8d | 60 | |
kingkingyyk | 3:12afbbb4b29d | 61 | wifly.init(); |
kingkingyyk | 3:12afbbb4b29d | 62 | } |
kingkingyyk | 3:12afbbb4b29d | 63 | |
kingkingyyk | 3:12afbbb4b29d | 64 | void checkWiflyConnection() { |
kingkingyyk | 3:12afbbb4b29d | 65 | if (!wifly.is_associated()) { |
kingkingyyk | 3:12afbbb4b29d | 66 | while (!wifly.connect()) |
kingkingyyk | 3:12afbbb4b29d | 67 | lcd.cls(); |
kingkingyyk | 3:12afbbb4b29d | 68 | lcd.locate(0,0); |
kingkingyyk | 3:12afbbb4b29d | 69 | lcd.printf("IP Address : %s\n", wifly.getIPAddress()); |
kingkingyyk | 3:12afbbb4b29d | 70 | |
kingkingyyk | 3:12afbbb4b29d | 71 | sendActuatorStatusFunc(); |
kingkingyyk | 1:15d917484a8d | 72 | } |
kingkingyyk | 1:15d917484a8d | 73 | } |
kingkingyyk | 1:15d917484a8d | 74 | |
kingkingyyk | 3:12afbbb4b29d | 75 | |
kingkingyyk | 3:12afbbb4b29d | 76 | void processCommand (TCPSocketServer tserver) { |
kingkingyyk | 3:12afbbb4b29d | 77 | TCPSocketConnection conn; |
kingkingyyk | 3:12afbbb4b29d | 78 | |
kingkingyyk | 3:12afbbb4b29d | 79 | if (tserver.accept(conn)==0) { |
kingkingyyk | 3:12afbbb4b29d | 80 | int result=conn.receive_all(read_data,100); |
kingkingyyk | 3:12afbbb4b29d | 81 | |
kingkingyyk | 3:12afbbb4b29d | 82 | string tempCmd (strtok(read_data,field_delimiter)); |
kingkingyyk | 3:12afbbb4b29d | 83 | string commandId = tempCmd.substr(tempCmd.length()-1,tempCmd.length()); |
kingkingyyk | 3:12afbbb4b29d | 84 | string ctrl (strtok(NULL,field_delimiter)); |
kingkingyyk | 3:12afbbb4b29d | 85 | string act (strtok(NULL,field_delimiter)); |
kingkingyyk | 3:12afbbb4b29d | 86 | string status (strtok(NULL,field_delimiter)); |
kingkingyyk | 3:12afbbb4b29d | 87 | |
kingkingyyk | 3:12afbbb4b29d | 88 | if (!ctrl.find(controller_name)) { |
kingkingyyk | 3:12afbbb4b29d | 89 | if (!commandId.compare("1")) { |
kingkingyyk | 3:12afbbb4b29d | 90 | if (!act.compare("AirCond")) { |
kingkingyyk | 3:12afbbb4b29d | 91 | int statusInt=status.compare(procCommand_OFF); |
kingkingyyk | 3:12afbbb4b29d | 92 | fan=statusInt; |
kingkingyyk | 3:12afbbb4b29d | 93 | led[1]=statusInt; |
kingkingyyk | 3:12afbbb4b29d | 94 | |
kingkingyyk | 3:12afbbb4b29d | 95 | for (int i=0;i<100;i++) send_data[i]=0; |
kingkingyyk | 3:12afbbb4b29d | 96 | strcpy(send_data,status.c_str()); |
kingkingyyk | 3:12afbbb4b29d | 97 | |
kingkingyyk | 3:12afbbb4b29d | 98 | conn.send_all(send_data,100); |
kingkingyyk | 3:12afbbb4b29d | 99 | wait(0.1); // No idea why. : https://developer.mbed.org/forum/mbed/topic/3812/ |
kingkingyyk | 3:12afbbb4b29d | 100 | } |
kingkingyyk | 3:12afbbb4b29d | 101 | } |
kingkingyyk | 3:12afbbb4b29d | 102 | } |
kingkingyyk | 3:12afbbb4b29d | 103 | |
kingkingyyk | 3:12afbbb4b29d | 104 | conn.close(); |
kingkingyyk | 3:12afbbb4b29d | 105 | } |
kingkingyyk | 3:12afbbb4b29d | 106 | } |
kingkingyyk | 3:12afbbb4b29d | 107 | |
kingkingyyk | 3:12afbbb4b29d | 108 | void getCommand() { |
kingkingyyk | 3:12afbbb4b29d | 109 | checkWiflyConnection(); |
kingkingyyk | 3:12afbbb4b29d | 110 | while (1) { |
kingkingyyk | 3:12afbbb4b29d | 111 | TCPSocketServer tserver; |
kingkingyyk | 3:12afbbb4b29d | 112 | tserver.bind(40002); |
kingkingyyk | 3:12afbbb4b29d | 113 | tserver.listen(); |
kingkingyyk | 3:12afbbb4b29d | 114 | |
kingkingyyk | 3:12afbbb4b29d | 115 | led[0]=1; |
kingkingyyk | 3:12afbbb4b29d | 116 | processCommand(tserver); |
kingkingyyk | 3:12afbbb4b29d | 117 | tserver.close(); |
kingkingyyk | 3:12afbbb4b29d | 118 | led[0]=0; |
kingkingyyk | 3:12afbbb4b29d | 119 | } |
kingkingyyk | 1:15d917484a8d | 120 | } |
tristanjph | 0:122702c9168a | 121 | |
kingkingyyk | 1:15d917484a8d | 122 | int main() { |
kingkingyyk | 3:12afbbb4b29d | 123 | set_time(0); |
kingkingyyk | 3:12afbbb4b29d | 124 | mbed_interface_disconnect(); //Disable debugging to improve ADC precision. | Comment this if you need debugging via serial. |
kingkingyyk | 3:12afbbb4b29d | 125 | fan=1; |
kingkingyyk | 3:12afbbb4b29d | 126 | led[1]=1; |
kingkingyyk | 3:12afbbb4b29d | 127 | setupWifly(); |
tristanjph | 0:122702c9168a | 128 | |
kingkingyyk | 3:12afbbb4b29d | 129 | getCommand(); |
tristanjph | 0:122702c9168a | 130 | } |