first commit

Dependencies:   C12832_lcd ConfigFile LM75B WiflyInterface mbed-rtos mbed

Fork of Xbee_Hello_World_B by Tristan Hughes

Files at this revision

API Documentation at this revision

Comitter:
kingkingyyk
Date:
Fri Dec 16 02:52:50 2016 +0000
Parent:
2:ae1a5862504b
Commit message:
First commit

Changed in this revision

ConfigFile.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show diff for this revision Revisions of this file
LM75B.lib Show annotated file Show diff for this revision Revisions of this file
WiflyInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
xbee_lib.lib Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigFile.lib	Fri Dec 16 02:52:50 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- a/EthernetInterface.lib	Fri Sep 16 15:36:31 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/EthernetInterface/#183490eb1b4a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Fri Dec 16 02:52:50 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/neilt6/code/LM75B/#7ac462ba84ac
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WiflyInterface.lib	Fri Dec 16 02:52:50 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/kingkingyyk/code/WiflyInterface/#e19f4c58a137
--- a/main.cpp	Fri Sep 16 15:36:31 2016 +0000
+++ b/main.cpp	Fri Dec 16 02:52:50 2016 +0000
@@ -1,86 +1,130 @@
 #include "mbed.h"
-#include "xbee.h"
 #include "C12832_lcd.h"
+#include "LM75B.h"
 #include <string>
-#include "EthernetInterface.h"
+#include "WiflyInterface.h"
+#include "rtos.h"
 
-xbee xbee1(p9,p10,p30); //Initalise xbee_lib
+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;
+    }
+};
+ 
+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 fan(p13);
+DigitalOut led [] ={(LED1),(LED2)};
+const char *server_ip="192.168.10.2";
+const int server_port=40000;
+const char *field_delimiter=";";
+const char *controller_name="TechRoomB_Control";
+const char *procCommand_ON="ON";
+const char *procCommand_OFF="OFF";
+char read_data[100];
+char send_data[100];
+char status_data[50];
+
 
-bool setupEthernet() {
+void sendActuatorStatusFunc () {
+    TCPSocketConnection conn;
+         
+    for (int i=0;i<100;i++) status_data[i]=0;
+    
+    if (fan==1) snprintf(status_data,50,"2;%s;AirCond;ON",controller_name);
+    else snprintf(status_data,50,"2;%s;AirCond;OFF",controller_name);
+    
+    if (conn.connect(server_ip,server_port)==0) conn.send_all(status_data,50);
+    
+    wait(0.1);
+    conn.close();
+}
+
+void setupWifly() {
     lcd.cls();
     lcd.locate(0,0);
-    lcd.printf("Start Ethernet Setup\n");
+    lcd.printf("Start WiFi Setup\n");
     
-    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;
+    wifly.init();
+}
+
+void checkWiflyConnection() {
+    if (!wifly.is_associated()) {
+        while (!wifly.connect()) 
+        lcd.cls();
+        lcd.locate(0,0);
+        lcd.printf("IP Address : %s\n", wifly.getIPAddress());
+        
+        sendActuatorStatusFunc();
     }
 }
 
-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 processCommand (TCPSocketServer tserver) {
+        TCPSocketConnection conn;
+        
+        if (tserver.accept(conn)==0) {
+            int result=conn.receive_all(read_data,100);
+                
+            string tempCmd (strtok(read_data,field_delimiter));
+            string commandId = tempCmd.substr(tempCmd.length()-1,tempCmd.length());
+            string ctrl (strtok(NULL,field_delimiter));
+            string act (strtok(NULL,field_delimiter));
+            string status (strtok(NULL,field_delimiter));
+                    
+            if (!ctrl.find(controller_name)) {
+                if (!commandId.compare("1")) {
+                    if (!act.compare("AirCond")) {
+                        int statusInt=status.compare(procCommand_OFF);
+                        fan=statusInt;
+                        led[1]=statusInt;
+                        
+                        for (int i=0;i<100;i++) send_data[i]=0;
+                        strcpy(send_data,status.c_str());
+                        
+                        conn.send_all(send_data,100);
+                        wait(0.1); // No idea why. : https://developer.mbed.org/forum/mbed/topic/3812/
+                    }
+                }
+            }
+            
+            conn.close();
+        }
+}
+
+void getCommand() {
+    checkWiflyConnection();
+    while (1) {
+        TCPSocketServer tserver;
+        tserver.bind(40002);
+        tserver.listen();
+        
+        led[0]=1;
+        processCommand(tserver);
+        tserver.close();
+        led[0]=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.
+    fan=1;
+    led[1]=1;
+    setupWifly();
 
-    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];
-                }
-            }
-        }
-    }
+    getCommand();
 }
\ No newline at end of file
--- a/mbed-rtos.lib	Fri Sep 16 15:36:31 2016 +0000
+++ b/mbed-rtos.lib	Fri Dec 16 02:52:50 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#3da5f554d8bf
+https://mbed.org/users/mbed_official/code/mbed-rtos/#ac4f3830f9ff
--- a/mbed.bld	Fri Sep 16 15:36:31 2016 +0000
+++ b/mbed.bld	Fri Dec 16 02:52:50 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/2e9cc70d1897
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/082adc85693f
\ No newline at end of file
--- a/xbee_lib.lib	Fri Sep 16 15:36:31 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/tristanjph/code/xbee_lib/#81b7110f5877