Advertisement
NittyGritty

ESP8266_Ping

May 24th, 2022
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.90 KB | None | 0 0
  1. /*
  2.   ESP8266 Pingcheck Wifi Module
  3.   Pings three IP Addresses. If one of them responses, the light is on.
  4.   2016 www.frag-duino.de
  5. */
  6.  
  7. #include <ESP8266WiFi.h>
  8. #include <WiFiClient.h>
  9. #include <ESP8266WebServer.h>
  10. #include <EEPROM.h>
  11. extern "C" {
  12. #include "user_interface.h"
  13. #include "ping.h"
  14. }
  15.  
  16. ESP8266WebServer server(80);
  17.  
  18. /*
  19.  * To set the new Wifi, enter credentials in the two variables and set RESET_EEPROM to true.
  20.  * During the next start it will save it into the EEPROM.
  21.  * Then set RESET_EEPROM to false and flash it again.
  22.  * When wifi changes, remember to first change the name over the webinterface.
  23. */
  24.  
  25. #define RESET_EEPROM false
  26. #define WIFI_SSID "Freifunk"
  27. #define WIFI_PASSWORD ""
  28.  
  29. // Settings
  30. const int PING_INTERVAL = 5000;
  31. const int PIN_LED = 2;
  32. const int count_hosts = 3;
  33. int connection_try = 15;
  34. char host1[15];
  35. char host2[15];
  36. char host3[15];
  37. boolean host_is_online[] = {false, false, false};
  38.  
  39. // EEPROM Addresses
  40. const int ADDRESS_IP1 = 0;
  41. const int ADDRESS_IP2 = 15;
  42. const int ADDRESS_IP3 = 30;
  43. const int ADDRESS_INIT = 105; // - 99
  44. const int ADDRESS_PASSWORD = 45; // - 75
  45. const int ADDRESS_SSID = 75; // - 105
  46. const int EEPROM_SIZE = 106;
  47.  
  48. // Variables
  49. IPAddress network_ip;
  50. char network_ssid[30];
  51. char network_password[30];
  52. struct ping_option pingopt;
  53. unsigned long last_ping = 0;
  54. unsigned long last_seen_online = 0;
  55. unsigned long currentMillis = 0;
  56. int currentHost = 0;
  57. int state_LED = LOW;
  58. boolean got_new_hosts;
  59. boolean save_wifi;
  60.  
  61. // Save the current config
  62. void writeIPtoEEPROM() {
  63.   for (int i = 0; i < 15; i++) {
  64.     EEPROM.write(ADDRESS_IP1 + i, host1[i]);
  65.     EEPROM.write(ADDRESS_IP2 + i, host2[i]);
  66.     EEPROM.write(ADDRESS_IP3 + i, host3[i]);
  67.   }
  68.   EEPROM.commit();
  69. }
  70.  
  71. void writeSSIDtoEEPROM(String new_ssid) {
  72.   for (int i = 0; i < 30; i++) {
  73.     if (i < new_ssid.length())
  74.       EEPROM.write(ADDRESS_SSID + i, new_ssid[i]);
  75.     else
  76.       EEPROM.write(ADDRESS_SSID + i, '\0');
  77.   }
  78.   EEPROM.commit();
  79. }
  80.  
  81. void writePASSWORDtoEEPROM(String new_password) {
  82.   for (int i = 0; i < 30; i++) {
  83.     if (i < new_password.length())
  84.       EEPROM.write(ADDRESS_PASSWORD + i, new_password[i]);
  85.     else
  86.       EEPROM.write(ADDRESS_PASSWORD + i, '\0');
  87.   }
  88.   EEPROM.commit();
  89. }
  90.  
  91. // Reply is called, when response arrives
  92. static void callback_reply (void* arg, void *pdata) {
  93.   struct ping_resp *pingresp = (struct ping_resp *)pdata;
  94.   if (pingresp->resp_time > 0) {
  95.     Serial.println(" Response");
  96.     last_seen_online = currentMillis;
  97.     host_is_online[currentHost] = true;
  98.   }
  99.   else {
  100.     Serial.println(" Timeout");
  101.     host_is_online[currentHost] = false;
  102.   }
  103. }
  104.  
  105. // Sends an echo request to an IP-Address
  106. void ping(uint32 ip)
  107. {
  108.   pingopt.ip = ip;
  109.   pingopt.count = 1;
  110.   pingopt.coarse_time = 200;
  111.   pingopt.recv_function = &callback_reply;
  112.   // pingopt.sent_function = NULL;
  113.   ping_start(&pingopt);
  114. }
  115.  
  116.  
  117. boolean checkIPsyntax(String ip) {
  118.   // Only 0-9 and dots
  119.   for (int i = 0; i <= 33; i++)
  120.     if (ip.indexOf(i) != -1)
  121.       return false;
  122.   if (ip.indexOf(0x2F) != -1)
  123.     return false;
  124.   for (int i = 0x3A; i <= 0x7F; i++)
  125.     if (ip.indexOf(i) != -1)
  126.       return false;
  127.  
  128.   // Count the amount of dots
  129.   int dots = 0;
  130.   for (int i = 0; i < ip.length(); i++)
  131.     if (ip[i] == '.')
  132.       dots++;
  133.   if (dots != 3)
  134.     return false;
  135.  
  136.   // Check the range of the oktets
  137.   int dot_1 = ip.indexOf('.');
  138.   int dot_2 = ip.indexOf('.', dot_1 + 1);
  139.   int dot_3 = ip.indexOf('.', dot_2 + 1);
  140.   int oktet_1 = ip.substring(0, dot_1).toInt();
  141.   int oktet_2 = ip.substring(dot_1 + 1, dot_2).toInt();
  142.   int oktet_3 = ip.substring(dot_2 + 1, dot_3).toInt();
  143.   int oktet_4 = ip.substring(dot_3 + 1).toInt();
  144.   if (oktet_1 >=  0 && oktet_1 <= 255 &&
  145.       oktet_2 >= 0 && oktet_2 <= 255 &&
  146.       oktet_3 >= 0 && oktet_3 <= 255 &&
  147.       oktet_4 >= 0 && oktet_4 <= 255 )
  148.     return true;
  149.   else
  150.     return false;
  151. }
  152.  
  153.  
  154. void handleRoot() {
  155.   Serial.print(currentMillis);
  156.   Serial.print(": HTTP-Request with ");
  157.   got_new_hosts = false;
  158.   save_wifi = false;
  159.   String temp_ssid;
  160.   String temp_password;
  161.  
  162.   if (server.args() > 0) {
  163.     Serial.print(server.args());
  164.     Serial.print(" args: ");
  165.     for (int a = 0; a < server.args(); a++) {
  166.       Serial.print("[");
  167.       Serial.print(server.argName(a));
  168.       Serial.print("=");
  169.       Serial.print(server.arg(a));
  170.       Serial.print("]");
  171.  
  172.       if (server.arg(a) == "Restart")  // Restart ESP8266
  173.         system_restart();
  174.       else if (server.arg(a) == "Save-Wifi") {
  175.         Serial.println("OK, Save-Wifi");
  176.         save_wifi = true;
  177.       } else if (server.argName(a) == "ssid") {
  178.         for (int i = 0; i < 30; i++) {
  179.           if (i < server.arg(a).length())
  180.             network_ssid[i] = server.arg(a)[i];
  181.           else
  182.             network_ssid[i] = '\0';
  183.         }
  184.       } else if (server.argName(a) == "password") {
  185.         for (int i = 0; i < 30; i++) {
  186.           if (i < server.arg(a).length())
  187.             network_password[i] = server.arg(a)[i];
  188.           else
  189.             network_password[i] = '\0';
  190.         }
  191.       } else if (server.argName(a) == "ip1" && checkIPsyntax(server.arg(a))) {
  192.         memset(host1, 0, sizeof(host1));
  193.         for (int i = 0; i < server.arg(a).length(); i++)
  194.           host1[i] = server.arg(a)[i];
  195.         got_new_hosts = true;
  196.         Serial.print(" OK1 ");
  197.       } else if (server.argName(a) == "ip2" && checkIPsyntax(server.arg(a))) {
  198.         memset(host2, 0, sizeof(host2));
  199.         for (int i = 0; i < server.arg(a).length(); i++)
  200.           host2[i] = server.arg(a)[i];
  201.         got_new_hosts = true;
  202.         Serial.print(" OK2 ");
  203.       } else if (server.argName(a) == "ip3" && checkIPsyntax(server.arg(a))) {
  204.         memset(host3, 0, sizeof(host3));
  205.         for (int i = 0; i < server.arg(a).length(); i++)
  206.           host3[i] = server.arg(a)[i];
  207.         got_new_hosts = true;
  208.         Serial.print(" OK3 ");
  209.       } else
  210.         Serial.print(" NACK; ");
  211.     }
  212.   } else Serial.print("no args");
  213.   Serial.println(".");
  214.  
  215.   // Save it
  216.   if (got_new_hosts)
  217.     writeIPtoEEPROM();
  218.  
  219.   if (save_wifi) {
  220.     writeSSIDtoEEPROM(network_ssid);
  221.     if (temp_password != "notchanged")
  222.       writePASSWORDtoEEPROM(network_password);
  223.   }
  224.  
  225.   // Print the HTML-Page
  226.   String result = "<html><head><title>PingCheck</title></head>\r\n<body>\r\n";
  227.   result += "PingCheck 2.0 (2015 Marcel Imig <a href=\"http://www.frag-duino.de\">www.frag-duino.de</a>)<br><br>\r\n\r\n";
  228.   result += "<fieldset name=\"wifistatus\"><legend>Wifi status</legend><form action=\"\" method=\"post\">";
  229.  
  230.   result += "<table>\r\n";
  231.  
  232.   result += "<tr><td><b>Network SSID:</b></td>\r\n";
  233.   result += "<td><input type=\"text\" name=\"ssid\" value=\"";
  234.   result += network_ssid;
  235.   result += "\" maxlength=\"30\"></td>\r\n</tr>\r\n";
  236.   result += "<tr><td><b>Network Password:</b></td>\r\n";
  237.   result += "<td><input type=\"password\" name=\"password\" value=\"notchanged\" maxlength=\"30\"></td>\r\n</tr>\r\n";
  238.   result += "</td>\r\n</tr>\r\n</table>\r\n";
  239.  
  240.   result += "<input type=\"Submit\" name=\"send\" value=\"Save-Wifi\" >\r\n";
  241.   result += "<input type=\"Submit\" name=\"send\" value=\"Restart\" ></form></fieldset>\r\n\r\n<br>\r\n<br>\r\n";
  242.  
  243.   result += "<fieldset name=\"hosts\"><legend>Hosts</legend>\r\n<b>Monitoring IPs:</b><br><br>\r\n\r\n";
  244.   result += "<form action=\"\" method=\"post\">";
  245.   result += "IP1: <input type=\"text\" name=\"ip1\" value=\"";
  246.   result += host1;
  247.   result += "\" ";
  248.   if (host_is_online[0])
  249.     result += "style=\"background-color:green\"";
  250.   else
  251.     result += "style=\"background-color:red\"";
  252.   result += " maxlength=\"15\"><br>\r\n";
  253.  
  254.   result += "IP2: <input type=\"text\" name=\"ip2\" value=\"";
  255.   result += host2;
  256.   result += "\" ";
  257.   if (host_is_online[1])
  258.     result += "style=\"background-color:green\"";
  259.   else
  260.     result += "style=\"background-color:red\"";
  261.   result += " maxlength=\"15\"><br>\r\n";
  262.  
  263.   result += "IP3: <input type=\"text\" name=\"ip3\" value=\"";
  264.   result += host3;
  265.   result += "\" ";
  266.   if (host_is_online[2])
  267.     result += "style=\"background-color:green\"";
  268.   else
  269.     result += "style=\"background-color:red\"";
  270.   result += " maxlength=\"15\"><br>\r\n";
  271.   result += "<input type=\"Submit\" name=\"send\" value=\"Save\"> \r\n";
  272.   result += "<input type=\"Submit\" name=\"send\" value=\"Refresh\">\r\n\r\n</form>\r\n";
  273.   result += "</body>\r\n</html>";
  274.   server.send(200, "text / html", result);
  275. }
  276.  
  277. void handleDump() {
  278.   Serial.println("EEPROM:");
  279.   for (int i = 0; i < EEPROM_SIZE; i++) {
  280.     Serial.print(i);
  281.     Serial.print(": ");
  282.     Serial.println((char) EEPROM.read(i));
  283.   }
  284.   Serial.println("END");
  285.   server.send(200, "text / html", "DUMPED ON SERIAL OUTPUT");
  286. }
  287.  
  288.  
  289. void setup() {
  290.   // Initialize Serial and LEDs
  291.   Serial.begin(115200);
  292.   delay(20);
  293.   Serial.println("\r\n Ready ");
  294.   pinMode(PIN_LED, OUTPUT);
  295.   digitalWrite(PIN_LED, HIGH);
  296.  
  297.   // Read IPs and Wifi-credentials from EEPROM
  298.   EEPROM.begin(EEPROM_SIZE);
  299.  
  300.   if (RESET_EEPROM) { // First start, initialize EEPROM
  301.     strncpy(host1, "192.168.123.1", 13);
  302.     strncpy(host2, "8.8.8.8", 7);
  303.     strncpy(host3, "8.8.4.4", 7);
  304.  
  305.     writeSSIDtoEEPROM(WIFI_SSID);
  306.     writePASSWORDtoEEPROM(WIFI_PASSWORD);
  307.     writeIPtoEEPROM();
  308.  
  309.     Serial.println("EEPROM initialized");
  310.     while (true)
  311.     {
  312.       digitalWrite(PIN_LED, HIGH);
  313.       delay(100);
  314.       digitalWrite(PIN_LED, LOW);
  315.       delay(100);
  316.     }
  317.   } else {
  318.     // Read from EEPROM
  319.     for (int i = 0; i < 15; i++) {
  320.       host1[i] = EEPROM.read(ADDRESS_IP1 + i);
  321.       host2[i] = EEPROM.read(ADDRESS_IP2 + i);
  322.       host3[i] = EEPROM.read(ADDRESS_IP3 + i);
  323.     }
  324.     for (int i = 0; i < 30; i++) // SSID
  325.       network_ssid[i] = EEPROM.read(ADDRESS_SSID + i);
  326.     for (int i = 0; i < 30; i++) // Password
  327.       network_password[i] = EEPROM.read(ADDRESS_PASSWORD + i);
  328.   }
  329.  
  330.   // Print hosts
  331.   Serial.println("Hosts from EEPROM:");
  332.   Serial.print("[1]: ");
  333.   Serial.println(host1);
  334.   Serial.print("[2]: ");
  335.   Serial.println(host2);
  336.   Serial.print("[3]: ");
  337.   Serial.println(host3);
  338.   Serial.print("Wifi from EEPROM: \"");
  339.   Serial.print(network_ssid);
  340.   Serial.print("\" \"");
  341.   Serial.print(network_password);
  342.   Serial.println("\"\r\n");
  343.  
  344.   // Connect to AP
  345.   WiFi.mode(WIFI_STA); // Set to client
  346.   WiFi.begin(network_ssid, network_password);
  347.  
  348.   while (WiFi.status() != WL_CONNECTED) {
  349.     delay(500);
  350.     Serial.println(".");
  351.     if (state_LED == HIGH)
  352.       state_LED = LOW;
  353.     else
  354.       state_LED = HIGH;
  355.     digitalWrite(PIN_LED, state_LED);
  356.   }
  357.  
  358.   Serial.print("Connected to ");
  359.   Serial.println(network_ssid);
  360.   Serial.print("IP address: ");
  361.   network_ip = WiFi.localIP();
  362.   Serial.println(network_ip);
  363.  
  364.   // Start the server
  365.   server.on("/", handleRoot);
  366.   server.on("/dump/", handleDump);
  367.   server.begin();
  368.  
  369.   digitalWrite(PIN_LED, HIGH);
  370.   delay(250);
  371.   digitalWrite(PIN_LED, LOW);
  372. }
  373.  
  374. void loop() {
  375.   server.handleClient();
  376.   currentMillis = millis(); // Read current runtime
  377.  
  378.   if (WiFi.status() != WL_CONNECTED) {
  379.     Serial.println("Not connected anymore, restarting");
  380.     system_restart();
  381.   }
  382.  
  383.   if (last_ping + PING_INTERVAL < currentMillis) {
  384.     Serial.print(currentMillis);
  385.     const char* host;
  386.  
  387.     currentHost = (currentHost + 1) % count_hosts;
  388.     if (currentHost == 0)
  389.       host = host1;
  390.     else if (currentHost == 1)
  391.       host = host2;
  392.     else
  393.       host = host3;
  394.  
  395.     uint32 address = ipaddr_addr(host);
  396.     if (host[0] == '0' && host[2] == '0' && host[4] == '0' && host[6] == '0')
  397.       Serial.println(": Skipping");
  398.     else {
  399.       Serial.print(": Pinging \"");
  400.       Serial.print(host);
  401.       Serial.print("\" - ");
  402.       ping(address);
  403.       last_ping = currentMillis;
  404.     }
  405.   }
  406.  
  407.   if (last_seen_online + (count_hosts * PING_INTERVAL) > currentMillis)
  408.     state_LED = HIGH;
  409.   else
  410.     state_LED = LOW;
  411.  
  412.   digitalWrite(PIN_LED, state_LED);
  413. }
  414.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement