我是新来的,如果事先问我对不起,我找不到相关的问题。
我编辑了一个获取thingspeak频道数据的代码,用于arduino uno上8频道的led或中继。根据数据1或0,led将打开或关闭。我正在使用esp8266。
espSerial.find("+IPD,1:0")块无法正常工作。
#include <SoftwareSerial.h>
SoftwareSerial espSerial(2, 3);
#define DEBUG true
String mySSID = "ssid";
String myPWD = "pass";
String myAPI = "CV4YEARDB91GTOXM"; // API Key
String myHOST = "api.thingspeak.com";
String myPORT = "80";
String myFIELD = "field1";
void setup()
{
pinMode(8,OUTPUT);
Serial.begin(9600);
espSerial.begin(115200);
espData("AT+RST", 1000, DEBUG);
espData("AT+CWMODE=1", 1000, DEBUG);
espData("AT+CWJAP=\""+ mySSID +"\",\""+ myPWD +"\"", 1000, DEBUG);
delay(1000); }
void loop()
{
String sendData = "GET /channels/716457/fields/1/last?key=CV4YEARDB91GTOXM";
espData("AT+CIPMUX=1", 1000, DEBUG);
espData("AT+CIPSTART=0,\"TCP\",\""+ myHOST +"\","+ myPORT, 1000, DEBUG);
espData("AT+CIPSEND=0," +String(sendData.length()+4),1000,DEBUG);
espSerial.find(">");
espSerial.println(sendData);
espData("AT+CIPCLOSE=0",1000,DEBUG);
delay(10000);
}
String espData(String command, const int timeout, boolean debug)
{
Serial.print(command);
Serial.println(" ");
String response = "";
espSerial.println(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (espSerial.available())
{
if (espSerial.find("+IPD,1:0")) {
digitalWrite(8,LOW); }
if (espSerial.find("+IPD,1:1")) {
digitalWrite(8,HIGH); }
}
}
if (debug)
{
Serial.print(response);
}
return response;
}发布于 2019-03-09 17:43:49
这是我的意图的解决方案,通过使用+IPD answer从thingspeak频道获取数据:
String espData(String command, const int timeout, boolean debug)
{
Serial.print(command);
Serial.println(" ");
String response = "";
espSerial.println(command);
long int time = millis();
while ( (time + timeout) > millis())
{
if (espSerial.available()>0)
{
if (espSerial.find("+IPD,0,1:")); {
while (espSerial.available()>0) {
String gelen = "";
char serialdenokunan;
serialdenokunan = espSerial.read();
gelen += serialdenokunan;
Serial.println(gelen);
if (gelen.indexOf("0")>=0) {
digitalWrite(8,LOW); }
if (gelen.indexOf("1")>=0) {
digitalWrite(8,HIGH); }
}}}
}
if (debug)
{
Serial.print(response);
}
return response;
}在单个连接的情况下,AT+CIPMUX=0,而不是IPD答案是+IPD,0:数据而不是+IPD,0,connectionnumber:数据。然后如果(espSerial.find("+IPD,0:"))在这种情况下工作。
感谢所有通过消息和帖子回复的人。
发布于 2019-04-14 03:07:12
有一种更简单的方法可以做到这一点。ThingSpeak在GitHub上创建了库,该库将处理与ThingSpeak通道之间的数据写入和读取。您可以尝试一个包含在库中的示例。
https://stackoverflow.com/questions/55055620
复制相似问题