Reading the webpage command (url parameters) and sending the command to the arduino over serial isn't the problem, however i cannot get the reading of the response stable: sometimes it works, and sometimes it doesn't.
The arduino (tried a nano and a mega2560) side is quite simple: it reads te command and gives feedback (ex prints the value of an variable). I know that the command arrives on the arduino and it uses serial.println to send the command to the esp01 (i printed the same on an other serial port so i know that it sends it). Also the same setup is able to send serial info from the arduino to the esp (the esp reads them in the main loop and reacts upon them, so the serial connection seems ok).
I am using the following code on the esp
PS: the arduino sends commands to the esp (starting with stwz_) when something happens (ex garagedoor opens).
// interface met arduino nano voor stand garagepoort etc
// bevat 2 grote onderelen:
// - doorsturen van de gegevens naar php script als dit gevraagd wordt door de aruidono
// - status opvragen en feedback geven
int RS_i=0 ;
byte RS_byte;
byte RS_data[40]; //Buffer voor RS232
int i;
String antwoord;
boolean debug = false;
//voor de inkomende seriele zaken te versturen naar de webserver
#include <ESP8266WiFi.h>
const char* ssid = "*****";
const char* password = "*****";
WiFiServer server(80);
void setup() {
Serial.begin(9600);
Serial.println("programma esp_schuifpoort_etc_v3");
delay(100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (debug) Serial.print(".");
}
server.begin();
if (debug) Serial.println("Server started");
}
void loop() {
/////////////////////////////////////////////
// RS232 - seriele poort verwerken //
/////////////////////////////////////////////
if (Serial.available()>0) // doe iets met de inkomende RS232 boodschappen
{ RS_byte=Serial.read();
RS_data[RS_i++]=RS_byte;
if (RS_byte==10||RS_byte==13) {RS_datafilter();}
}
/////////////////////////////////////////////
// WEBSERVER //
/////////////////////////////////////////////
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return; //alles hieronder wordt dus NIET meer uitgevoerd!!!!, alles hiervoor zetten dus!
}
antwoord = ""; //resetten van het antwoord
// Wait until the client sends some data
if (debug) Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
if (debug) Serial.println(req);
client.flush();
// Match the request
//int val;
boolean ok_lezen = 0; //bijhouden of er uitgelezen mag worden of niet
if (req.indexOf("comm=vijver") != -1) {//we willen de vijver hoogte
Serial.println("HH");
ok_lezen = true;
}
else if (req.indexOf("comm=regenwater") != -1) { //we willen de regenwater hoogte
Serial.println("GH");
ok_lezen = true;
}
else if (req.indexOf("comm=ventilS") != -1) { //we willen de regenwater hoogte
Serial.println("VH");
ok_lezen = true;
}
else if (req.indexOf("comm=garagepoort") != -1) { //we willen de regenwater hoogte
Serial.println("PH");
ok_lezen = true;
}
else if (req.indexOf("comm=schuifpoort") != -1) { //we willen de regenwater hoogte
Serial.println("SH");
ok_lezen = true;
}
else if (req.indexOf("comm=ventilZH") != -1) { //we willen de regenwater hoogte
int a = req.indexOf("comm=ventilZH");
String commando = req.substring(a+11,a+14);
Serial.println(commando);
ok_lezen = true;
}
else {
if (debug) Serial.println("invalid request");
client.stop();
//return;
antwoord = "verkeerde input, mogelijke commandos: comm=vijver ; regenwater ; ventilS ; garagepoort ; schuifpoort ; ventilZHx met x de gewenste stand";
}
// wachten op de response via serieel
if ( ok_lezen == true) {
unsigned long totwanneer = millis() + 2500; //om loops te voorkomen, zie verder
int leesfout = 0;
byte RS_byte2 = 48;
while (RS_byte2 != 10 || RS_byte2 != 13) {
delay(0); // om de watchdog bezig te houden
//Serial.println("benhier");
if (millis() > totwanneer){antwoord="fout, geen reactie van arduino binnen de gestelde tijd";break;} //om loops te voorkomen geven we de arduino 2 seconden de tijd om te antwoorden
if (Serial.available()>0){
RS_byte2=Serial.read();
//Serial.println("benhier2");
if (RS_byte2==10||RS_byte2==13) {
//Serial.println("10 of 13 ontvangen");
break;
}
antwoord += char(RS_byte2);
//antwoord += RS_byte2;
Serial.println(char(RS_byte2));
if (millis() > totwanneer){leesfout = 1;break;} //om loops te voorkomen geven we de arduino 2 seconden de tijd om te antwoorden
}
}
if (leesfout == 1) antwoord="fout, timeout in de leesroutine";
leesfout = 0;
} else {
antwoord = "geen goede vraag gekregen";
}
// Prepare the response
antwoord += "\n";
// Send the response to the client
client.print(antwoord);
delay(1);
if (debug) Serial.println("Client disonnected");
antwoord = "";
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
//-------------------------------------------------------------------------------
// Extra RS232 routines
//-------------------------------------------------------------------------------
void RS_datafilter() // Filter inkomende RS232 boodschappen
{ int aantal; boolean NB_init;
//voor de gewone statusupdates
if(RS_data[0]=='s' && RS_data[1]=='t' && RS_data[2]=='w' && RS_data[3]=='z' && RS_data[4]=='_') //de arduino moet het commando starten met stwz_
{
// doorsturen naar het php script die de verdere verwerking doet
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
const char* host = "10.0.2.183"; //ip adres van de server die het commando zal verwerekn
if (!client.connect(host, httpPort)) {
if (debug) Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/test/hoogte_poort.php?";
for (i=5; i<sizeof(RS_data);i++) {if (RS_data[i] != 0){url += (char)RS_data[i];}} //we nemen het begin er niet bij stwz_
if (debug) Serial.print("Requesting URL: ");
if (debug) Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
if (debug) Serial.print(line);
}
if (debug) Serial.println();
if (debug) Serial.println("closing connection");
}
RS_cleardata();
}
void RS_cleardata() // Wis RS232 buffer
{ for (i=0; i<sizeof(RS_data);i++) {RS_data[i]=0;}
RS_i=0;
}
Does anyone know what i am doing wrong? Most of the time it shows a page with nothing on it (not even the message its supposed to give when it exceeds the 2seconds) and sometimes it gives that message, and once in a while it shows the correct one.
On other occasions it shows data that's intend for the other serial loop (non webpage).
The strangest thing is...if i try it manually (not connected to esp but to terminal) it works...