
Skeleton program for Federico's 4YP project.
Dependencies: WebSocketClient WiflyInterface mbed messages
Fork of IoT_Ex by
Revision 5:0c7d131e6089, committed 2016-10-06
- Comitter:
- defrost
- Date:
- Thu Oct 06 07:58:31 2016 +0000
- Parent:
- 4:9e98e7679a62
- Child:
- 6:424e225d2a91
- Commit message:
- - Added comments, cleaned up code
Changed in this revision
--- a/headers/Commands.h Tue Oct 04 16:52:21 2016 +0000 +++ b/headers/Commands.h Thu Oct 06 07:58:31 2016 +0000 @@ -55,10 +55,10 @@ // Wifi Commands #define NO_WIFI_CMD 0 -#define CHANGEVAR_WIFI_CMD 1 +#define CV_LED_WIFI_CMD 1 // Change variable commands: -#define CV_LED 0 +#define CV_LED 1 extern StatusReg IotStatus;
--- a/headers/globals.h Tue Oct 04 16:52:21 2016 +0000 +++ b/headers/globals.h Thu Oct 06 07:58:31 2016 +0000 @@ -1,12 +1,34 @@ -// ************* -// * globals.h * -// ************* -// -// Created: 2015/03/19 -// By: Damien Frost -// -// Description: -// Provides global definitions. +/** +* @author Damien Frost +* +* @section LICENSE +* +* Copyright (c) 2016 Damien Frost +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +* +* @file "globals.h" +* +* @section DESCRIPTION +* Global definitions for the Internet of Things example. +* +*/ #ifndef IQ_GLOBALS_H #define IQ_GLOBALS_H @@ -26,21 +48,22 @@ #define WS_PORT 4444 #define SERVER_IP "192.168.1.99" -extern char* wifissid; -extern char* wifipassword; - +// Hardware: extern Serial pc; extern InterruptIn UIBut1; extern Timer DisplayTimer; extern DigitalOut Led; -extern WiflyInterface eth; - +// Variables: extern int ReconnectAttempts; extern int SendCounter; extern int IoT_ID; extern float TempSensor; +extern char* wifissid; +extern char* wifipassword; +// Communication: +extern WiflyInterface eth; extern Websocket ws; // Functions: @@ -54,7 +77,7 @@ int SetupNetwork(int Tries); bool ConnectToServer(int Tries); void SendNetworkData(void); -void ReceiveNetworkData(unsigned int * wifi_cmd, unsigned int * var, float * value); +void ReceiveNetworkData(unsigned int * wifi_cmd, float * value); void ModifyVariable(unsigned int wifi_var, float wifi_data);
--- a/main.cpp Tue Oct 04 16:52:21 2016 +0000 +++ b/main.cpp Thu Oct 06 07:58:31 2016 +0000 @@ -1,3 +1,36 @@ +/** +* @author Damien Frost +* +* @section LICENSE +* +* Copyright (c) 2016 Damien Frost +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +* +* @file "main.cpp" +* +* @section DESCRIPTION +* Simple Internet of Things main program. The device sends data every 3 +* seconds, and can receive data from a server. +* +*/ + #include "mbed.h" #include "globals.h" #include "WiflyInterface.h" @@ -12,12 +45,13 @@ #define FUNCNAME "IoT" #include "messages.h" + // Main Loop! int main() { unsigned int wifi_cmd = NO_WIFI_CMD; float wifi_data = 0.0f; - unsigned int wifi_var = 0; unsigned int ADCRaw; + char msg[128]; // Set the IoT ID: IoT_ID = 1; @@ -30,17 +64,23 @@ INFO(""); INFO("Starting up..."); INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); - + + // Configure the ADC to sample the internal temperature sensor. You cannot + // use AnalogIn() unfortunately... ConfigureADC(); + + // Connect to the wifi network. It will basically get stuck here until it + // connects to the network. SetupNetwork(5000); + // Configure the baud rate of the wifi shield: + // This will make our wireless transmissions much faster. ws.setBaud(115200); wait(0.5f); - - - char msg[128]; + // Check to see we are connected to the network: if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){ + // Try to connect to the WebSocket server: sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT); ws.Initialize(msg); INFO("Connecting to Websocket Server on %s...", msg); @@ -53,44 +93,40 @@ IotStatus.ClearFlag(SF_SERVERCONNECTED); INFO("Could not connect to server, will try again later."); ReconnectAttempts++; - if(ReconnectAttempts > 5){ - INFO("Failed after %d reconnect attempts. Resetting the Wifi Shield...", ReconnectAttempts); - SetupNetwork(1); - ReconnectAttempts = 0; - } } } - + // Start the display timer which will send data to the server every + // 3 seconds. + DisplayTimer.start(); - DisplayTimer.start(); - // Inifinite loop: + // Inifinite main loop: while(1) { // Process the wifi command: if(wifi_cmd > NO_WIFI_CMD){ - switch(wifi_cmd){ - case CHANGEVAR_WIFI_CMD: - ModifyVariable(wifi_var, wifi_data); - break; - default: - break; - } + // Modify the desired variable: + ModifyVariable(wifi_cmd, wifi_data); + // Reset the command: wifi_cmd = NO_WIFI_CMD; } // Check for new wifi data: if((wifi_cmd == NO_WIFI_CMD)){ - ReceiveNetworkData(&wifi_cmd, &wifi_var, &wifi_data); + ReceiveNetworkData(&wifi_cmd, &wifi_data); } // Send the network data every 3 seconds: if(DisplayTimer.read()>(3.0f)){ // Sample the internal temperature sensor: STARTADCCONVERSION; + // Wait for the conversion to complete: while(!ADCCONVERSIONCOMPLETE); + // Save the raw value from the ADC: ADCRaw = ADC1->DR; + // Calculate the temperature using information from the datasheet: TempSensor = ((((float)ADCRaw)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f; + // Output the result: DBG("TempSensor = %.5f", TempSensor); DBG("ADC1->DR = %d", ADCRaw); @@ -104,7 +140,5 @@ DisplayTimer.reset(); } - - - } -} + } // while(1) +} // main()
--- a/source/ADC.cpp Tue Oct 04 16:52:21 2016 +0000 +++ b/source/ADC.cpp Thu Oct 06 07:58:31 2016 +0000 @@ -43,11 +43,11 @@ INFO("ADC configuration complete!"); - DBG("ADC Registers:\n\r"); - DBG("The SR Register reads: %d\n\r", ADC1->SR); - DBG("The CR1 Register reads: %d\n\r", ADC1->CR1); - DBG("The CR2 Register reads: %d\n\r", ADC1->CR2); - DBG("The JSQR Register reads: %d\n\r", ADC1->JSQR); + DBG("ADC Registers:"); + DBG("The SR Register reads: %d", ADC1->SR); + DBG("The CR1 Register reads: %d", ADC1->CR1); + DBG("The CR2 Register reads: %d", ADC1->CR2); + DBG("The JSQR Register reads: %d", ADC1->JSQR); return; } \ No newline at end of file
--- a/source/globals.cpp Tue Oct 04 16:52:21 2016 +0000 +++ b/source/globals.cpp Thu Oct 06 07:58:31 2016 +0000 @@ -1,9 +1,34 @@ -// *************** -// * globals.cpp * -// *************** -// -// Created: 2015/03/19 -// By: Damien Frost +/** +* @author Damien Frost +* +* @section LICENSE +* +* Copyright (c) 2016 Damien Frost +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +* +* @file "globals.cpp" +* +* @section DESCRIPTION +* Global definitions for the IoT example program. +* +*/ #include "mbed.h" #include "globals.h" @@ -15,21 +40,23 @@ #define FUNCNAME "GBL" #include "messages.h" -char* wifissid = "SC"; -char* wifipassword = "smartcellshield"; - +// Hardware declarations: Serial pc(USBTX, USBRX); InterruptIn UIBut1(USER_BUTTON); Timer DisplayTimer; DigitalOut Led(LED1); -WiflyInterface eth(D8, D2, D6, D5, wifissid, wifipassword, WPA2); - +// Global variable declarations: int ReconnectAttempts = 0; int SendCounter = 0; extern int IoT_ID = 0; float TempSensor = 0.0f; +char* wifissid = "MyHomeNetwork"; +char* wifipassword = "MyHomeNetworkPassword"; +// Wifily interface declaration: +WiflyInterface eth(D8, D2, D6, D5, wifissid, wifipassword, WPA2); +// WebSocket declaration: Websocket ws; @@ -129,7 +156,7 @@ return; } -void ReceiveNetworkData(unsigned int * wifi_cmd, unsigned int * var, float * value){ +void ReceiveNetworkData(unsigned int * wifi_cmd, float * value){ char msg_buffer[CHARMSGBUFF]; char msg_buffer2[CHARMSGBUFF]; int resp; @@ -139,7 +166,7 @@ if(resp == 1){ INFO("Received: %s", msg_buffer); sscanf(msg_buffer, "%d,%s", wifi_cmd, msg_buffer2); - if(*wifi_cmd == CHANGEVAR_WIFI_CMD){ + if(*wifi_cmd == CV_LED_WIFI_CMD){ // Get one more value: sscanf(msg_buffer2, "%f", value); } @@ -149,7 +176,6 @@ }else{ //DBG("Did not receive anything :(\n\r"); *wifi_cmd = NO_WIFI_CMD; - *var = 0; *value = 0.0f; } }