IOT Project

Dependencies:   WiflyInterface mbed

Fork of IOT-Websocket_Wifly_HelloWorld by avnish aggarwal

Revision:
4:d1e0e52f7f6b
Parent:
3:034dbd0b2002
Child:
5:62cd993d82a6
--- a/main.cpp	Thu May 08 17:21:02 2014 +0000
+++ b/main.cpp	Fri Jun 06 13:44:53 2014 +0000
@@ -1,8 +1,17 @@
-#define OLD
-#ifdef OLD
+//This Program is used to turn the LED ON/OFF via telent (Wifly connected)
 #include "mbed.h"
 #include "WiflyInterface.h"
 #include "Websocket.h"
+#include <string.h>
+
+#define ECHO_SERVER_PORT   7
+
+
+PwmOut servo(p22);
+DigitalOut dir(LED1);
+
+#define FWD 1
+#define REV 0
 
 
 /* wifly interface:
@@ -14,153 +23,43 @@
 *     - WPA is the security
 */
 //apps board
-//WiflyInterface wifly(p9, p10, p30, p29, "iotlab", "42F67YxLX4AawRdcj", WPA);
+WiflyInterface wifly(p9, p10, p30, p29, "MY_WIFI", "", NONE);
 
 //pololu
-WiflyInterface wifly(p28, p27, p26, NC, "iotlab", "42F67YxLX4AawRdcj", WPA);
+//WiflyInterface wifly(p28, p27, p26, NC, "iotlab", "42F67YxLX4AawRdcj", WPA);
 
 int main() {
     
     char    recv[128];
     
     wifly.init(); //Use DHCP
+    printf("1\r\n");
     while (!wifly.connect());
     printf("IP Address is %s\n\r", wifly.getIPAddress());
 
-    Websocket ws("ws://echo.websocket.org/");
-    Websocket ws1("ws://sockets.mbed.org:443/ws/demo/wo");
-    while (!ws.connect());
-
-    while (1) {
-            ws.send("WebSocket Hello World over Wifly AA");
-            wait(1.0);
-            printf("send OK\n\r");
-            if (ws.read(recv)) 
-                printf("read: %s\r\n", recv);
-    }
-}
-
-#endif
-
-#ifdef MMA
-//Uses the measured z-acceleration to drive leds 2 and 3 of the mbed
-
-#include "mbed.h"
-#include "MMA7660.h"
-
-MMA7660 MMA(p28, p27);
-
-DigitalOut connectionLed(LED4);
-PwmOut Xaxis_p(LED1);
-PwmOut Yaxis_p(LED2);
-PwmOut Zaxis_p(LED3);
+   TCPSocketServer server;
+    
+    server.bind(ECHO_SERVER_PORT);
+    server.listen();
 
-int main() {  
-    if (MMA.testConnection())
-        connectionLed = 1;
-        
-    while(1) {
-        Xaxis_p = MMA.x();
-        //Zaxis_n = -MMA.z();
-        Yaxis_p = MMA.y();
-        //Zaxis_n = -MMA.y();
-        Zaxis_p = MMA.z();
-        //Zaxis_n = -MMA.z();
-        
-    }
-    
-
-}
-#endif
-
-#ifdef NEW
-#include "mbed.h"
-//#include "Wifly.h"
-#include "WiflyInterface.h"
-#include "Websocket.h"
-//#include "ADXL345.h"
-
-//ADXL345 accelerometer(p5, p6, p7, p8);
-DigitalIn tcp(p20);
-
-DigitalOut Gled(p29);
-DigitalOut Rled(p27);
-DigitalOut Yled(p28);
-
-//Wifly wifly(p9, p10, p22, "mbed", "password", true);
-WiflyInterface wifly(p9, p10, p25, p26, "bubbles", "", NONE);
-//Websocket ws("ws://sockets.mbed.org/ws/sensors/wo",&wifly);
-Websocket ws("ws://sockets.mbed.org/ws/sensors/wo");
-
-#include "MMA7660.h"
-
-MMA7660 MMA(p28, p27);
+    printf("\nWait for new connection...\n");
+    TCPSocketConnection client;
+    server.accept(client);
 
-DigitalOut connectionLed(LED4);
-PwmOut Xaxis_p(LED1);
-PwmOut Yaxis_p(LED2);
-PwmOut Zaxis_p(LED3);
-
-int main() {
-    char json_str[100];
-
-    int readings[3] = {0, 0, 0};
-
-    //Go into standby mode to configure the device.
-    //accelerometer.setPowerControl(0x00);
-    //accelerometer.setDataFormatControl(0x0B);
-    //accelerometer.setDataRate(ADXL345_3200HZ);
-    //accelerometer.setPowerControl(0x08);
- 
-#ifdef DOTHIS   
-    while (!wifly.cmdMode()) {
-        wifly.send("a\r\n");
-    }
-    //wifly.send("set sys iofunc 0x40\r\n", "AOK");
-#endif
-    wifly.init();           // new code
-    printf("here\n");
-    while (1) {
-        Rled = 1;
-        Yled = 0;
-        Gled = 0;
+    char buffer[256];
+    servo.period_us(50);
+    while (true) {
+        int n = client.receive(buffer, sizeof(buffer));
+        if (n <= 0) continue;
+        buffer[n] = 0;
+        printf("String is : %s\r\n",buffer);
 
-#ifdef DOTHIS  
-        while (!wifly.join()) {
-            wifly.reset();
-        }
-#endif
-
-        Rled = 0;
-        Yled = 1;
-        Gled = 0;
-
-        while (!ws.connect());
-
-        Rled = 0;
-        Yled = 0;
-        Gled = 1;
-
-        while (1) {
-        
-            wait(0.1);
-            
-            Xaxis_p = MMA.x();
-            Yaxis_p = MMA.y();
-            Zaxis_p = MMA.z();
-            
-            //we read accelerometers values
-            //accelerometer.getOutput(readings);
-
-            //sprintf(json_str, "{\"id\":\"wifly_acc\",\"ax\":\"%d\",\"ay\":\"%d\",\"az\":\"%d\"}", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
-            sprintf(json_str, "{\"id\":\"wifly_acc\",\"ax\":\"%d\",\"ay\":\"%d\",\"az\":\"%d\"}", (int16_t)Xaxis_p, (int16_t)Yaxis_p, (int16_t)Zaxis_p);
-            ws.send(json_str);
-
-            if (tcp.read() != 1) {
-                wifly.reset();
-                break;
-            }
-        }
+        client.send_all(buffer, n);
+        if (!(strcmp (buffer, "f")))
+            dir = FWD;
+        else if (!(strcmp(buffer,"r")))
+            dir = REV;
+        servo.pulsewidth_us(10);
+        wait_us(1);
     }
-}
-#endif
\ No newline at end of file
+}
\ No newline at end of file