Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By yoavshtainer
#34164 @martinayotte

it's work great, thank you!!

but i have one more question: the ESP8266WebServer.h can work whis just one client each time, do you think that i can do multiple clinet that work on the same server in diffrent timeing?

it's make any sense?

thank you!
User avatar
By martinayotte
#34199 To be able to manage multiple clients at the same time, we need to have multi-threaded code, which doesn't come for free under ArduinoESP, but still doable, maybe with a kind of RTOS port library. I know there is one in STM32Duino, but I've not got enough time to try it out.
Also, there is maybe issue in WifiServer code that still present, here is what I found back in April, but no body answered at that time : viewtopic.php?f=32&t=2612#p14992
When I get a chance, maybe I will look again at this issue.
User avatar
By yoavshtainer
#34274 I tried to work with my PC and then return to the ESP client and it's work (I entered to the ESP client code delay of 10 sec between connections). if I do it for a 1 hour cycle it dosn't work for 6 ESP (in a 10 min one will connect send data and disconnect for the last 50 min)?

edit: i'm trying to send data from the client to the server (i'm not konw html good) so i treid to use post from the client side but i dont know what i need to do in the server side...

if you can help me with this i will be very happy... my code is base on yours and i'm party new in this...
Code: Select all/*
 */

#include <ESP8266WiFi.h>

const char *ssid = "MyESPAP";
const char *password = "MyPassword";
const char *host = "192.168.4.1";

int rate_toogle = 0;


void setup() {
  Serial.begin(115200);
  delay(10);
  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"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void loop() {
  delay(8000);
  if (rate_toogle == 0)
    rate_toogle = 1;
  else
    rate_toogle = 0;
  Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  String url = "/frate"; 
  if (rate_toogle == 0)
    url = "/srate"; 
    String count2 = "/count2";
    String count1 = "/count1?";
    String num = "5";
  Serial.print("Requesting URL: ");
  Serial.println(url);
  client.print(String("GET ") + count1 + "COUNTER =  " + num + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
  client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
  delay(10);
  client.print(String("POST ") + count2 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Content-Type: application\r\nCon­tent-Length: 20\r\n\r\napples=12&oranges=45\r\n\r\n");
 delay(10);
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  Serial.println();
  Serial.println("closing connection");
}


Code: Select all/*
 */


#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char *ssid = "MyESPAP";
const char *password = "MyPassword";

ESP8266WebServer server(80);
String count = "0";
int led = 2;
int blink_count = 0;
int blink_rate = 250;

void handleRoot() {
  server.send(200, "text/html", "<html><body><h1>MyESPAP Home Page</h1>\r\n<a href=\"/frate\">Fast Rate</a><br>\r\n<a href=\"/srate\">Slow Rate</a><br>\r\n<h2 href=\"/count1\">counter =  "+ count + " </h2><br>\r\n</body></html>");
}

void handleFRate() {
  blink_rate = 125;
  Serial.println("FastRate mode !");
  server.send(200, "text/html", "<html><head><script>window.onload =  function() { setInterval(function() {window.location.replace('/');}, 1000); };</script></head><body><h1>Fast Rate mode</h1></body></html>");
}

void handleSRate() {
  blink_rate = 500;
  Serial.println("SlowRate mode !");
  server.send(200, "text/html", "<html><head><script>window.onload =  function() { setInterval(function() {window.location.replace('/');}, 1000); };</script></head><body><h1>Slow Rate mode</h1></body></html>");
}

void handleCount1() {
 
  Serial.println("MSG mode !");
  server.send(200, "text/html", "<html><head><script>window.onload =  function() { setInterval(function() {window.location.replace('/');}, 1000); };</script></head><body><h1>count1</h1></body></html>");

}

void handleCount2() {
 
  Serial.println("MSG mode !");
  server.send(200, "text/html", "<html><head><script>window.onload =  function() { setInterval(function() {window.location.replace('/');}, 1000); };</script></head><body><h1>count2</h1></body></html>");

}
void setup() {
  pinMode(led, OUTPUT);
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring Access Point ...");
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/", handleRoot);
  server.on("/frate", handleFRate);
  server.on("/srate", handleSRate);
  server.on("/count1", handleCount1);
  server.on("/count2", handleCount2);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
  if (blink_count >= blink_rate) {
    digitalWrite(led, HIGH);
    blink_count = 0;
  }
  if (blink_count == blink_rate / 2) {
    digitalWrite(led, LOW);
  }
  delay(1);
  blink_count++;
}