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