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

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

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?

UserRevisionLine numberNew contents of line
cryptoc 3:d00432b8cb4a 1 /**
cryptoc 3:d00432b8cb4a 2
cryptoc 3:d00432b8cb4a 3
cryptoc 0:a96892f55b07 4 xAP enabled one wire master part I
cryptoc 0:a96892f55b07 5 Author: Zhongying Qiao
cryptoc 0:a96892f55b07 6 Date: 10th March 2014
cryptoc 0:a96892f55b07 7
cryptoc 0:a96892f55b07 8 Updated@24th July 2015
cryptoc 0:a96892f55b07 9
cryptoc 0:a96892f55b07 10 Description:
cryptoc 0:a96892f55b07 11
cryptoc 0:a96892f55b07 12 The program is intended for master-slave communication between mbed and DS1920 thermometer iButton;
cryptoc 0:a96892f55b07 13
cryptoc 0:a96892f55b07 14 -> Program will first connect to Ethernet, display iButton ID dynamically onto a webpage via websocket: sockets.mbed.org/
cryptoc 0:a96892f55b07 15 choose 'ws' for channel 'georg' (see address in code below)
cryptoc 0:a96892f55b07 16
cryptoc 0:a96892f55b07 17 -> Once the temperature measurement exceeds a preset threshold, an xAP message will be sent from mbed,
cryptoc 0:a96892f55b07 18 which then triggers a python script running on another computer to listen at UDP port 3639. Upon authenticating the
cryptoc 0:a96892f55b07 19 source(mbed), the script triggers another Ruby program that launches an API call to Twitter account '@qzynjls' to send a tweet.
cryptoc 0:a96892f55b07 20
cryptoc 0:a96892f55b07 21 -> To see how the program can interact with twitter API to tweet via mbed, import program "xAP_enable_oneWire_Master_Part2";
cryptoc 0:a96892f55b07 22
cryptoc 0:a96892f55b07 23
cryptoc 0:a96892f55b07 24 Python(3) and Ruby script for TwitterAPI:
cryptoc 0:a96892f55b07 25
cryptoc 0:a96892f55b07 26 -> master.py
cryptoc 0:a96892f55b07 27
cryptoc 0:a96892f55b07 28 import socket
cryptoc 0:a96892f55b07 29 from subprocess import Popen, PIPE, STDOUT
cryptoc 0:a96892f55b07 30 BROADCAST_PORT = 3639
cryptoc 0:a96892f55b07 31 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cryptoc 0:a96892f55b07 32 s.bind(('0.0.0.0', BROADCAST_PORT))
cryptoc 0:a96892f55b07 33
cryptoc 0:a96892f55b07 34 while True:
cryptoc 0:a96892f55b07 35 data, addr= s.recvfrom(500)
cryptoc 0:a96892f55b07 36
cryptoc 0:a96892f55b07 37 if addr[0]=='172.20.177.4':
cryptoc 0:a96892f55b07 38 print ('launching slave process...')
cryptoc 0:a96892f55b07 39 slave = Popen(['ruby', 'slave.rb'], stdin=PIPE, stdout=PIPE,
cryptoc 0:a96892f55b07 40 stderr=STDOUT)
cryptoc 0:a96892f55b07 41 # read user input, expression to be evaluated:
cryptoc 0:a96892f55b07 42 #line = input('Enter expression or exit:')
cryptoc 0:a96892f55b07 43 # write that line to slave's stdin
cryptoc 0:a96892f55b07 44 #slave.stdin.write(bytes(line+'\n', 'UTF-8'))
cryptoc 0:a96892f55b07 45 # result will be a list of lines:
cryptoc 0:a96892f55b07 46 result = []
cryptoc 0:a96892f55b07 47 print('hey there')
cryptoc 0:a96892f55b07 48 # read slave output line by line, until we reach "[end]"
cryptoc 0:a96892f55b07 49 while True:
cryptoc 0:a96892f55b07 50 print('hey there')
cryptoc 0:a96892f55b07 51 # check if slave has terminated:
cryptoc 0:a96892f55b07 52 if slave.poll() is not None:
cryptoc 0:a96892f55b07 53 print ('slave has terminated.')
cryptoc 0:a96892f55b07 54 exit()
cryptoc 0:a96892f55b07 55 # read one line, remove newline chars and trailing spaces:
cryptoc 0:a96892f55b07 56 line = slave.stdout.readline().rstrip()
cryptoc 0:a96892f55b07 57
cryptoc 0:a96892f55b07 58 print ('line:', line)
cryptoc 0:a96892f55b07 59 if line == '[end]':
cryptoc 0:a96892f55b07 60 break
cryptoc 0:a96892f55b07 61 result.append(line)
cryptoc 0:a96892f55b07 62 print ('result:')
cryptoc 0:a96892f55b07 63 print ('\n'.join(result))
cryptoc 0:a96892f55b07 64 sleep(10)
cryptoc 0:a96892f55b07 65 else:
cryptoc 0:a96892f55b07 66 print('detect xAP heartbeat, will not be tweeted!')
cryptoc 0:a96892f55b07 67
cryptoc 0:a96892f55b07 68 ->slave.rb
cryptoc 0:a96892f55b07 69
cryptoc 0:a96892f55b07 70 require 'rubygems'
cryptoc 0:a96892f55b07 71 require 'oauth'
cryptoc 0:a96892f55b07 72 require 'json'
cryptoc 0:a96892f55b07 73
cryptoc 0:a96892f55b07 74 consumer_key = OAuth::Consumer.new(
cryptoc 0:a96892f55b07 75   #add your own OAuth consumer key here )
cryptoc 0:a96892f55b07 76 access_token = OAuth::Token.new(
cryptoc 0:a96892f55b07 77   #add your own OAuth Token here )
cryptoc 0:a96892f55b07 78
cryptoc 0:a96892f55b07 79 # read command from standard input:
cryptoc 0:a96892f55b07 80 while true
cryptoc 0:a96892f55b07 81
cryptoc 0:a96892f55b07 82
cryptoc 0:a96892f55b07 83   # if command is "exit", terminate:
cryptoc 0:a96892f55b07 84     # else evaluate command, send result to standard output:
cryptoc 0:a96892f55b07 85
cryptoc 0:a96892f55b07 86     baseurl = "https://api.twitter.com"
cryptoc 0:a96892f55b07 87     path    = "/1.1/statuses/update.json"
cryptoc 0:a96892f55b07 88     address = URI("#{baseurl}#{path}")
cryptoc 0:a96892f55b07 89     request = Net::HTTP::Post.new address.request_uri
cryptoc 0:a96892f55b07 90     request.set_form_data(
cryptoc 0:a96892f55b07 91       "status" => "hello from bitmessage!",
cryptoc 0:a96892f55b07 92     )
cryptoc 0:a96892f55b07 93
cryptoc 0:a96892f55b07 94     # Set up HTTP.
cryptoc 0:a96892f55b07 95     http             = Net::HTTP.new address.host, address.port
cryptoc 0:a96892f55b07 96     http.use_ssl     = true
cryptoc 0:a96892f55b07 97     http.verify_mode = OpenSSL::SSL::VERIFY_PEER
cryptoc 0:a96892f55b07 98
cryptoc 0:a96892f55b07 99     # Issue the request.
cryptoc 0:a96892f55b07 100     request.oauth! http, consumer_key, access_token
cryptoc 0:a96892f55b07 101     http.start
cryptoc 0:a96892f55b07 102     response = http.request request
cryptoc 0:a96892f55b07 103
cryptoc 0:a96892f55b07 104     # Parse and print the Tweet if the response code was 200
cryptoc 0:a96892f55b07 105     tweet = nil
cryptoc 0:a96892f55b07 106     if response.code == '200' then
cryptoc 0:a96892f55b07 107         tweet = JSON.parse(response.body)
cryptoc 0:a96892f55b07 108         puts "Successfully sent #{tweet["text"]}"
cryptoc 0:a96892f55b07 109     else
cryptoc 0:a96892f55b07 110         puts "Could not send the Tweet! " +
cryptoc 0:a96892f55b07 111         "Code:#{response.code} Body:#{response.body}"
cryptoc 0:a96892f55b07 112     end
cryptoc 0:a96892f55b07 113     # and append [end] so that master knows it's the last line:
cryptoc 0:a96892f55b07 114     #print eval(cmd),"\n"
cryptoc 0:a96892f55b07 115     #cmd = STDIN.gets
cryptoc 0:a96892f55b07 116     # remove whitespaces:
cryptoc 0:a96892f55b07 117     #cmd.chop!
cryptoc 0:a96892f55b07 118     # if command is "exit", terminate:
cryptoc 0:a96892f55b07 119
cryptoc 0:a96892f55b07 120     print "mission acomplished! \n"
cryptoc 0:a96892f55b07 121     print "[end]\n"
cryptoc 0:a96892f55b07 122     # flush stdout to avoid buffering issues:
cryptoc 0:a96892f55b07 123     STDOUT.flush
cryptoc 0:a96892f55b07 124
cryptoc 3:d00432b8cb4a 125 end
cryptoc 3:d00432b8cb4a 126
cryptoc 3:d00432b8cb4a 127
cryptoc 3:d00432b8cb4a 128
cryptoc 3:d00432b8cb4a 129 **/