using websocket to dynamically display temperature on webpage; tweets from mbed via xAP

Dependencies:   BroadcastSend EthernetInterface OneWire SimpleSMTPClient mbed-rpc mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
cryptoc
Date:
Thu Feb 18 18:33:40 2016 +0000
Child:
1:e3a492c2b1d2
Commit message:
add project description readme.txt

Changed in this revision

BroadcastSend.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
OneWire.lib Show annotated file Show diff for this revision Revisions of this file
SimpleSMTPClient.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rpc.lib Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
readme.txt Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BroadcastSend.lib	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/mbed/code/BroadcastSend/#c6af880e0d98
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OneWire.lib	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/sroy/code/OneWire/#0743b94f9435
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleSMTPClient.lib	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/sunifu/code/SimpleSMTPClient/#0847fa2294a0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,120 @@
+/** xAP enabled one wire master part I
+Author: Zhongying Qiao
+Date: 10th March 2014
+
+Updated@24th July 2015
+
+**/
+
+//one wire communication
+#include "mbed.h"
+#include "rtos.h"
+#include "OneWire.h"
+#include "Network.h"
+#include "DS1920.h"
+#include "demo_for_temperature.h"
+//xAP
+#include "EthernetInterface.h"
+//webpage
+#include "Websocket.h"
+
+
+
+int main() {
+    char recv[30];   
+    
+    puts("program started\r");
+    
+    const int BROADCAST_PORT=3639 ;//designated port for xAP sending UDP packets
+    
+    EthernetInterface eth;
+    eth.init();//use DHCP
+    //printf("result from init %d\r\n",eth.init("172.20.177.241 ", "255.255.255.0", "172.20.177.1"));
+    //printf("IP Address is %s\r\n", eth.getIPAddress());
+    //printf("gateway is %s\r\n", eth.getGateway());
+    //printf("mask is %s\r\n", eth.getNetworkMask());
+    eth.connect();
+    printf("IP Address is %s\n", eth.getIPAddress());
+    
+    
+    UDPSocket udp;
+    udp.init();
+    printf("broadcasting success? %d \n", udp.set_broadcasting(true));
+    Endpoint broadcast;
+    printf("set address result %d\n",  broadcast.set_address("255.255.255.255", BROADCAST_PORT));
+    
+    
+    
+    char *packet_a = "xap-header\n"
+                 "{\n"
+                         "v=12\n"
+                         "hop=1\n"
+                         "uid=FF101200\n"
+                         "class=SMS.Message\n"
+                         "source=georg.mbedlpc1768.ibutton7E\n"
+                 "}\n"
+                 "Outbound\n"
+                 "{\n"
+                    "msg=(This is an xAP message from mbed, Ethernet connected!)\n"
+                    "num=(00447401696842)\n"   
+                 "}\n";
+    
+        printf("Broadcasting...\n");
+        printf( "bytes sent= %d\n",udp.sendTo(broadcast, packet_a, strlen(packet_a)));
+        
+
+
+            
+// xAP message composed to send warning email once a certain temperature condition is met.
+           char *packet = "xap-header\n"
+                 "{\n"
+                         "v=12\n"
+                         "hop=1\n"
+                         "uid=FF89EA00\n"
+                         "class=Mail.Outgoing \n"
+                         "source=georg.mbedlpc1768.ibutton7E\n"
+                 "}\n"
+                "Message\n"
+                 "{\n"
+                 "To=(xxxxxxx@student.gla.ac.uk)\n"
+                 "From=(Email From: temperature monitor )\n"
+                 "Subject=(Email Subject: Temperature Alert)\n"
+                 "CC=(example@gmail.com)\n"
+                 "}\n"
+
+               "Body.Text\n"
+                "{\n"
+                "Text.1=(The temperature xxx is exceeding safety limit)\n"
+                "Text.2=(The current temperature is xxx)\n"
+                "}\n"
+                "Signature\n"
+                "{\n"
+                "Text.1=(iButton sensors)\n"
+                "Text.2=(ID 7E00080292A87A10)\n"  
+                "}\n";
+while(true){
+    printf("tem is %f \r\n", temperature);
+      
+    reset_thread();
+    if(temperature> 17.0){
+                            
+                            printf("temperature alart\n");
+                            printf( "bytes sent= %d\n",udp.sendTo(broadcast, packet, strlen(packet)));
+                            udp.sendTo(broadcast, packet, strlen(packet));
+                            wait(1.0);
+                        
+                            }
+    
+    Websocket ws("ws://sockets.mbed.org:443/ws/georg/rw");//("ws://sockets.mbed.org/ws/georg/rw");
+    ws.connect();
+    
+    sprintf(recv, "Temperature of device %llX is %f: \n",rom, temperature); //E000080292CA1010
+    
+    ws.send(recv);
+    temperature =0;
+    wait(2.0);
+    }
+    
+    
+    
+} 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rpc.lib	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/mbed-official/code/mbed-rpc/#d3e03663a6f4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#5aed8bae1001
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/bad568076d81
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.txt	Thu Feb 18 18:33:40 2016 +0000
@@ -0,0 +1,122 @@
+xAP enabled one wire master part I
+Author: Zhongying Qiao
+Date: 10th March 2014
+
+Updated@24th July 2015
+
+Description:
+
+The program is intended for master-slave communication between mbed and DS1920 thermometer iButton;
+
+-> Program will first connect to Ethernet, display iButton ID dynamically onto a webpage via websocket: sockets.mbed.org/ 
+choose 'ws' for channel 'georg' (see address in code below)
+
+-> Once the temperature measurement exceeds a preset threshold, an xAP message will be sent from mbed,
+which then triggers a python script running on another computer to listen at UDP port 3639. Upon authenticating the 
+source(mbed), the script triggers another Ruby program that launches an API call to Twitter account '@qzynjls' to send a tweet. 
+
+-> To see how the program can interact with twitter API to tweet via mbed, import program "xAP_enable_oneWire_Master_Part2";
+
+
+Python(3) and Ruby script for TwitterAPI: 
+
+-> master.py
+
+import socket 
+from subprocess import Popen, PIPE, STDOUT 
+BROADCAST_PORT = 3639 
+s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
+s.bind(('0.0.0.0', BROADCAST_PORT)) 
+ 
+while True: 
+    data, addr= s.recvfrom(500) 
+ 
+    if addr[0]=='172.20.177.4': 
+        print ('launching slave process...') 
+        slave = Popen(['ruby', 'slave.rb'], stdin=PIPE, stdout=PIPE, 
+stderr=STDOUT) 
+        # read user input, expression to be evaluated: 
+        #line = input('Enter expression or exit:') 
+        # write that line to slave's stdin 
+        #slave.stdin.write(bytes(line+'\n', 'UTF-8')) 
+        # result will be a list of lines: 
+        result = [] 
+        print('hey there') 
+        # read slave output line by line, until we reach "[end]" 
+        while True: 
+            print('hey there') 
+            # check if slave has terminated: 
+            if slave.poll() is not None: 
+                print ('slave has terminated.') 
+                exit() 
+            # read one line, remove newline chars and trailing spaces: 
+            line = slave.stdout.readline().rstrip() 
+ 
+            print ('line:', line) 
+            if line == '[end]': 
+                break 
+            result.append(line) 
+        print ('result:') 
+        print ('\n'.join(result)) 
+        sleep(10) 
+    else: 
+        print('detect xAP heartbeat, will not be tweeted!') 
+        
+->slave.rb
+
+require 'rubygems' 
+require 'oauth' 
+require 'json' 
+ 
+consumer_key = OAuth::Consumer.new( 
+  #add your own OAuth consumer key here ) 
+access_token = OAuth::Token.new( 
+  #add your own OAuth Token here )  
+ 
+# read command from standard input: 
+while true 
+ 
+ 
+  # if command is "exit", terminate: 
+    # else evaluate command, send result to standard output: 
+ 
+    baseurl = "https://api.twitter.com" 
+    path    = "/1.1/statuses/update.json" 
+    address = URI("#{baseurl}#{path}") 
+    request = Net::HTTP::Post.new address.request_uri 
+    request.set_form_data( 
+      "status" => "hello from bitmessage!", 
+    ) 
+ 
+    # Set up HTTP. 
+    http             = Net::HTTP.new address.host, address.port 
+    http.use_ssl     = true 
+    http.verify_mode = OpenSSL::SSL::VERIFY_PEER 
+ 
+    # Issue the request. 
+    request.oauth! http, consumer_key, access_token 
+    http.start 
+    response = http.request request 
+ 
+    # Parse and print the Tweet if the response code was 200 
+    tweet = nil 
+    if response.code == '200' then 
+        tweet = JSON.parse(response.body) 
+        puts "Successfully sent #{tweet["text"]}" 
+    else 
+        puts "Could not send the Tweet! " + 
+        "Code:#{response.code} Body:#{response.body}" 
+    end 
+    # and append [end] so that master knows it's the last line: 
+    #print eval(cmd),"\n" 
+    #cmd = STDIN.gets 
+    # remove whitespaces: 
+    #cmd.chop! 
+    # if command is "exit", terminate: 
+ 
+    print "mission acomplished! \n" 
+    print "[end]\n" 
+    # flush stdout to avoid buffering issues: 
+    STDOUT.flush 
+ 
+end 
\ No newline at end of file