Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8015

Beginners • Re: Esp32 data. Use Pi for the large display

$
0
0
ESP32 code as promised (Arduino).
Make sure to change the SSID and PASSWORD to your settings.
You will see your ESP32 IP address in the serial monitor window.

Code:

#include <Arduino.h>#include <WiFi.h>#define PRINTF_DEBUG 1const char*   ssid     = "YOUR_SSID";const char*   password = "YOUR_PASSWORD";// Define timeout time in milliseconds (example: 2000ms = 2s)const long    timeoutTime = 2000;// Set web server port number to 80WiFiServer    _server(80);WiFiClient    _clientX;// Current timeunsigned long _ulngCurrentTime = millis();// Previous timeunsigned long _ulngPreviousTime = 0; // Variable to store the HTTP requestString        _header;int           _intMyValue;void setup(void){  int intRetries = 0;  int intS;  _intMyValue = 142;  // Connect to Wi-Fi network with SSID and password  #ifdef PRINTF_DEBUG  Serial.begin(115200);  Serial.print("Connecting to ");  Serial.println(ssid);  #endif  // Set WiFi to station mode and disconnect from an AP if it was previously connected  WiFi.mode(WIFI_STA);  WiFi.disconnect();  delay(100);  WiFi.begin(ssid, password);  intRetries = 0;  while ((intS = WiFi.status()) != WL_CONNECTED && intRetries < 10)   {    digitalWrite(LED_BUILTIN, HIGH);    delay(250);    digitalWrite(LED_BUILTIN, LOW);    delay(250);    #ifdef PRINTF_DEBUG    Serial.print(".");    #endif    intRetries++;  }  if (intRetries < 10)  {    // Print local IP address and start web server    #ifdef PRINTF_DEBUG    Serial.println("");    Serial.println("WiFi connected.");    Serial.println("IP address: ");    Serial.println(WiFi.localIP());    #endif    _server.begin();  }  else  {    #ifdef PRINTF_DEBUG    Serial.println("\r\nNot connected to internet");    #endif        for (intS = 0; intS < 20; intS++)    {      digitalWrite(LED_BUILTIN, HIGH);      delay(100);      digitalWrite(LED_BUILTIN, LOW);      delay(100);    }  }}void serveUpWEBpage(void){  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)  // and a content-type so the client knows what's coming, then a blank line:  _clientX.println("HTTP/1.1 200 OK");  _clientX.println("Content-type:text/html");  _clientX.println("Connection: close");  _clientX.println();    _clientX.println("<!DOCTYPE html><html>");  // Make the page refresh every 1s  _clientX.println("<meta http-equiv=\"refresh\" content=\"1\" >");    // Feel free to change the background-color and font-size attributes to fit your preferences  _clientX.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");  _clientX.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");  _clientX.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");  _clientX.println("</style></head>");    // Web Page Heading  _clientX.println("<body><h1>ESP32 Bike HIT</h1>");  String strX = "<h2>";  _clientX.println("<h2>WARM UP time (m)</h2>");  strX += String(_intMyValue);  strX += "</h2>";  _clientX.println(strX);    _clientX.println("</body></html>");    // The HTTP response ends with another blank line  _clientX.println();  _intMyValue--;}void loop(void){  _clientX = _server.available();  if (_clientX)  {         String currentLine = "";    _ulngCurrentTime = millis();    _ulngPreviousTime = _ulngCurrentTime;    while (_clientX.connected() && _ulngCurrentTime - _ulngPreviousTime <= timeoutTime)    {       _ulngCurrentTime = millis();      if (_clientX.available())      {        char c = _clientX.read();        _header += c;        if (c == '\n')         {          // if the current line is blank, you got two newline characters in a row.          // that's the end of the client HTTP request, so send a response:          if (currentLine.length() == 0)           {            serveUpWEBpage();          }           else           { // if you got a newline, then clear currentLine            currentLine = "";          }        }         else if (c != '\r')        {  // if you got anything else but a carriage return character, add it to the end of the currentLine          currentLine += c;        }      }    }    _clientX.stop();    _header = "";  }}

Statistics: Posted by MarkDH102 — Sun Dec 14, 2025 2:47 pm



Viewing all articles
Browse latest Browse all 8015

Trending Articles