Advertisement
cymplecy

sgh_weather.py

Mar 16th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import scratch
  3. import os as os
  4. import paho.mqtt.client as mqtt
  5. import time as time
  6. from sgh_GetJSONFromURL import GetJSONFromURL
  7.  
  8. getjsonfromurl = GetJSONFromURL()
  9.  
  10.  
  11.    
  12. def listen():
  13.     while True:
  14.         try:
  15.            yield s.receive()
  16.         except scratch.ScratchError:
  17.            raise StopIteration
  18.  
  19. s = scratch.Scratch()
  20.  
  21.  
  22. result = False
  23. while result is False:
  24.     try:
  25.         # connect
  26.         s.connect()
  27.         print ("connected")
  28.         result = True
  29.     except:
  30.         print "waiting"
  31.         pass
  32.     time.sleep(1)
  33.  
  34.  
  35. data = getjsonfromurl.getJSON("http://api.openweathermap.org/data/2.5/weather?q=Chorley,uk&appid=4041655e60abaea9a9134b6e78ca864f")
  36. print data
  37. s.sensorupdate({"main_temp" : data.get("main_temp")})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement