ESP32 mit WIFI und MQTT rx und tx
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * eepromsettings.c
  3. *
  4. * Created on: 14.05.2020
  5. * Author: agr
  6. */
  7. #include "eepromsettings.h"
  8. #include "struct.h"
  9. /**
  10. * read_EEPROM_Settings function
  11. * This function is used to read the EEPROM settings at startup
  12. *
  13. * Overview:
  14. * - Set the PIN for the RESET-button to input and activate pullups
  15. * - Load the stored data from EEPROM into the eeprom_config struct
  16. * - Check if a config is stored or the reset button is pressed. If one of the conditions is ture, set the defaults
  17. */
  18. void read_EEPROM_Settings() {
  19. pinMode(RESET_PIN, INPUT);
  20. digitalWrite(RESET_PIN, HIGH);
  21. // read the current config
  22. EEPROM_readAnything(0, eeprom_config);
  23. // check if config is present or if reset button is pressed
  24. if (eeprom_config.config_set != 1 || digitalRead(RESET_PIN) == LOW) {
  25. // set default values
  26. #ifdef DEBUG
  27. Serial.println(eeprom_config.config_set);
  28. Serial.println(digitalRead(RESET_PIN));
  29. #endif
  30. set_EEPROM_Default();
  31. #ifdef DEBUG
  32. Serial.println("write default config");
  33. #endif
  34. // write the config to eeprom
  35. EEPROM_writeAnything(0, eeprom_config);
  36. }
  37. }
  38. /**
  39. * print_EEPROM_Settings() function
  40. *
  41. * This function is used for debugging the configuration.
  42. * It prints the actual configuration to the serial port.
  43. */
  44. #ifdef DEBUG
  45. void print_EEPROM_Settings() {
  46. Serial.print("IP: ");
  47. for(int i = 0; i<4; i++) {
  48. Serial.print(eeprom_config.ip[i]);
  49. if (i<3) {
  50. Serial.print('.');
  51. }
  52. }
  53. Serial.println();
  54. Serial.print("Subnet: ");
  55. for(int i = 0; i<4; i++) {
  56. Serial.print(eeprom_config.subnet[i]);
  57. if (i<3) {
  58. Serial.print('.');
  59. }
  60. }
  61. Serial.println();
  62. Serial.print("Gateway: ");
  63. for(int i = 0; i<4; i++) {
  64. Serial.print(eeprom_config.gateway[i]);
  65. if (i<3) {
  66. Serial.print('.');
  67. }
  68. }
  69. Serial.println();
  70. Serial.print("DNS Server: ");
  71. for(int i = 0; i<4; i++) {
  72. Serial.print(eeprom_config.dns_server[i]);
  73. if (i<3) {
  74. Serial.print('.');
  75. }
  76. }
  77. Serial.println();
  78. Serial.print("MQTT Server: ");
  79. for(int i = 0; i<4; i++) {
  80. Serial.print(eeprom_config.mqtt_server[i]);
  81. if (i<3) {
  82. Serial.print('.');
  83. }
  84. }
  85. Serial.println();
  86. Serial.print("MAC: ");
  87. for (int a=0;a<6;a++) {
  88. Serial.print(eeprom_config.mac[a],HEX);
  89. if(a<5) {
  90. Serial.print(":");
  91. }
  92. }
  93. Serial.println();
  94. Serial.print("MQTTserver Port: ");
  95. Serial.println(eeprom_config.mqttserverPort);
  96. Serial.print("Webserver Port: ");
  97. Serial.println(eeprom_config.webserverPort);
  98. Serial.print("USE DHCP: ");
  99. Serial.println(eeprom_config.use_dhcp);
  100. Serial.print(" DHCP renew every ");
  101. Serial.print(eeprom_config.dhcp_refresh_minutes);
  102. Serial.println(" minutes");
  103. Serial.print("Config Set: ");
  104. Serial.println(eeprom_config.config_set);
  105. }
  106. #endif
  107. // #############################################################################################################################################################
  108. /**
  109. * renewDHCP() function
  110. * Renew the DHCP relase in a given interval.
  111. *
  112. * Overview:
  113. * - Check if interval = 0 and set it to 1
  114. * - Check if renew interval is reached and renew the lease
  115. */
  116. void renewDHCP(int interval) {
  117. unsigned long interval_millis = interval * 60000;
  118. if (interval == 0 ) {
  119. interval = 1;
  120. }
  121. if (eeprom_config.use_dhcp==1) {
  122. if((millis() - last_dhcp_renew) > interval_millis) {
  123. last_dhcp_renew=millis();
  124. dhcp_state = Ethernet.maintain();
  125. }
  126. }
  127. }
  128. /**
  129. * setupNetwork() function
  130. * This function is used to setupup the network according to the values stored in the eeprom
  131. *
  132. * Overview:
  133. * - First of all read the EEPROM settings
  134. * - Display a link to the ethernet setup
  135. * - Check if DHCP should be used, if not create instaces of IPAddress for ip, gateway, subnet and dns_server
  136. * - Invoke Ethernet.begin with all parameters if no dhcp is active (Ethernet.begin(mac, ip, dns_server, gateway, subnet);).
  137. * - If DHCP is used invoke only with mac (Ethernet.begin(mac);) and display the ip on the serial console.
  138. */
  139. void setupNetwork() {
  140. read_EEPROM_Settings();
  141. #ifdef DEBUG
  142. print_EEPROM_Settings();
  143. #endif
  144. // byte mac[] = { eeprom_config.mac[0], eeprom_config.mac[1], eeprom_config.mac[2], eeprom_config.mac[3], eeprom_config.mac[4], eeprom_config.mac[5] };
  145. if (eeprom_config.use_dhcp != 1) {
  146. IPAddress ip(eeprom_config.ip[0], eeprom_config.ip[1], eeprom_config.ip[2], eeprom_config.ip[3]);
  147. IPAddress gateway (eeprom_config.gateway[0],eeprom_config.gateway[1],eeprom_config.gateway[2],eeprom_config.gateway[3]);
  148. IPAddress subnet (eeprom_config.subnet[0], eeprom_config.subnet[1], eeprom_config.subnet[2], eeprom_config.subnet[3]);
  149. IPAddress dns_server (eeprom_config.dns_server[0], eeprom_config.dns_server[1], eeprom_config.dns_server[2], eeprom_config.dns_server[3]);
  150. Ethernet.begin(eeprom_config.mac, ip, dns_server, gateway, subnet);
  151. } else {
  152. if (Ethernet.begin(eeprom_config.mac) == 0) {
  153. Serial.print("Failed to configure Ethernet using DHCP");
  154. }
  155. Serial.println(Ethernet.localIP());
  156. }
  157. }
  158. // END Network section #########################################################################################################################################