Académique Documents
Professionnel Documents
Culture Documents
def repeat():
temp1=analog_read(3)
mV = round((temp1 * (Vref / 1024.0)),2)
temp = round(((mV - 500) / 10.0), 2)
print temp
time.sleep(1)
humi=analog_read(2)
print humi
time.sleep(1)
params = urllib.urlencode({'field1': temp, 'field2': humi, 'key':'XXXXXXXXXXXX'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
conn.close()
except:
print "connection failed"
if __name__ == "__main__":
while True:
repeat()
time.sleep(16)
Q3) IoT Based Appliance Control using MQTT Protocol
Solution:
pip install paho-maqtt
if (str(msg.payload) == "ONLED1"):
GPIO.output(3,True)
return 0
if (str(msg.payload) == "ONLED2"):
GPIO.output(5,True)
return 0
if (str(msg.payload) == "OFFLED1"):
GPIO.output(3,False)
return 0
if (str(msg.payload) == "OFFLED2"):
GPIO.output(5,False)
return 0
#main program
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
# Endless loop waiting to receive messages. .
client.loop_forever()
Keys and tokens