HIT Project #3 https://community.freescale.com/docs/DOC-99621
Dependencies: EthernetInterface WebSocketClient mbed-rtos mbed
MonkeyDo!
These are the demo files for Freescale HIT project #3: Monkey Do. It uses a FRDM-AUTO + a FRDM-K64F to demo websockets for a simple IoT application.
See the main MonkeyDo page for all of the schematics, videos, GitHub links, etc for everything else!
https://community.freescale.com/docs/DOC-99621
main.cpp@1:d87a428e88ee, 2014-07-17 (annotated)
- Committer:
- emh203
- Date:
- Thu Jul 17 00:18:36 2014 +0000
- Revision:
- 1:d87a428e88ee
- Parent:
- 0:29f58b9daa2c
Version for 1st project release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emh203 | 0:29f58b9daa2c | 1 | #include "System.h" |
emh203 | 0:29f58b9daa2c | 2 | |
emh203 | 0:29f58b9daa2c | 3 | char StringBuffer[256]; |
emh203 | 0:29f58b9daa2c | 4 | |
emh203 | 0:29f58b9daa2c | 5 | #define TEMPERATURE_FILTER_SIZE 16 |
emh203 | 0:29f58b9daa2c | 6 | |
emh203 | 0:29f58b9daa2c | 7 | float TemperatureFilterBuffer[TEMPERATURE_FILTER_SIZE] = {0}; |
emh203 | 0:29f58b9daa2c | 8 | |
emh203 | 0:29f58b9daa2c | 9 | uint32_t TemperatureFilterIndex = 0; |
emh203 | 0:29f58b9daa2c | 10 | |
emh203 | 0:29f58b9daa2c | 11 | float Temperature = 0; |
emh203 | 0:29f58b9daa2c | 12 | |
emh203 | 0:29f58b9daa2c | 13 | Ticker SystemTicker; |
emh203 | 0:29f58b9daa2c | 14 | |
emh203 | 0:29f58b9daa2c | 15 | volatile uint32_t SensorUpdateTicker = 0; |
emh203 | 0:29f58b9daa2c | 16 | |
emh203 | 0:29f58b9daa2c | 17 | //This is used for some general purpose timing in the main loop |
emh203 | 0:29f58b9daa2c | 18 | void SystemTick() |
emh203 | 0:29f58b9daa2c | 19 | { |
emh203 | 0:29f58b9daa2c | 20 | if(SensorUpdateTicker < 0xFFFFFFFF) |
emh203 | 0:29f58b9daa2c | 21 | SensorUpdateTicker++; |
emh203 | 0:29f58b9daa2c | 22 | } |
emh203 | 0:29f58b9daa2c | 23 | |
emh203 | 0:29f58b9daa2c | 24 | int main() |
emh203 | 0:29f58b9daa2c | 25 | { |
emh203 | 0:29f58b9daa2c | 26 | uint32_t i; |
emh203 | 0:29f58b9daa2c | 27 | |
emh203 | 0:29f58b9daa2c | 28 | DISABLE_HIGH_CURRENT_RELAY; |
emh203 | 0:29f58b9daa2c | 29 | DISABLE_LOW_CURRENT_RELAY; |
emh203 | 0:29f58b9daa2c | 30 | |
emh203 | 0:29f58b9daa2c | 31 | InitTerminal(); //Initialize the Terminal State machine |
emh203 | 0:29f58b9daa2c | 32 | |
emh203 | 0:29f58b9daa2c | 33 | //Setup ouur ticker for 1mS |
emh203 | 0:29f58b9daa2c | 34 | SystemTicker.attach_us(SystemTick,1000); |
emh203 | 0:29f58b9daa2c | 35 | |
emh203 | 0:29f58b9daa2c | 36 | PC.printf("\r\n\r\nMonkey Do!\r\n"); |
emh203 | 0:29f58b9daa2c | 37 | |
emh203 | 0:29f58b9daa2c | 38 | |
emh203 | 0:29f58b9daa2c | 39 | PC.printf("Ethernet Init!\r\n"); |
emh203 | 0:29f58b9daa2c | 40 | eth.init(); //Use DHCP |
emh203 | 0:29f58b9daa2c | 41 | eth.connect(); |
emh203 | 0:29f58b9daa2c | 42 | PC.printf("IP Address is %s\r\n", eth.getIPAddress()); |
emh203 | 0:29f58b9daa2c | 43 | |
emh203 | 0:29f58b9daa2c | 44 | Websocket ws("ws://sockets.mbed.org:443/ws/emh203/rw"); |
emh203 | 0:29f58b9daa2c | 45 | |
emh203 | 0:29f58b9daa2c | 46 | ws.connect(); |
emh203 | 0:29f58b9daa2c | 47 | |
emh203 | 0:29f58b9daa2c | 48 | while (1) { |
emh203 | 0:29f58b9daa2c | 49 | |
emh203 | 0:29f58b9daa2c | 50 | //Send sensor data to the websocket every second |
emh203 | 0:29f58b9daa2c | 51 | if(SensorUpdateTicker >= 1000) |
emh203 | 0:29f58b9daa2c | 52 | { |
emh203 | 0:29f58b9daa2c | 53 | SensorUpdateTicker = 0; |
emh203 | 0:29f58b9daa2c | 54 | |
emh203 | 0:29f58b9daa2c | 55 | Temperature = 0; |
emh203 | 0:29f58b9daa2c | 56 | |
emh203 | 0:29f58b9daa2c | 57 | //Take a bunch of measurements and average to help with Data converter Noise |
emh203 | 0:29f58b9daa2c | 58 | |
emh203 | 0:29f58b9daa2c | 59 | for(i=0;i<TEMPERATURE_FILTER_SIZE;i++) |
emh203 | 0:29f58b9daa2c | 60 | { |
emh203 | 1:d87a428e88ee | 61 | Temperature += ((TempSensor.read() * 3.3f)// * //convert to volts |
emh203 | 1:d87a428e88ee | 62 | - 0.500f) // subtract off 0c offset .... you may need to calibrate... see MCP9700A-E/TO data sheet |
emh203 | 1:d87a428e88ee | 63 | * 100.0f; // Scale to Degrees C |
emh203 | 0:29f58b9daa2c | 64 | } |
emh203 | 0:29f58b9daa2c | 65 | Temperature = Temperature / TEMPERATURE_FILTER_SIZE; |
emh203 | 0:29f58b9daa2c | 66 | |
emh203 | 0:29f58b9daa2c | 67 | sprintf(StringBuffer,"temp %.1f\r\n",Temperature); |
emh203 | 0:29f58b9daa2c | 68 | ws.send(StringBuffer); |
emh203 | 0:29f58b9daa2c | 69 | } |
emh203 | 0:29f58b9daa2c | 70 | |
emh203 | 0:29f58b9daa2c | 71 | //See if we have an incoming command, if so process it |
emh203 | 0:29f58b9daa2c | 72 | if(ws.read(StringBuffer) == true) |
emh203 | 0:29f58b9daa2c | 73 | { |
emh203 | 0:29f58b9daa2c | 74 | //our command structure is a simple ascii string. |
emh203 | 0:29f58b9daa2c | 75 | // |
emh203 | 0:29f58b9daa2c | 76 | //Cmd arg1 arg2 ..... |
emh203 | 0:29f58b9daa2c | 77 | // |
emh203 | 0:29f58b9daa2c | 78 | //The command is the 1st string. Each argument is separated by white space |
emh203 | 0:29f58b9daa2c | 79 | // |
emh203 | 0:29f58b9daa2c | 80 | //for example: |
emh203 | 0:29f58b9daa2c | 81 | // |
emh203 | 0:29f58b9daa2c | 82 | //ssr 1 0 |
emh203 | 0:29f58b9daa2c | 83 | // |
emh203 | 0:29f58b9daa2c | 84 | //is the "ssr" command with 2 arguments: 1 and 0 |
emh203 | 0:29f58b9daa2c | 85 | // |
emh203 | 0:29f58b9daa2c | 86 | // |
emh203 | 0:29f58b9daa2c | 87 | //There are many ways to process a command but I will use my "Terminal" processor |
emh203 | 0:29f58b9daa2c | 88 | //It normally is used to emulated a basic command line terminal from a serial port but it |
emh203 | 0:29f58b9daa2c | 89 | //can easily process ourwebsocket data. |
emh203 | 0:29f58b9daa2c | 90 | // |
emh203 | 0:29f58b9daa2c | 91 | // |
emh203 | 0:29f58b9daa2c | 92 | |
emh203 | 0:29f58b9daa2c | 93 | |
emh203 | 0:29f58b9daa2c | 94 | |
emh203 | 0:29f58b9daa2c | 95 | //see if this is the command we sent out. The Mbed Websocket server sends input messagest |
emh203 | 0:29f58b9daa2c | 96 | //to all who are attached. This means we get our own messages back. |
emh203 | 0:29f58b9daa2c | 97 | |
emh203 | 0:29f58b9daa2c | 98 | if(strncmp(StringBuffer,"temp",4) == 0) |
emh203 | 0:29f58b9daa2c | 99 | { |
emh203 | 0:29f58b9daa2c | 100 | //this means we go the temp message back, do nothing |
emh203 | 0:29f58b9daa2c | 101 | } |
emh203 | 0:29f58b9daa2c | 102 | else |
emh203 | 0:29f58b9daa2c | 103 | { |
emh203 | 0:29f58b9daa2c | 104 | //The first thing to do is copy the data into the terminal input queue |
emh203 | 0:29f58b9daa2c | 105 | ByteArrayEnqueue(&TERMINAL_INPUT_QUEUE,(uint8_t *)StringBuffer,strlen(StringBuffer)); |
emh203 | 0:29f58b9daa2c | 106 | //We also need a put in a new line character so the terminal will think a new command has been entered |
emh203 | 0:29f58b9daa2c | 107 | ByteEnqueue(&TERMINAL_INPUT_QUEUE,'\r'); |
emh203 | 0:29f58b9daa2c | 108 | } |
emh203 | 0:29f58b9daa2c | 109 | |
emh203 | 0:29f58b9daa2c | 110 | //Now that the data is in the terminal input queue, the ProcessTerminal() function will do the rest |
emh203 | 0:29f58b9daa2c | 111 | } |
emh203 | 0:29f58b9daa2c | 112 | |
emh203 | 0:29f58b9daa2c | 113 | //This will process any incoming data to the terminal |
emh203 | 0:29f58b9daa2c | 114 | ProcessTerminal(); |
emh203 | 0:29f58b9daa2c | 115 | |
emh203 | 0:29f58b9daa2c | 116 | //In addition to the websocket interface, we will also have a simple virtual command line terminal over the USB virtual com port. |
emh203 | 0:29f58b9daa2c | 117 | //As of 2014-06-24 the Serial IRQ routines are not function in the mbed libraries for the FRDM-K64. For the mean time |
emh203 | 0:29f58b9daa2c | 118 | //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 |
emh203 | 0:29f58b9daa2c | 119 | //move everything. Ideally I would want interrupts to keep everything flowing but this will be good enough for now. |
emh203 | 0:29f58b9daa2c | 120 | |
emh203 | 0:29f58b9daa2c | 121 | |
emh203 | 0:29f58b9daa2c | 122 | if(PC.readable()) { |
emh203 | 0:29f58b9daa2c | 123 | ByteEnqueue(&TERMINAL_INPUT_QUEUE,PC.getc()); |
emh203 | 0:29f58b9daa2c | 124 | } |
emh203 | 0:29f58b9daa2c | 125 | if(BytesInQueue(&TERMINAL_OUTPUT_QUEUE)>0) { |
emh203 | 0:29f58b9daa2c | 126 | if(PC.writeable()) { |
emh203 | 0:29f58b9daa2c | 127 | PC.putc(ForcedByteDequeue(&TERMINAL_OUTPUT_QUEUE)); |
emh203 | 0:29f58b9daa2c | 128 | } |
emh203 | 0:29f58b9daa2c | 129 | } |
emh203 | 0:29f58b9daa2c | 130 | |
emh203 | 0:29f58b9daa2c | 131 | } |
emh203 | 0:29f58b9daa2c | 132 | |
emh203 | 0:29f58b9daa2c | 133 | |
emh203 | 0:29f58b9daa2c | 134 | |
emh203 | 0:29f58b9daa2c | 135 | } |
emh203 | 0:29f58b9daa2c | 136 | |
emh203 | 0:29f58b9daa2c | 137 | |
emh203 | 0:29f58b9daa2c | 138 |