Advertisement
microrobotics

ESP Wroom 32 with OLED WebServer

May 11th, 2023
1,577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This is a simple example of using an ESP32 with a built-in OLED display, and a web server to update the display. This example uses the ESPAsyncWebServer library and Adafruit's SSD1306 library to control the OLED display.
  3.  
  4. This example creates a simple web server on the ESP32. When you navigate to the root of the server (http://<esp32-ip-address>/), you can pass a parameter with the message you want to display on the OLED screen (http://<esp32-ip-address>/?<your_message>). The ESP32 will then display this message on the OLED screen.
  5. */
  6.  
  7. void setup() {
  8.   // Serial port for debugging purposes
  9.   Serial.begin(115200);
  10.  
  11.   // Specify the SDA and SCL pins. SDA=5, SCL=4
  12.   Wire.begin(5, 4);
  13.  
  14.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  15.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
  16.     Serial.println(F("SSD1306 allocation failed"));
  17.     for(;;); // Don't proceed, loop forever
  18.   }
  19.  
  20.   display.clearDisplay();
  21.   display.setTextSize(1);
  22.   display.setTextColor(WHITE);
  23.   display.setCursor(0,0);
  24.   display.println(F("Connecting to WiFi..."));
  25.   display.display();
  26.  
  27.   WiFi.begin(ssid, password);
  28.   while (WiFi.status() != WL_CONNECTED) {
  29.     delay(1000);
  30.     Serial.println("Connecting to WiFi...");
  31.   }
  32.   display.println(F("WiFi connected"));
  33.   display.display();
  34.  
  35.   // Route for root / web page
  36.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  37.     String message = request->getParam(0)->value();
  38.     display.clearDisplay();
  39.     display.setCursor(0,0);
  40.     display.println(message);
  41.     display.display();
  42.     request->send(200, "text/plain", "Message received");
  43.   });
  44.  
  45.   // Start server
  46.   server.begin();
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement