HIT Project #3 https://community.freescale.com/docs/DOC-99621
Dependencies: EthernetInterface WebSocketClient mbed-rtos mbed
main.cpp
00001 #include "System.h" 00002 00003 char StringBuffer[256]; 00004 00005 #define TEMPERATURE_FILTER_SIZE 16 00006 00007 float TemperatureFilterBuffer[TEMPERATURE_FILTER_SIZE] = {0}; 00008 00009 uint32_t TemperatureFilterIndex = 0; 00010 00011 float Temperature = 0; 00012 00013 Ticker SystemTicker; 00014 00015 volatile uint32_t SensorUpdateTicker = 0; 00016 00017 //This is used for some general purpose timing in the main loop 00018 void SystemTick() 00019 { 00020 if(SensorUpdateTicker < 0xFFFFFFFF) 00021 SensorUpdateTicker++; 00022 } 00023 00024 int main() 00025 { 00026 uint32_t i; 00027 00028 DISABLE_HIGH_CURRENT_RELAY; 00029 DISABLE_LOW_CURRENT_RELAY; 00030 00031 InitTerminal(); //Initialize the Terminal State machine 00032 00033 //Setup ouur ticker for 1mS 00034 SystemTicker.attach_us(SystemTick,1000); 00035 00036 PC.printf("\r\n\r\nMonkey Do!\r\n"); 00037 00038 00039 PC.printf("Ethernet Init!\r\n"); 00040 eth.init(); //Use DHCP 00041 eth.connect(); 00042 PC.printf("IP Address is %s\r\n", eth.getIPAddress()); 00043 00044 Websocket ws("ws://sockets.mbed.org:443/ws/emh203/rw"); 00045 00046 ws.connect(); 00047 00048 while (1) { 00049 00050 //Send sensor data to the websocket every second 00051 if(SensorUpdateTicker >= 1000) 00052 { 00053 SensorUpdateTicker = 0; 00054 00055 Temperature = 0; 00056 00057 //Take a bunch of measurements and average to help with Data converter Noise 00058 00059 for(i=0;i<TEMPERATURE_FILTER_SIZE;i++) 00060 { 00061 Temperature += ((TempSensor.read() * 3.3f)// * //convert to volts 00062 - 0.500f) // subtract off 0c offset .... you may need to calibrate... see MCP9700A-E/TO data sheet 00063 * 100.0f; // Scale to Degrees C 00064 } 00065 Temperature = Temperature / TEMPERATURE_FILTER_SIZE; 00066 00067 sprintf(StringBuffer,"temp %.1f\r\n",Temperature); 00068 ws.send(StringBuffer); 00069 } 00070 00071 //See if we have an incoming command, if so process it 00072 if(ws.read(StringBuffer) == true) 00073 { 00074 //our command structure is a simple ascii string. 00075 // 00076 //Cmd arg1 arg2 ..... 00077 // 00078 //The command is the 1st string. Each argument is separated by white space 00079 // 00080 //for example: 00081 // 00082 //ssr 1 0 00083 // 00084 //is the "ssr" command with 2 arguments: 1 and 0 00085 // 00086 // 00087 //There are many ways to process a command but I will use my "Terminal" processor 00088 //It normally is used to emulated a basic command line terminal from a serial port but it 00089 //can easily process ourwebsocket data. 00090 // 00091 // 00092 00093 00094 00095 //see if this is the command we sent out. The Mbed Websocket server sends input messagest 00096 //to all who are attached. This means we get our own messages back. 00097 00098 if(strncmp(StringBuffer,"temp",4) == 0) 00099 { 00100 //this means we go the temp message back, do nothing 00101 } 00102 else 00103 { 00104 //The first thing to do is copy the data into the terminal input queue 00105 ByteArrayEnqueue(&TERMINAL_INPUT_QUEUE,(uint8_t *)StringBuffer,strlen(StringBuffer)); 00106 //We also need a put in a new line character so the terminal will think a new command has been entered 00107 ByteEnqueue(&TERMINAL_INPUT_QUEUE,'\r'); 00108 } 00109 00110 //Now that the data is in the terminal input queue, the ProcessTerminal() function will do the rest 00111 } 00112 00113 //This will process any incoming data to the terminal 00114 ProcessTerminal(); 00115 00116 //In addition to the websocket interface, we will also have a simple virtual command line terminal over the USB virtual com port. 00117 //As of 2014-06-24 the Serial IRQ routines are not function in the mbed libraries for the FRDM-K64. For the mean time 00118 //We will just manually shuffle data in and out of the terminal queues. The baud rate is slow enough in that the main loop will have plenty of time to 00119 //move everything. Ideally I would want interrupts to keep everything flowing but this will be good enough for now. 00120 00121 00122 if(PC.readable()) { 00123 ByteEnqueue(&TERMINAL_INPUT_QUEUE,PC.getc()); 00124 } 00125 if(BytesInQueue(&TERMINAL_OUTPUT_QUEUE)>0) { 00126 if(PC.writeable()) { 00127 PC.putc(ForcedByteDequeue(&TERMINAL_OUTPUT_QUEUE)); 00128 } 00129 } 00130 00131 } 00132 00133 00134 00135 } 00136 00137 00138
Generated on Tue Jul 12 2022 19:25:59 by
1.7.2
Eli Hughes