Websocket example

Dependencies:   WebSocketClient

Fork of Websocket_Ethernet_HelloWorld by mbed_example

Committer:
Dontydonty
Date:
Sun Dec 03 18:19:22 2017 +0000
Revision:
8:1b4bf6e5e029
Web sock et

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Dontydonty 8:1b4bf6e5e029 1 #include "config.h"
Dontydonty 8:1b4bf6e5e029 2 #include "ATcommand.h"
Dontydonty 8:1b4bf6e5e029 3
Dontydonty 8:1b4bf6e5e029 4 int configFromFile(char* path, Serial *xbee, Serial *pc, char * url){
Dontydonty 8:1b4bf6e5e029 5 pc->printf("\n\rConfig Filename: %s", path);
Dontydonty 8:1b4bf6e5e029 6
Dontydonty 8:1b4bf6e5e029 7 int maxRetry = 2;
Dontydonty 8:1b4bf6e5e029 8
Dontydonty 8:1b4bf6e5e029 9 FILE* file = fopen(path, "r");
Dontydonty 8:1b4bf6e5e029 10 char line[256];
Dontydonty 8:1b4bf6e5e029 11 while (fgets(line, sizeof(line), file)) {
Dontydonty 8:1b4bf6e5e029 12 bool delimiterFound = false;
Dontydonty 8:1b4bf6e5e029 13
Dontydonty 8:1b4bf6e5e029 14 // Line is a comment
Dontydonty 8:1b4bf6e5e029 15 if( (line[0]=='#') || (line[0]=='\n')){
Dontydonty 8:1b4bf6e5e029 16 pc->printf("\n\r%s", line);
Dontydonty 8:1b4bf6e5e029 17
Dontydonty 8:1b4bf6e5e029 18 // Line is url config
Dontydonty 8:1b4bf6e5e029 19 }else if(strstr(line, "url") != NULL){
Dontydonty 8:1b4bf6e5e029 20 int ptr = 0;
Dontydonty 8:1b4bf6e5e029 21 for(int i =0;i<sizeof(line)&& line[i]!='\0'; i++){
Dontydonty 8:1b4bf6e5e029 22 if(!delimiterFound){
Dontydonty 8:1b4bf6e5e029 23 delimiterFound = (line[i]=='=');
Dontydonty 8:1b4bf6e5e029 24 }else{
Dontydonty 8:1b4bf6e5e029 25 url[ptr] = line[i];
Dontydonty 8:1b4bf6e5e029 26 ptr++;
Dontydonty 8:1b4bf6e5e029 27 }
Dontydonty 8:1b4bf6e5e029 28 }
Dontydonty 8:1b4bf6e5e029 29 pc->printf("Url:%s", url);
Dontydonty 8:1b4bf6e5e029 30
Dontydonty 8:1b4bf6e5e029 31 // Others line is AT COMMAND
Dontydonty 8:1b4bf6e5e029 32 }else{
Dontydonty 8:1b4bf6e5e029 33 int ptr = 0;
Dontydonty 8:1b4bf6e5e029 34 bool EOL = false;
Dontydonty 8:1b4bf6e5e029 35 pc->printf("AT Command\n\r");
Dontydonty 8:1b4bf6e5e029 36 for(int i =0;i<sizeof(line)&& !EOL; i++){
Dontydonty 8:1b4bf6e5e029 37 if(!delimiterFound){
Dontydonty 8:1b4bf6e5e029 38 delimiterFound = (line[i]=='=');
Dontydonty 8:1b4bf6e5e029 39 pc->printf("%c",line[i]);
Dontydonty 8:1b4bf6e5e029 40 }else{
Dontydonty 8:1b4bf6e5e029 41
Dontydonty 8:1b4bf6e5e029 42 // Send command
Dontydonty 8:1b4bf6e5e029 43 char *cmd = NULL;
Dontydonty 8:1b4bf6e5e029 44 char *params = NULL;
Dontydonty 8:1b4bf6e5e029 45 int cmd_len = 0;
Dontydonty 8:1b4bf6e5e029 46 int param_len = 0;
Dontydonty 8:1b4bf6e5e029 47
Dontydonty 8:1b4bf6e5e029 48 for( ;i<sizeof(line)&& line[i]!= '\0'; i=i+2){
Dontydonty 8:1b4bf6e5e029 49 unsigned char c = ahex2bin(line[i],line[i+1]);
Dontydonty 8:1b4bf6e5e029 50 if(c == 0xDA){
Dontydonty 8:1b4bf6e5e029 51 EOL =true;
Dontydonty 8:1b4bf6e5e029 52 }else if(cmd_len <2){
Dontydonty 8:1b4bf6e5e029 53 cmd = (char*)realloc(cmd ,sizeof(char)*(cmd_len+1));
Dontydonty 8:1b4bf6e5e029 54 cmd[cmd_len++] = c;
Dontydonty 8:1b4bf6e5e029 55 }else{
Dontydonty 8:1b4bf6e5e029 56 params = (char*)realloc(params ,sizeof(char)*(param_len+1));
Dontydonty 8:1b4bf6e5e029 57 params[param_len++] = c;
Dontydonty 8:1b4bf6e5e029 58 }
Dontydonty 8:1b4bf6e5e029 59 }
Dontydonty 8:1b4bf6e5e029 60
Dontydonty 8:1b4bf6e5e029 61 do{
Dontydonty 8:1b4bf6e5e029 62 bool exit = false;
Dontydonty 8:1b4bf6e5e029 63 sendAT(xbee, pc, cmd, params,param_len);
Dontydonty 8:1b4bf6e5e029 64 wait_ms(200);
Dontydonty 8:1b4bf6e5e029 65 ATWR (xbee, pc);
Dontydonty 8:1b4bf6e5e029 66 wait_ms(200);
Dontydonty 8:1b4bf6e5e029 67 apply(xbee, pc);
Dontydonty 8:1b4bf6e5e029 68 wait_ms(200);
Dontydonty 8:1b4bf6e5e029 69 // Verify command status
Dontydonty 8:1b4bf6e5e029 70 char status = getStatusResponse(xbee, pc);
Dontydonty 8:1b4bf6e5e029 71 if(status == 0x00){
Dontydonty 8:1b4bf6e5e029 72 pc->printf("\n\rStatus OK");
Dontydonty 8:1b4bf6e5e029 73 exit =true;
Dontydonty 8:1b4bf6e5e029 74 }else{
Dontydonty 8:1b4bf6e5e029 75 if(status==0x02){
Dontydonty 8:1b4bf6e5e029 76 pc->printf("\n\Invalid Command %d\n\r",status);
Dontydonty 8:1b4bf6e5e029 77 }
Dontydonty 8:1b4bf6e5e029 78 toggleError(xbee, pc, 200, "Warning status fail");
Dontydonty 8:1b4bf6e5e029 79 maxRetry--;
Dontydonty 8:1b4bf6e5e029 80 }
Dontydonty 8:1b4bf6e5e029 81 }while(maxRetry>0 && !exit);
Dontydonty 8:1b4bf6e5e029 82 free(cmd);
Dontydonty 8:1b4bf6e5e029 83 free(params);
Dontydonty 8:1b4bf6e5e029 84
Dontydonty 8:1b4bf6e5e029 85 EOL =true;
Dontydonty 8:1b4bf6e5e029 86 }
Dontydonty 8:1b4bf6e5e029 87 }
Dontydonty 8:1b4bf6e5e029 88 }
Dontydonty 8:1b4bf6e5e029 89 }
Dontydonty 8:1b4bf6e5e029 90 fclose(file);
Dontydonty 8:1b4bf6e5e029 91 pc->printf("\n\rClose file");
Dontydonty 8:1b4bf6e5e029 92 return 0;
Dontydonty 8:1b4bf6e5e029 93 }
Dontydonty 8:1b4bf6e5e029 94
Dontydonty 8:1b4bf6e5e029 95
Dontydonty 8:1b4bf6e5e029 96 unsigned char ahex2bin (unsigned char MSB, unsigned char LSB){
Dontydonty 8:1b4bf6e5e029 97 if (MSB > '9') MSB -= 7; // Convert MSB (0x30..0x3F)
Dontydonty 8:1b4bf6e5e029 98 if (LSB > '9') LSB -= 7; // Convert LSB (0x30..0x3F)
Dontydonty 8:1b4bf6e5e029 99 return (MSB << 4) | (LSB & 0x0F);
Dontydonty 8:1b4bf6e5e029 100 }
Dontydonty 8:1b4bf6e5e029 101