9:18 am

May 14, 2020

I am trying to set up a simple talk back function that will turn an LED on and OFF. I have followed the tutorial here https://ch.mathworks.com/help/thingspeak/control-a-light-with-talkback-and-esp32.html and have verified that the web side of the example works correctly (I can click buttons on a website and add talkback commands in the queue normally).
When I click to add a command, the terminal shows it is received and prints
6
LED_ON
0
However when I try to compare it to the set command string (if(newCommand == "LED_ON")) as shown in the example I linked, it doesn't work. I tried using the compare function of the String class, but it still doesn't work. I'm not sure what the issue is. Shouldn't the string newCommand contain only the command itself as a string? Why does it also print the string length and then a 0?
The following function is called in loop(). There is no other code running at this point (only the necessary pin setup etc for the LED). It was directly copied from the above tutorial and, and only the names of the talkBackID and API key were modified.
Any help is greatly appreciated.
void checkTalkBack()
{ // Create the TalkBack URI
String tbURI = String("/talkbacks/") + String(talkBackID) + String("/commands/execute");
// Create the message body for the POST out of the values
String postMessage = String("api_key=") + String(talkBackAPIKey);
// Make a string for any commands that might be in the queue
String newCommand = String();
// Make the POST to ThingSpeak
int x = httpPOST(tbURI, postMessage, newCommand);
client.stop();
// Check the result
if(x == 200){
Serial.println("checking queue...");
// check for a command returned from TalkBack
if(newCommand.length() != 0){
Serial.println(" Latest command from queue: ");
Serial.println(newCommand.c_str());
if(newCommand == "LED_ON"){
Serial.println("OK");
digitalWrite(LED_BUILTIN, HIGH);
}
if(newCommand == "LED_OFF"){
Serial.println("OK");
digitalWrite(LED_BUILTIN, LOW);
}
}
else{
Serial.println(" Nothing new.");
}
}
else{
Serial.println("Problem checking queue. HTTP error code " + String(x));
}
}
// General function to POST to ThingSpeak
int httpPOST(String uri, String postMessage, String &response){
bool connectSuccess = false;
connectSuccess = client.connect("api.thingspeak.com",80);
if(!connectSuccess){
return -301;
}
postMessage += "&headers=false";
String Headers = String("POST ") + uri + String(" HTTP/1.1
") +
String("Host: api.thingspeak.com
") +
String("Content-Type: application/x-www-form-urlencoded
") +
String("Connection: close
") +
String("Content-Length: ") + String(postMessage.length()) +
String("
");
client.print(Headers);
client.print(postMessage);
long startWaitForResponseAt = millis();
while(client.available() == 0 && millis() - startWaitForResponseAt < 5000){
delay(100);
}
if(client.available() == 0){
return -304; // Didn't get server response in time
}
if(!client.find(const_cast("HTTP/1.1"))){
return -303; // Couldn't parse response (didn't find HTTP/1.1)
}
int status = client.parseInt();
if(status != 200){
return status;
}
if(!client.find(const_cast("
"))){
return -303;
}
String tempString = String(client.readString());
response = tempString;
return status;
}
6:23 pm


Moderators
March 7, 2017

Thanks for pointing this out. We also recently ran into this issue and were able to fix it using this code
if (newCommand.indexOf("TURN_ON")>0){
Serial.println("OPEN the door");
digitalWrite(LED_BUILTIN, HIGH);
}
I think the HTTP post function is the root of the error - and it didn't do this when we first wrote the code. Also it only seems to happen for ESP32, i tried on some other devices and did not get this effect.
My guess is that its in the wifi library. If you were to downgrade one version, my guess is that we would not have this problem.
The code above worked for us, we have recorded the problem and will plan to fix it in a future release. Let us know if that code works for you.
FYI, MATLAB answers is the preferred location for ThingSpeak Community support.
Most Users Ever Online: 166
Currently Online:
24 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
rw950431: 272
Vinod: 240
piajola: 95
turgo: 70
vespapierre: 63
Adarsh_Murthy: 62
Member Stats:
Guest Posters: 1
Members: 8665
Moderators: 1
Admins: 2
Forum Stats:
Groups: 3
Forums: 14
Topics: 1600
Posts: 5760
Newest Members:
huldacormier, bogart, estebanmateo93, mikewrite, onlinebusiness, winzy99Moderators: cstapels: 460
Administrators: Hans: 405, lee: 457