I'm using an ESP8266 connected to an Arduino Uno via SoftwareSerial to make a post request to an API. The ESP8266 is able to connect to a WIFI connection. When I try to send some data (temperatures values captured by the module DHT22) to the server I always get a access denied. It always happens when the command "esp.println(tcpstart)" is executed and I receive the response "--> Cannot initiate TCP connection" in the Serial Monitor thanks to the prints I added. "tcpstart" is the AT command "AT+CIPSTART". The Arduino Uno is flashed with the firmware: ai-thinker-0.9.5.2-115200.bin. This is my code:
#include <SoftwareSerial.h>
#include <DHT.h>;
#define RX_PIN 2
#define TX_PIN 3
#define DHTTYPE DHT22
#define DHTPIN 7
SoftwareSerial esp(RX_PIN, TX_PIN); // Rx, Tx
DHT dht(DHTPIN, DHTTYPE);
float temp;
String ssid = "XXXXX";
String pass = "YYYYY";
//Complete API URL to do the post: https://api.test.com:bbbb/zzz/zzzz/v2/z?zzzzzzz
String server = "https://api.test.com";
String ip = "aa.aaa.aa.aa";
int port = bbbb;
String uri = "/zzz/zzzz/v2/z?zzzzzzz";
bool connection;
String data;
String* response;
void setup(){
Serial.begin(9600);
esp.begin(115200);
esp.setTimeout(5000);
dht.begin();
}
void loop(){
reset();
readSensors();
while(connection == false)
connectWifi();
connection = false;
httppost();
}
void readSensors(){
temp = dht.readTemperature();
data = String(temp);
}
void reset(){
esp.println("AT+RST");
delay(300);
esp.println("AT+CWMODE=1");
delay(300);
esp.println("AT+RST");
delay(300);
esp.println("AT+CIPMUX=0");
delay(300);
}
void connectWifi(){
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"";
delay(300);
esp.println(cmd);
if(esp.find("OK")){
Serial.println("Wifi Connected");
connection = true;
}else{
Serial.println("--> Wifi not Connected");
connection = false;
}
}
void httppost(){
String postRequest =
"POST " + uri + " HTTP/1.1\r\n" +
"Host: " + server + ":" + port + "\r\n" +
"Accept: *" "/" "*\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n" +
data;
String tcpStart = "AT+CIPSTART=\"TCP\",\"" + ip + "\"," + port;
String sendCmd = "AT+CIPSEND=" + postRequest.length();
esp.println(tcpStart); //start TCP connection
delay(300);
if(esp.find("OK")){
Serial.println("TCP connection OK");
esp.println(sendCmd); //send the data over TCP connection
delay(300);
if(esp.find(">")){
Serial.println("Sending packet");
esp.println(postRequest);
delay(300);
if(esp.find("SEND OK")){
Serial.println("Packet sent");
while(esp.available()){ //number of bytes/char available for reading
char tmpResp = esp.read(); //read one char at the time
Serial.print(tmpResp);
if (tmpResp == '\0') continue; //terminate the while when end of the data
}
esp.println("AT+CIPCLOSE"); //close TCP connection
}else{
Serial.println("--> An error occured while sending packet");
}
}else{
Serial.println("--> ESP8266 is not listening for incoming data");
}
}else{
Serial.println("--> Cannot initiate TCP connection");
}
}
Do you have an idea of the problem it might be?