Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- const char *ssid = "WETAP-ESP8266";
- const char *password = "507317123456789";
- IPAddress local_IP(192,168,42,1);
- IPAddress gateway(192,168,42,1);
- IPAddress subnet(255,255,255,0);
- ESP8266WebServer HTTPserver(80);
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 64
- #define OLED_RESET -1 //4 // Reset pin # (or -1 if sharing Arduino reset pin)
- #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- uint8_t LED1pin = 4;
- bool LED1status = LOW;
- uint8_t LED2pin = 5;
- bool LED2status = LOW;
- bool display_ok = false; // indique si l'afficheur est OK
- void setup()
- {
- /* a supprimer ensuite */
- // Serial.begin(115200);
- // Serial.println("\nSetup");
- /* /a supprimer ensuite */
- Wire.begin();
- for (int di=0; di < 20; di++) {
- if(display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
- display_ok=true;
- delay(50);
- display.display();
- display.clearDisplay();
- display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
- display.setTextSize(1);
- display.setCursor(0,0);
- break;
- }
- delay(100);
- }
- if ( ! display_ok) {
- Serial.begin(115200);
- Serial.println(F("Display KO !"));
- }
- // pinMode(LED1pin, OUTPUT);
- // pinMode(LED2pin, OUTPUT);
- if (display_ok) {
- display.clearDisplay();
- display.setCursor(0,0);
- display.print("Set.AP conf:");
- display.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "OK" : "KO!");
- display.display();
- delay(500);
- display.clearDisplay();
- display.setCursor(0,0);
- display.print("Set.soft-AP:");
- display.println(WiFi.softAP(ssid,password) ? "OK" : "KO!");
- display.display();
- delay(500);
- }
- else {
- Serial.print("Setting soft-AP configuration ... ");
- Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
- Serial.print("Setting soft-AP ... ");
- Serial.println(WiFi.softAP(ssid,password) ? "Ready" : "Failed!");
- Serial.print("Soft-AP IP address = ");
- Serial.println(WiFi.softAPIP());
- }
- //WiFi.softAP(ssid);
- //WiFi.softAP(ssid, password, channel, hidden, max_connection)
- HTTPserver.on("/", handle_OnConnect);
- HTTPserver.on("/led1on", handle_led1on);
- HTTPserver.on("/led1off", handle_led1off);
- HTTPserver.on("/led2on", handle_led2on);
- HTTPserver.on("/led2off", handle_led2off);
- HTTPserver.onNotFound(handle_NotFound);
- HTTPserver.begin();
- if (display_ok) {
- display.clearDisplay();
- display.setCursor(0,0);
- display.println("HTTP server started");
- display.display();
- delay(1000);
- display.clearDisplay();
- display.setCursor(0,0);
- display.println(WiFi.softAPIP());
- display.setCursor(0,8);
- display.println("Nb.stations:");
- display.display();
- }
- else {
- Serial.println("HTTP server started");
- }
- }
- void loop() {
- if (display_ok) {
- display.setCursor(100,8);
- display.print(WiFi.softAPgetStationNum());
- }
- else {
- Serial.print(F("Nb.stations: "));
- Serial.println(WiFi.softAPgetStationNum());
- }
- static byte drapeau=-1;
- HTTPserver.handleClient();
- if(LED1status)
- {/*digitalWrite(LED1pin, HIGH);*/}
- else
- {/*digitalWrite(LED1pin, LOW);*/}
- if(LED2status)
- {/*digitalWrite(LED2pin, HIGH);*/}
- else
- {/*digitalWrite(LED2pin, LOW);*/}
- if (display_ok) {
- // Animation
- display.fillRect(SCREEN_WIDTH-8, SCREEN_HEIGHT-8, 8, 8, SSD1306_BLACK);
- if (++drapeau==4) {
- drapeau=0;
- }
- display.drawRoundRect(SCREEN_WIDTH-5-drapeau, SCREEN_HEIGHT-5-drapeau, (drapeau+1)*2, (drapeau+1)*2, 10, SSD1306_WHITE);
- // /Animation
- display.display();
- delay(100);
- }
- }
- void handle_OnConnect() {
- LED1status = LOW;
- LED2status = LOW;
- }
- void handle_led1on() {
- LED1status = HIGH;
- if (display_ok) {
- display.setCursor(0,16);
- display.print(F("LED1:ON "));
- }
- else {
- Serial.println(F("LED1 Status: ON"));
- }
- HTTPserver.send(200, "text/html", SendHTML(true,LED2status));
- }
- void handle_led1off() {
- LED1status = LOW;
- if (display_ok) {
- display.setCursor(0,16);
- display.print(F("LED1:OFF "));
- }
- else {
- Serial.println(F("LED1 Status: OFF"));
- }
- HTTPserver.send(200, "text/html", SendHTML(false,LED2status));
- }
- void handle_led2on() {
- LED2status = HIGH;
- if (display_ok) {
- display.setCursor(0,16);
- display.print(F("LED2:ON "));
- }
- else {
- Serial.println(F("LED2 Status: ON"));
- }
- HTTPserver.send(200, "text/html", SendHTML(LED1status,true));
- }
- void handle_led2off() {
- LED2status = LOW;
- if (display_ok) {
- display.setCursor(0,16);
- display.print(F("LED2:OFF "));
- }
- else {
- Serial.println(F("LED2 Status: OFF"));
- }
- HTTPserver.send(200, "text/html", SendHTML(LED1status,false));
- }
- void handle_NotFound(){
- HTTPserver.send(404, "text/plain", "Not found");
- }
- String SendHTML(uint8_t led1stat,uint8_t led2stat){
- String ptr = "<!DOCTYPE html> <html>\n";
- ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
- ptr +="<title>LED Control</title>\n";
- ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
- ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
- ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
- ptr +=".button-on {background-color: #1abc9c;}\n";
- ptr +=".button-on:active {background-color: #16a085;}\n";
- ptr +=".button-off {background-color: #34495e;}\n";
- ptr +=".button-off:active {background-color: #2c3e50;}\n";
- ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
- ptr +="</style>\n";
- ptr +="</head>\n";
- ptr +="<body>\n";
- ptr +="<h1>ESP8266 Web Server</h1>\n";
- ptr +="<h3>Using Access Point(AP) Mode</h3>\n";
- if(led1stat)
- {ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
- else
- {ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}
- if(led2stat)
- {ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
- else
- {ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}
- ptr +="</body>\n";
- ptr +="</html>\n";
- return ptr;
- }
Add Comment
Please, Sign In to add comment