This sketch does what I want except it doesn't "release" the connection. If I disconnect the client the arduino doesn't seem to "get" that. Even downloading the sketch again does not help. The only way I can re-connect to the client is to power down the arduino and start over... then it works .
// create by Nur Ahmad
//github.com/wiwidnadw
//Youtube channel : Digitalneering
//video tutorial for this project : https://youtu.be/jn_oRLLJz64
//Support my platform on github and Youtube by Like, share and subs ! Enjoy
#include <WiFi.h>
#include <arduino_secrets.h>
IPAddress ip(192, 168, 1, 153);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1);
#define potentio 17
#define led1 3
#define led2 4
#define led3 5
const char* ssid = SECRET_SSID;
const char* password = SECRET_PASS;
WiFiServer server(80);
void setup()
{
WiFiClient client;
client.setTimeout(2);
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(potentio, INPUT);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// WiFi.config(ip,dns,gateway,subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
int value = 0;
void loop(){
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
while (client.connected()){
char c = client.peek();
if (c != -1){
String request = client.readStringUntil('\r');
//String request = client.readString();
Serial.println(request);
Serial.print(request);
if(request.indexOf("led1") != -1 ) {
digitalWrite(led1, !digitalRead(led1));
}
if(request.indexOf("led2") != -1 ) {
digitalWrite(led2, !digitalRead(led2));
}
if(request.indexOf("led3") != -1 ) {
digitalWrite(led3, !digitalRead(led3));
}
}int sensorvalue = analogRead(potentio);
//Serial.println(sensorvalue);
client.print(sensorvalue);
client.print(",");
client.print("Led 1 = ");
client.print(digitalRead(led1));
client.print(",");
client.print("Led 2 = ");
client.println(digitalRead(led2));
}
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("");
//client.println("<meta http-equiv=\"refresh\" content=\"5\" >");
client.print(",");
delay(1);
client.stop();
Serial.println("Client disconnected");
}
}
The only thing I can offer as a clue is that I inserted an "If" to read the buffer if the peek saw something there to read. Before I did that, I could disconnect and reconnect the client. I have tried to retrace what I did that screwed it up but I can't figure it out.
thank you for your assistance