#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;
}
esp8266 – AT+CIPSEND command does not return “>” in Arduino code but working manually – Stack Overflow was last modified: December 31st, 2020 by