我正在尝试使用ESP8266模块(slave)使用Arduino(master)将数据上传到thingspeak.com。但是AT+CIPSEND
命令不会返回">"调用时(或串口无法找到它)。当我上传空白草图并在串行监视器中使用AT命令时,我能够上传数据。
硬件连接:
答案 0 :(得分:0)
尝试此代码并确保连接正确,波特率应为9600。
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
#define DEBUG true
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mySerial.begin(9600);
sendData("AT+RST\r\n", 2000, DEBUG); // rst
sendData("AT+CWJAP=\"wifi namne\",\"password\"\r\n", 3000, DEBUG); //enter name and password
}
void loop() {
int key;
sendData("AT+CWMODE=1\r\n", 1000, DEBUG); // access point
sendData("AT+CIFSR\r\n", 1000, DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections
sendData("AT+CIPSTART=4,\"TCP\",\"api.thingspeak.com\",80\r\n", 1000, DEBUG); // turn on server on port 80
key = 23; //random number for testing
String web = "GET http://api.thingspeak.com/update?api_key=KTQXXXXXXXXXXXXX&field1="";
web += key;
web += "HTTP / 1.0";
web += "\r\n";
web += "\r\n";
String cipsend = "AT + CIPSEND = ";
cipsend += 4;
cipsend += ", ";
cipsend += String(web.length());
cipsend += "\r\n";
sendData(cipsend, 1000, DEBUG);
sendData(web, 10000, DEBUG);
String closeCommand = "AT + CIPCLOSE = ";
closeCommand += 4;// append connection id
closeCommand += "\r\n";
sendData(closeCommand, 3000, DEBUG);
}
String sendData(String command, const int timeout, boolean debug) {
String response = "";
mySerial.print(command); // send the read character to the esp8266
long int time = millis();
while ( (time + timeout) > millis()) {
while (mySerial.available()) {
// output to the serial window
char c = mySerial.read(); // read the next character.
response += c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}
将ESP的Rx和Tx引脚连接到Arduino的引脚2和3。
答案 1 :(得分:0)
我建议您使用LM1117 3.3V IC作为ESP电源的独立单元。这有助于您为ESP实现稳定的电源。此外,Master的TX(本例中为Arduino板)应连接到从机的RX引脚(在本例中为ESP8266)。同样将主设备的RX连接到从设备的TX。
还有一个建议;一些ESP8266具有115200波特率固件,而有些则具有9600波特率。要检查这一点,请尝试在接收器终端窗口更改波特率。如果您是可以使用的,可以尝试使用AT+UART_DEF
命令将ESP板的波特率设置为所需的波特率。波特率命令AT+UART_CUR
和AT+UART_DEF
分别为当前会话或当前和未来会话配置整个UART。手册的命令原型是:
AT+UART_DEF=<baudrate>, <databits>, <stopbits>, <parity>, <flow control>
实施例:
AT+UART_DEF=115200, 8, 1, 0, 3
为了更好地理解此命令,请查看此链接"ESP8266 Command Set"