NAO to NAO robot communication with ThingSpeak

Objectives:

  1. Allow a NAO to communicate data/sysnchronisation signals with another NAO.
  2. Will attempt to use ThingSpeak (ThingSpeak POST/GET docs) (ThingSpeak API) (ThingSpeak source and other info on GitHub) using an example from the BrickPi.

How does NAO connect to the internet when away from home?

Using iPhone HotSpot. Works. If connecting to NAO with a computer the computer must be on the same hotspot.

Can NAO use ThingSpeak?


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()


Overwriting NAO_to_ThingSpeak.py

In [10]:
!python NAO_to_ThingSpeak.py


0 {"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {}}}}
200 OK
1 {"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {}}}}
200 OK
2 {"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {}}}}
200 OK
3 {"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {}}}}
200 OK
4 {"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {}}}}
200 OK

Reading data back from ThingSpeak


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()


Overwriting NAO_from_ThingSpeak.py

In [12]:
!python NAO_from_ThingSpeak.py


url:  /channels/12012/feeds.json?results=5
200 OK
{"channel":{"id":12012,"name":"NAO_to_ThingSpeak_test","description":"Test if NAO robot can speak to ThingSpeak","field1":"NAO_1","created_at":"2014-05-02T16:27:29Z","updated_at":"2014-05-03T14:02:31Z","last_entry_id":40},"feeds":[{"created_at":"2014-05-03T14:01:14Z","entry_id":36,"field1":"{\"glossary\": {\"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",\"GlossList\": {}}}}"},{"created_at":"2014-05-03T14:01:30Z","entry_id":37,"field1":"{\"glossary\": {\"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",\"GlossList\": {}}}}"},{"created_at":"2014-05-03T14:01:47Z","entry_id":38,"field1":"{\"glossary\": {\"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",\"GlossList\": {}}}}"},{"created_at":"2014-05-03T14:02:03Z","entry_id":39,"field1":"{\"glossary\": {\"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",\"GlossList\": {}}}}"},{"created_at":"2014-05-03T14:02:20Z","entry_id":40,"field1":"{\"glossary\": {\"title\": \"example glossary\",\"GlossDiv\": {\"title\": \"S\",\"GlossList\": {}}}}"}]}

In [ ]: