Advertisement
patrickmx

HelloServerBlink.ino

Apr 3rd, 2024
38
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // appuyer sur la touche BOOT avant l'upload (marquée 0 sur le layout)
  2. // testé OK sur "Lolin S2 mini  v1.0.0 original wifi Iot ESP32 - s2fn4r2"
  3.  
  4.  
  5. #include <WiFi.h>
  6. #include <WiFiClient.h>
  7. #include <WebServer.h>
  8. #include <ESPmDNS.h>
  9.  
  10. const char* ssid = "monwifi"; //vous le saurez pas
  11. const char* password = "monpw"; // ça non plus
  12. const char* static_ip = "192.168.0.7";
  13. int builtinLed = 15; // 15 sur Lolin S2 mini
  14.  
  15. //code morse
  16. long int dureeTrait=500; //durée d'un trait morse
  17. long int dureePoint=200; //durée d'un point morse
  18. long int dureeStop=100; //durée entre 2 caractères
  19.  
  20. WebServer server(80);
  21.  
  22. const int led = 15; // 15 sur Lolin S2 mini
  23.  
  24. void handleRoot() {
  25.   digitalWrite(led, 1);
  26.   server.send(200, "text/plain", "hello from esp32 s2 mini ! Emet SOS sur builtinLED");
  27.   digitalWrite(led, 0);
  28. }
  29.  
  30. void handleNotFound() {
  31.   digitalWrite(led, 1);
  32.   String message = "File Not Found\n\n";
  33.   message += "URI: ";
  34.   message += server.uri();
  35.   message += "\nMethod: ";
  36.   message += (server.method() == HTTP_GET) ? "GET" : "POST";
  37.   message += "\nArguments: ";
  38.   message += server.args();
  39.   message += "\n";
  40.   for (uint8_t i = 0; i < server.args(); i++) {
  41.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  42.   }
  43.   server.send(404, "text/plain", message);
  44.   digitalWrite(led, 0);
  45. }
  46.  
  47. void setup(void) {
  48.   pinMode(builtinLed, OUTPUT);
  49.   digitalWrite(led, 0);
  50.   Serial.begin(115200);
  51.   WiFi.mode(WIFI_STA);
  52.   WiFi.begin(ssid, password);// Connecte au réseau WiFi
  53.   //WiFi.config(IPAddress(static_ip), IPAddress(0), IPAddress(0)); // Configure l'adresse IP statique
  54.   WiFi.setAutoReconnect(true); // Active la reconnexion automatique
  55.   Serial.println("");
  56.  
  57.   // Wait for connection
  58.   while (WiFi.status() != WL_CONNECTED) {
  59.     delay(500);
  60.     Serial.print(".");
  61.   }
  62.   Serial.println("");
  63.   Serial.print("Connected to ");
  64.   Serial.println(ssid);
  65.   Serial.print("IP address: ");
  66.   Serial.println(WiFi.localIP());
  67.  
  68.   if (MDNS.begin("esp32")) {
  69.     Serial.println("MDNS responder started");
  70.   }
  71.  
  72.   server.on("/", handleRoot);
  73.  
  74.   server.on("/inline", []() {
  75.     server.send(200, "text/plain", "this works as well");
  76.   });
  77.  
  78.   server.onNotFound(handleNotFound);
  79.  
  80.   server.begin();
  81.   Serial.println("HTTP server started");
  82. }
  83.  
  84. void loop(void) {
  85.   server.handleClient();
  86.   delay(2);//allow the cpu to switch to other tasks
  87.     // 3 traits
  88.   digitalWrite(builtinLed, HIGH);   // turn the LED on (HIGH is the voltage level)
  89.   delay(dureeTrait);                       // wait for a second
  90.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  91.   delay(dureeStop);                       // wait for a second
  92.     digitalWrite(builtinLed, HIGH);   // turn the LED on (HIGH is the voltage level)
  93.   delay(dureeTrait);                       // wait for a second
  94.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  95.   delay(dureeStop);                       // wait for a second
  96.     digitalWrite(builtinLed, HIGH);   // turn the LED on (HIGH is the voltage level)
  97.   delay(dureeTrait);                       // wait for a second
  98.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  99.   delay(dureeStop);                       // wait for a second
  100.   delay(dureeStop);                       // wait for a second
  101.   delay(dureeStop);                       // wait for a second
  102.  
  103.   // 3 points
  104.   digitalWrite(builtinLed, HIGH);   // turn the LED on (HIGH is the voltage level)
  105.   delay(dureePoint);                       // wait for a second
  106.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  107.   delay(dureeStop);                       // wait for a second
  108.     digitalWrite(builtinLed, HIGH);   // turn the LED on (HIGH is the voltage level)
  109.   delay(dureePoint);                       // wait for a second
  110.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  111.   delay(dureeStop);                       // wait for a second
  112.     digitalWrite(builtinLed, HIGH);   // turn the LED on (HIGH is the voltage level)
  113.   delay(dureePoint);                       // wait for a second
  114.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  115.   delay(dureeStop);                       // wait for a second
  116.   delay(dureeStop);                       // wait for a second
  117.   delay(dureeStop);                       // wait for a second
  118.  
  119.     // 3 traits
  120.   digitalWrite(builtinLed, HIGH);   // trait1
  121.   delay(dureeTrait);                       // wait for a second
  122.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  123.   delay(dureeStop);                       // wait for a second
  124.     digitalWrite(builtinLed, HIGH);   // trait2
  125.   delay(dureeTrait);                       // wait for a second
  126.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  127.   delay(dureeStop);                       // wait for a second
  128.     digitalWrite(builtinLed, HIGH);   // trait3
  129.   delay(dureeTrait);                       // wait for a second
  130.   digitalWrite(builtinLed, LOW);    // turn the LED off by making the voltage LOW
  131.   delay(dureeStop);                       // wait for a second
  132.  
  133.   delay(1000);                       // wait for a second
  134. }
  135.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement