Skeleton program for Federico's 4YP project.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of IoT_Ex by
main.cpp@2:7abdaa5a9209, 2016-10-04 (annotated)
- Committer:
- defrost
- Date:
- Tue Oct 04 13:59:13 2016 +0000
- Revision:
- 2:7abdaa5a9209
- Parent:
- 1:4403f2ed1c1f
- Child:
- 3:f20e114eb2ee
- Internal temperature sensing works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
defrost | 0:c5607b31fb07 | 1 | #include "mbed.h" |
defrost | 1:4403f2ed1c1f | 2 | #include "globals.h" |
defrost | 0:c5607b31fb07 | 3 | #include "WiflyInterface.h" |
defrost | 1:4403f2ed1c1f | 4 | #include "Commands.h" |
defrost | 0:c5607b31fb07 | 5 | #include "Websocket.h" |
defrost | 1:4403f2ed1c1f | 6 | #include "ADC.h" |
defrost | 0:c5607b31fb07 | 7 | |
defrost | 2:7abdaa5a9209 | 8 | //#define DEBUG |
defrost | 0:c5607b31fb07 | 9 | #define INFOMESSAGES |
defrost | 0:c5607b31fb07 | 10 | #define WARNMESSAGES |
defrost | 0:c5607b31fb07 | 11 | #define ERRMESSAGES |
defrost | 0:c5607b31fb07 | 12 | #define FUNCNAME "IoT" |
defrost | 0:c5607b31fb07 | 13 | #include "messages.h" |
defrost | 0:c5607b31fb07 | 14 | |
defrost | 0:c5607b31fb07 | 15 | // Main Loop! |
defrost | 0:c5607b31fb07 | 16 | int main() { |
defrost | 0:c5607b31fb07 | 17 | unsigned int wifi_cmd = NO_WIFI_CMD; |
defrost | 0:c5607b31fb07 | 18 | float wifi_data = 0.0f; |
defrost | 0:c5607b31fb07 | 19 | unsigned int wifi_var = 0; |
defrost | 2:7abdaa5a9209 | 20 | unsigned int ADCRaw; |
defrost | 0:c5607b31fb07 | 21 | |
defrost | 0:c5607b31fb07 | 22 | // Set the IoT ID: |
defrost | 0:c5607b31fb07 | 23 | IoT_ID = 1; |
defrost | 0:c5607b31fb07 | 24 | |
defrost | 0:c5607b31fb07 | 25 | // Send a startup message to serial port: |
defrost | 1:4403f2ed1c1f | 26 | INFO(""); |
defrost | 1:4403f2ed1c1f | 27 | INFO(""); |
defrost | 0:c5607b31fb07 | 28 | INFO("Starting up..."); |
defrost | 0:c5607b31fb07 | 29 | INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); |
defrost | 0:c5607b31fb07 | 30 | |
defrost | 2:7abdaa5a9209 | 31 | ConfigureADC(); |
defrost | 0:c5607b31fb07 | 32 | SetupNetwork(5000); |
defrost | 0:c5607b31fb07 | 33 | // Configure the baud rate of the wifi shield: |
defrost | 0:c5607b31fb07 | 34 | ws.setBaud(115200); |
defrost | 0:c5607b31fb07 | 35 | wait(0.5f); |
defrost | 0:c5607b31fb07 | 36 | |
defrost | 0:c5607b31fb07 | 37 | |
defrost | 0:c5607b31fb07 | 38 | |
defrost | 0:c5607b31fb07 | 39 | char msg[128]; |
defrost | 0:c5607b31fb07 | 40 | if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){ |
defrost | 0:c5607b31fb07 | 41 | sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT); |
defrost | 0:c5607b31fb07 | 42 | ws.Initialize(msg); |
defrost | 0:c5607b31fb07 | 43 | INFO("Connecting to Websocket Server on %s...", msg); |
defrost | 0:c5607b31fb07 | 44 | if(ws.connect()){ |
defrost | 0:c5607b31fb07 | 45 | // Set a status flag: |
defrost | 0:c5607b31fb07 | 46 | INFO("Connected."); |
defrost | 0:c5607b31fb07 | 47 | IotStatus.SetFlag(SF_SERVERCONNECTED); |
defrost | 0:c5607b31fb07 | 48 | }else{ |
defrost | 0:c5607b31fb07 | 49 | // We could not connect right now.. |
defrost | 0:c5607b31fb07 | 50 | IotStatus.ClearFlag(SF_SERVERCONNECTED); |
defrost | 0:c5607b31fb07 | 51 | INFO("Could not connect to server, will try again later."); |
defrost | 0:c5607b31fb07 | 52 | ReconnectAttempts++; |
defrost | 0:c5607b31fb07 | 53 | if(ReconnectAttempts > 5){ |
defrost | 0:c5607b31fb07 | 54 | INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts); |
defrost | 0:c5607b31fb07 | 55 | SetupNetwork(1); |
defrost | 0:c5607b31fb07 | 56 | ReconnectAttempts = 0; |
defrost | 0:c5607b31fb07 | 57 | } |
defrost | 0:c5607b31fb07 | 58 | } |
defrost | 0:c5607b31fb07 | 59 | } |
defrost | 0:c5607b31fb07 | 60 | |
defrost | 0:c5607b31fb07 | 61 | |
defrost | 0:c5607b31fb07 | 62 | DisplayTimer.start(); |
defrost | 0:c5607b31fb07 | 63 | // Inifinite loop: |
defrost | 0:c5607b31fb07 | 64 | while(1) { |
defrost | 0:c5607b31fb07 | 65 | |
defrost | 0:c5607b31fb07 | 66 | // Process the wifi command: |
defrost | 0:c5607b31fb07 | 67 | if(wifi_cmd > NO_WIFI_CMD){ |
defrost | 0:c5607b31fb07 | 68 | switch(wifi_cmd){ |
defrost | 0:c5607b31fb07 | 69 | case CHANGEVAR_WIFI_CMD: |
defrost | 0:c5607b31fb07 | 70 | ModifyVariable(wifi_var, wifi_data); |
defrost | 0:c5607b31fb07 | 71 | break; |
defrost | 0:c5607b31fb07 | 72 | default: |
defrost | 0:c5607b31fb07 | 73 | break; |
defrost | 0:c5607b31fb07 | 74 | } |
defrost | 0:c5607b31fb07 | 75 | wifi_cmd = NO_WIFI_CMD; |
defrost | 0:c5607b31fb07 | 76 | } |
defrost | 0:c5607b31fb07 | 77 | |
defrost | 0:c5607b31fb07 | 78 | // Check for new wifi data: |
defrost | 0:c5607b31fb07 | 79 | if((wifi_cmd == NO_WIFI_CMD)){ |
defrost | 0:c5607b31fb07 | 80 | ReceiveNetworkData(&wifi_cmd, &wifi_var, &wifi_data); |
defrost | 0:c5607b31fb07 | 81 | } |
defrost | 0:c5607b31fb07 | 82 | |
defrost | 0:c5607b31fb07 | 83 | // Send the network data every 3 seconds: |
defrost | 0:c5607b31fb07 | 84 | if(DisplayTimer.read()>(3.0f)){ |
defrost | 1:4403f2ed1c1f | 85 | // Sample the internal temperature sensor: |
defrost | 1:4403f2ed1c1f | 86 | STARTADCCONVERSION; |
defrost | 1:4403f2ed1c1f | 87 | while(!ADCCONVERSIONCOMPLETE); |
defrost | 2:7abdaa5a9209 | 88 | ADCRaw = ADC1->DR; |
defrost | 2:7abdaa5a9209 | 89 | TempSensor = ((((float)ADCRaw)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f; |
defrost | 1:4403f2ed1c1f | 90 | DBG("TempSensor = %.5f", TempSensor); |
defrost | 2:7abdaa5a9209 | 91 | DBG("ADC1->DR = %d", ADCRaw); |
defrost | 0:c5607b31fb07 | 92 | |
defrost | 0:c5607b31fb07 | 93 | // Send data over network: |
defrost | 0:c5607b31fb07 | 94 | SendNetworkData(); |
defrost | 0:c5607b31fb07 | 95 | |
defrost | 0:c5607b31fb07 | 96 | // Increment a counter: |
defrost | 0:c5607b31fb07 | 97 | SendCounter++; |
defrost | 0:c5607b31fb07 | 98 | |
defrost | 0:c5607b31fb07 | 99 | // Reset the timer: |
defrost | 0:c5607b31fb07 | 100 | DisplayTimer.reset(); |
defrost | 2:7abdaa5a9209 | 101 | |
defrost | 2:7abdaa5a9209 | 102 | if(Led == 1){ |
defrost | 2:7abdaa5a9209 | 103 | Led = 0; |
defrost | 2:7abdaa5a9209 | 104 | }else{ |
defrost | 2:7abdaa5a9209 | 105 | Led = 1; |
defrost | 2:7abdaa5a9209 | 106 | } |
defrost | 0:c5607b31fb07 | 107 | } |
defrost | 0:c5607b31fb07 | 108 | |
defrost | 0:c5607b31fb07 | 109 | |
defrost | 0:c5607b31fb07 | 110 | } |
defrost | 0:c5607b31fb07 | 111 | } |