my

Dependencies:   EthernetInterface FreescaleIAP WebSocketClient mbed-rtos mbed-src3

Files at this revision

API Documentation at this revision

Comitter:
zain_mbed
Date:
Wed Nov 23 11:58:46 2016 +0000
Commit message:
ll

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
FreescaleIAP.lib Show annotated file Show diff for this revision Revisions of this file
WebSocketClient.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-src.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Wed Nov 23 11:58:46 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/zain_mbed/code/EthernetInterface/#027a7530fed6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FreescaleIAP.lib	Wed Nov 23 11:58:46 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/Sissors/code/FreescaleIAP/#186db0d96fcf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketClient.lib	Wed Nov 23 11:58:46 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/WebSocketClient/#466f90b7849a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Nov 23 11:58:46 2016 +0000
@@ -0,0 +1,411 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "Websocket.h"
+#include "FreescaleIAP.h"
+#define ECHO_SERVER_PORT   560
+extern "C" void mbed_reset();
+extern "C" void mbed_mac_address(char *mac);
+TCPSocketServer server2;
+TCPSocketConnection client;
+DigitalOut green(LED_GREEN);
+DigitalOut red(LED_RED);
+DigitalOut blue(LED_BLUE);
+DigitalOut inpin(PTC2);
+InterruptIn pb(PTC3);
+Serial pc(USBTX, USBRX);
+Serial xbee(PTC17,PTC16); // uart rx
+bool flag=false;
+int i=0;
+
+EthernetInterface eth;
+
+char *sourceip;
+char *swebsocket=NULL;
+char *sip=NULL;
+char *ssubnet=NULL;
+char *sgw=NULL;
+char *spch=NULL; 
+int counter=0;
+int k=0;
+char id[20];
+
+void pb_hit_interrupt (void)
+ {
+   inpin=0;
+   int address = flash_size() - SECTOR_SIZE;
+   erase_sector(address);
+   green=1;
+   red=1;
+   blue=0;
+   wait(3);
+   NVIC_SystemReset();
+ }
+   
+
+string memory()
+{
+inpin=0;
+  
+pb.mode(PullUp);
+    // Delay for initial pullup to take effect
+ wait(.01);
+    // Attach the address of the interrupt handler routine for pushbutton
+ pb.fall(&pb_hit_interrupt);
+ printf("in memory \n");
+ int address = flash_size() - SECTOR_SIZE;           //Write in last sector
+  char *data = (char *)address;
+  char back[65];
+  strncpy(back,data,65);
+  back[strlen(back)]='\0';
+  //printf("back %s \n",back);
+  int length=strlen(back);
+  printf("\n length: %d \n",length);
+  //  char c=".";
+    int k=0;
+    for (int i=0;i<60;i++)
+    {
+        printf("%c",data[i]);
+        if(data[i]=='.')
+        {
+            k++;
+            }
+        
+        }
+        printf("\n k %d \n",k);
+        if (k>5){
+
+    spch = strtok (back,",");
+swebsocket=spch;
+swebsocket[strlen(swebsocket)]='\0';
+
+spch = strtok (NULL, ",");
+sip=spch;
+sip[strlen(sip)]='\0';
+spch = strtok (NULL, ",");
+ssubnet=spch;
+ssubnet[strlen(ssubnet)]='\0';
+spch = strtok (NULL, ",");
+sgw=spch;
+spch = strtok (NULL, ",");
+   
+ printf(swebsocket);
+ printf("\n"); 
+ 
+ printf(sip);
+ printf("\n");
+ 
+ printf(ssubnet);
+ printf("\n"); 
+      
+ printf(sgw);
+ printf("\n"); 
+ 
+  eth.init(sip,ssubnet,sgw);
+  eth.connect();
+      char *mac;
+       mac=eth.getMACAddress();
+    sourceip=eth.getIPAddress();
+     pc.printf("mac - ddress: %s \r\n",mac);
+     int l =strlen (mac);
+
+       strncpy(id,mac,l);
+           printf("mac_addr is id %s \n",id);
+           int hhh=strlen(id);
+           printf("mac_addr len %d \n",hhh);
+           id[hhh]='\0';
+pc.printf("Connected - IP address: %s \r\n",eth.getIPAddress());
+printf("swebsocket length %d \n",strlen(swebsocket));
+
+ }
+
+return swebsocket;
+ }
+ 
+void ftc()
+{ inpin=0;
+ printf("\n first time configuration \n");
+eth.init("192.168.68.100","255.255.255.0","192.168.68.1");
+    char *mac;
+    mac=eth.getMACAddress();
+    sourceip=eth.getIPAddress();
+     eth.connect();
+     pc.printf("mac - ddress: %s \r\n",mac); 
+     pc.printf("Connected - IP address: %s \r\n",sourceip);
+    server2.bind(ECHO_SERVER_PORT);
+    server2.listen();
+ printf("Waiting for connection \n");;
+ server2.accept(client);
+       // client.set_blocking(false, 1500); // Timeout after (1.5)s
+        
+        printf("Connection from: %s\n", client.get_address());
+        // 
+        char buffer[256];
+        while (true) {
+          strcat(mac,".");
+          client.send(mac, strlen(mac));
+            int n = client.receive(buffer, sizeof(buffer));
+            if (n <= 0) break;
+            
+            // print received message to terminal
+            buffer[n] = '\0';
+printf("Received message from Client :'%s'\n",buffer);
+            int kn=strlen(buffer);
+            if (kn>5)
+            {
+                printf("actual message from Client :'%s'\n",buffer);
+                 int address = flash_size() - SECTOR_SIZE;           //Write in last sector
+    printf("address %d \n",address);
+    char *data = (char *)address;
+     printf(" \n Starting\r\n"); 
+     erase_sector(address);
+    
+    int len=strlen(buffer);
+    printf("store length %d \n",len);
+    program_flash(address, buffer,len);        //10 integers of 4 bytes each: 40 bytes length
+    printf("Resulting flash: \r\n");
+    printf("stored data %s\r\n", data);
+    printf("Done\r\n\n");
+      client.send("received", 9);
+                NVIC_SystemReset();
+                }
+            // reverse the message
+            
+            if (n <= 0) break;
+        }
+        
+        client.close();
+         
+        NVIC_SystemReset();
+      }
+    
+    
+    
+    
+    
+    
+int main()
+{
+ inpin=0;
+ pb.mode(PullUp);
+    // Delay for initial pullup to take effect
+ wait(.01);
+    // Attach the address of the interrupt handler routine for pushbutton
+ pb.fall(&pb_hit_interrupt);
+ 
+ printf("Hello World\n");
+ char macc[6];
+     
+
+char *w_socket_addr=new char [50];
+    wait(3);
+string wes=  memory();
+const char *web_socket_ip=wes.c_str();
+printf("return string %s \n",web_socket_ip);
+int hkh=strlen(web_socket_ip);
+printf("string length %d \n",hkh);
+
+     char buf[200];
+
+     xbee.baud(9600);
+
+
+  
+ 
+         if (hkh>0)
+     {
+         strcpy(w_socket_addr,"ws://");
+         strcat(w_socket_addr,web_socket_ip);
+         strcat(w_socket_addr,"/EchoChamber/echo");
+         
+      }
+    else
+    {
+     
+      ftc();  
+      }
+ w_socket_addr[strlen(w_socket_addr)]='\0';
+ Websocket ws(w_socket_addr);
+     printf("connecting to webserver \n");
+     ws.connect();
+     printf("connected \n");
+     
+     xbee.printf("ar\n");
+     if(xbee.readable())
+    {xbee.gets(buf,25);
+      printf(buf);   
+    }
+      wait(1);
+     xbee.printf("AT$sr\n");
+char *ip=NULL;
+char *subnet=NULL;
+char *gw=NULL;
+
+char *rxid;
+
+char m[100];
+char *pch;
+wait(1);
+bool a;
+
+
+
+   
+      
+while(1)
+{      green=1;
+   red=0;
+   blue=1;
+    if (i>=455000)
+   {  
+   
+        if(ws.is_connected())
+        {  counter=counter+1;
+        printf("keep alive \n");
+        ws.send("keep alive");
+      
+            //printf(ws);
+        }
+                    
+        if(!ws.is_connected())
+        { printf("not alive \n");
+        
+        NVIC_SystemReset();
+            }
+        
+       i=0;
+         
+    }
+    i++;
+if (counter>=2)
+{ 
+     printf("counter %d \n",counter);;
+     printf("reset \n");
+     
+     //    ws.fillFields("ws://192.168.0.54:8080/EchoChamber/echo");
+   NVIC_SystemReset();
+      ws.close();
+   //  eth.disconnect();
+
+
+      
+
+}
+
+
+
+if(xbee.readable())
+{
+     green=0;
+   red=1;
+   blue=1;
+    memset(buf, 0, sizeof(buf));
+xbee.gets(buf,27);
+char end[5]="end";
+ printf(" \n received buffer %s \n",buf);
+printf("counter %d \n",counter);
+ 
+strcat(buf,id);
+strcat(buf,end);
+printf("\n buffer is %s \n",id);
+char start[100]="start";
+int tsy=strlen(buf);
+printf("len %d \n",tsy);
+if (tsy>10)
+{printf("\n id is %s \n",buf);
+strcat(start,sourceip);
+strcat(start,"}");
+strcat(start,buf);
+printf("sending %s \n",start); 
+if (ws.is_connected()){
+ws.send(start);
+  memset(start, 0, sizeof(start));
+    memset(buf, 0, sizeof(buf));
+    printf("clear %s \n",start);
+        printf("buf %s \n",buf);
+}
+else{
+   printf("closed \n");
+   NVIC_SystemReset();
+      
+    }
+
+
+printf("\n");
+}
+}
+    memset(m, 0, sizeof(m));
+a=ws.read(m);
+
+k=strlen(m);
+
+
+if (strcmp(m,"stay alive")==0)
+{ printf(m);
+printf("\n");
+   counter--;
+   }
+
+   
+if (a==true && k>5 && k<35){
+       printf("received ipstring  %s",m);
+printf("\n");
+       }
+if (a==true && k>=40 && k<62)
+{
+      pch = strtok (m,"+");
+      rxid=pch;
+        if(strcmp(rxid,id)==0)
+        {
+     printf("matched \n");
+     printf("websocket %s \n",wes);
+     printf(id);
+     printf("\n");
+     pch = strtok (NULL, "+");
+     ip=pch;
+     printf(ip);
+     printf("\n");
+     pch = strtok (NULL, "+");
+     subnet=pch;
+     printf("%s\n",subnet);
+     pch = strtok (NULL, "+");
+     gw=pch;
+     printf(gw);
+     printf("\n");
+     char *store=new char[60];
+     strcat(store,web_socket_ip);
+     strcat(store,",");
+    strcat(store,ip);
+    strcat(store,",");
+    strcat(store,subnet);
+    strcat(store,",");
+    strcat(store,gw);
+    strcat(store,",");
+    printf("cat data %s \n",store);
+   
+    int address = flash_size() - SECTOR_SIZE;           //Write in last sector
+    printf("address %d \n",address);
+  
+     printf(" \n Starting\r\n"); 
+     erase_sector(address);
+    
+    int len=strlen(store);
+    printf("store length %d \n",len);
+    program_flash(address, store,len);        //10 integers of 4 bytes each: 40 bytes length
+    printf("Resulting flash: \r\n");
+  //  printf("stored data %s\r\n", data);
+    printf("Done\r\n\n");
+      NVIC_SystemReset();
+ 
+    wait(1);
+    }
+
+}
+    
+  memset(&m[0], 0, sizeof(m));
+}//loop is ending here
+
+
+
+
+}
+   
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Nov 23 11:58:46 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#bd07334df5b1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Wed Nov 23 11:58:46 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/zain_mbed/code/mbed-src3/#f9edfcefc276