Objectives:
Using iPhone HotSpot. Works. If connecting to NAO with a computer the computer must be on the same hotspot.
In [1]:
%%file NAO_to_ThingSpeak.py
""" A simple example to test if NAO can connect to thingspeak.com.
"""
from naoqi import ALProxy
import httplib, urllib
import time
# NAO_IP = "mistcalf.local"
NAO_IP = "192.168.0.13"
tts = ALProxy("ALTextToSpeech", NAO_IP, 9559)
def main():
tts.say("I'm going to connect to the internet now!")
for i in range (5):
params = urllib.urlencode({'field1': i, 'key':'9PBYIQ1RWXJ6XZBO'}) # use your API key generated in the thingspeak channels for the value of 'key'
headers = {"Content-typZZe": "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()
print i, msg
print response.status, response.reason
data = response.read()
conn.close()
except:
print "connection failed"
tts.say("I sent some stuff to the internet!")
# If using ThingSpeak web service there is a 15s limit. Install locally or on own webserver for faster usage.
time.sleep(16)
tts.say("Yay, all sent!")
if __name__ == "__main__":
main()
In [10]:
!python NAO_to_ThingSpeak.py
In [2]:
%%file NAO_from_ThingSpeak.py
""" A simple example to test if NAO can get data from thingspeak.com.
"""
from naoqi import ALProxy
import httplib, urllib
import time
# NAO_IP = "mistcalf.local"
#NAO_IP = "192.168.0.13"
tts = ALProxy("ALTextToSpeech", NAO_IP, 9559)
def main():
tts.say("I'm going to get some data from the internet now!")
# use your API key generated in the thingspeak channels for the value of 'key'
# nb Different API key to read.
conn = httplib.HTTPConnection("api.thingspeak.com:80")
channel_id = 12012
field_id = "NAO_1"
url = "/channels/" + str(channel_id) + "/feeds.json?results=5"
print "url: ", url
try:
conn.request("GET", url)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
conn.close()
except:
print "connection failed"
tts.say("I got some stuff from the internet!")
if __name__ == "__main__":
main()
In [12]:
!python NAO_from_ThingSpeak.py
In [ ]: