using websocket to dynamically display temperature on webpage; tweets from mbed via xAP
Dependencies: BroadcastSend EthernetInterface OneWire SimpleSMTPClient mbed-rpc mbed-rtos mbed
main.cpp@3:d00432b8cb4a, 2016-02-18 (annotated)
- Committer:
- cryptoc
- Date:
- Thu Feb 18 19:57:07 2016 +0000
- Revision:
- 3:d00432b8cb4a
- Parent:
- 0:a96892f55b07
change readme.txt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cryptoc | 0:a96892f55b07 | 1 | /** xAP enabled one wire master part I |
cryptoc | 0:a96892f55b07 | 2 | Author: Zhongying Qiao |
cryptoc | 0:a96892f55b07 | 3 | Date: 10th March 2014 |
cryptoc | 0:a96892f55b07 | 4 | |
cryptoc | 0:a96892f55b07 | 5 | Updated@24th July 2015 |
cryptoc | 0:a96892f55b07 | 6 | |
cryptoc | 0:a96892f55b07 | 7 | **/ |
cryptoc | 0:a96892f55b07 | 8 | |
cryptoc | 0:a96892f55b07 | 9 | //one wire communication |
cryptoc | 0:a96892f55b07 | 10 | #include "mbed.h" |
cryptoc | 0:a96892f55b07 | 11 | #include "rtos.h" |
cryptoc | 0:a96892f55b07 | 12 | #include "OneWire.h" |
cryptoc | 0:a96892f55b07 | 13 | #include "Network.h" |
cryptoc | 0:a96892f55b07 | 14 | #include "DS1920.h" |
cryptoc | 0:a96892f55b07 | 15 | #include "demo_for_temperature.h" |
cryptoc | 0:a96892f55b07 | 16 | //xAP |
cryptoc | 0:a96892f55b07 | 17 | #include "EthernetInterface.h" |
cryptoc | 0:a96892f55b07 | 18 | //webpage |
cryptoc | 0:a96892f55b07 | 19 | #include "Websocket.h" |
cryptoc | 0:a96892f55b07 | 20 | |
cryptoc | 0:a96892f55b07 | 21 | |
cryptoc | 0:a96892f55b07 | 22 | |
cryptoc | 0:a96892f55b07 | 23 | int main() { |
cryptoc | 0:a96892f55b07 | 24 | char recv[30]; |
cryptoc | 0:a96892f55b07 | 25 | |
cryptoc | 0:a96892f55b07 | 26 | puts("program started\r"); |
cryptoc | 0:a96892f55b07 | 27 | |
cryptoc | 0:a96892f55b07 | 28 | const int BROADCAST_PORT=3639 ;//designated port for xAP sending UDP packets |
cryptoc | 0:a96892f55b07 | 29 | |
cryptoc | 0:a96892f55b07 | 30 | EthernetInterface eth; |
cryptoc | 0:a96892f55b07 | 31 | eth.init();//use DHCP |
cryptoc | 0:a96892f55b07 | 32 | //printf("result from init %d\r\n",eth.init("172.20.177.241 ", "255.255.255.0", "172.20.177.1")); |
cryptoc | 0:a96892f55b07 | 33 | //printf("IP Address is %s\r\n", eth.getIPAddress()); |
cryptoc | 0:a96892f55b07 | 34 | //printf("gateway is %s\r\n", eth.getGateway()); |
cryptoc | 0:a96892f55b07 | 35 | //printf("mask is %s\r\n", eth.getNetworkMask()); |
cryptoc | 0:a96892f55b07 | 36 | eth.connect(); |
cryptoc | 0:a96892f55b07 | 37 | printf("IP Address is %s\n", eth.getIPAddress()); |
cryptoc | 0:a96892f55b07 | 38 | |
cryptoc | 0:a96892f55b07 | 39 | |
cryptoc | 0:a96892f55b07 | 40 | UDPSocket udp; |
cryptoc | 0:a96892f55b07 | 41 | udp.init(); |
cryptoc | 0:a96892f55b07 | 42 | printf("broadcasting success? %d \n", udp.set_broadcasting(true)); |
cryptoc | 0:a96892f55b07 | 43 | Endpoint broadcast; |
cryptoc | 0:a96892f55b07 | 44 | printf("set address result %d\n", broadcast.set_address("255.255.255.255", BROADCAST_PORT)); |
cryptoc | 0:a96892f55b07 | 45 | |
cryptoc | 0:a96892f55b07 | 46 | |
cryptoc | 0:a96892f55b07 | 47 | |
cryptoc | 0:a96892f55b07 | 48 | char *packet_a = "xap-header\n" |
cryptoc | 0:a96892f55b07 | 49 | "{\n" |
cryptoc | 0:a96892f55b07 | 50 | "v=12\n" |
cryptoc | 0:a96892f55b07 | 51 | "hop=1\n" |
cryptoc | 0:a96892f55b07 | 52 | "uid=FF101200\n" |
cryptoc | 0:a96892f55b07 | 53 | "class=SMS.Message\n" |
cryptoc | 0:a96892f55b07 | 54 | "source=georg.mbedlpc1768.ibutton7E\n" |
cryptoc | 0:a96892f55b07 | 55 | "}\n" |
cryptoc | 0:a96892f55b07 | 56 | "Outbound\n" |
cryptoc | 0:a96892f55b07 | 57 | "{\n" |
cryptoc | 0:a96892f55b07 | 58 | "msg=(This is an xAP message from mbed, Ethernet connected!)\n" |
cryptoc | 0:a96892f55b07 | 59 | "num=(00447401696842)\n" |
cryptoc | 0:a96892f55b07 | 60 | "}\n"; |
cryptoc | 0:a96892f55b07 | 61 | |
cryptoc | 0:a96892f55b07 | 62 | printf("Broadcasting...\n"); |
cryptoc | 0:a96892f55b07 | 63 | printf( "bytes sent= %d\n",udp.sendTo(broadcast, packet_a, strlen(packet_a))); |
cryptoc | 0:a96892f55b07 | 64 | |
cryptoc | 0:a96892f55b07 | 65 | |
cryptoc | 0:a96892f55b07 | 66 | |
cryptoc | 0:a96892f55b07 | 67 | |
cryptoc | 0:a96892f55b07 | 68 | // xAP message composed to send warning email once a certain temperature condition is met. |
cryptoc | 0:a96892f55b07 | 69 | char *packet = "xap-header\n" |
cryptoc | 0:a96892f55b07 | 70 | "{\n" |
cryptoc | 0:a96892f55b07 | 71 | "v=12\n" |
cryptoc | 0:a96892f55b07 | 72 | "hop=1\n" |
cryptoc | 0:a96892f55b07 | 73 | "uid=FF89EA00\n" |
cryptoc | 0:a96892f55b07 | 74 | "class=Mail.Outgoing \n" |
cryptoc | 0:a96892f55b07 | 75 | "source=georg.mbedlpc1768.ibutton7E\n" |
cryptoc | 0:a96892f55b07 | 76 | "}\n" |
cryptoc | 0:a96892f55b07 | 77 | "Message\n" |
cryptoc | 0:a96892f55b07 | 78 | "{\n" |
cryptoc | 0:a96892f55b07 | 79 | "To=(xxxxxxx@student.gla.ac.uk)\n" |
cryptoc | 0:a96892f55b07 | 80 | "From=(Email From: temperature monitor )\n" |
cryptoc | 0:a96892f55b07 | 81 | "Subject=(Email Subject: Temperature Alert)\n" |
cryptoc | 0:a96892f55b07 | 82 | "CC=(example@gmail.com)\n" |
cryptoc | 0:a96892f55b07 | 83 | "}\n" |
cryptoc | 0:a96892f55b07 | 84 | |
cryptoc | 0:a96892f55b07 | 85 | "Body.Text\n" |
cryptoc | 0:a96892f55b07 | 86 | "{\n" |
cryptoc | 0:a96892f55b07 | 87 | "Text.1=(The temperature xxx is exceeding safety limit)\n" |
cryptoc | 0:a96892f55b07 | 88 | "Text.2=(The current temperature is xxx)\n" |
cryptoc | 0:a96892f55b07 | 89 | "}\n" |
cryptoc | 0:a96892f55b07 | 90 | "Signature\n" |
cryptoc | 0:a96892f55b07 | 91 | "{\n" |
cryptoc | 0:a96892f55b07 | 92 | "Text.1=(iButton sensors)\n" |
cryptoc | 0:a96892f55b07 | 93 | "Text.2=(ID 7E00080292A87A10)\n" |
cryptoc | 0:a96892f55b07 | 94 | "}\n"; |
cryptoc | 0:a96892f55b07 | 95 | while(true){ |
cryptoc | 0:a96892f55b07 | 96 | printf("tem is %f \r\n", temperature); |
cryptoc | 0:a96892f55b07 | 97 | |
cryptoc | 0:a96892f55b07 | 98 | reset_thread(); |
cryptoc | 0:a96892f55b07 | 99 | if(temperature> 17.0){ |
cryptoc | 0:a96892f55b07 | 100 | |
cryptoc | 0:a96892f55b07 | 101 | printf("temperature alart\n"); |
cryptoc | 0:a96892f55b07 | 102 | printf( "bytes sent= %d\n",udp.sendTo(broadcast, packet, strlen(packet))); |
cryptoc | 0:a96892f55b07 | 103 | udp.sendTo(broadcast, packet, strlen(packet)); |
cryptoc | 0:a96892f55b07 | 104 | wait(1.0); |
cryptoc | 0:a96892f55b07 | 105 | |
cryptoc | 0:a96892f55b07 | 106 | } |
cryptoc | 0:a96892f55b07 | 107 | |
cryptoc | 0:a96892f55b07 | 108 | Websocket ws("ws://sockets.mbed.org:443/ws/georg/rw");//("ws://sockets.mbed.org/ws/georg/rw"); |
cryptoc | 0:a96892f55b07 | 109 | ws.connect(); |
cryptoc | 0:a96892f55b07 | 110 | |
cryptoc | 0:a96892f55b07 | 111 | sprintf(recv, "Temperature of device %llX is %f: \n",rom, temperature); //E000080292CA1010 |
cryptoc | 0:a96892f55b07 | 112 | |
cryptoc | 0:a96892f55b07 | 113 | ws.send(recv); |
cryptoc | 0:a96892f55b07 | 114 | temperature =0; |
cryptoc | 0:a96892f55b07 | 115 | wait(2.0); |
cryptoc | 0:a96892f55b07 | 116 | } |
cryptoc | 0:a96892f55b07 | 117 | |
cryptoc | 0:a96892f55b07 | 118 | |
cryptoc | 0:a96892f55b07 | 119 | |
cryptoc | 0:a96892f55b07 | 120 | } |