|
| 1 | +#include <ESP8266WiFi.h> |
| 2 | +#include <ESP8266WebServer.h> |
| 3 | +#include <SPI.h> |
| 4 | +#include <Wire.h> |
| 5 | +#include <Adafruit_GFX.h> |
| 6 | +#include <Adafruit_SSD1306.h> |
| 7 | + |
| 8 | +const char* ssid = "pi"; // Enter SSID here |
| 9 | +const char* password = "raspberry"; //Enter Password here |
| 10 | + |
| 11 | +ESP8266WebServer server(80); |
| 12 | + |
| 13 | +uint8_t LEDpin = 2; |
| 14 | +bool LEDstatus = LOW; |
| 15 | + |
| 16 | +#define SCREEN_WIDTH 128 // OLED display width, in pixels |
| 17 | +#define SCREEN_HEIGHT 64 // OLED display height, in pixels |
| 18 | + |
| 19 | +// Declaration for SSD1306 display connected using I2C |
| 20 | +#define OLED_RESET -1 // Reset pin |
| 21 | +#define SCREEN_ADDRESS 0x3C |
| 22 | +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); |
| 23 | +String message; // Global variable to store the received message |
| 24 | + |
| 25 | +void setup() { |
| 26 | + Serial.begin(9600); |
| 27 | + delay(100); |
| 28 | + pinMode(LEDpin, OUTPUT); |
| 29 | + |
| 30 | + // Initialize the OLED display |
| 31 | + if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { |
| 32 | + Serial.println(F("SSD1306 allocation failed")); |
| 33 | + for(;;); |
| 34 | + } |
| 35 | + display.display(); |
| 36 | + delay(2000); // Pause for 2 seconds |
| 37 | + |
| 38 | + // Clear the display buffer |
| 39 | + display.clearDisplay(); |
| 40 | + display.setTextSize(1); |
| 41 | + display.setTextColor(SSD1306_WHITE); |
| 42 | + display.setCursor(0,0); |
| 43 | + display.println("Connecting to WiFi..."); |
| 44 | + display.display(); |
| 45 | + |
| 46 | + // Connect to WiFi |
| 47 | + Serial.println("Connecting to " + String(ssid)); |
| 48 | + WiFi.begin(ssid, password); |
| 49 | + |
| 50 | + while (WiFi.status() != WL_CONNECTED) { |
| 51 | + delay(1000); |
| 52 | + Serial.print("."); |
| 53 | + } |
| 54 | + |
| 55 | + Serial.println(""); |
| 56 | + Serial.println("WiFi connected..!"); |
| 57 | + Serial.print("Got IP: "); |
| 58 | + Serial.println(WiFi.localIP()); |
| 59 | + |
| 60 | + // Setup HTTP server endpoints |
| 61 | + server.on("/", handle_OnConnect); |
| 62 | + server.on("/ledon", handle_ledon); |
| 63 | + server.on("/ledoff", handle_ledoff); |
| 64 | + server.on("/handle_msg",handle_msg); |
| 65 | + server.onNotFound(handle_NotFound); |
| 66 | + |
| 67 | + server.begin(); |
| 68 | + Serial.println("HTTP server started"); |
| 69 | +} |
| 70 | + |
| 71 | +void loop() { |
| 72 | + server.handleClient(); |
| 73 | + |
| 74 | + // Control LED based on LEDstatus |
| 75 | + digitalWrite(LEDpin, LEDstatus ? HIGH : LOW); |
| 76 | + |
| 77 | + // Update OLED display with serial output |
| 78 | + updateDisplay(); |
| 79 | +} |
| 80 | + |
| 81 | +void handle_OnConnect() { |
| 82 | + LEDstatus = LOW; |
| 83 | + server.send(200, "text/html", SendHTML(false)); |
| 84 | +} |
| 85 | + |
| 86 | +void handle_ledon() { |
| 87 | + LEDstatus = HIGH; |
| 88 | + server.send(200, "text/html", SendHTML(true)); |
| 89 | +} |
| 90 | + |
| 91 | +void handle_ledoff() { |
| 92 | + LEDstatus = LOW; |
| 93 | + server.send(200, "text/html", SendHTML(false)); |
| 94 | +} |
| 95 | + |
| 96 | +void handle_msg() { |
| 97 | + if (server.args() > 0) { // Check if there are any arguments in the request |
| 98 | + for (uint8_t i = 0; i < server.args(); i++) { // Iterate through each argument |
| 99 | + if (server.argName(i) == "message") { // Check if the argument is named "message" |
| 100 | + message = server.arg(i); // Store the value of the "message" argument |
| 101 | + break; // Exit the loop after finding the "message" argument |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Process the received message |
| 107 | + // Here you can perform any actions you want with the received message |
| 108 | + // For this example, let's just print the message to the Serial monitor |
| 109 | + Serial.print("Received message: "); |
| 110 | + Serial.println(message); |
| 111 | + |
| 112 | + // Update the OLED display with the latest message |
| 113 | + updateDisplay(); |
| 114 | + |
| 115 | + // You can add further processing logic here based on the received message |
| 116 | + |
| 117 | + // Send a response back to the client |
| 118 | + server.send(200, "text/plain", "Message received: " + message); |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +void handle_NotFound(){ |
| 125 | + server.send(404, "text/plain", "Not found"); |
| 126 | +} |
| 127 | + |
| 128 | +String SendHTML(uint8_t led){ |
| 129 | + String ptr = "<!DOCTYPE html>\n"; |
| 130 | + ptr +="<html>\n"; |
| 131 | + ptr +="<head>\n"; |
| 132 | + ptr +="<title>LED Control</title>\n"; |
| 133 | + ptr +="</head>\n"; |
| 134 | + ptr +="<body>\n"; |
| 135 | + ptr +="<h1>LED</h1>\n"; |
| 136 | + ptr +="<p>Click to switch LED on and off.</p>\n"; |
| 137 | + ptr +="<form method=\"get\">\n"; |
| 138 | + if(led) |
| 139 | + ptr +="<input type=\"button\" value=\"LED ON\" onclick=\"window.location.href='/ledoff'\">\n"; |
| 140 | + else |
| 141 | + ptr +="<input type=\"button\" value=\"LED OFF\" onclick=\"window.location.href='/ledon'\">\n"; |
| 142 | + ptr +="</form>\n"; |
| 143 | +ptr += "<form method=\"get\" action=\"/handle_msg\">\n"; |
| 144 | +ptr += "<input type=\"text\" name=\"message\">\n"; |
| 145 | +ptr += "<input type=\"submit\" value=\"Send Message\">\n"; |
| 146 | +ptr += "</form>\n"; |
| 147 | + ptr +="<p><a href=\"/temp\">Get Temperature</a></p>\n"; |
| 148 | + ptr +="<p><a href=\"/humidity\">Get Humidity</a></p>\n"; |
| 149 | + ptr +="</body>\n"; |
| 150 | + ptr +="</html>\n"; |
| 151 | + return ptr; |
| 152 | +} |
| 153 | + |
| 154 | +void updateDisplay() { |
| 155 | + // Clear the display buffer |
| 156 | + display.clearDisplay(); |
| 157 | + display.setTextSize(1); |
| 158 | + display.setTextColor(SSD1306_WHITE); |
| 159 | + display.setCursor(0,0); |
| 160 | + |
| 161 | + // Print serial outputs |
| 162 | + display.println("LED Status: " + String(LEDstatus ? "OFF" : "ON")); |
| 163 | + display.println("IP Address: " + WiFi.localIP().toString()); |
| 164 | + display.println("Received message:"); // Display a label |
| 165 | + display.println(message); |
| 166 | + // Display buffer on OLED |
| 167 | + display.display(); |
| 168 | +} |
0 commit comments