HIT Project #3 https://community.freescale.com/docs/DOC-99621

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

MonkeyDo!

/media/uploads/emh203/monkey-20plug2.png

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

Committer:
emh203
Date:
Fri Jul 04 22:55:47 2014 +0000
Revision:
0:29f58b9daa2c
Child:
1:d87a428e88ee
1st add

Who changed what in which revision?

UserRevisionLine numberNew 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
emh203 0:29f58b9daa2c 6 #define TEMPERATURE_FILTER_SIZE 16
emh203 0:29f58b9daa2c 7
emh203 0:29f58b9daa2c 8 float TemperatureFilterBuffer[TEMPERATURE_FILTER_SIZE] = {0};
emh203 0:29f58b9daa2c 9
emh203 0:29f58b9daa2c 10 uint32_t TemperatureFilterIndex = 0;
emh203 0:29f58b9daa2c 11
emh203 0:29f58b9daa2c 12 float Temperature = 0;
emh203 0:29f58b9daa2c 13
emh203 0:29f58b9daa2c 14 Ticker SystemTicker;
emh203 0:29f58b9daa2c 15
emh203 0:29f58b9daa2c 16 volatile uint32_t SensorUpdateTicker = 0;
emh203 0:29f58b9daa2c 17
emh203 0:29f58b9daa2c 18 //This is used for some general purpose timing in the main loop
emh203 0:29f58b9daa2c 19 void SystemTick()
emh203 0:29f58b9daa2c 20 {
emh203 0:29f58b9daa2c 21 if(SensorUpdateTicker < 0xFFFFFFFF)
emh203 0:29f58b9daa2c 22 SensorUpdateTicker++;
emh203 0:29f58b9daa2c 23 }
emh203 0:29f58b9daa2c 24
emh203 0:29f58b9daa2c 25 int main()
emh203 0:29f58b9daa2c 26 {
emh203 0:29f58b9daa2c 27 uint32_t i;
emh203 0:29f58b9daa2c 28
emh203 0:29f58b9daa2c 29 DISABLE_HIGH_CURRENT_RELAY;
emh203 0:29f58b9daa2c 30 DISABLE_LOW_CURRENT_RELAY;
emh203 0:29f58b9daa2c 31
emh203 0:29f58b9daa2c 32 InitTerminal(); //Initialize the Terminal State machine
emh203 0:29f58b9daa2c 33
emh203 0:29f58b9daa2c 34 // enable the usb uart rx fifo
emh203 0:29f58b9daa2c 35 // UART_PFIFO_REG(UART0) |= 0x08;
emh203 0:29f58b9daa2c 36
emh203 0:29f58b9daa2c 37 //Setup ouur ticker for 1mS
emh203 0:29f58b9daa2c 38 SystemTicker.attach_us(SystemTick,1000);
emh203 0:29f58b9daa2c 39
emh203 0:29f58b9daa2c 40 PC.printf("\r\n\r\nMonkey Do!\r\n");
emh203 0:29f58b9daa2c 41
emh203 0:29f58b9daa2c 42
emh203 0:29f58b9daa2c 43 PC.printf("Ethernet Init!\r\n");
emh203 0:29f58b9daa2c 44 eth.init(); //Use DHCP
emh203 0:29f58b9daa2c 45 eth.connect();
emh203 0:29f58b9daa2c 46 PC.printf("IP Address is %s\r\n", eth.getIPAddress());
emh203 0:29f58b9daa2c 47
emh203 0:29f58b9daa2c 48 Websocket ws("ws://sockets.mbed.org:443/ws/emh203/rw");
emh203 0:29f58b9daa2c 49
emh203 0:29f58b9daa2c 50 ws.connect();
emh203 0:29f58b9daa2c 51
emh203 0:29f58b9daa2c 52 while (1) {
emh203 0:29f58b9daa2c 53
emh203 0:29f58b9daa2c 54 //Send sensor data to the websocket every second
emh203 0:29f58b9daa2c 55 if(SensorUpdateTicker >= 1000)
emh203 0:29f58b9daa2c 56 {
emh203 0:29f58b9daa2c 57 SensorUpdateTicker = 0;
emh203 0:29f58b9daa2c 58
emh203 0:29f58b9daa2c 59 Temperature = 0;
emh203 0:29f58b9daa2c 60
emh203 0:29f58b9daa2c 61 //Take a bunch of measurements and average to help with Data converter Noise
emh203 0:29f58b9daa2c 62
emh203 0:29f58b9daa2c 63 for(i=0;i<TEMPERATURE_FILTER_SIZE;i++)
emh203 0:29f58b9daa2c 64 {
emh203 0:29f58b9daa2c 65 Temperature += ((TempSensor.read() * 3.3)// * //convert to volts
emh203 0:29f58b9daa2c 66 - 0.500) // subtract off 0c offset .... you may need to calibrate... see MCP9700A-E/TO data sheet
emh203 0:29f58b9daa2c 67 * 100; // Scale to Degrees C
emh203 0:29f58b9daa2c 68 }
emh203 0:29f58b9daa2c 69 Temperature = Temperature / TEMPERATURE_FILTER_SIZE;
emh203 0:29f58b9daa2c 70
emh203 0:29f58b9daa2c 71 sprintf(StringBuffer,"temp %.1f\r\n",Temperature);
emh203 0:29f58b9daa2c 72 ws.send(StringBuffer);
emh203 0:29f58b9daa2c 73 }
emh203 0:29f58b9daa2c 74
emh203 0:29f58b9daa2c 75 //See if we have an incoming command, if so process it
emh203 0:29f58b9daa2c 76 if(ws.read(StringBuffer) == true)
emh203 0:29f58b9daa2c 77 {
emh203 0:29f58b9daa2c 78 //our command structure is a simple ascii string.
emh203 0:29f58b9daa2c 79 //
emh203 0:29f58b9daa2c 80 //Cmd arg1 arg2 .....
emh203 0:29f58b9daa2c 81 //
emh203 0:29f58b9daa2c 82 //The command is the 1st string. Each argument is separated by white space
emh203 0:29f58b9daa2c 83 //
emh203 0:29f58b9daa2c 84 //for example:
emh203 0:29f58b9daa2c 85 //
emh203 0:29f58b9daa2c 86 //ssr 1 0
emh203 0:29f58b9daa2c 87 //
emh203 0:29f58b9daa2c 88 //is the "ssr" command with 2 arguments: 1 and 0
emh203 0:29f58b9daa2c 89 //
emh203 0:29f58b9daa2c 90 //
emh203 0:29f58b9daa2c 91 //There are many ways to process a command but I will use my "Terminal" processor
emh203 0:29f58b9daa2c 92 //It normally is used to emulated a basic command line terminal from a serial port but it
emh203 0:29f58b9daa2c 93 //can easily process ourwebsocket data.
emh203 0:29f58b9daa2c 94 //
emh203 0:29f58b9daa2c 95 //
emh203 0:29f58b9daa2c 96
emh203 0:29f58b9daa2c 97
emh203 0:29f58b9daa2c 98
emh203 0:29f58b9daa2c 99 //see if this is the command we sent out. The Mbed Websocket server sends input messagest
emh203 0:29f58b9daa2c 100 //to all who are attached. This means we get our own messages back.
emh203 0:29f58b9daa2c 101
emh203 0:29f58b9daa2c 102 if(strncmp(StringBuffer,"temp",4) == 0)
emh203 0:29f58b9daa2c 103 {
emh203 0:29f58b9daa2c 104 //this means we go the temp message back, do nothing
emh203 0:29f58b9daa2c 105 }
emh203 0:29f58b9daa2c 106 else
emh203 0:29f58b9daa2c 107 {
emh203 0:29f58b9daa2c 108 //The first thing to do is copy the data into the terminal input queue
emh203 0:29f58b9daa2c 109 ByteArrayEnqueue(&TERMINAL_INPUT_QUEUE,(uint8_t *)StringBuffer,strlen(StringBuffer));
emh203 0:29f58b9daa2c 110 //We also need a put in a new line character so the terminal will think a new command has been entered
emh203 0:29f58b9daa2c 111 ByteEnqueue(&TERMINAL_INPUT_QUEUE,'\r');
emh203 0:29f58b9daa2c 112 }
emh203 0:29f58b9daa2c 113
emh203 0:29f58b9daa2c 114 //Now that the data is in the terminal input queue, the ProcessTerminal() function will do the rest
emh203 0:29f58b9daa2c 115 }
emh203 0:29f58b9daa2c 116
emh203 0:29f58b9daa2c 117 //This will process any incoming data to the terminal
emh203 0:29f58b9daa2c 118 ProcessTerminal();
emh203 0:29f58b9daa2c 119
emh203 0:29f58b9daa2c 120 //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 121 //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 122 //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 123 //move everything. Ideally I would want interrupts to keep everything flowing but this will be good enough for now.
emh203 0:29f58b9daa2c 124
emh203 0:29f58b9daa2c 125
emh203 0:29f58b9daa2c 126 if(PC.readable()) {
emh203 0:29f58b9daa2c 127 ByteEnqueue(&TERMINAL_INPUT_QUEUE,PC.getc());
emh203 0:29f58b9daa2c 128 }
emh203 0:29f58b9daa2c 129 if(BytesInQueue(&TERMINAL_OUTPUT_QUEUE)>0) {
emh203 0:29f58b9daa2c 130 if(PC.writeable()) {
emh203 0:29f58b9daa2c 131 PC.putc(ForcedByteDequeue(&TERMINAL_OUTPUT_QUEUE));
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
emh203 0:29f58b9daa2c 139 }
emh203 0:29f58b9daa2c 140
emh203 0:29f58b9daa2c 141
emh203 0:29f58b9daa2c 142