我想使用arduino uno和传感器来监视2HP IM的状况,并希望在Thingspeak中使用esp8266将数据推送到云中。但是我在编译时仍然收到错误“ Softrere serial esp8266”被重新声明为另一种符号。请解决这个问题 这是我的编码:
#include "SoftwareSerial.h"
#define RX 10
#define TX 11
String AP = "wifi name"; // CHANGE ME
String PASS = "wifi password"; // CHANGE ME
String API = "api key"; // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX,false,256);
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
valSensor = getSensorData();
String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
int getSensorData(){
return random(1000); // Replace with
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
这是我得到的错误:
Arduino: 1.8.9 (Windows 8), Board: "Generic ESP8266 Module, 80 MHz, Flash, Disabled, ck, 26 MHz, 40MHz, DOUT (compatible), 512K (no SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"
C:\Users\Admin\Desktop\n12\n12.ino: In function 'void setup()':
n12:19:10: error: expected primary-expression before '.' token
esp8266.begin(115200);
^
C:\Users\Admin\Desktop\n12\n12.ino: In function 'void loop()':
n12:30:9: error: expected primary-expression before '.' token
esp8266.println(getData);delay(1500);countTrueCommand++;
^
C:\Users\Admin\Desktop\n12\n12.ino: In function 'void sendCommand(String, int, char*)':
n12:43:12: error: expected primary-expression before '.' token
esp8266.println(command);//at+cipsend
^
n12:44:15: error: expected primary-expression before '.' token
if(esp8266.find(readReplay))//ok
^
exit status 1
expected primary-expression before '.' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.