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)