问题是 +IPD 响应的值没有显示在 ReadLastEntryThingSpeak() 中,所以我的代码不正确或者我必须添加一些东西?我搜索了很多网站 Arduino 论坛、Thingspeak 论坛等。但我没有看到任何解决方案。:(
我尝试了这个系统 5 天,但它不起作用,为什么?
#include <SoftwareSerial.h>
#include <string.h>
#define esp8266 Serial1
#define SSID "test" // put here the name of your wifi network
#define PASS "lolman123"
#define IP "184.106.153.149"
int counter = 120;
String keep;
String valuetosend;
const byte maxDataLength = 30; // maxDataLength is the maximum length
allowed for received data.
char receivedChars[31] ;
boolean newData = false;
String GET = "GET /update?key=FYPW04TOYR3FX2QH&"; // put here your
thingspeak key
String GET1 = "field1=";
String readGET1 = "GET
https://api.thingspeak.com/channels/864152/fields/1/last";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
esp8266.begin(115200);
esp8266.println("AT");
delay(5000);
if(esp8266.find("OK")){
connectWiFi();
Serial.println("WIFI CONNECTED");
}
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
newData = false;
}
void loop() {
// put your main code here, to run repeatedly:
int readBtn = digitalRead(7);
int readSend = digitalRead(8);
if (readBtn == 0)
{
ReadLastEntryThingSpeak();
}
if (readSend == 0)
{
WriteThingSpeak();
}
}
boolean connectWiFi(){
esp8266.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
esp8266.println(cmd);
delay(5000);
if(esp8266.find("OK")){
Serial.println("OK");
return true;
}else{
Serial.println("KO");
return false;
}
}
void updateFunction(String valuetosend){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
esp8266.println(cmd);
delay(2000);
if(esp8266.find("Error")){
Serial.print("Error1");
return;
}
cmd = GET + GET1;
cmd += valuetosend;
cmd += "\r\n";
Serial.print(cmd);
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
esp8266.print(cmd);
}else{
esp8266.println("AT+CIPCLOSE");
}
}
void ReadLastEntryThingSpeak()
{
String cmd = "AT+CIPSTART=\"TCP\",\""; //connect to thingspeak
cmd += IP;
cmd += "\",80";
esp8266.println(cmd);
delay(2000);
if(esp8266.find("Error")){
Serial.print("Error1");
return;
}
cmd = readGET1;
cmd += "\r\n";
Serial.print(cmd);
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
esp8266.print(cmd);
}else{
esp8266.println("AT+CIPCLOSE");
}
delay(2000);
recvWithStartEndMarkers(); // check to see if we have
received any new commands
if (newData) { processCommand(); }
}
void WriteThingSpeak ()
{
String valuetosend = String(counter);
updateFunction(valuetosend);
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = ':';
char endMarker = 'C';
if (esp8266.available() > 0)
{
char rc = esp8266.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
if (ndx < maxDataLength) { receivedChars[ndx] = rc;
ndx++; }
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) { recvInProgress = true; }
}
}
void processCommand()
{
Serial.print("Recieved data = "); Serial.println(receivedChars);
newData = false;
}
我有 Received data = 的结果,但实际输出是 Received data = 120。