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 [17]:
%%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
            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 [18]:
!python NAO_to_ThingSpeak.py


[I] 4359 qi.eventloop: Creating event loop while no qi::Application() is running
[I] 4359 qimessaging.session: Session listener created on tcp://0.0.0.0:0
[I] 4359 qi.eventloop: Creating event loop while no qi::Application() is running
[I] 4359 qimessaging.transportserver: TransportServer will listen on: tcp://169.254.9.8:53208
[I] 4359 qimessaging.transportserver: TransportServer will listen on: tcp://192.168.0.10:53208
[I] 4359 qimessaging.transportserver: TransportServer will listen on: tcp://127.0.0.1:53208
[I] 4359 qimessaging.transportserver: TransportServer will listen on: tcp://10.137.0.26:53208
0
200 OK
1
200 OK
2
200 OK
3
200 OK
4
200 OK

Reading data back from ThingSpeak


In [130]:
%%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" + "&key=6K5W4CH476UP7F9"
    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 [131]:
!python NAO_from_ThingSpeak.py


[I] 4359 qi.eventloop: Creating event loop while no qi::Application() is running
[I] 4359 qimessaging.session: Session listener created on tcp://0.0.0.0:0
[I] 4359 qi.eventloop: Creating event loop while no qi::Application() is running
[I] 4359 qimessaging.transportserver: TransportServer will listen on: tcp://169.254.9.8:53770
[I] 4359 qimessaging.transportserver: TransportServer will listen on: tcp://192.168.0.10:53770
[I] 4359 qimessaging.transportserver: TransportServer will listen on: tcp://127.0.0.1:53770
[I] 4359 qimessaging.transportserver: TransportServer will listen on: tcp://10.137.0.26:53770
url:  /channels/12012/feeds.json?results=5&key=6K5W4CH476UP7F9
400 Bad Request
-1

In [ ]: