ESP32 vs ESP8266 server differences

klausi313
Posts: 2
Joined: Sat Jul 09, 2022 7:48 am

ESP32 vs ESP8266 server differences

Postby klausi313 » Sat Jul 09, 2022 7:56 am

Hello,
I try to port an Arduino code form ESP8266 to ESP32 ( Wifi Kit 32)
I changed the relavant server includes form
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

to
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
#include <WebServer.h>

but it does not work. The ESP32 is connected to the Wifi but the server doesn't run. Is there anything what I need further to consider/change. Thanks for any hint
Klaus

martinius96
Posts: 33
Joined: Thu Dec 13, 2018 1:39 am

Re: ESP32 vs ESP8266 server differences

Postby martinius96 » Sun Jul 10, 2022 6:04 pm

Hello there, your changes looks okay.
I am running webserver on ESP8266 and I have never used these header files.

Code: Select all

#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
Also for ESP32 I haven't used these:

Code: Select all

#include <HTTPClient.h>
#include <WiFiClient.h>
Or should I understand it like you have webserver and also you are running HTTP Clients there that are communicating with other server? Because these are client-side libraries for communication to other server, I think these are not related to webserver running on ESP32 in any way.

How rest or your code looks like?
Webserver related code looks identical for ESP8266 and ESP32 in my implementations. I am running them in my WiFi thermostat project. I see difference only at the begin of code when webserver object is related to class based on microcontroller.
There can be issue in your case you have used WiFiServer and not Webserver for ESP8266 and that code isn't fully compatible with ESP32 if you just rewrite files to it...

Webserver related code for ESP8266 and ESP32 - working

Code: Select all

ESP8266WebServer server(80); //FOR ESP8266
WebServer server(80); //FOR ESP32

//--------> REST OF CODE IS IDENTICAL FOR ESP8266 and FOR ESP32

//in setup()
  server.on("/", handleGet); //ROOT PAGE - JSON OUTPUT by server
 //other pages, can use also specify POST method if you want to receive datas via server from clients
  server.begin();
  
  //in loop()
  server.handleClient();
  
  //webserver function that will print output to client
  void handleGet() {
  String page = "{\n";
  page += F("\"Hysteresis\":");
  page += String(read_String(100));
  page += F(",\n");
  page += F("\"Target_Temperature\":");
  page += String(read_String(10));
  page += F(",\n");
  page += F("\"Actual_Temperature\":");
  page += String(teplota) + "\n";
  page += F("}\n");
  server.send(200, "application/json", stranka);
}
So with this code I am able to print JSON output to client:
Image

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 vs ESP8266 server differences

Postby lbernstone » Sun Jul 10, 2022 7:26 pm

It is possible to support both devices in the same code with an ifdef:

Code: Select all

#ifdef ESP32
#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
#include <Update.h>
WebServer server(80);
HTTPClient client;
#elif ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <Updater.h>
ESP8266WebServer server(80);
ESP8266HTTPClient client;
#endif

klausi313
Posts: 2
Joined: Sat Jul 09, 2022 7:48 am

Re: ESP32 vs ESP8266 server differences

Postby klausi313 » Mon Jul 11, 2022 8:09 am

Thanks for your support.
Its my first webserver project, so I do not have a lot of experience with this so I took a existing project.
Now I solved it :-)
In the 8266 coder there was a clinet.flush() command. With removing it on ESP3 the server runs

erintse01
Posts: 1
Joined: Thu Nov 17, 2022 6:36 am

Re: ESP32 vs ESP8266 server differences

Postby erintse01 » Thu Nov 17, 2022 6:39 am

This is a common question, today I have written an article about ESP32 vs ESP8266, Which is Better and How to Choose?
https://www.embedic.com/technology/deta ... -to-choose

Hope it helps.

Who is online

Users browsing this forum: No registered users and 114 guests