|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /*
- * eepromsettings.c
- *
- * Created on: 14.05.2020
- * Author: agr
- */
-
- #include "eepromsettings.h"
- #include "struct.h"
-
- /**
- * read_EEPROM_Settings function
- * This function is used to read the EEPROM settings at startup
- *
- * Overview:
- * - Set the PIN for the RESET-button to input and activate pullups
- * - Load the stored data from EEPROM into the eeprom_config struct
- * - Check if a config is stored or the reset button is pressed. If one of the conditions is ture, set the defaults
- */
- void read_EEPROM_Settings() {
- pinMode(RESET_PIN, INPUT);
- digitalWrite(RESET_PIN, HIGH);
-
- // read the current config
- EEPROM_readAnything(0, eeprom_config);
-
- // check if config is present or if reset button is pressed
- if (eeprom_config.config_set != 1 || digitalRead(RESET_PIN) == LOW) {
- // set default values
- #ifdef DEBUG
- Serial.println(eeprom_config.config_set);
- Serial.println(digitalRead(RESET_PIN));
- #endif
-
- set_EEPROM_Default();
- #ifdef DEBUG
- Serial.println("write default config");
- #endif
- // write the config to eeprom
- EEPROM_writeAnything(0, eeprom_config);
- }
- }
-
- /**
- * print_EEPROM_Settings() function
- *
- * This function is used for debugging the configuration.
- * It prints the actual configuration to the serial port.
- */
- #ifdef DEBUG
- void print_EEPROM_Settings() {
- Serial.print("IP: ");
- for(int i = 0; i<4; i++) {
- Serial.print(eeprom_config.ip[i]);
- if (i<3) {
- Serial.print('.');
- }
- }
- Serial.println();
-
- Serial.print("Subnet: ");
- for(int i = 0; i<4; i++) {
- Serial.print(eeprom_config.subnet[i]);
- if (i<3) {
- Serial.print('.');
- }
- }
- Serial.println();
-
- Serial.print("Gateway: ");
- for(int i = 0; i<4; i++) {
- Serial.print(eeprom_config.gateway[i]);
- if (i<3) {
- Serial.print('.');
- }
- }
- Serial.println();
-
- Serial.print("DNS Server: ");
- for(int i = 0; i<4; i++) {
- Serial.print(eeprom_config.dns_server[i]);
- if (i<3) {
- Serial.print('.');
- }
- }
- Serial.println();
-
- Serial.print("MQTT Server: ");
- for(int i = 0; i<4; i++) {
- Serial.print(eeprom_config.mqtt_server[i]);
- if (i<3) {
- Serial.print('.');
- }
- }
- Serial.println();
-
- Serial.print("MAC: ");
- for (int a=0;a<6;a++) {
- Serial.print(eeprom_config.mac[a],HEX);
- if(a<5) {
- Serial.print(":");
- }
- }
- Serial.println();
- Serial.print("MQTTserver Port: ");
- Serial.println(eeprom_config.mqttserverPort);
-
- Serial.print("Webserver Port: ");
- Serial.println(eeprom_config.webserverPort);
-
- Serial.print("USE DHCP: ");
- Serial.println(eeprom_config.use_dhcp);
-
- Serial.print(" DHCP renew every ");
- Serial.print(eeprom_config.dhcp_refresh_minutes);
- Serial.println(" minutes");
-
- Serial.print("Config Set: ");
- Serial.println(eeprom_config.config_set);
-
- }
- #endif
-
- // #############################################################################################################################################################
-
-
- /**
- * renewDHCP() function
- * Renew the DHCP relase in a given interval.
- *
- * Overview:
- * - Check if interval = 0 and set it to 1
- * - Check if renew interval is reached and renew the lease
- */
- void renewDHCP(int interval) {
- unsigned long interval_millis = interval * 60000;
-
- if (interval == 0 ) {
- interval = 1;
- }
- if (eeprom_config.use_dhcp==1) {
- if((millis() - last_dhcp_renew) > interval_millis) {
- last_dhcp_renew=millis();
- dhcp_state = Ethernet.maintain();
- }
- }
- }
-
-
- /**
- * setupNetwork() function
- * This function is used to setupup the network according to the values stored in the eeprom
- *
- * Overview:
- * - First of all read the EEPROM settings
- * - Display a link to the ethernet setup
- * - Check if DHCP should be used, if not create instaces of IPAddress for ip, gateway, subnet and dns_server
- * - Invoke Ethernet.begin with all parameters if no dhcp is active (Ethernet.begin(mac, ip, dns_server, gateway, subnet);).
- * - If DHCP is used invoke only with mac (Ethernet.begin(mac);) and display the ip on the serial console.
- */
- void setupNetwork() {
- read_EEPROM_Settings();
-
- #ifdef DEBUG
- print_EEPROM_Settings();
- #endif
-
- // 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] };
-
- if (eeprom_config.use_dhcp != 1) {
- IPAddress ip(eeprom_config.ip[0], eeprom_config.ip[1], eeprom_config.ip[2], eeprom_config.ip[3]);
- IPAddress gateway (eeprom_config.gateway[0],eeprom_config.gateway[1],eeprom_config.gateway[2],eeprom_config.gateway[3]);
- IPAddress subnet (eeprom_config.subnet[0], eeprom_config.subnet[1], eeprom_config.subnet[2], eeprom_config.subnet[3]);
- IPAddress dns_server (eeprom_config.dns_server[0], eeprom_config.dns_server[1], eeprom_config.dns_server[2], eeprom_config.dns_server[3]);
- Ethernet.begin(eeprom_config.mac, ip, dns_server, gateway, subnet);
- } else {
- if (Ethernet.begin(eeprom_config.mac) == 0) {
- Serial.print("Failed to configure Ethernet using DHCP");
- }
- Serial.println(Ethernet.localIP());
- }
- }
- // END Network section #########################################################################################################################################
|