He has adapted an example to my needs, but I can't get it to work.
Whatever I try (I'm not a code guy), I can't find why this doesn't work.
The only thing I want is to control an output from a ESP01 module with a button AND to read an input of the same module.
He suggested to use AJAX for this because it only reloads a part of the page that contains the variables.
In another project this works fine (it only switches an output via a button), but now the Input is added it doesn't work anymore.
The code contains an INO and an Indexh part.
Is there someone that can make out why this doesn't work?
The suggestion was that there is a "space" in the Indexh that corrupts the whole code.
Any suggestions???
The INO part is:
-----------------------------------------------
#include <ESP8266WiFi.h>
#include "indexl.h"
const char* ssid = "MySSID";
const char* password = "Pass";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
int DigitalPin[] = {2, 0};
void setup() {
Serial.begin(115200);
delay(10);
pinMode(2,INPUT);
pinMode(2, INPUT_PULLUP);
pinMode(0,OUTPUT);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String command1 = client.readStringUntil('/');
String command = client.readStringUntil('/');
Serial.println(command1);
Serial.println(command);
if (command == "digital") {
int pin, value;
pin = client.parseInt();
Serial.println(pin);
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
else {
value = digitalRead(pin);
}
client.print(F("digital,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
else if (command == "status") {
int pin, value;
client.print(F("status"));
for (int thisPin = 0; thisPin < 3; thisPin++) {
pin = DigitalPin[thisPin];
value = digitalRead(pin);
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
}
{
value = analogRead(A0);
float lux=value;
client.print(F("#A0"));
client.print(F("="));
client.print(lux);
}
client.println("");
}
else { // Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += file1;
client.flush();
// Send the response to the client
while(s.length()>2000)
{
String dummy = s.substring(0,2000);
client.print(dummy);
s.replace(dummy," ");
}
client.print(s);
delay(1);
Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is destroyed
}
}
---------------------------------- End of the Ino part --------------------------------
-----------------------------------Begin of the Indexh part-------------------------
#ifndef header_h
#define header_h
String file1=
"<html>\r\n"
"<head>\r\n"
"<title>ESP8266 I/O Demo</title>\r\n"
"<script type=\"text/javascript\">\r\n"
"window.onload=Pinstatus;\r\n"
"function Pinstatus(){\r\n"
"morestatus();\r\n"
"}\r\n"
"function morestatus(){\r\n"
"setTimeout(morestatus, 4000);\r\n"
"document.getElementById(\"description\").innerHTML = \"Processing Status\";\r\n"
"server = \"status/99\";\r\n"
"request = new XMLHttpRequest();\r\n"
"request.onreadystatechange = updateasyncstatus;\r\n"
"request.open(\"GET\", server, true);\r\n"
"request.send(null);\r\n"
"}\r\n"
"function updateasyncstatus(){\r\n"
"if ((request.readyState == 4) && (request.status == 200))\r\n"
"{\r\n"
"result = request.responseText;\r\n"
"document.getElementById(\"description\").innerHTML = result;\r\n"
"fullset = result.split(\"#\");\r\n"
"document.getElementById(\"description\").innerHTML = fullset;\r\n"
"for(i = 1; i < fullset.length; i++){\r\n"
"PinPair = fullset[i];\r\n"
"singleset = PinPair.split(\"=\");\r\n"
"PN = singleset[0];\r\n"
"Pinstatus = singleset[1];\r\n"
"if (PN = 2)\r\n"
"{\r\n"
"ActNum = \"action\" + PN;\r\n"
"TxtNum = \"text\" + PN;\r\n"
"if (Pinstatus == 0)\r\n"
"{\r\n"
"PinAct = \"1\";\r\n"
"text = \"Off\";\r\n"
"}\r\n"
"else\r\n"
"{\r\n"
"PinAct = \"0\";\r\n"
"text = \"On\";\r\n"
"}\r\n"
"document.getElementById(ActNum).value = PinAct;\r\n"
"document.getElementById(TxtNum).innerHTML = text;\r\n"
"}\r\n"
"if (PN == 0)\r\n"
"{\r\n"
"TxtNum = \"text\" + PN;\r\n"
"if (Pinstatus == 1)\r\n"
"{\r\n"
"text = \"On\";\r\n"
"}\r\n"
"else\r\n"
"{\r\n"
"text = \"Off\";\r\n"
"}\r\n"
"document.getElementById(TxtNum).innerHTML = text;\r\n"
"}\r\n"
"}\r\n"
"}\r\n"
"function sendbutton(Pin,action){\r\n"
"document.getElementById(\"description\").innerHTML = \"Processing Button Click\";\r\n"
"server = \"digital/\" + Pin + \"/\" + action;\r\n"
"request = new XMLHttpRequest();\r\n"
"request.onreadystatechange = updateasyncbutton;\r\n"
"request.open(\"GET\", server, true);\r\n"
"request.send(null);\r\n"
"}\r\n"
"function updateasyncbutton(){\r\n"
"if ((request.readyState == 4) && (request.status == 200))\r\n"
"{\r\n"
"result = request.responseText;\r\n"
"document.getElementById(\"description\").innerHTML = result;\r\n"
"singleset = result.split(\",\");\r\n"
"PinType = singleset[0];\r\n"
"PinNum = singleset[1];\r\n"
"Pinstatus = singleset[2];\r\n"
"ActNum = \"action\" + PinNum;\r\n"
"TxtNum = \"text\" + PinNum;\r\n"
"if (Pinstatus == 0)\r\n"
"{\r\n"
"PinAct = \"1\";\r\n"
"text = \"Off\";\r\n"
"}\r\n"
"else\r\n"
"{\r\n"
"PinAct = \"0\";\r\n"
"text = \"On\";\r\n"
"}\r\n"
"document.getElementById(ActNum).value = PinAct;\r\n"
"document.getElementById(TxtNum).innerHTML = text;\r\n"
"document.getElementById(\"description\").innerHTML = result;\r\n"
"}\r\n"
"}\r\n"
"}\r\n"
"</script>\r\n"
"</head>\r\n"
"<font face=\"Arial\">\r\n"
"<table name=\"Table\" border=\"1\" cellpadding=\"6\">\r\n"
"<tr> <th align=\"center\" colspan=\"6\" >Digital Input</th></tr>\r\n"
"<tr>\r\n"
"<td align=\"center\">\r\n"
"GPIO 2\r\n"
"<br>\r\n"
"<p id=\"text2\">Off</p>\r\n"
"</td>\r\n"
"</tr>\r\n"
"<tr> <th align=\"center\" colspan=\"6\" >Digital Output</th></tr>\r\n"
"<tr>\r\n"
"<td align=\"center\">\r\n"
"<input type=\"hidden\" name=\"pin\" value=\"0\" id=\"pin0\"/>\r\n"
"<input type=\"hidden\" name=\"action\" value=\"0\" id=\"action0\"/>\r\n"
"<button onclick=\"sendbutton(document.getElementById('pin0').value,document.getElementById('action0').value);\">GPIO 0</button>\r\n"
"<p id=\"text0\">Off</p>"
"</td>\r\n"
"</tr>\r\n"
"</table>\r\n"
"<br><br>\r\n"
"<br><br>\r\n"
"<p id=\"description\"> - </p>\r\n"
"</font>\r\n"
"</html>\r\n";
#endif
------------------------------------------------End of the indexh part--------------------