• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Use esp8266 sketches on esp32

Q&A Forum › Category: ESP32 › Use esp8266 sketches on esp32
0 Vote Up Vote Down
luki pirker asked 7 years ago

Holla Rui,
(how) is it possible to run this esp8266 sketch on an esp32 ?
Thanks
Luki
/*————————————————–
HTTP 1.1 Webserver for ESP8266
for ESP8266 adapted Arduino IDE
Stefan Thesen 04/2015
Running stable for days
(in difference to all samples I tried)
Does HTTP 1.1 with defined connection closing.
Reconnects in case of lost WiFi.
Handles empty requests in a defined manner.
Handle requests for non-exisiting pages correctly.
This demo allows to switch two functions:
Function 1 creates serial output and toggels GPIO2
Function 2 just creates serial output.
Serial output can e.g. be used to steer an attached
Arduino, Raspberry etc.
————————————————–*/
//#include <ESP8266WiFi.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

const char* ssid = “A1-2ec0a1”;
const char* password = “tw26bom6wblr”;
unsigned long ulReqcount;
unsigned long ulReconncount;

// Create an instance of the server on Port 80
//WiFiServer server(80);
WebServer server(80);
void setup()
{
// setup globals
ulReqcount=0;
ulReconncount=0;

// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);

// start serial
Serial.begin(115200);
delay(1);

// inital connect
WiFi.mode(WIFI_STA);
WiFiStart();
}
void WiFiStart()
{
ulReconncount++;

// 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 WLAN is connected
if (WiFi.status() != WL_CONNECTED)
{
WiFiStart();
}

// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}

// Wait until the client sends some data
Serial.println(“new client”);
unsigned long ultimeout = millis()+250;
while(!client.available() && (millis()<ultimeout) )
{
delay(1);
}
if(millis()>ultimeout)
{
Serial.println(“client connection time-out!”);
return;
}

// Read the first line of the request
String sRequest = client.readStringUntil(‘\r’);
//Serial.println(sRequest);
client.flush();

// stop client, if request is empty
if(sRequest==””)
{
Serial.println(“empty request! – stopping client”);
client.stop();
return;
}

// get path; end of path is either space or ?
// Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
String sPath=””,sParam=””, sCmd=””;
String sGetstart=”GET “;
int iStart,iEndSpace,iEndQuest;
iStart = sRequest.indexOf(sGetstart);
if (iStart>=0)
{
iStart+=+sGetstart.length();
iEndSpace = sRequest.indexOf(” “,iStart);
iEndQuest = sRequest.indexOf(“?”,iStart);

// are there parameters?
if(iEndSpace>0)
{
if(iEndQuest>0)
{
// there are parameters
sPath = sRequest.substring(iStart,iEndQuest);
sParam = sRequest.substring(iEndQuest,iEndSpace);
}
else
{
// NO parameters
sPath = sRequest.substring(iStart,iEndSpace);
}
}
}

///////////////////////////////////////////////////////////////////////////////
// output parameters to serial, you may connect e.g. an Arduino and react on it
///////////////////////////////////////////////////////////////////////////////
if(sParam.length()>0)
{
int iEqu=sParam.indexOf(“=”);
if(iEqu>=0)
{
sCmd = sParam.substring(iEqu+1,sParam.length());
Serial.println(sCmd);
}
}

///////////////////////////
// format the html response
///////////////////////////
String sResponse,sHeader;

////////////////////////////
// 404 for non-matching path
////////////////////////////
if(sPath!=”/”)
{
sResponse=”<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>”;

sHeader = “HTTP/1.1 404 Not found\r\n”;
sHeader += “Content-Length: “;
sHeader += sResponse.length();
sHeader += “\r\n”;
sHeader += “Content-Type: text/html\r\n”;
sHeader += “Connection: close\r\n”;
sHeader += “\r\n”;
}
///////////////////////
// format the html page
///////////////////////
else
{
ulReqcount++;
sResponse = “<html><head><title>Demo f&uumlr ESP8266 Steuerung</title></head><body>”;
sResponse += “<font color=\”#000000\”><body bgcolor=\”#d0d0f0\”>”;
sResponse += “<meta name=\”viewport\” content=\”width=device-width, initial-scale=1.0, user-scalable=yes\”>”;
sResponse += “<h1>Demo f&uumlr ESP8266 Steuerung</h1>”;
sResponse += “Funktion 1 schaltet GPIO2 und erzeugt eine serielle Ausgabe.<BR>”;
sResponse += “Funktion 2 erzeugt nur eine serielle Ausgabe.<BR>”;
sResponse += “<FONT SIZE=+1>”;
sResponse += “<p>Funktion 1 <a href=\”?pin=FUNCTION1ON\”><button>einschalten</button></a>&nbsp;<a href=\”?pin=FUNCTION1OFF\”><button>ausschalten</button></a></p>”;
sResponse += “<p>Funktion 2 <a href=\”?pin=FUNCTION2ON\”><button>einschalten</button></a>&nbsp;<a href=\”?pin=FUNCTION2OFF\”><button>ausschalten</button></a></p>”;

//////////////////////
// react on parameters
//////////////////////
if (sCmd.length()>0)
{
// write received command to html page
sResponse += “Kommando:” + sCmd + “<BR>”;

// switch GPIO
if(sCmd.indexOf(“FUNCTION1ON”)>=0)
{
digitalWrite(2, 1);
}
else if(sCmd.indexOf(“FUNCTION1OFF”)>=0)
{
digitalWrite(2, 0);
}
}

sResponse += “<FONT SIZE=-2>”;
sResponse += “<BR>Aufrufz&auml;hler=”;
sResponse += ulReqcount;
sResponse += ” – Verbindungsz&auml;hler=”;
sResponse += ulReconncount;
sResponse += “<BR>”;
sResponse += “Stefan Thesen 04/2015<BR>”;
sResponse += “</body></html>”;

sHeader = “HTTP/1.1 200 OK\r\n”;
sHeader += “Content-Length: “;
sHeader += sResponse.length();
sHeader += “\r\n”;
sHeader += “Content-Type: text/html\r\n”;
sHeader += “Connection: close\r\n”;
sHeader += “\r\n”;
}

// Send the response to the client
client.print(sHeader);
client.print(sResponse);

// and stop the client
client.stop();
Serial.println(“Client disonnected”);
}

