need help with Talk back, commands and fields

조회 수: 2 (최근 30일)
Pavel Plotinnov
Pavel Plotinnov 2020년 2월 15일
댓글: Vinod 2020년 2월 19일
Hi,
I try to control some relays thr TalkBack app.
Using ESP 8266.
Everything connecting to Internet.
I got stuck on how to activate relay. I used Key, used ID. I cant find how to send command thru TalkBack
I dont know what Command ID for, and how to send command string.
Need some help please
Also here is my code and some screenshots:
#include "ThingSpeak.h"
#include "ESP8266WiFi.h"
const char ssid[] = "XXX"; // your network SSID (name)
const char pass[] = "YYY"; // your network password
#define RELAY1 12
#define RELAY2 13
#define RELAY3 14
#define RELAY4 16
WiFiClient client;
//---------Channel Details---------//
unsigned long counterChannelNumber = 123; // Channel ID
const char * myCounterReadAPIKey = "ZZZ"; // Read API Key
const int FieldNumber1 = 1; // The field you wish to read
const int FieldNumber2 = 2; // The field you wish to read
const int FieldNumber3 = 3; // The field you wish to read
const int FieldNumber4 = 4; // The field you wish to read
//-------------------------------//
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop()
{
//----------------- Network -----------------//
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println(" ....");
while (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass);
delay(5000);
}
Serial.println("Connected to Wi-Fi Succesfully.");
}
//--------- End of Network connection--------//
//---------------- Channel 1 ----------------//
Serial.print("Reading field 1 ");
long R_Status = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
int statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200)
{
Serial.print("Read field 1 Successful ");
// Serial.println(temp);
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
Serial.println(R_Status);
if(R_Status==1)
{
digitalWrite(RELAY1, HIGH); //Relay 1 ON
Serial.println("Relay 1 ON");
}
if(R_Status==2)
{
digitalWrite(RELAY1, LOW); //Relay 1 OFF
Serial.println("Relay 1 OFF");
}
Serial.print("Reading field 2 ");
delay(5000);
long R_Status2 = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
int statusCode2 = ThingSpeak.getLastReadStatus();
if (statusCode2 == 200)
{
Serial.print("Read field 2 Successful ");
// Serial.println(temp);
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
Serial.println(R_Status2);
if(R_Status2==1)
{
digitalWrite(RELAY2, HIGH); //Relay 2 ON
Serial.println("Relay 1 ON");
}
if(R_Status2==2)
{
digitalWrite(RELAY2, LOW); //Relay 2 OFF
Serial.println("Relay 1 OFF");
}
Serial.print("Reading field 3 ");
delay(5000);
long R_Status3 = ThingSpeak.readLongField(counterChannelNumber, FieldNumber3, myCounterReadAPIKey);
int statusCode3 = ThingSpeak.getLastReadStatus();
if (statusCode3 == 200)
{
Serial.print("Read field 3 Successful ");
// Serial.println(temp);
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
Serial.println(R_Status3);
if(R_Status3==1)
{
digitalWrite(RELAY3, HIGH); //Relay 3 ON
Serial.println("Relay 3 ON");
}
if(R_Status3==2)
{
digitalWrite(RELAY3, LOW); //Relay 3 OFF
Serial.println("Relay 3 OFF");
}
Serial.print("Reading field 4 ");
delay(5000);
long R_Status4 = ThingSpeak.readLongField(counterChannelNumber, FieldNumber4, myCounterReadAPIKey);
int statusCode4 = ThingSpeak.getLastReadStatus();
if (statusCode4 == 200)
{
Serial.print("Read field 4 Successful ");
// Serial.println(temp);
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
Serial.println(R_Status4);
if(R_Status4==1)
{
digitalWrite(RELAY4, HIGH); //Relay 4 ON
Serial.println("Relay 4 ON");
}
if(R_Status4==2)
{
digitalWrite(RELAY4, LOW); //Relay 4 OFF
Serial.println("Relay 4 OFF");
}
delay(10000); // ThingSpeak will only accept updates every 10 seconds.
}

답변 (2개)

Vinod
Vinod 2020년 2월 15일
편집: Vinod 2020년 2월 15일
Have you taken a look at the Thingspeak examples? This one might be relevant to what you're trying to do:
  댓글 수: 6
Pavel Plotinnov
Pavel Plotinnov 2020년 2월 18일
Hi Vinod,
I found a different solution. I created library with key and all other needed information.
Now i am stuck at how to send command.
Could you please help me with sending commands?
#include "ThingSpeak.h"
#include "secrets.h"
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
#include <ESP8266WiFi.h>
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key index number (needed only for WEP)
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
String tbURI = String("/talkbacks/") + String(myTalkBackID) + String("/commands/execute");
// Create the message body for the POST out of the values
String postMessage = String("api_key=") + String(myTalkBackKey);
// Make a string for any commands 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.print(" Latest command from queue: ");
Serial.println(newCommand);
if(newCommand == "TURN_ON"){
digitalWrite(LED_BUILTIN, HIGH);
}
if(newCommand == "TURN_OFF"){
digitalWrite(LED_BUILTIN, LOW);
}
}
else{
Serial.println(" Nothing new.");
}
}
else{
Serial.println("Problem checking queue. HTTP error code " + String(x));
}
delay(60000); // Wait 60 seconds to check queue again
}
// 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\r\n") +
String("Host: api.thingspeak.com\r\n") +
String("Content-Type: application/x-www-form-urlencoded\r\n") +
String("Connection: close\r\n") +
String("Content-Length: ") + String(postMessage.length()) +
String("\r\n\r\n");
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<char *>("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<char *>("\n\r\n"))){
return -303;
}
String tempString = String(client.readString());
response = tempString;
return status;
}
#define SECRET_SSID "VODAFONE-2187" // replace MySSID with your WiFi network name
#define SECRET_PASS "PAVEL2020" // replace MyPassword with your WiFi password
#define SECRET_CH_ID 000000 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "L9OFNUSRO8DXUKTZ"
#define myTalkBackID "37487"
#define myTalkBackKey "8GG3QMZN8A0T68NO"
Pavel Plotinnov
Pavel Plotinnov 2020년 2월 18일
as I understand I should go to
https://api.thingspeak.com/talkbacks/37487/commands/execute.json
api_key=
8GG3QMZN8A0T68NO
in order to execute command.
But the page doesnt open

댓글을 달려면 로그인하십시오.


Pavel Plotinnov
Pavel Plotinnov 2020년 2월 18일
How to execute commands on TalkBack?
  댓글 수: 3
Pavel Plotinnov
Pavel Plotinnov 2020년 2월 19일
working on it
Vinod
Vinod 2020년 2월 19일
Please change GET to POST in postman.
Please refer to the help on the right side of the Talkback page.

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Act on Data에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by