ESP32 mit WIFI und MQTT rx und tx
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

struct.h 2.8KB

4 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * struct.h
  3. *
  4. * Created on: 07.04.2020
  5. * Author: agr
  6. */
  7. #ifndef STRUCT_H_
  8. #define STRUCT_H_
  9. #include <PubSubClient.h>
  10. #include <Ethernet.h>
  11. #include <EEPROM.h>
  12. struct config_t
  13. {
  14. byte config_set;
  15. byte use_dhcp;
  16. byte dhcp_refresh_minutes;
  17. byte mac[6];
  18. byte ip[4];
  19. byte gateway[4];
  20. byte subnet[4];
  21. byte dns_server[4];
  22. unsigned int webserverPort;
  23. byte mqtt_server[4];
  24. unsigned int mqttserverPort;
  25. } eeprom_config;
  26. void set_EEPROM_Default() {
  27. eeprom_config.config_set=1; // dont change! It's used to check if the config is already set
  28. eeprom_config.use_dhcp=1; // use DHCP per default
  29. eeprom_config.dhcp_refresh_minutes=60; // refresh the DHCP every 60 minutes
  30. // set the default MAC address. In this case its DE:AD:BE:EF:FE:ED
  31. eeprom_config.mac[0]=0xDE;
  32. eeprom_config.mac[1]=0xAD;
  33. eeprom_config.mac[2]=0xBE;
  34. eeprom_config.mac[3]=0xEF;
  35. eeprom_config.mac[4]=0xFE;
  36. eeprom_config.mac[5]=0xED;
  37. // set the default IP address for the arduino. In this case its 192.168.1.97
  38. eeprom_config.ip[0]=192;
  39. eeprom_config.ip[1]=168;
  40. eeprom_config.ip[2]=1;
  41. eeprom_config.ip[3]=97;
  42. // set the default GATEWAY. In this case its 192.168.1.1
  43. eeprom_config.gateway[0]=192;
  44. eeprom_config.gateway[1]=168;
  45. eeprom_config.gateway[2]=1;
  46. eeprom_config.gateway[3]=1;
  47. // set the default SUBNET. In this case its 255.255.255.0
  48. eeprom_config.subnet[0]=255;
  49. eeprom_config.subnet[1]=255;
  50. eeprom_config.subnet[2]=255;
  51. eeprom_config.subnet[3]=0;
  52. // set the default DNS SERVER. In this case its 192.168.1.1
  53. eeprom_config.dns_server[0]=192;
  54. eeprom_config.dns_server[1]=168;
  55. eeprom_config.dns_server[2]=1;
  56. eeprom_config.dns_server[3]=1;
  57. // set the default Webserver Port. In this case its Port 80
  58. eeprom_config.webserverPort=80;
  59. // set the default MQTT SERVER. In this case its 192.168.1.35
  60. eeprom_config.mqtt_server[0]=192;
  61. eeprom_config.mqtt_server[1]=168;
  62. eeprom_config.mqtt_server[2]=1;
  63. eeprom_config.mqtt_server[3]=35;
  64. // set the default MqttServer Port. In this case its Port 1883
  65. eeprom_config.mqttserverPort=1883;
  66. }
  67. #define P(name) static const unsigned char name[] __attribute__(( section(".progmem." #name) ))
  68. int EEPROM_readAnything(int ee, config_t& value)
  69. {
  70. byte* p = (byte*)(void*)&value;
  71. unsigned int i;
  72. for (i = 0; i < sizeof(value); i++)
  73. *p++ = EEPROM.read(ee++);
  74. return i;
  75. }
  76. int EEPROM_writeAnything(int ee, const config_t& value)
  77. {
  78. const byte* p = (const byte*)(const void*)&value;
  79. unsigned int i;
  80. for (i = 0; i < sizeof(value); i++)
  81. EEPROM.write(ee++, *p++);
  82. return i;
  83. }
  84. EthernetClient MQTTethClient;
  85. PubSubClient MQTTclient(MQTTethClient);
  86. #endif /* STRUCT_H_ */