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

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

readme.txt

Committer:
cryptoc
Date:
2016-02-18
Revision:
3:d00432b8cb4a
Parent:
0:a96892f55b07

File content as of revision 3:d00432b8cb4a:

/** 


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 



**/