Skeleton program for Federico's 4YP project.

Dependencies:   WebSocketClient WiflyInterface mbed messages

Fork of IoT_Ex by Damien Frost

Committer:
defrost
Date:
Tue Nov 29 15:02:01 2016 +0000
Revision:
10:e8b66718a103
Parent:
8:5afd599875e4
- Works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
defrost 5:0c7d131e6089 1 /**
defrost 5:0c7d131e6089 2 * @author Damien Frost
defrost 5:0c7d131e6089 3 *
defrost 5:0c7d131e6089 4 * @section LICENSE
defrost 5:0c7d131e6089 5 *
defrost 5:0c7d131e6089 6 * Copyright (c) 2016 Damien Frost
defrost 5:0c7d131e6089 7 *
defrost 5:0c7d131e6089 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
defrost 5:0c7d131e6089 9 * of this software and associated documentation files (the "Software"), to deal
defrost 5:0c7d131e6089 10 * in the Software without restriction, including without limitation the rights
defrost 5:0c7d131e6089 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
defrost 5:0c7d131e6089 12 * copies of the Software, and to permit persons to whom the Software is
defrost 5:0c7d131e6089 13 * furnished to do so, subject to the following conditions:
defrost 5:0c7d131e6089 14 *
defrost 5:0c7d131e6089 15 * The above copyright notice and this permission notice shall be included in
defrost 5:0c7d131e6089 16 * all copies or substantial portions of the Software.
defrost 5:0c7d131e6089 17 *
defrost 5:0c7d131e6089 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
defrost 5:0c7d131e6089 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
defrost 5:0c7d131e6089 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
defrost 5:0c7d131e6089 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
defrost 5:0c7d131e6089 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
defrost 5:0c7d131e6089 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
defrost 5:0c7d131e6089 24 * THE SOFTWARE.
defrost 5:0c7d131e6089 25 *
defrost 5:0c7d131e6089 26 * @file "globals.cpp"
defrost 5:0c7d131e6089 27 *
defrost 5:0c7d131e6089 28 * @section DESCRIPTION
defrost 5:0c7d131e6089 29 * Global definitions for the IoT example program.
defrost 5:0c7d131e6089 30 *
defrost 5:0c7d131e6089 31 */
defrost 1:4403f2ed1c1f 32
defrost 1:4403f2ed1c1f 33 #include "mbed.h"
defrost 1:4403f2ed1c1f 34 #include "globals.h"
defrost 10:e8b66718a103 35 #include "pwm.h"
defrost 1:4403f2ed1c1f 36
defrost 10:e8b66718a103 37 #define DEBUG
defrost 1:4403f2ed1c1f 38 #define INFOMESSAGES
defrost 1:4403f2ed1c1f 39 #define WARNMESSAGES
defrost 1:4403f2ed1c1f 40 #define ERRMESSAGES
defrost 1:4403f2ed1c1f 41 #define FUNCNAME "GBL"
defrost 1:4403f2ed1c1f 42 #include "messages.h"
defrost 1:4403f2ed1c1f 43
defrost 5:0c7d131e6089 44 // Hardware declarations:
defrost 1:4403f2ed1c1f 45 Serial pc(USBTX, USBRX);
defrost 1:4403f2ed1c1f 46 InterruptIn UIBut1(USER_BUTTON);
defrost 1:4403f2ed1c1f 47 Timer DisplayTimer;
defrost 8:5afd599875e4 48 DigitalOut Led(D5);
defrost 1:4403f2ed1c1f 49
defrost 5:0c7d131e6089 50 // Global variable declarations:
defrost 1:4403f2ed1c1f 51 int ReconnectAttempts = 0;
defrost 1:4403f2ed1c1f 52 int SendCounter = 0;
defrost 1:4403f2ed1c1f 53 extern int IoT_ID = 0;
defrost 1:4403f2ed1c1f 54 float TempSensor = 0.0f;
defrost 10:e8b66718a103 55 float VoltageMeasurement = 0.0f;
defrost 10:e8b66718a103 56 float CurrentMeasurement = 0.0f;
defrost 10:e8b66718a103 57 float PwmPeriod_us = 20.0f;
defrost 10:e8b66718a103 58 float Duty_us = 10.0f;
defrost 8:5afd599875e4 59 char* wifissid = "SC";
defrost 8:5afd599875e4 60 char* wifipassword = "smartcellshield";
defrost 10:e8b66718a103 61 DigitalOut db(PC_10);
defrost 10:e8b66718a103 62 AnalogIn VoltageSensor(VOLTAGE_SENSOR_PIN);
defrost 10:e8b66718a103 63 AnalogIn CurrentSensor(CURRENT_SENSOR_PIN);
defrost 10:e8b66718a103 64
defrost 1:4403f2ed1c1f 65
defrost 5:0c7d131e6089 66 // Wifily interface declaration:
defrost 8:5afd599875e4 67 WiflyInterface eth(D8, D2, D6, LED1, wifissid, wifipassword, WPA2);
defrost 5:0c7d131e6089 68 // WebSocket declaration:
defrost 1:4403f2ed1c1f 69 Websocket ws;
defrost 1:4403f2ed1c1f 70
defrost 1:4403f2ed1c1f 71
defrost 1:4403f2ed1c1f 72 int SetupNetwork(int Tries){
defrost 1:4403f2ed1c1f 73 // Initialize the interface.
defrost 1:4403f2ed1c1f 74 // If no param is passed to init() then DHCP will be used on connect()
defrost 1:4403f2ed1c1f 75 int s = eth.init();
defrost 1:4403f2ed1c1f 76 int attempts = 1;
defrost 1:4403f2ed1c1f 77
defrost 1:4403f2ed1c1f 78 wait(1);
defrost 1:4403f2ed1c1f 79 if (s != NULL) {
defrost 1:4403f2ed1c1f 80 ERR("Could not initialise. Halting!");
defrost 1:4403f2ed1c1f 81 exit(0);
defrost 1:4403f2ed1c1f 82 }
defrost 1:4403f2ed1c1f 83
defrost 1:4403f2ed1c1f 84 INFO("Connecting to: %s", wifissid);
defrost 1:4403f2ed1c1f 85 DBG("Getting IP address...");
defrost 1:4403f2ed1c1f 86
defrost 1:4403f2ed1c1f 87 while (1) {
defrost 1:4403f2ed1c1f 88 // Connect to network:
defrost 1:4403f2ed1c1f 89 s = eth.connect();
defrost 1:4403f2ed1c1f 90 // If connection fails, retry for 5 attempts:
defrost 1:4403f2ed1c1f 91 if (s == false || s < 0) {
defrost 1:4403f2ed1c1f 92 INFO("Could not connect to network. Retrying!");
defrost 1:4403f2ed1c1f 93 attempts++;
defrost 1:4403f2ed1c1f 94 wait(1);
defrost 1:4403f2ed1c1f 95 } else {
defrost 1:4403f2ed1c1f 96
defrost 1:4403f2ed1c1f 97 break;
defrost 1:4403f2ed1c1f 98 }
defrost 1:4403f2ed1c1f 99 if(attempts > Tries){
defrost 1:4403f2ed1c1f 100 ERR("Network connection failed after %d attempts", Tries);
defrost 1:4403f2ed1c1f 101 return 0;
defrost 1:4403f2ed1c1f 102 }
defrost 1:4403f2ed1c1f 103 }
defrost 1:4403f2ed1c1f 104 INFO("Connected to: %s", wifissid);
defrost 1:4403f2ed1c1f 105 INFO("Got IP address: %s", eth.getIPAddress());
defrost 1:4403f2ed1c1f 106 IotStatus.SetFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 107 return 1;
defrost 1:4403f2ed1c1f 108
defrost 1:4403f2ed1c1f 109 }
defrost 1:4403f2ed1c1f 110
defrost 1:4403f2ed1c1f 111 void SendNetworkData(void){
defrost 1:4403f2ed1c1f 112 char msg_buffer[CHARMSGBUFF];
defrost 1:4403f2ed1c1f 113 int intresult;
defrost 1:4403f2ed1c1f 114
defrost 1:4403f2ed1c1f 115 if(IotStatus.CheckFlag(SF_SERVERCONNECTED)){
defrost 10:e8b66718a103 116 sprintf(msg_buffer, "%d,%d,%.5f,%.5f,%.5f,%.5f", IoT_ID, SendCounter,TempSensor, VoltageMeasurement, CurrentMeasurement, PwmPeriod_us);
defrost 1:4403f2ed1c1f 117 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 1:4403f2ed1c1f 118 intresult = ws.send(msg_buffer);
defrost 1:4403f2ed1c1f 119 }else{
defrost 1:4403f2ed1c1f 120 intresult = -1;
defrost 1:4403f2ed1c1f 121 }
defrost 1:4403f2ed1c1f 122 DBG("intresult: %d", intresult);
defrost 1:4403f2ed1c1f 123
defrost 1:4403f2ed1c1f 124 if(intresult < 0){
defrost 1:4403f2ed1c1f 125 // Clear a status flag:
defrost 1:4403f2ed1c1f 126 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 127 // Check to see if the wireless is still connected:
defrost 1:4403f2ed1c1f 128 DBG("Checking network status...");
defrost 1:4403f2ed1c1f 129 if(eth.checkNetworkStatus() != 3){
defrost 1:4403f2ed1c1f 130 IotStatus.ClearFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 131 // Connect to the wireless network:
defrost 1:4403f2ed1c1f 132 if(IotStatus.CheckFlag(SF_AUTOCONNECT)){
defrost 1:4403f2ed1c1f 133 INFO("Reconnecting to Network...");
defrost 1:4403f2ed1c1f 134 if(SetupNetwork(1)>0){
defrost 1:4403f2ed1c1f 135 IotStatus.SetFlag(SF_WIRELESSCONNECTED);
defrost 1:4403f2ed1c1f 136 INFO("Connected to Network.");
defrost 1:4403f2ed1c1f 137 }else{
defrost 1:4403f2ed1c1f 138 WARN("Could not re-connect to the wireless network.");
defrost 1:4403f2ed1c1f 139 }
defrost 1:4403f2ed1c1f 140 }
defrost 1:4403f2ed1c1f 141 }else{
defrost 1:4403f2ed1c1f 142 DBG("Network connected.");
defrost 1:4403f2ed1c1f 143 }
defrost 1:4403f2ed1c1f 144
defrost 1:4403f2ed1c1f 145 if(IotStatus.CheckFlag(SF_AUTOCONNECT) && IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
defrost 1:4403f2ed1c1f 146 // Server connection was closed, try to reconnect:
defrost 1:4403f2ed1c1f 147 INFO("Reconnecting to Websocket Server on ws://%s:%d/ws...", SERVER_IP, WS_PORT);
defrost 1:4403f2ed1c1f 148 if(!ws.connect()){
defrost 1:4403f2ed1c1f 149 WARN("Could not connect to the server again...");
defrost 1:4403f2ed1c1f 150 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 151 ReconnectAttempts++;
defrost 1:4403f2ed1c1f 152 if(ReconnectAttempts > 4){
defrost 1:4403f2ed1c1f 153 INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts);
defrost 1:4403f2ed1c1f 154 SetupNetwork(1);
defrost 1:4403f2ed1c1f 155 ReconnectAttempts = 0;
defrost 1:4403f2ed1c1f 156 }
defrost 1:4403f2ed1c1f 157 }else{
defrost 1:4403f2ed1c1f 158 INFO("Connected to ws://%s:%d/ws", SERVER_IP, WS_PORT);
defrost 1:4403f2ed1c1f 159 // Set a status flag:
defrost 1:4403f2ed1c1f 160 IotStatus.SetFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 161 }
defrost 1:4403f2ed1c1f 162 }
defrost 1:4403f2ed1c1f 163 }
defrost 1:4403f2ed1c1f 164
defrost 1:4403f2ed1c1f 165 return;
defrost 1:4403f2ed1c1f 166 }
defrost 1:4403f2ed1c1f 167
defrost 5:0c7d131e6089 168 void ReceiveNetworkData(unsigned int * wifi_cmd, float * value){
defrost 1:4403f2ed1c1f 169 char msg_buffer[CHARMSGBUFF];
defrost 1:4403f2ed1c1f 170 char msg_buffer2[CHARMSGBUFF];
defrost 2:7abdaa5a9209 171 int resp;
defrost 2:7abdaa5a9209 172 if(IotStatus.CheckFlag(SF_SERVERCONNECTED)){
defrost 2:7abdaa5a9209 173 // Check for data on the websocket:
defrost 2:7abdaa5a9209 174 resp = ws.readmsg(msg_buffer);
defrost 2:7abdaa5a9209 175 if(resp == 1){
defrost 2:7abdaa5a9209 176 INFO("Received: %s", msg_buffer);
defrost 2:7abdaa5a9209 177 sscanf(msg_buffer, "%d,%s", wifi_cmd, msg_buffer2);
defrost 5:0c7d131e6089 178 if(*wifi_cmd == CV_LED_WIFI_CMD){
defrost 3:f20e114eb2ee 179 // Get one more value:
defrost 2:7abdaa5a9209 180 sscanf(msg_buffer2, "%f", value);
defrost 2:7abdaa5a9209 181 }
defrost 10:e8b66718a103 182 if(*wifi_cmd == CV_PWM_PERIOD_US_CMD){
defrost 10:e8b66718a103 183 // Get one more value:
defrost 10:e8b66718a103 184 sscanf(msg_buffer2, "%f", value);
defrost 10:e8b66718a103 185 }
defrost 2:7abdaa5a9209 186 }else if(resp == -1){
defrost 2:7abdaa5a9209 187 // Connection to the server is lost:
defrost 2:7abdaa5a9209 188 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 1:4403f2ed1c1f 189 }else{
defrost 2:7abdaa5a9209 190 //DBG("Did not receive anything :(\n\r");
defrost 2:7abdaa5a9209 191 *wifi_cmd = NO_WIFI_CMD;
defrost 2:7abdaa5a9209 192 *value = 0.0f;
defrost 1:4403f2ed1c1f 193 }
defrost 1:4403f2ed1c1f 194 }
defrost 1:4403f2ed1c1f 195 return;
defrost 1:4403f2ed1c1f 196 }
defrost 1:4403f2ed1c1f 197
defrost 1:4403f2ed1c1f 198 void ModifyVariable(unsigned int wifi_var, float wifi_data){
defrost 1:4403f2ed1c1f 199 // modifies something in the SCS Controller:
defrost 1:4403f2ed1c1f 200 switch(wifi_var){
defrost 10:e8b66718a103 201 case CV_LED_WIFI_CMD:
defrost 3:f20e114eb2ee 202 if(wifi_data > 0){
defrost 3:f20e114eb2ee 203 Led = 1;
defrost 3:f20e114eb2ee 204 }else{
defrost 3:f20e114eb2ee 205 Led = 0;
defrost 3:f20e114eb2ee 206 }
defrost 1:4403f2ed1c1f 207 break;
defrost 10:e8b66718a103 208 case CV_PWM_PERIOD_US_CMD:
defrost 10:e8b66718a103 209 DBG("wifi_data: %.3f", wifi_data);
defrost 10:e8b66718a103 210 PwmPeriod_us = wifi_data;
defrost 10:e8b66718a103 211 SetPWMPeriodAndDuty_us(PwmPeriod_us);
defrost 10:e8b66718a103 212 break;
defrost 10:e8b66718a103 213
defrost 1:4403f2ed1c1f 214
defrost 1:4403f2ed1c1f 215 default:
defrost 1:4403f2ed1c1f 216 break;
defrost 1:4403f2ed1c1f 217 }
defrost 1:4403f2ed1c1f 218 return;
defrost 1:4403f2ed1c1f 219 }
defrost 1:4403f2ed1c1f 220
defrost 1:4403f2ed1c1f 221
defrost 1:4403f2ed1c1f 222