Display analog voltages from mbed board in a browser. Uses smoothie.js called from html page. mbed sends AnalogIn, AnalogOut, and temperature signals.
Dependencies: C12832_lcd EthernetInterface LM75B MMA7660 WebSocketClient mbed-rtos mbed
main.cpp@0:5f2b64d6ed81, 2014-06-09 (annotated)
- Committer:
- thomas_sullivan
- Date:
- Mon Jun 09 22:54:42 2014 +0000
- Revision:
- 0:5f2b64d6ed81
Displays analog input data in a browser using smoothie.js. Use similar to an oscilloscope for debugging or data output. Corresponding html and javascript code available.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomas_sullivan | 0:5f2b64d6ed81 | 1 | //IOT Class Project. June 9, 2014. Tom Sullivan |
thomas_sullivan | 0:5f2b64d6ed81 | 2 | |
thomas_sullivan | 0:5f2b64d6ed81 | 3 | #include "mbed.h" |
thomas_sullivan | 0:5f2b64d6ed81 | 4 | #include "LM75B.h" |
thomas_sullivan | 0:5f2b64d6ed81 | 5 | #include "C12832_lcd.h" |
thomas_sullivan | 0:5f2b64d6ed81 | 6 | #include "EthernetInterface.h" |
thomas_sullivan | 0:5f2b64d6ed81 | 7 | #include "NTPClient.h" |
thomas_sullivan | 0:5f2b64d6ed81 | 8 | #include "MMA7660.h" |
thomas_sullivan | 0:5f2b64d6ed81 | 9 | #include "Websocket.h" |
thomas_sullivan | 0:5f2b64d6ed81 | 10 | |
thomas_sullivan | 0:5f2b64d6ed81 | 11 | C12832_LCD lcd; |
thomas_sullivan | 0:5f2b64d6ed81 | 12 | DigitalOut led1(LED1); |
thomas_sullivan | 0:5f2b64d6ed81 | 13 | MMA7660 MMA(p28, p27); |
thomas_sullivan | 0:5f2b64d6ed81 | 14 | AnalogIn pot20(p20); |
thomas_sullivan | 0:5f2b64d6ed81 | 15 | AnalogIn pot19(p19); |
thomas_sullivan | 0:5f2b64d6ed81 | 16 | AnalogIn ain17(p17); |
thomas_sullivan | 0:5f2b64d6ed81 | 17 | AnalogOut aout18(p18); |
thomas_sullivan | 0:5f2b64d6ed81 | 18 | BusIn joy(p15,p12,p13,p16); //DigitalIn joyUp(p15), joyDown(p12), joyLeft(p13), joyRight(p16); |
thomas_sullivan | 0:5f2b64d6ed81 | 19 | |
thomas_sullivan | 0:5f2b64d6ed81 | 20 | Ticker myTicker1; |
thomas_sullivan | 0:5f2b64d6ed81 | 21 | EthernetInterface eth; |
thomas_sullivan | 0:5f2b64d6ed81 | 22 | NTPClient ntp; |
thomas_sullivan | 0:5f2b64d6ed81 | 23 | time_t currentTime; |
thomas_sullivan | 0:5f2b64d6ed81 | 24 | char dataOut[128]; |
thomas_sullivan | 0:5f2b64d6ed81 | 25 | |
thomas_sullivan | 0:5f2b64d6ed81 | 26 | //Temperature variables |
thomas_sullivan | 0:5f2b64d6ed81 | 27 | LM75B tmp(p28,p27); |
thomas_sullivan | 0:5f2b64d6ed81 | 28 | float tempNow, tempHI=80,tempLO=60; |
thomas_sullivan | 0:5f2b64d6ed81 | 29 | float coolHyst = 1, heatHyst = 1; |
thomas_sullivan | 0:5f2b64d6ed81 | 30 | int Pressing = 0; |
thomas_sullivan | 0:5f2b64d6ed81 | 31 | int heatOn = 0, coolOn = 0; //disabled = -1, enabled/Off = 0, enabled/On = 1 |
thomas_sullivan | 0:5f2b64d6ed81 | 32 | |
thomas_sullivan | 0:5f2b64d6ed81 | 33 | //set flag that indicates it is time to output data to the web socket |
thomas_sullivan | 0:5f2b64d6ed81 | 34 | void sendData() { |
thomas_sullivan | 0:5f2b64d6ed81 | 35 | led1 = 1; //!led1; |
thomas_sullivan | 0:5f2b64d6ed81 | 36 | } |
thomas_sullivan | 0:5f2b64d6ed81 | 37 | |
thomas_sullivan | 0:5f2b64d6ed81 | 38 | int main() |
thomas_sullivan | 0:5f2b64d6ed81 | 39 | { |
thomas_sullivan | 0:5f2b64d6ed81 | 40 | printf("\n\r-----Starting Project-----\n\r"); |
thomas_sullivan | 0:5f2b64d6ed81 | 41 | //Connect to ethernet. Use DHCP. ********************** |
thomas_sullivan | 0:5f2b64d6ed81 | 42 | eth.init(); |
thomas_sullivan | 0:5f2b64d6ed81 | 43 | eth.connect(); |
thomas_sullivan | 0:5f2b64d6ed81 | 44 | printf("IP Address is %s\n\r", eth.getIPAddress()); |
thomas_sullivan | 0:5f2b64d6ed81 | 45 | |
thomas_sullivan | 0:5f2b64d6ed81 | 46 | |
thomas_sullivan | 0:5f2b64d6ed81 | 47 | //Connect to mbed websocket server |
thomas_sullivan | 0:5f2b64d6ed81 | 48 | printf("Connecting to mbed websocket server channel 445\n\r"); |
thomas_sullivan | 0:5f2b64d6ed81 | 49 | Websocket ws("ws://sockets.mbed.org/ws/445/rw"); |
thomas_sullivan | 0:5f2b64d6ed81 | 50 | //Websocket ws("17.244.173.207");//ws://sockets.mbed.org/ws/445/rw"); |
thomas_sullivan | 0:5f2b64d6ed81 | 51 | ws.connect(); |
thomas_sullivan | 0:5f2b64d6ed81 | 52 | printf("Done connecting\n\r"); |
thomas_sullivan | 0:5f2b64d6ed81 | 53 | |
thomas_sullivan | 0:5f2b64d6ed81 | 54 | //Attach ticker with X second timing |
thomas_sullivan | 0:5f2b64d6ed81 | 55 | printf("Setting ticker\n\r"); |
thomas_sullivan | 0:5f2b64d6ed81 | 56 | myTicker1.attach(&sendData, 0.5); //send data out every half second |
thomas_sullivan | 0:5f2b64d6ed81 | 57 | float potOut,senseIn,sigOut; |
thomas_sullivan | 0:5f2b64d6ed81 | 58 | |
thomas_sullivan | 0:5f2b64d6ed81 | 59 | int i=0; |
thomas_sullivan | 0:5f2b64d6ed81 | 60 | while(1) { |
thomas_sullivan | 0:5f2b64d6ed81 | 61 | |
thomas_sullivan | 0:5f2b64d6ed81 | 62 | //Print information to LCD |
thomas_sullivan | 0:5f2b64d6ed81 | 63 | //lcd.cls(); |
thomas_sullivan | 0:5f2b64d6ed81 | 64 | //lcd.locate(0,3); |
thomas_sullivan | 0:5f2b64d6ed81 | 65 | |
thomas_sullivan | 0:5f2b64d6ed81 | 66 | //If the time is right, send data to the websocket server |
thomas_sullivan | 0:5f2b64d6ed81 | 67 | if (led1) { |
thomas_sullivan | 0:5f2b64d6ed81 | 68 | i=i+1; |
thomas_sullivan | 0:5f2b64d6ed81 | 69 | if (i >= 20) { |
thomas_sullivan | 0:5f2b64d6ed81 | 70 | i=0; |
thomas_sullivan | 0:5f2b64d6ed81 | 71 | }; |
thomas_sullivan | 0:5f2b64d6ed81 | 72 | |
thomas_sullivan | 0:5f2b64d6ed81 | 73 | //printf("Sending message\n\r"); |
thomas_sullivan | 0:5f2b64d6ed81 | 74 | //accel_z = MMA.z(); |
thomas_sullivan | 0:5f2b64d6ed81 | 75 | //sprintf(dataOut,"{\"Temp\":%.1f,\"X\":%.1f,\"Y\":%.1f,\"Z\":%.1f}",tempNow,MMA.x(),MMA.y(),MMA.z()); |
thomas_sullivan | 0:5f2b64d6ed81 | 76 | potOut=pot19.read(); |
thomas_sullivan | 0:5f2b64d6ed81 | 77 | senseIn = ain17.read() + 0.1; |
thomas_sullivan | 0:5f2b64d6ed81 | 78 | sigOut = ((float)i)/20; |
thomas_sullivan | 0:5f2b64d6ed81 | 79 | //printf("%d, %.2f\n\r",i,sigOut); |
thomas_sullivan | 0:5f2b64d6ed81 | 80 | sprintf(dataOut,"{\"X\":%.2f,\"Y\":%.2f,\"Z\":%.2f}",potOut,senseIn,sigOut); |
thomas_sullivan | 0:5f2b64d6ed81 | 81 | //printf("%s\n\r",dataOut); |
thomas_sullivan | 0:5f2b64d6ed81 | 82 | //lcd.cls(); |
thomas_sullivan | 0:5f2b64d6ed81 | 83 | //lcd.locate(0,3); |
thomas_sullivan | 0:5f2b64d6ed81 | 84 | //lcd.printf("Pot19: %.2f\n",potOut); |
thomas_sullivan | 0:5f2b64d6ed81 | 85 | //lcd.printf("Sense: %.2f\n",senseIn); |
thomas_sullivan | 0:5f2b64d6ed81 | 86 | //lcd.printf("SigOut: %.2f\n",sigOut); |
thomas_sullivan | 0:5f2b64d6ed81 | 87 | |
thomas_sullivan | 0:5f2b64d6ed81 | 88 | ws.send(dataOut);// %d",heatOn); |
thomas_sullivan | 0:5f2b64d6ed81 | 89 | led1 = 0; |
thomas_sullivan | 0:5f2b64d6ed81 | 90 | } |
thomas_sullivan | 0:5f2b64d6ed81 | 91 | } |
thomas_sullivan | 0:5f2b64d6ed81 | 92 | } |