Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of IoT_Ex by
source/iQ_globals.cpp@0:c5607b31fb07, 2016-10-04 (annotated)
- Committer:
- defrost
- Date:
- Tue Oct 04 11:34:47 2016 +0000
- Revision:
- 0:c5607b31fb07
- Initial Commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| defrost | 0:c5607b31fb07 | 1 | // ****************** |
| defrost | 0:c5607b31fb07 | 2 | // * iQ_globals.cpp * |
| defrost | 0:c5607b31fb07 | 3 | // ****************** |
| defrost | 0:c5607b31fb07 | 4 | // |
| defrost | 0:c5607b31fb07 | 5 | // Created: 2015/03/19 |
| defrost | 0:c5607b31fb07 | 6 | // By: Damien Frost |
| defrost | 0:c5607b31fb07 | 7 | |
| defrost | 0:c5607b31fb07 | 8 | #include "mbed.h" |
| defrost | 0:c5607b31fb07 | 9 | #include "iQ_globals.h" |
| defrost | 0:c5607b31fb07 | 10 | |
| defrost | 0:c5607b31fb07 | 11 | //#define DEBUG |
| defrost | 0:c5607b31fb07 | 12 | #define INFOMESSAGES |
| defrost | 0:c5607b31fb07 | 13 | #define WARNMESSAGES |
| defrost | 0:c5607b31fb07 | 14 | #define ERRMESSAGES |
| defrost | 0:c5607b31fb07 | 15 | #define FUNCNAME "GBL" |
| defrost | 0:c5607b31fb07 | 16 | #include "messages.h" |
| defrost | 0:c5607b31fb07 | 17 | |
| defrost | 0:c5607b31fb07 | 18 | char* wifissid = "SC"; |
| defrost | 0:c5607b31fb07 | 19 | char* wifipassword = "smartcellshield"; |
| defrost | 0:c5607b31fb07 | 20 | |
| defrost | 0:c5607b31fb07 | 21 | Serial pc(USBTX, USBRX); |
| defrost | 0:c5607b31fb07 | 22 | InterruptIn UIBut1(USER_BUTTON); |
| defrost | 0:c5607b31fb07 | 23 | Timer DisplayTimer; |
| defrost | 0:c5607b31fb07 | 24 | |
| defrost | 0:c5607b31fb07 | 25 | WiflyInterface eth(D8, D2, D6, LED1, wifissid, wifipassword, WPA2); |
| defrost | 0:c5607b31fb07 | 26 | |
| defrost | 0:c5607b31fb07 | 27 | int ReconnectAttempts = 0; |
| defrost | 0:c5607b31fb07 | 28 | int SendCounter = 0; |
| defrost | 0:c5607b31fb07 | 29 | extern int IoT_ID = 0; |
| defrost | 0:c5607b31fb07 | 30 | |
| defrost | 0:c5607b31fb07 | 31 | Websocket ws; |
| defrost | 0:c5607b31fb07 | 32 | |
| defrost | 0:c5607b31fb07 | 33 | |
| defrost | 0:c5607b31fb07 | 34 | int SetupNetwork(int Tries){ |
| defrost | 0:c5607b31fb07 | 35 | // Initialize the interface. |
| defrost | 0:c5607b31fb07 | 36 | // If no param is passed to init() then DHCP will be used on connect() |
| defrost | 0:c5607b31fb07 | 37 | int s = eth.init(); |
| defrost | 0:c5607b31fb07 | 38 | int attempts = 1; |
| defrost | 0:c5607b31fb07 | 39 | |
| defrost | 0:c5607b31fb07 | 40 | wait(1); |
| defrost | 0:c5607b31fb07 | 41 | if (s != NULL) { |
| defrost | 0:c5607b31fb07 | 42 | ERR("Could not initialise. Halting!"); |
| defrost | 0:c5607b31fb07 | 43 | exit(0); |
| defrost | 0:c5607b31fb07 | 44 | } |
| defrost | 0:c5607b31fb07 | 45 | |
| defrost | 0:c5607b31fb07 | 46 | INFO("Connecting to: %s", wifissid); |
| defrost | 0:c5607b31fb07 | 47 | DBG("Getting IP address..."); |
| defrost | 0:c5607b31fb07 | 48 | |
| defrost | 0:c5607b31fb07 | 49 | while (1) { |
| defrost | 0:c5607b31fb07 | 50 | // Connect to network: |
| defrost | 0:c5607b31fb07 | 51 | s = eth.connect(); |
| defrost | 0:c5607b31fb07 | 52 | // If connection fails, retry for 5 attempts: |
| defrost | 0:c5607b31fb07 | 53 | if (s == false || s < 0) { |
| defrost | 0:c5607b31fb07 | 54 | INFO("Could not connect to network. Retrying!"); |
| defrost | 0:c5607b31fb07 | 55 | attempts++; |
| defrost | 0:c5607b31fb07 | 56 | wait(1); |
| defrost | 0:c5607b31fb07 | 57 | } else { |
| defrost | 0:c5607b31fb07 | 58 | |
| defrost | 0:c5607b31fb07 | 59 | break; |
| defrost | 0:c5607b31fb07 | 60 | } |
| defrost | 0:c5607b31fb07 | 61 | if(attempts > Tries){ |
| defrost | 0:c5607b31fb07 | 62 | ERR("Network connection failed after %d attempts", Tries); |
| defrost | 0:c5607b31fb07 | 63 | return 0; |
| defrost | 0:c5607b31fb07 | 64 | } |
| defrost | 0:c5607b31fb07 | 65 | } |
| defrost | 0:c5607b31fb07 | 66 | INFO("Connected to: %s", wifissid); |
| defrost | 0:c5607b31fb07 | 67 | INFO("Got IP address: %s", eth.getIPAddress()); |
| defrost | 0:c5607b31fb07 | 68 | IotStatus.SetFlag(SF_WIRELESSCONNECTED); |
| defrost | 0:c5607b31fb07 | 69 | return 1; |
| defrost | 0:c5607b31fb07 | 70 | |
| defrost | 0:c5607b31fb07 | 71 | } |
| defrost | 0:c5607b31fb07 | 72 | |
| defrost | 0:c5607b31fb07 | 73 | void SendNetworkData(void){ |
| defrost | 0:c5607b31fb07 | 74 | char msg_buffer[CHARMSGBUFF]; |
| defrost | 0:c5607b31fb07 | 75 | int intresult; |
| defrost | 0:c5607b31fb07 | 76 | |
| defrost | 0:c5607b31fb07 | 77 | if(IotStatus.CheckFlag(SF_SERVERCONNECTED)){ |
| defrost | 0:c5607b31fb07 | 78 | sprintf(msg_buffer, "%d,%d", IoT_ID, SendCounter); |
| defrost | 0:c5607b31fb07 | 79 | INFO("Sending: %s", msg_buffer); // When this line is commented out, the mbed never tries to reconnect to the server after one try. SUPER. Keeping this here also uses precious CPU time |
| defrost | 0:c5607b31fb07 | 80 | intresult = ws.send(msg_buffer); |
| defrost | 0:c5607b31fb07 | 81 | }else{ |
| defrost | 0:c5607b31fb07 | 82 | intresult = -1; |
| defrost | 0:c5607b31fb07 | 83 | } |
| defrost | 0:c5607b31fb07 | 84 | DBG("intresult: %d", intresult); |
| defrost | 0:c5607b31fb07 | 85 | |
| defrost | 0:c5607b31fb07 | 86 | if(intresult < 0){ |
| defrost | 0:c5607b31fb07 | 87 | // Clear a status flag: |
| defrost | 0:c5607b31fb07 | 88 | IotStatus.ClearFlag(SF_SERVERCONNECTED); |
| defrost | 0:c5607b31fb07 | 89 | // Check to see if the wireless is still connected: |
| defrost | 0:c5607b31fb07 | 90 | DBG("Checking network status..."); |
| defrost | 0:c5607b31fb07 | 91 | if(eth.checkNetworkStatus() != 3){ |
| defrost | 0:c5607b31fb07 | 92 | IotStatus.ClearFlag(SF_WIRELESSCONNECTED); |
| defrost | 0:c5607b31fb07 | 93 | // Connect to the wireless network: |
| defrost | 0:c5607b31fb07 | 94 | if(IotStatus.CheckFlag(SF_AUTOCONNECT)){ |
| defrost | 0:c5607b31fb07 | 95 | INFO("Reconnecting to Network..."); |
| defrost | 0:c5607b31fb07 | 96 | if(SetupNetwork(1)>0){ |
| defrost | 0:c5607b31fb07 | 97 | IotStatus.SetFlag(SF_WIRELESSCONNECTED); |
| defrost | 0:c5607b31fb07 | 98 | INFO("Connected to Network."); |
| defrost | 0:c5607b31fb07 | 99 | }else{ |
| defrost | 0:c5607b31fb07 | 100 | WARN("Could not re-connect to the wireless network."); |
| defrost | 0:c5607b31fb07 | 101 | } |
| defrost | 0:c5607b31fb07 | 102 | } |
| defrost | 0:c5607b31fb07 | 103 | }else{ |
| defrost | 0:c5607b31fb07 | 104 | DBG("Network connected."); |
| defrost | 0:c5607b31fb07 | 105 | } |
| defrost | 0:c5607b31fb07 | 106 | |
| defrost | 0:c5607b31fb07 | 107 | if(IotStatus.CheckFlag(SF_AUTOCONNECT) && IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){ |
| defrost | 0:c5607b31fb07 | 108 | // Server connection was closed, try to reconnect: |
| defrost | 0:c5607b31fb07 | 109 | INFO("Reconnecting to Websocket Server on ws://%s:%d/ws...", SERVER_IP, WS_PORT); |
| defrost | 0:c5607b31fb07 | 110 | if(!ws.connect()){ |
| defrost | 0:c5607b31fb07 | 111 | WARN("Could not connect to the server again..."); |
| defrost | 0:c5607b31fb07 | 112 | IotStatus.ClearFlag(SF_SERVERCONNECTED); |
| defrost | 0:c5607b31fb07 | 113 | ReconnectAttempts++; |
| defrost | 0:c5607b31fb07 | 114 | if(ReconnectAttempts > 4){ |
| defrost | 0:c5607b31fb07 | 115 | INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts); |
| defrost | 0:c5607b31fb07 | 116 | SetupNetwork(1); |
| defrost | 0:c5607b31fb07 | 117 | ReconnectAttempts = 0; |
| defrost | 0:c5607b31fb07 | 118 | } |
| defrost | 0:c5607b31fb07 | 119 | }else{ |
| defrost | 0:c5607b31fb07 | 120 | INFO("Connected to ws://%s:%d/ws", SERVER_IP, WS_PORT); |
| defrost | 0:c5607b31fb07 | 121 | // Set a status flag: |
| defrost | 0:c5607b31fb07 | 122 | IotStatus.SetFlag(SF_SERVERCONNECTED); |
| defrost | 0:c5607b31fb07 | 123 | } |
| defrost | 0:c5607b31fb07 | 124 | } |
| defrost | 0:c5607b31fb07 | 125 | } |
| defrost | 0:c5607b31fb07 | 126 | |
| defrost | 0:c5607b31fb07 | 127 | return; |
| defrost | 0:c5607b31fb07 | 128 | } |
| defrost | 0:c5607b31fb07 | 129 | |
| defrost | 0:c5607b31fb07 | 130 | void ReceiveNetworkData(unsigned int * wifi_cmd, unsigned int * var, float * value){ |
| defrost | 0:c5607b31fb07 | 131 | char msg_buffer[CHARMSGBUFF]; |
| defrost | 0:c5607b31fb07 | 132 | char msg_buffer2[CHARMSGBUFF]; |
| defrost | 0:c5607b31fb07 | 133 | // Check for data on the websocket: |
| defrost | 0:c5607b31fb07 | 134 | if(ws.readmsg(msg_buffer)){ |
| defrost | 0:c5607b31fb07 | 135 | INFO("Received: %s", msg_buffer); |
| defrost | 0:c5607b31fb07 | 136 | sscanf(msg_buffer, "%d,%s", wifi_cmd, msg_buffer2); |
| defrost | 0:c5607b31fb07 | 137 | if(*wifi_cmd == CHANGEVAR_WIFI_CMD){ |
| defrost | 0:c5607b31fb07 | 138 | // Get two more values: |
| defrost | 0:c5607b31fb07 | 139 | sscanf(msg_buffer2, "%d,%f", var, value); |
| defrost | 0:c5607b31fb07 | 140 | }else{ |
| defrost | 0:c5607b31fb07 | 141 | // Get one: |
| defrost | 0:c5607b31fb07 | 142 | sscanf(msg_buffer2, "%f", value); |
| defrost | 0:c5607b31fb07 | 143 | } |
| defrost | 0:c5607b31fb07 | 144 | }else{ |
| defrost | 0:c5607b31fb07 | 145 | //DBG("Did not receive anything :(\n\r"); |
| defrost | 0:c5607b31fb07 | 146 | *wifi_cmd = NO_WIFI_CMD; |
| defrost | 0:c5607b31fb07 | 147 | *var = 0; |
| defrost | 0:c5607b31fb07 | 148 | *value = 0.0f; |
| defrost | 0:c5607b31fb07 | 149 | } |
| defrost | 0:c5607b31fb07 | 150 | return; |
| defrost | 0:c5607b31fb07 | 151 | } |
| defrost | 0:c5607b31fb07 | 152 | |
| defrost | 0:c5607b31fb07 | 153 | void ModifyVariable(unsigned int wifi_var, float wifi_data){ |
| defrost | 0:c5607b31fb07 | 154 | // modifies something in the SCS Controller: |
| defrost | 0:c5607b31fb07 | 155 | switch(wifi_var){ |
| defrost | 0:c5607b31fb07 | 156 | case CV_LED: |
| defrost | 0:c5607b31fb07 | 157 | |
| defrost | 0:c5607b31fb07 | 158 | break; |
| defrost | 0:c5607b31fb07 | 159 | |
| defrost | 0:c5607b31fb07 | 160 | default: |
| defrost | 0:c5607b31fb07 | 161 | break; |
| defrost | 0:c5607b31fb07 | 162 | } |
| defrost | 0:c5607b31fb07 | 163 | return; |
| defrost | 0:c5607b31fb07 | 164 | } |
| defrost | 0:c5607b31fb07 | 165 | |
| defrost | 0:c5607b31fb07 | 166 | |
| defrost | 0:c5607b31fb07 | 167 |
