|
- /*
- * 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 #########################################################################################################################################
|