Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Anybody need any work?
#29
Leaving this here for posterity. Its a python script that posts smarthings data to thingspeak.

http://amazingrcvideos.blogspot.com/2013...cript.html

Code:
# Import module statements
import re, sys, httplib, urllib
from bs4 import BeautifulSoup
from mechanize import ParseResponse, urlopen, urljoin

# To read the information about a device, you will need the proper device ID. Enter device ID from the web URL in place of XXXXXXXXXX.
if len(sys.argv) == 1:
    uri = "https://graph.api.smartthings.com/device/show/XXXXXXXXXX"
else:
    uri = sys.argv[1]

# Open URL and parse out information to see what forms are required to login
response = urlopen(urljoin(uri, ""))
forms = ParseResponse(response, backwards_compat=False)
form = forms[0]
# Uncomment line below to print out the form names used for logging in
#print form

# Enter information for username and password. Must put information in quotes.
form["j_username"] = "username"
form["j_password"] = "password"

# Send login information and read webpage data
text_data = urlopen(form.click()).read()

# START DATA PARSING
# Get HTML code of web page to parse through
soup = BeautifulSoup(text_data)

# Find line that has desired data. This case grabs the line with temperature.
text = soup.find_all(text=re.compile("temperature"))

# Get indices to parse out desired data
start_idx = text[0].index(':')+2
end_idx = text[0].index('F')-1

# Store information in a variable
var1 = text[0][start_idx:end_idx]

# Repeat for other sensors
text_data = urlopen('https://graph.api.smartthings.com/device/show/YYYYYYYY').read()
soup = BeautifulSoup(text_data)
text = soup.find_all(text=re.compile("temperature"))
start_idx = text[0].index(':')+2
end_idx = text[0].index('F')-1
var2 = text[0][start_idx:end_idx]

# SEND DATA TO THINGSPEAK
# Package data together in the next line. The temp data is low by about 4 degrees so I added 4. Get your API KEY from the Channel you set up on ThingSpeak.
params = urllib.urlencode({'field1': str(int(var1)+5), 'field2': str(int(var2)+5), 'key':'YOUR API KEY'})

# Other code to send information to ThingSpeak
headers = {"Content-type": "application/x-www-form-urlencoded","Accept":"text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
#print response.status, response.reason
data = response.read()
conn.close()
Reply


Messages In This Thread
Anybody need any work? - by Dean Roddey - 07-28-2012, 06:25 PM
Anybody need any work? - by MikeMelga - 07-31-2012, 08:09 AM
Anybody need any work? - by Dean Roddey - 07-31-2012, 09:52 AM
Anybody need any work? - by MikeMelga - 08-01-2012, 03:15 AM
Anybody need any work? - by wuench - 08-01-2012, 04:42 AM
Anybody need any work? - by MikeMelga - 08-01-2012, 07:46 AM
Anybody need any work? - by Bugman - 05-10-2013, 07:28 AM
Anybody need any work? - by Dean Roddey - 05-10-2013, 10:43 AM
Anybody need any work? - by Bugman - 05-10-2013, 11:01 AM
Anybody need any work? - by Dean Roddey - 05-10-2013, 11:15 AM
Anybody need any work? - by Bugman - 05-10-2013, 11:16 AM
Anybody need any work? - by Bugman - 05-11-2013, 10:53 AM
Anybody need any work? - by Chad - 06-07-2013, 02:18 PM
Anybody need any work? - by Dean Roddey - 06-07-2013, 02:36 PM
Anybody need any work? - by Dean Roddey - 06-07-2013, 03:45 PM
Anybody need any work? - by Dean Roddey - 10-21-2014, 08:04 AM
Anybody need any work? - by potts.mike - 10-21-2014, 07:15 PM
Anybody need any work? - by Dean Roddey - 10-21-2014, 07:58 PM
Anybody need any work? - by potts.mike - 10-21-2014, 08:06 PM
Anybody need any work? - by Dean Roddey - 10-21-2014, 08:16 PM
Anybody need any work? - by potts.mike - 10-22-2014, 03:57 PM
Anybody need any work? - by Dean Roddey - 10-22-2014, 04:11 PM
Anybody need any work? - by dgage - 10-22-2014, 05:51 PM
Anybody need any work? - by Dean Roddey - 10-22-2014, 06:07 PM
Anybody need any work? - by potts.mike - 10-22-2014, 06:49 PM
Anybody need any work? - by Dean Roddey - 10-22-2014, 07:29 PM
Anybody need any work? - by potts.mike - 10-22-2014, 07:36 PM
Anybody need any work? - by Dean Roddey - 10-23-2014, 09:37 AM
Anybody need any work? - by potts.mike - 11-11-2014, 08:25 PM
Anybody need any work? - by Dean Roddey - 11-11-2014, 09:27 PM
Anybody need any work? - by Dean Roddey - 11-24-2014, 02:06 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)