Question Tags: ESP 32, ESP8266, Library
10 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

Hi Luki,
I’ve never tried, but what does it happen when you compile it?

0 Vote Up Vote Down
luki pirker answered 7 years ago

Holla Rui, First, the scetch runs on esp8266. On esp32 one can compil it and it probably runs but the screen keeps empty. I think there is a problem with the librarys. Hope you can fix ist. Muito obregado. Luki

0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

I’m not sure why that happen, can you provide more details about the error? Unfortunately due to the high volume of questions I can’t debug custom code… I can only help with problems with my examples or trying to explain/solve specific error messages…

0 Vote Up Vote Down
luki pirker answered 7 years ago

Holla Rui,
on SerialMonitor appeares:
Connecting to xxxxxxx
…..
WiFi connected
Server started
10.0.0.2
new client
Client disonnected
new client
and with Firefox as well as WIN Edge I get the message:
Can’t reach this page.
May be there is some connection to the problem: using your : ESP32_WIFI_Multisensoer
When I click on : “Change mode” >> “Manual”, “Auto PIR” .. opens.
Then when I click one of them eg “Manul”  again “Change mode” appears but not the buttons.
Hope you can help me.
Muito obregado
Luki
 
 

0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

Hi again!
I’m not sure, if I’m understanding your questions… Which example are you having problems with? The ESP32_WIFI_Multisensoer example or the code that you’ve posted here?

0 Vote Up Vote Down
luki pirker answered 7 years ago

Hi Rui,
on both sketches!
With the sketch I posted (Autor Stefan These)
1. Serial Monitor prints:
    Connecting to Axxxxxxx
    …..
    WiFi connected
    Server started
    10.0.0.2
2. The screen stays empty only the message: Can’t reach this page
3. When I type: “http://10.0.0.2/?pin=FUNCTION1ON&#8221; in the commandline
4. serial monitor prints:
         new client
         FUNCTION1ON
         Client disonnected
5. screen keeps empty
What happens with the ESP32_WIFI_Multisensoer example:
1.  WiFi connected.    IP address:    10.0.0.2
2. When click on Changemode / Manual:
    New Client.
    GET / HTTP/1.1
    Host: 10.0.0.2
but no result on the screen
3.  command in commandline:  http://10.0.0.2/?mode=0
4.  New Client.
     GET /?mode=0 HTTP/1.1
     Host: 10.0.0.2
5.  the “ON” button appears.
6. Click on the “ON”-button: no result on screen only
     “GET / HTTP/1 ” an serial monitor

I hope i could explain the problem and also hope you can solve it.
Used “WIFI.h” by Ivan Grokhotkov date 28.07.18
Thanks
LUKI
 
 
 
 
 

0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

Which web browser are you using? Are you using Google Chrome? Can you try in different web browser to see if that problem is solved… I honestly don’t know why the web server is not loading (even though it’s working in the background).

0 Vote Up Vote Down
luki pirker answered 7 years ago

Holla Rui,
With “ESP32_WIFI_Multisensoer”
Firefox and MS Edge dont work.
Google Chrome:  after several clicks (up to ten)  the mode changes.
With “HTTP 1.1 Webserver for ESP8266” the sketch I posted: non oft the prowsers is working.
Thanks
Luki
 

0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

I’ve spent the last couple of hours trying to find a solution for this problem. Can you try this new example? https://bit.ly/2QSdixc

If your web server crashes with multiple clients, please add the following lines to your code: https://rntlab.com/question/solved-esp32-web-server-drops-connection-crashes/

I think it should be working now (that example is for the ESP32).

0 Vote Up Vote Down
Dattatraya Apte answered 6 years ago

Dear Luki.
 
I think this may be happening because the pin configuration of ESP8266 and ESP 32 is different. You may have to change pin numbers corresponding to ESP 32 in the sketch.
 
I may be wrong, but you can try.

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.