1

I 'm trying to write a code which will send a json file from a flask server to chregraphe in order to program a NAO robot. I want to parse a random number from 1 to 3 and NAO say the number accordingly. Unfortunately i haven't managed to do it.

The code that i use to send the number from flask server is

import http.client
import json
import random
from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    IP = "10.0.0.23"  # The IP address of my robot
    PORT = 9559

    
    number = random.randint(1, 3)

    data = json.dumps({"number": number})

    # Connect to Choregraphe
    conn = http.client.HTTPConnection(IP, PORT)

    # Send the data to Choregraphe
    headers = { "Content-Type": "application/json" }
    conn.request("POST", "/module/ALMemory/value/ExampleKey", data, headers)

    return "Data sent to Choregraphe: " + data

if __name__ == '__main__':
    app.run()

I'm trying to send the number to the "Examplekey"

Also i use a python script box in choregraph with the beneath code

def onInput_onStart(self, p):
    # Connect to ALMemory
    memory = ALProxy("ALMemory")

    # Read the value stored under the key "ExampleKey"
    data = memory.getData("ExampleKey")

    # Parse the JSON string into a dictionary
    data = json.loads(data)

    # Get the "number" value from the dictionary
    number = data["number"]

   if number == 1:
     text = "one"
   elif number == 2:
     text = "two"
  else
    text = "three"


  tts = ALProxy("ALTextToSpeech", "10.0.0.23", 9559)
  tts.say(text)

1 Answers1

0

Welcome Tolis to StackOverflow!

While it might be starting a Flask server, your first snippet of code seems rather focused on sending an HTTP request to http://10.0.0.23:9559, where I suppose NAOqi is listening to - and NAOqi does not "speak" HTTP, it speaks Qi.

If you want to send data via HTTP, you will need to get a Flask server running on the robot, and call the relevant NAOqi APIs in response.

But bringing Flask to the robot (via Choregraphe) is no easy task. Instead I would recommend that your client code directly uses the Python SDK for NAOqi 2.5 (better for Python 2.x) or libQi Python (better for Python 3.x).

The client code would look like this (not tested):

import qi

if __name__ == "__main__":
    app = qi.Application(sys.argv)
    app.start()
    memory = session.service("ALMemory")
    number = random.randint(1, 3)
    service.setData("ExampleKey", number)
Victor Paléologue
  • 2,025
  • 1
  • 17
  • 27