Websocket example

Dependencies:   WebSocketClient

Fork of Websocket_Ethernet_HelloWorld by mbed_example

Files at this revision

API Documentation at this revision

Comitter:
Dontydonty
Date:
Sun Dec 03 18:19:22 2017 +0000
Parent:
7:26199246b2b4
Commit message:
Web sock et

Changed in this revision

ATcommand.cpp Show annotated file Show diff for this revision Revisions of this file
ATcommand.h Show annotated file Show diff for this revision Revisions of this file
config.cpp Show annotated file Show diff for this revision Revisions of this file
config.h 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
seg.cpp Show annotated file Show diff for this revision Revisions of this file
seg.h Show annotated file Show diff for this revision Revisions of this file
diff -r 26199246b2b4 -r 1b4bf6e5e029 ATcommand.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ATcommand.cpp	Sun Dec 03 18:19:22 2017 +0000
@@ -0,0 +1,298 @@
+#include "ATcommand.h"
+int timeout = 5000;
+char off[9] = {0x7E,0x00,0x05,0x09,0x01,0x44,0x30,0x04,0x7D};
+char on[9]  = {0x7E,0x00,0x05,0x09,0x01,0x44,0x30,0x05,0x7C};
+
+//--------------------------------------------
+//------------- Application ------------------
+//--------------------------------------------
+void print(Serial *pc, char buff[]);
+void toggleError(Serial *xbee, Serial *pc, int delay, char * msg){
+    pc->printf("\n\r !!!!!!!!!!! %s !!!!!!!!!!!",msg);
+    flush(xbee,pc);
+    send(xbee,pc, on, 9);  flush(xbee,pc);
+    ATWR(xbee, pc);        flush(xbee,pc);
+    apply(xbee, pc);       flush(xbee,pc);
+    
+    wait_ms(delay);
+    
+    send(xbee,pc, off, 9);  flush(xbee,pc);
+    ATWR(xbee, pc);         flush(xbee,pc);
+    apply(xbee, pc);        flush(xbee,pc);
+}
+
+void waitForConnection(Serial *xbee, Serial *pc, char * macAddr){
+    while(ND(xbee, pc, macAddr)==-1){}
+    pc->printf("MAC FOUND");
+    flush(xbee,pc);
+}
+
+void sendAccData(Serial *xbee, Serial *pc, char * macAddr ,char D[4]){
+    pc->printf("\n\rTransmit Data Request");
+    sendTR(xbee, pc, macAddr, D, 4);    
+}
+
+int decodePacket(Serial *xbee, Serial *pc, char *packet, int len){
+    int ret = 0;
+    switch(packet[0]){
+        case 0x92:  char * params= NULL;
+                    params = readParam(xbee, pc);
+                    free(params);    
+                    ret =  -1;
+                    break;
+        case 0x8B:  bool status = (packet[6]== 0x00);
+                    if(status){
+                        pc->printf("\n\rData send succes");
+                        ret= 0;
+                    }else{
+                        pc->printf("\n\rData send failed. Status: 0x%X",packet[6]);
+                        ret= -1;
+                    }
+            
+                    break;
+        default:    pc->printf("\n\rTO DO: decoder for frame type 0x%x",packet[0]); break;
+    }
+    return ret;
+}
+
+int receiveAcknowledge(Serial *xbee, Serial *pc){
+    char *packet = NULL;
+    int ret = 0;
+    if(0 == isStartDelimiter(xbee, pc)){
+        int l = getLenght(xbee, pc);
+        packet = readFrame(xbee, pc);
+        ret = decodePacket(xbee, pc, packet,l );
+    }else{
+         toggleError(xbee, pc, 200, "Error in FrameID");
+         ret = -1;   
+    }
+    free(packet);
+    return ret;
+}
+
+//--------------------------------------------
+//----------------- Network ------------------
+//--------------------------------------------
+char getStatusResponse(Serial *xbee, Serial *pc){
+    char *buf;
+    char status;
+    buf = read(xbee, pc,2, "AT Command");
+    free(buf);
+    
+    char *frame = NULL;
+    frame =  readFrame(xbee, pc);
+    status = frame[5];
+    free(frame);
+    return status;
+}
+
+int ND(Serial *xbee, Serial *pc, char * macAddr){
+    char AT_ND[2] =  {0x4E,0x44};
+    sendAT(xbee, pc, AT_ND, NULL, 0);
+    free(AT_ND);
+    
+    int time = timeout;
+    while(time >=0){
+        //wait_ms(10);
+        time -= 10;
+        if(xbee->readable()){
+            isStartDelimiter(xbee,pc);
+            int len = getLenght(xbee,pc);
+            
+            char *c;
+            c = readFrame(xbee, pc);
+            
+            
+            if(len>14){
+                pc->printf("\n\r\tMAC adresse:");   
+                for(int i=0; i<8;i++){
+                    macAddr[i] =  c[i+7];
+                    pc->printf("0x%x ", c[i+7]);
+                }   
+            }else{
+                toggleError(xbee, pc, 200, "\n\r !!! Mac adresse not return !!!");
+                return -1;
+            }
+            flush(xbee,pc);
+            free(c);
+            return 0;
+        }
+    }
+    pc->printf("WARNING : Connection Lost!");
+    return -1;
+}
+
+
+//--------------------------------------------
+//------------------- MAC --------------------
+//--------------------------------------------
+int isStartDelimiter(Serial *xbee, Serial *pc){
+    pc->printf("\n\r\tRECEIVE:");
+    return (xbee->getc() == 0x7E) ? 0 : -1;
+}
+int getLenght(Serial *xbee, Serial *pc){
+    char *buf;
+    buf = read(xbee, pc, 2, "Lenght");
+    int result = buf[0] << 8 | buf[1];                
+    free(buf);
+    return result;                       
+}
+char * readFrame(Serial *xbee, Serial *pc){
+    int l = 0;
+    char *packet = NULL;
+    while(xbee->readable()) {
+        packet = (char*)realloc(packet ,sizeof(char)*(l+1));
+        packet[l++] = xbee->getc();
+    }
+    //wait_ms(10);  // CPU to fast  
+    pc->printf("\n\r\tFrame:");   
+    for(int i=0; i<l; i++){
+        pc->printf("0x%x ",packet[i]);
+    }
+    return packet;
+}
+char * readParam(Serial *xbee, Serial *pc){
+    int l = 0;
+    char *packet = NULL;
+    while(xbee->readable()) {
+        packet = (char*)realloc(&packet[0],sizeof(char)*(l+1));
+        packet[l++] = xbee->getc();
+    }    
+    //wait_ms(50);  // CPU to fast
+    pc->printf("\n\r\tParams:");   
+    for(int i=0; i<l; i++){
+        pc->printf("0x%x ",packet[i]);
+    }
+    return packet;
+}
+
+
+
+//--------------------------------------------
+//------------------- PHY --------------------
+//--------------------------------------------
+char read(Serial *xbee, Serial *pc, char* msg){
+    char c;
+    pc->printf("\n\r\t %s:",msg);
+    c = xbee->getc();
+    pc->printf(" 0x%X", c);
+    if(strstr(msg, "Status")!= NULL){
+        if((int)c != 0){
+            toggleError(xbee, pc, 200, "Bad Status");
+        }
+    }
+    //wait_ms(10);// CPU to fast  
+    return c;
+}
+void send(Serial* xbee, Serial* pc, char * b, int size){
+    pc->printf("\n\r\tSEND:");
+    for(int i=0 ;i<size; i=i++){
+        xbee->putc(b[i]);
+        pc->printf(" 0x%X", b[i]);
+    }
+}
+void sendAT(Serial *xbee, Serial *pc, char* command, char* params, int lenparams){
+    uint8_t len = 4 +  lenparams;
+    //printf("Lenght is: %d\n", len);
+
+    char c[4+len];
+    c[0]= 0x7E;
+    c[1]= len >> 8 ;
+    c[2]= len;
+    
+    c[3]= 0x09;
+    c[4]= 0x01;
+    c[5] = command[0];
+    c[6] = command[1];
+    int i =0;
+    for(; i<lenparams; i++){
+        c[i+7] = params[i];
+    }
+    char check;
+    check = checksum(c,lenparams+4 );
+    c[i+7] = check;
+    
+    send(xbee,pc, c,len+4);
+    free(c);
+}
+
+void sendTR(Serial *xbee, Serial *pc, char macAddr[8], char RFdata[], int lenparams ){
+    uint8_t len = 14 +  lenparams;
+
+    char c[4+len];
+    c[0]= 0x7E;
+    c[1]= len >> 8 ;
+    c[2]= len;
+    
+    c[3]= 0x10;
+    c[4]= 0x01;
+    c[5] = macAddr[0];
+    c[6] = macAddr[1];
+    c[7] = macAddr[2];
+    c[8] = macAddr[3];
+    c[9] = macAddr[4];
+    c[10] = macAddr[5];
+    c[11] = macAddr[6];
+    c[12] = macAddr[7];
+    c[13] = 0xFF;
+    c[14] = 0xFE;
+    c[15] = 0x00;
+    c[16] = 0x00;
+    int i =0;
+    for(; i<lenparams; i++){
+        c[i+17] = RFdata[i];
+    }
+    char check;
+    check = checksum(c,len);
+    c[i+17] = check;
+    
+    send(xbee,pc, c,4+len);
+    free(c);
+}
+
+char checksum(char* array, int size){
+    char checksum = 0x00;
+    for(int x = 3; x < size+3; x++){
+      checksum += array[x];
+      //printf("\n\r 0x%X --> 0x%x",array[x],checksum);
+    }
+    //printf("\n\r substract 0xFF");
+    checksum = 0xFF - checksum;
+    //printf("\n\rChecksum is: 0x%x\n", checksum);
+    return checksum; 
+}
+
+void ATWR(Serial *xbee, Serial *pc){
+    char c[8] = {0x7E,0x00,0x04,0x09,0x01,0x57,0x52,0x4C};
+    send(xbee,pc, c,8);
+    //free(c);
+}
+void apply(Serial *xbee, Serial *pc){
+    char c[8] = {0x7E,0x00,0x04,0x09,0x01,0x41,0x43,0x71};
+    send(xbee,pc, c,8);
+    //free(c);
+}
+void flush(Serial *xbee, Serial *pc){
+    while(xbee->readable()) {
+        char c = xbee->getc();
+        //pc->printf("%X ",c);
+    }
+}
+
+
+char * read(Serial *xbee, Serial *pc, int nbyte,  char* msg){
+    pc->printf("\n\r\t%s:",msg);
+    char *buff = NULL;
+    if(xbee->readable()) {
+        for(int i=0; i<nbyte; i++){
+            buff = (char*)realloc(buff ,sizeof(char)*(i+1));
+            buff[i] = xbee->getc();
+        }
+    }
+    
+    //wait_ms(10); // CPU to fast
+    for(int i=0; i<nbyte; i++){
+        pc->printf("0x%X ", buff[i]);
+    }
+    return buff;
+}
diff -r 26199246b2b4 -r 1b4bf6e5e029 ATcommand.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ATcommand.h	Sun Dec 03 18:19:22 2017 +0000
@@ -0,0 +1,47 @@
+#ifndef AT_COMMAND
+#define AT_COMMAND
+
+#define STATUS_OK                0
+#define STATUS_ERROR             1
+#define STATUS_INVALID_COMMAND   2
+#define STATUS_INVALID_PARAMETER 3
+#define STATUS_TX_FAILURE        4
+
+#define IO_SAMPLE_RX_INDICATOR   0x92
+
+#define AT_COMMAND_RESPONSE      0x88
+#define PacketAcknowledged       1
+#define PacketBroadcast          2
+
+#include "mbed.h"
+
+// Application
+void print(Serial *pc, char buff[]);
+void toggleError(Serial *xbee, Serial *pc,int timeout, char * msg);
+//void waitForConnection(Serial *xbee, Serial *pc, char * macAddr);
+void sendAccData(Serial *xbee, Serial *pc, char * macAddr ,char D[4]);
+int decodePacket(Serial *xbee, Serial *pc, char *packet, int len);
+int receiveAcknowledge(Serial *xbee, Serial *pc);
+
+// Network
+char getStatusResponse(Serial *xbee, Serial *pc);
+int ND(Serial *xbee, Serial *pc, char * macAddr);
+
+//  MAC
+int  isStartDelimiter(Serial *xbee, Serial *pc);
+int  getLenght(Serial *xbee, Serial *pc);
+char * readFrame(Serial *xbee, Serial *pc);
+char * readParam(Serial *xbee, Serial *pc);
+
+// PHY
+char read (Serial *xbee, Serial *pc, char *msg);
+char * read(Serial *xbee, Serial *pc, int nbyte,  char* msg);
+void send (Serial *xbee, Serial *pc, char *b, int size);
+void sendAT(Serial *xbee, Serial *pc, char* command, char* params, int lenparams);
+void sendTR(Serial *xbee, Serial *pc, char* macAddr, char *RFdata,int len);
+char checksum(char* array, int size);
+void ATWR (Serial *xbee, Serial *pc);
+void apply(Serial *xbee, Serial *pc);
+void flush(Serial *xbee, Serial *pc);
+
+#endif
diff -r 26199246b2b4 -r 1b4bf6e5e029 config.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.cpp	Sun Dec 03 18:19:22 2017 +0000
@@ -0,0 +1,101 @@
+#include "config.h"
+#include "ATcommand.h"
+
+int configFromFile(char* path, Serial *xbee, Serial *pc, char * url){
+    pc->printf("\n\rConfig Filename: %s", path); 
+    
+    int maxRetry = 2;
+    
+    FILE* file = fopen(path, "r");
+    char line[256];
+    while (fgets(line, sizeof(line), file)) {
+        bool delimiterFound = false;
+        
+        // Line is a comment
+        if( (line[0]=='#') || (line[0]=='\n')){
+            pc->printf("\n\r%s", line); 
+            
+        // Line is url config
+        }else if(strstr(line, "url") != NULL){
+            int ptr = 0;
+            for(int i =0;i<sizeof(line)&& line[i]!='\0'; i++){
+                if(!delimiterFound){
+                    delimiterFound = (line[i]=='=');
+                }else{
+                    url[ptr] = line[i];
+                    ptr++;
+                }
+            }
+            pc->printf("Url:%s", url); 
+        
+        // Others line is AT COMMAND
+        }else{
+            int ptr = 0;
+            bool EOL = false;
+            pc->printf("AT Command\n\r"); 
+            for(int i =0;i<sizeof(line)&& !EOL; i++){
+                if(!delimiterFound){
+                    delimiterFound = (line[i]=='=');
+                    pc->printf("%c",line[i]); 
+                }else{
+                    
+                    // Send command
+                    char *cmd = NULL;
+                    char *params = NULL;
+                    int cmd_len = 0;
+                    int param_len = 0;
+                    
+                    for( ;i<sizeof(line)&& line[i]!= '\0'; i=i+2){
+                        unsigned char c = ahex2bin(line[i],line[i+1]);
+                        if(c == 0xDA){
+                            EOL =true;
+                        }else if(cmd_len <2){
+                            cmd = (char*)realloc(cmd ,sizeof(char)*(cmd_len+1));
+                            cmd[cmd_len++] = c;
+                        }else{
+                            params = (char*)realloc(params ,sizeof(char)*(param_len+1));
+                            params[param_len++] =  c;
+                        }
+                    }
+                    
+                    do{
+                        bool exit = false;
+                        sendAT(xbee, pc, cmd, params,param_len);
+                        wait_ms(200);
+                        ATWR (xbee, pc);
+                        wait_ms(200);
+                        apply(xbee, pc);
+                        wait_ms(200);                   
+                        // Verify command status
+                        char status = getStatusResponse(xbee, pc);
+                        if(status == 0x00){
+                            pc->printf("\n\rStatus OK"); 
+                            exit =true;      
+                        }else{
+                            if(status==0x02){
+                              pc->printf("\n\Invalid Command %d\n\r",status);  
+                            }     
+                            toggleError(xbee, pc, 200, "Warning status fail");  
+                            maxRetry--;    
+                        }
+                    }while(maxRetry>0 && !exit);
+                    free(cmd);
+                    free(params);
+                
+                    EOL =true;
+                }
+            }
+        }
+    }
+    fclose(file);
+    pc->printf("\n\rClose file"); 
+    return 0;
+}
+
+
+unsigned char ahex2bin (unsigned char MSB, unsigned char LSB){  
+    if (MSB > '9') MSB -= 7;   // Convert MSB (0x30..0x3F)  
+    if (LSB > '9') LSB -= 7;   // Convert LSB (0x30..0x3F)  
+    return (MSB << 4) | (LSB & 0x0F); 
+}
+ 
diff -r 26199246b2b4 -r 1b4bf6e5e029 config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h	Sun Dec 03 18:19:22 2017 +0000
@@ -0,0 +1,6 @@
+#ifndef CONFIG
+#define CONFIG
+#include "mbed.h"
+int configFromFile(char* path, Serial *xbee, Serial *pc, char * url);
+unsigned char ahex2bin (unsigned char MSB, unsigned char LSB);
+#endif
diff -r 26199246b2b4 -r 1b4bf6e5e029 main.cpp
--- a/main.cpp	Fri Jun 23 14:05:26 2017 -0500
+++ b/main.cpp	Sun Dec 03 18:19:22 2017 +0000
@@ -1,51 +1,92 @@
-/* Copyright C2014 ARM, MIT License
- *
- * Author: Doug Anson (doug.anson@arm.com)
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files the "Software", to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
+#include "mbed.h"
+#include "config.h"
+#include "seg.h"
+#include "ATcommand.h"
 
-#include "mbed.h"
+LocalFileSystem local("local");  
+Serial xbee(p9, p10);  /*tx, rx */ 
+DigitalOut rst(p5);
+Serial pc(USBTX, USBRX);
+Serial seg(p28, p27);
+DigitalOut btnState(p20);
+
+char url[256];
+
+// Check connection
+#define TIMEOUT 1000
+#define RETRY_DELAY 100
+int connectionTimeout = TIMEOUT;
+char *packet = NULL;
+
 #include "EthernetInterface.h"
 #include "Websocket.h"
 
