使用Thingspeak对讲来控制Arduino时出错

时间:2019-12-06 06:02:43

标签: arduino iot

我面临的错误是关于使用Thingspeak的对讲功能来控制我的Arduino LED。它无法执行对讲命令来关闭LED。错误在于我无法获得对讲。请帮助我

#include "ThingSpeak.h"
#include <Ethernet.h>
#define redLED 8
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F};

const String channelsAPIKey = "";
const String talkBackAPIKey = "";
const String talkBackID = "";
const String talkCommandID = "";
const unsigned int getTalkBackInterval = 10 * 1000;
const unsigned int updateChannelsInterval = 15 * 1000;
String talkBackCommand;


long lastConnectionTimeChannels = 0;
boolean lastConnectedChannels = false;
int failedCounterChannels = 0;
long lastConnectionTimeTalkBack = 0;
boolean lastConnectedTalkBack = false;
int failedCounterTalkBack = 0;
char charIn;



// Arduino Ethernet Client is initialized
EthernetClient client;

void setup()
{
  Ethernet.init(10);  // Most Arduino Ethernet hardware
  Serial.begin(9600);  //Initialize serial
  // start the Ethernet connection:
  pinMode(redLED, OUTPUT);
  digitalWrite(redLED, HIGH);
}
void loop()
{
  getTalkBack();

}

void getTalkBack()
{



  String tsData;
  tsData = talkBackID + "/commands/execute?api_key=" + talkBackAPIKey;
  if ((!client.connected() && (millis() - lastConnectionTimeTalkBack > getTalkBackInterval)))
  {
    if (client.connect("api.thingspeak.com", 80))
    {
      client.println("GET /talkbacks/" + tsData + " HTTP/1.0");
      client.println();
      lastConnectionTimeTalkBack = millis();
      if (client.connected())
      {
        Serial.println("---------------------------------------");
        Serial.println("GET TalkBack command");
        Serial.println();
        Serial.println("Connecting to ThingSpeak");
        Serial.println();
        Serial.println();
        Serial.println("Server response");
        Serial.println();
        failedCounterTalkBack = 0;

        while (client.connected() && !client.available()) delay(2000); //waits for data
        while (client.connected() || client.available())
        {
          charIn = client.read();
          talkBackCommand += charIn;
          Serial.print(charIn);
          if (talkBackCommand == "LED_ON")
          {
            digitalWrite(redLED, HIGH);
          }

          if (talkBackCommand == "LED_OFF")
          {
            digitalWrite(redLED, LOW);
          }
        }

        if (talkBackCommand = talkBackCommand.substring(talkBackCommand.indexOf("_CMD_") + 5));
        {
          Serial.println();
          Serial.println();
          Serial.println("Disconnected");
          Serial.println();
          Serial.println("--------");
          Serial.println();
          Serial.println("talkback command was");
          Serial.println();
          Serial.println("--------");
          Serial.println();
          Serial.println(talkBackCommand);
          Serial.println("--------");
          Serial.println();

        }

      }
      else
      {
        failedCounterTalkBack++;
        Serial.println("Connection to ThingSpeak failed (" + String(failedCounterTalkBack, DEC) + ")");
        Serial.println();
        lastConnectionTimeChannels = millis();
      }
    }
  }
  if (failedCounterTalkBack > 3 )
  {
    startEthernet();
  }
  client.stop();
  Serial.flush();
}

下面是我的串行监视器的图像。它表明我可以捕获命令但无法执行。

1 个答案:

答案 0 :(得分:0)

好像您正在读取所有接收到的字节并将其附加到talkBackCommand。因此,talkBackCommand是“ HTTP / 1.1 200 OK Date ....”,而if (talkBackCommand == "LED_ON")永远不会是true

我认为您想查看接收到的数据是否包含命令LED_ONLED_OFF。您可以执行以下操作:

if (talkBackCommand.indexOf("LED_ON") != -1)

indexOf()String中找到一个String,并返回索引;如果找不到,则返回-1

在此处查看有关indexOf()的更多信息:https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/indexof/