- A fork of Damien's program.

Dependencies:   WebSocketClient WiflyInterface mbed messages

Fork of BatteryModelTester by Damien Frost

Committer:
defrost
Date:
Thu Nov 24 16:54:56 2016 +0000
Revision:
8:5afd599875e4
Parent:
5:0c7d131e6089
Child:
9:9b6a54e63942
- Updated messages.h to its own library

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 "main.cpp"
defrost 5:0c7d131e6089 27 *
defrost 5:0c7d131e6089 28 * @section DESCRIPTION
defrost 5:0c7d131e6089 29 * Simple Internet of Things main program. The device sends data every 3
defrost 5:0c7d131e6089 30 * seconds, and can receive data from a server.
defrost 5:0c7d131e6089 31 *
defrost 5:0c7d131e6089 32 */
defrost 5:0c7d131e6089 33
defrost 0:c5607b31fb07 34 #include "mbed.h"
defrost 1:4403f2ed1c1f 35 #include "globals.h"
defrost 0:c5607b31fb07 36 #include "WiflyInterface.h"
defrost 1:4403f2ed1c1f 37 #include "Commands.h"
defrost 0:c5607b31fb07 38 #include "Websocket.h"
defrost 1:4403f2ed1c1f 39 #include "ADC.h"
defrost 0:c5607b31fb07 40
defrost 2:7abdaa5a9209 41 //#define DEBUG
defrost 0:c5607b31fb07 42 #define INFOMESSAGES
defrost 0:c5607b31fb07 43 #define WARNMESSAGES
defrost 0:c5607b31fb07 44 #define ERRMESSAGES
defrost 0:c5607b31fb07 45 #define FUNCNAME "IoT"
defrost 0:c5607b31fb07 46 #include "messages.h"
defrost 0:c5607b31fb07 47
defrost 5:0c7d131e6089 48
defrost 0:c5607b31fb07 49 // Main Loop!
defrost 0:c5607b31fb07 50 int main() {
defrost 0:c5607b31fb07 51 unsigned int wifi_cmd = NO_WIFI_CMD;
defrost 0:c5607b31fb07 52 float wifi_data = 0.0f;
defrost 2:7abdaa5a9209 53 unsigned int ADCRaw;
defrost 5:0c7d131e6089 54 char msg[128];
defrost 0:c5607b31fb07 55
defrost 0:c5607b31fb07 56 // Set the IoT ID:
defrost 8:5afd599875e4 57 IoT_ID = 5;
defrost 0:c5607b31fb07 58
defrost 3:f20e114eb2ee 59 // Set the Auto reconnect flag:
defrost 3:f20e114eb2ee 60 IotStatus.SetFlag(SF_AUTOCONNECT);
defrost 3:f20e114eb2ee 61
defrost 0:c5607b31fb07 62 // Send a startup message to serial port:
defrost 1:4403f2ed1c1f 63 INFO("");
defrost 1:4403f2ed1c1f 64 INFO("");
defrost 0:c5607b31fb07 65 INFO("Starting up...");
defrost 0:c5607b31fb07 66 INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock);
defrost 5:0c7d131e6089 67
defrost 5:0c7d131e6089 68 // Configure the ADC to sample the internal temperature sensor. You cannot
defrost 5:0c7d131e6089 69 // use AnalogIn() unfortunately...
defrost 2:7abdaa5a9209 70 ConfigureADC();
defrost 5:0c7d131e6089 71
defrost 5:0c7d131e6089 72 // Connect to the wifi network. It will basically get stuck here until it
defrost 5:0c7d131e6089 73 // connects to the network.
defrost 0:c5607b31fb07 74 SetupNetwork(5000);
defrost 5:0c7d131e6089 75
defrost 0:c5607b31fb07 76 // Configure the baud rate of the wifi shield:
defrost 5:0c7d131e6089 77 // This will make our wireless transmissions much faster.
defrost 0:c5607b31fb07 78 ws.setBaud(115200);
defrost 0:c5607b31fb07 79 wait(0.5f);
defrost 0:c5607b31fb07 80
defrost 5:0c7d131e6089 81 // Check to see we are connected to the network:
defrost 0:c5607b31fb07 82 if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){
defrost 5:0c7d131e6089 83 // Try to connect to the WebSocket server:
defrost 0:c5607b31fb07 84 sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT);
defrost 0:c5607b31fb07 85 ws.Initialize(msg);
defrost 0:c5607b31fb07 86 INFO("Connecting to Websocket Server on %s...", msg);
defrost 0:c5607b31fb07 87 if(ws.connect()){
defrost 0:c5607b31fb07 88 // Set a status flag:
defrost 0:c5607b31fb07 89 INFO("Connected.");
defrost 0:c5607b31fb07 90 IotStatus.SetFlag(SF_SERVERCONNECTED);
defrost 0:c5607b31fb07 91 }else{
defrost 0:c5607b31fb07 92 // We could not connect right now..
defrost 0:c5607b31fb07 93 IotStatus.ClearFlag(SF_SERVERCONNECTED);
defrost 0:c5607b31fb07 94 INFO("Could not connect to server, will try again later.");
defrost 0:c5607b31fb07 95 ReconnectAttempts++;
defrost 0:c5607b31fb07 96 }
defrost 0:c5607b31fb07 97 }
defrost 0:c5607b31fb07 98
defrost 5:0c7d131e6089 99 // Start the display timer which will send data to the server every
defrost 5:0c7d131e6089 100 // 3 seconds.
defrost 5:0c7d131e6089 101 DisplayTimer.start();
defrost 3:f20e114eb2ee 102
defrost 5:0c7d131e6089 103 // Inifinite main loop:
defrost 0:c5607b31fb07 104 while(1) {
defrost 0:c5607b31fb07 105
defrost 0:c5607b31fb07 106 // Process the wifi command:
defrost 0:c5607b31fb07 107 if(wifi_cmd > NO_WIFI_CMD){
defrost 5:0c7d131e6089 108 // Modify the desired variable:
defrost 5:0c7d131e6089 109 ModifyVariable(wifi_cmd, wifi_data);
defrost 5:0c7d131e6089 110 // Reset the command:
defrost 0:c5607b31fb07 111 wifi_cmd = NO_WIFI_CMD;
defrost 0:c5607b31fb07 112 }
defrost 0:c5607b31fb07 113
defrost 0:c5607b31fb07 114 // Check for new wifi data:
defrost 0:c5607b31fb07 115 if((wifi_cmd == NO_WIFI_CMD)){
defrost 5:0c7d131e6089 116 ReceiveNetworkData(&wifi_cmd, &wifi_data);
defrost 0:c5607b31fb07 117 }
defrost 0:c5607b31fb07 118
defrost 0:c5607b31fb07 119 // Send the network data every 3 seconds:
defrost 0:c5607b31fb07 120 if(DisplayTimer.read()>(3.0f)){
defrost 1:4403f2ed1c1f 121 // Sample the internal temperature sensor:
defrost 1:4403f2ed1c1f 122 STARTADCCONVERSION;
defrost 5:0c7d131e6089 123 // Wait for the conversion to complete:
defrost 1:4403f2ed1c1f 124 while(!ADCCONVERSIONCOMPLETE);
defrost 5:0c7d131e6089 125 // Save the raw value from the ADC:
defrost 2:7abdaa5a9209 126 ADCRaw = ADC1->DR;
defrost 5:0c7d131e6089 127 // Calculate the temperature using information from the datasheet:
defrost 2:7abdaa5a9209 128 TempSensor = ((((float)ADCRaw)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f;
defrost 5:0c7d131e6089 129 // Output the result:
defrost 1:4403f2ed1c1f 130 DBG("TempSensor = %.5f", TempSensor);
defrost 2:7abdaa5a9209 131 DBG("ADC1->DR = %d", ADCRaw);
defrost 0:c5607b31fb07 132
defrost 0:c5607b31fb07 133 // Send data over network:
defrost 0:c5607b31fb07 134 SendNetworkData();
defrost 0:c5607b31fb07 135
defrost 0:c5607b31fb07 136 // Increment a counter:
defrost 0:c5607b31fb07 137 SendCounter++;
defrost 0:c5607b31fb07 138
defrost 0:c5607b31fb07 139 // Reset the timer:
defrost 0:c5607b31fb07 140 DisplayTimer.reset();
defrost 2:7abdaa5a9209 141
defrost 0:c5607b31fb07 142 }
defrost 5:0c7d131e6089 143 } // while(1)
defrost 5:0c7d131e6089 144 } // main()