-// Blinky
-DigitalOut led(LED1);
-
-int main() {   
+int main() {
+    display_dot(&seg);   
+    pc.printf("------Application Start------"); 
+    rst = 0;
+    wait_ms(1000); 
+    rst = 1;
+    wait_ms(1000); 
+    flush(&xbee, &pc);
+    
+    int result = configFromFile("/local/config.txt", &xbee, &pc, url);
+    pc.printf("OK Start receiving data");
+    char d[4];
+    
+    
 
-    // announce
-    printf("Websocket Example v1.0.0\r\n");
-    
     // Create a network interface and connect
-    EthernetInterface eth;
-    eth.connect();
-    printf("IP Address is %s\n\r", eth.get_ip_address());
+    //EthernetInterface eth;
+    //eth.connect();
+    //printf("IP Address is %s\n\r", eth.get_ip_address());
 
     // Create a websocket instance
-    Websocket ws("ws://example.com:8080/", &eth);
+    Websocket ws("ws://localhost.com:8080/");
     int connect_error = ws.connect();
     
     // begin main loop
     while (true) {
-
-        // blink... 
-        led = !led; 
         wait(0.5);
-        
         int error_c = ws.send("Hello World\r\n");
     }
-}
\ No newline at end of file
+    
+    
+    
+    
+    /*while(1){
+        while(xbee.readable()) {
+            if(0 == isStartDelimiter(&xbee, &pc)){
+                int len =  getLenght(&xbee, &pc);
+                packet  =  readFrame(&xbee, &pc);
+                
+                pc.printf("\n\rPacket type 0x%x",packet[0]);
+                
+                if(packet[0]== 0x90){
+                    char * params= NULL;
+                    params = readParam(&xbee, &pc);
+                    displayAngle(&seg,params);
+                    free(params);  
+                    
+                }else if(packet[0]== 0x92){
+                    char * params= NULL;
+                    params = readParam(&xbee, &pc);
+                    pc.printf("\n\r\tButtonState 0x%x",params[4]);
+                    if(params[4]== 0x10){
+                        btnState = 1;
+                    }else if(params[4]== 0x00){
+                        btnState = 0;
+                    }
+                    free(params);  
+
+                }else if(decodePacket(&xbee, &pc, packet, len)==-1){
+                    toggleError(&xbee, &pc, 200, "Packet Error");    
+                }
+                free(packet);
+            }else{
+                toggleError(&xbee, &pc, 200, "Error in FrameID");    
+            }    
+            flush(&xbee, &pc);
+        }
+    }*/
+}
diff -r 26199246b2b4 -r 1b4bf6e5e029 seg.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/seg.cpp	Sun Dec 03 18:19:22 2017 +0000
@@ -0,0 +1,15 @@
+#include "seg.h"
+
+void displayAngle(Serial * seg, char d[4]){
+     // Display Dot on 7segment
+    seg->putc(0x79);seg->putc(0x00);seg->putc(d[0]);
+    seg->putc(0x79);seg->putc(0x01);seg->putc(d[1]);
+    seg->putc(0x79);seg->putc(0x02);seg->putc(d[2]);
+    seg->putc(0x79);seg->putc(0x03);seg->putc(d[3]);
+}
+
+void display_dot(Serial * seg){
+     // Display Dot on 7segment
+    seg->putc(0x77);
+    seg->putc(0x02);
+}
diff -r 26199246b2b4 -r 1b4bf6e5e029 seg.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/seg.h	Sun Dec 03 18:19:22 2017 +0000
@@ -0,0 +1,9 @@
+#ifndef _7SEG_
+#define _7SEG_
+
+#include "mbed.h"
+
+void display_dot(Serial * seg);
+void displayAngle(Serial * seg,  char d[4]);
+
+#endif