Post topics, source code that relate to the Arduino Platform

User avatar
By Cicciotto
#90877 I made a circuit with Arduino Nano and ESP-01s module for monitoring voltage of solar panels
and send the data to a local server with Emoncms. Unfortunately, the ESP-01 after connecting to the network is unable to communicate with the server:
Code: Select all16:43:56.975 -> AT+CIPSTART=”TCP”,”192.168.xxx.xx”,80
16:43:56.975 -> CONNECT
16:43:57.029 ->
16:43:57.029 -> OK
16:43:57.978 -> AT+CIPSEND=144
16:43:57.978 ->
16:43:57.978 -> OK
16:43:57.978 -> >
16:43:59.135 -> Recv 144 bytes
16:43:59.135 ->
16:43:59.135 -> SEND OK
16:43:59.135 ->
16:43:59.135 -> +IPD,487:HTTP/1.1 400 Bad Request
16:43:59.182 -> Date: Sat, 13 Mar 2021 15:43:59 GMT
16:43:59.235 -> Server: Apache/2.4.38 (Raspbian)
16:43:59.282 -> Content-Length: 303
16:43:59.282 -> Connection: close
16:43:59.336 -> Content-Type: text/html; charset=iso-8859-1
16:43:59.336 ->
16:43:59.336 ->
16:43:59.436 ->
16:43:59.436 -> 400 Bad Request
16:43:59.436 ->
16:43:59.483 -> Bad Request
16:43:59.483 -> Your browser sent a request that this server could not understand.
16:43:59.583 ->
16:43:59.583 ->
16:43:59.583 -> Apache/2.4.38 (Raspbian) Server at localhost Port 80
16:43:59.636 ->
16:43:59.683 -> CLOSED
16:44:01.122 -> AT+CIPCLOSE=0
16:44:01.169 -> MUX=0
16:44:01.169 ->
16:44:01.169 -> ERROR


The code I am trying is:
Code: Select all#include <SoftwareSerial.h>

#define DEBUG true
SoftwareSerial esp8266(10,11);
#define APIKEY “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX” // api key emoncms
#define TARGET_IP “192.xxx.xxx.xx” //local IP
#define TARGET_PORT “80”
#define ID “xxxxxx” //name of wireless
#define PASS “xxxxxxxxx” //wifi password

void setup()
{
Serial.begin(9600);
esp8266.begin(9600);
///inizializzazione modulo ESP8266
sendData(“AT+RST\r\n”,1000,DEBUG);
sendData(“AT+CWMODE=1\r\n”,1000,DEBUG)
String cmd=”AT+CWJAP=\””;
cmd+=ID;
cmd+=”\”,\””;
cmd+=PASS;
cmd+=”\””;
sendData( cmd+”\r\n”,1000,DEBUG);
sendData(“AT+CIPMUX=0\r\n”,1000,DEBUG);
}
void loop()
{
float voltage_Batt = (sensorValue_batt * (5.0 / 1023.0)*2);
Serial.println(“voltage_Batt”);
Serial.println(voltage_Batt);
char outstr1[15];
dtostrf(voltage_Batt,4, 2, outstr1);
String valor1 = outstr1;
/// invio a emoncms
String webpage = “AT+CIPSTART=\”TCP\”,\””;
webpage += TARGET_IP;
webpage += “\”,80\r\n”;
sendData(webpage,1000,DEBUG);
String webpage1 = “GET /input/post.json?json={Volt_adc0_arduino:”+ valor1 +”}&apikey=”+APIKEY+” HTTP/1.1\r\n”;
webpage1+=”Host: “;
webpage1+=TARGET_IP;
webpage1+=”\r\n”;
webpage1+=”User-Agent: emoncms\r\n” ;
webpage1+=”\r\n”;
/// invio comando AT + lunghezza caratteri
String cipsend = “AT+CIPSEND=”;
cipsend+= webpage1.length();
cipsend+=”\r\n”;
sendData(cipsend,2000,DEBUG);
sendData(webpage1,2000,DEBUG);
//// comando AT di chiusura connessione
sendData(“AT+CIPCLOSE=0\r\n”,1500,DEBUG);
/// DELAY invio dati a emoncms
delay(3000);
}
//// routine di debug tramite porta seriale
String sendData(String command, const int timeout, boolean debug)
{
String response = “”;
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}

Could someone help me figure out where am I wrong?
Thank
User avatar
By JurajA
#90886 add one more webpage1+=”\r\n”;
the HTTP request should have an empty line after headers.

after CIPSEND command you should wait for > before sending the data