WolfLarsen33

Untitled

Mar 29th, 2021 (edited)
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3.  
  4. const char *ssid = "WETAP-ESP8266";
  5. const char *password = "507317123456789";
  6.  
  7. IPAddress local_IP(192,168,42,1);
  8. IPAddress gateway(192,168,42,1);
  9. IPAddress subnet(255,255,255,0);
  10.  
  11. ESP8266WebServer HTTPserver(80);
  12.  
  13. #include <Wire.h>
  14. #include <Adafruit_GFX.h>
  15. #include <Adafruit_SSD1306.h>
  16.  
  17. #define SCREEN_WIDTH 128
  18. #define SCREEN_HEIGHT 64
  19.  
  20. #define OLED_RESET     -1 //4 // Reset pin # (or -1 if sharing Arduino reset pin)
  21. #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  22.  
  23. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  24.  
  25.  
  26. uint8_t LED1pin = 4;
  27. bool LED1status = LOW;
  28.  
  29. uint8_t LED2pin = 5;
  30. bool LED2status = LOW;
  31.  
  32. bool display_ok = false;  // indique si l'afficheur est OK
  33.  
  34. void setup()
  35. {
  36.   /* a supprimer ensuite */
  37. //  Serial.begin(115200);
  38. //  Serial.println("\nSetup");
  39.   /* /a supprimer ensuite */
  40.  
  41.   Wire.begin();
  42.   for (int di=0; di < 20; di++) {
  43.     if(display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  44.       display_ok=true;
  45.       delay(50);
  46.       display.display();
  47.       display.clearDisplay();
  48.       display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
  49.       display.setTextSize(1);
  50.       display.setCursor(0,0);
  51.       break;
  52.     }
  53.     delay(100);
  54.   }
  55.  
  56.   if ( ! display_ok) {
  57.     Serial.begin(115200);
  58.     Serial.println(F("Display KO !"));
  59.   }
  60.  
  61. //  pinMode(LED1pin, OUTPUT);
  62. //  pinMode(LED2pin, OUTPUT);
  63.  
  64.   if (display_ok) {
  65.     display.clearDisplay();
  66.     display.setCursor(0,0);
  67.     display.print("Set.AP conf:");
  68.     display.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "OK" : "KO!");
  69.     display.display();
  70.     delay(500);
  71.     display.clearDisplay();
  72.     display.setCursor(0,0);
  73.     display.print("Set.soft-AP:");
  74.     display.println(WiFi.softAP(ssid,password) ? "OK" : "KO!");
  75.     display.display();
  76.     delay(500);
  77.   }
  78.   else {
  79.     Serial.print("Setting soft-AP configuration ... ");
  80.     Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  81.     Serial.print("Setting soft-AP ... ");
  82.     Serial.println(WiFi.softAP(ssid,password) ? "Ready" : "Failed!");
  83.     Serial.print("Soft-AP IP address = ");
  84.     Serial.println(WiFi.softAPIP());
  85.   }
  86.  
  87.   //WiFi.softAP(ssid);
  88.   //WiFi.softAP(ssid, password, channel, hidden, max_connection)
  89.  
  90.  
  91.   HTTPserver.on("/", handle_OnConnect);
  92.   HTTPserver.on("/led1on", handle_led1on);
  93.   HTTPserver.on("/led1off", handle_led1off);
  94.   HTTPserver.on("/led2on", handle_led2on);
  95.   HTTPserver.on("/led2off", handle_led2off);
  96.   HTTPserver.onNotFound(handle_NotFound);
  97.  
  98.   HTTPserver.begin();
  99.   if (display_ok) {
  100.     display.clearDisplay();
  101.     display.setCursor(0,0);
  102.     display.println("HTTP server started");
  103.     display.display();
  104.     delay(1000);
  105.     display.clearDisplay();
  106.     display.setCursor(0,0);
  107.     display.println(WiFi.softAPIP());
  108.     display.setCursor(0,8);
  109.     display.println("Nb.stations:");
  110.     display.display();
  111.   }
  112.   else {
  113.     Serial.println("HTTP server started");
  114.   }
  115.  
  116. }
  117.  
  118. void loop() {
  119.   if (display_ok) {
  120.     display.setCursor(100,8);
  121.     display.print(WiFi.softAPgetStationNum());
  122.   }
  123.   else {
  124.     Serial.print(F("Nb.stations: "));
  125.     Serial.println(WiFi.softAPgetStationNum());
  126.   }
  127.   static byte drapeau=-1;
  128.  
  129.   HTTPserver.handleClient();
  130.   if(LED1status)
  131.   {/*digitalWrite(LED1pin, HIGH);*/}
  132.   else
  133.   {/*digitalWrite(LED1pin, LOW);*/}
  134.  
  135.   if(LED2status)
  136.   {/*digitalWrite(LED2pin, HIGH);*/}
  137.   else
  138.   {/*digitalWrite(LED2pin, LOW);*/}
  139.  
  140.   if (display_ok) {
  141.     // Animation
  142.     display.fillRect(SCREEN_WIDTH-8, SCREEN_HEIGHT-8, 8, 8, SSD1306_BLACK);
  143.     if (++drapeau==4) {
  144.       drapeau=0;
  145.     }
  146.     display.drawRoundRect(SCREEN_WIDTH-5-drapeau, SCREEN_HEIGHT-5-drapeau, (drapeau+1)*2, (drapeau+1)*2, 10, SSD1306_WHITE);
  147.     // /Animation
  148.     display.display();
  149.     delay(100);
  150.   }
  151.  
  152. }
  153.  
  154. void handle_OnConnect() {
  155.   LED1status = LOW;
  156.   LED2status = LOW;
  157. }
  158.  
  159. void handle_led1on() {
  160.   LED1status = HIGH;
  161.   if (display_ok) {
  162.     display.setCursor(0,16);
  163.     display.print(F("LED1:ON  "));
  164.   }
  165.   else {
  166.     Serial.println(F("LED1 Status: ON"));
  167.   }
  168.   HTTPserver.send(200, "text/html", SendHTML(true,LED2status));
  169. }
  170.  
  171. void handle_led1off() {
  172.   LED1status = LOW;
  173.   if (display_ok) {
  174.     display.setCursor(0,16);
  175.     display.print(F("LED1:OFF "));
  176.   }
  177.   else {
  178.     Serial.println(F("LED1 Status: OFF"));
  179.   }
  180.   HTTPserver.send(200, "text/html", SendHTML(false,LED2status));
  181. }
  182.  
  183. void handle_led2on() {
  184.   LED2status = HIGH;
  185.   if (display_ok) {
  186.     display.setCursor(0,16);
  187.     display.print(F("LED2:ON  "));
  188.   }
  189.   else {
  190.     Serial.println(F("LED2 Status: ON"));
  191.   }
  192.   HTTPserver.send(200, "text/html", SendHTML(LED1status,true));
  193. }
  194.  
  195. void handle_led2off() {
  196.   LED2status = LOW;
  197.   if (display_ok) {
  198.     display.setCursor(0,16);
  199.     display.print(F("LED2:OFF "));
  200.   }
  201.   else {
  202.     Serial.println(F("LED2 Status: OFF"));
  203.   }
  204.   HTTPserver.send(200, "text/html", SendHTML(LED1status,false));
  205. }
  206.  
  207. void handle_NotFound(){
  208.   HTTPserver.send(404, "text/plain", "Not found");
  209. }
  210.  
  211. String SendHTML(uint8_t led1stat,uint8_t led2stat){
  212.   String ptr = "<!DOCTYPE html> <html>\n";
  213.   ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  214.   ptr +="<title>LED Control</title>\n";
  215.   ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  216.   ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  217.   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";
  218.   ptr +=".button-on {background-color: #1abc9c;}\n";
  219.   ptr +=".button-on:active {background-color: #16a085;}\n";
  220.   ptr +=".button-off {background-color: #34495e;}\n";
  221.   ptr +=".button-off:active {background-color: #2c3e50;}\n";
  222.   ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  223.   ptr +="</style>\n";
  224.   ptr +="</head>\n";
  225.   ptr +="<body>\n";
  226.   ptr +="<h1>ESP8266 Web Server</h1>\n";
  227.   ptr +="<h3>Using Access Point(AP) Mode</h3>\n";
  228.  
  229.    if(led1stat)
  230.   {ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
  231.   else
  232.   {ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}
  233.  
  234.   if(led2stat)
  235.   {ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
  236.   else
  237.   {ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}
  238.  
  239.   ptr +="</body>\n";
  240.   ptr +="</html>\n";
  241.   return ptr;
  242. }
  243.  
  244.  
Add Comment
Please, Sign In to add comment