dynamic ip configuration

This commit is contained in:
Siwat Sirichai 2023-09-12 00:37:33 +07:00
parent 82be4f0342
commit 9520802f7b
2 changed files with 167 additions and 30 deletions

View File

@ -12,14 +12,14 @@
#include <lcd.hpp> #include <lcd.hpp>
// Network Connectivity // Network Connectivity
#define HOSTNAME "espmega-pro-r3" char HOSTNAME[15];
const IPAddress IP(192, 168, 0, 210); IPAddress IP(0, 0, 0, 0);
const IPAddress SUBNET(255, 255, 255, 0); IPAddress SUBNET(0, 0, 0, 0);
const IPAddress GATEWAY(192, 168, 0, 1); IPAddress GATEWAY(0, 0, 0, 0);
const IPAddress DNS(10, 192, 1, 1); IPAddress DNS(0, 0, 0, 0);
const IPAddress MQTT_SERVER(192, 168, 0, 26); IPAddress MQTT_SERVER(0, 0, 0, 0);
uint16_t MQTT_PORT = 0;
bool standalone = true; bool standalone = true;
const int MQTT_PORT = 1883;
#define MQTT_BASE_TOPIC "/espmega/ProR3" #define MQTT_BASE_TOPIC "/espmega/ProR3"
char STATE_REQUEST_TOPIC[75] = MQTT_BASE_TOPIC "/requeststate"; char STATE_REQUEST_TOPIC[75] = MQTT_BASE_TOPIC "/requeststate";
// #define MQTT_USE_AUTH // #define MQTT_USE_AUTH
@ -41,7 +41,7 @@ const uint8_t pwm_pins[PWM_COUNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1
bool pwm_states[PWM_COUNT]; bool pwm_states[PWM_COUNT];
uint8_t pwm_states_eeprom[PWM_COUNT]; uint8_t pwm_states_eeprom[PWM_COUNT];
uint16_t pwm_values[PWM_COUNT]; uint16_t pwm_values[PWM_COUNT];
uint8_t pwm_values_eeprom[PWM_COUNT*2]; uint8_t pwm_values_eeprom[PWM_COUNT * 2];
// output = m*input+c // output = m*input+c
const float pwm_linear_scaling_m[PWM_COUNT] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; const float pwm_linear_scaling_m[PWM_COUNT] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
const float pwm_linear_scaling_c[PWM_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; const float pwm_linear_scaling_c[PWM_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
@ -81,6 +81,21 @@ char AC_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/temperature";
char AC_ROOM_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/room_temperature"; char AC_ROOM_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/room_temperature";
char AC_HUMIDITY_TOPIC[75] = MQTT_BASE_TOPIC "/ac/humidity"; char AC_HUMIDITY_TOPIC[75] = MQTT_BASE_TOPIC "/ac/humidity";
// EEPROM ADDRESS
#define EEPROM_ADDRESS_AC_MODE 0 // 01bytes
#define EEPROM_ADDRESS_AC_TEMPERATURE 1 // 01bytes
#define EEPROM_ADDRESS_AC_FAN_SPEED 2 // 01bytes
#define EEPROM_ADDRESS_PWM_STATE 3 // 16bytes, thru 18
#define EEPROM_ADDRESS_PWM_VALUE 19 // 32bytes, thru 50
#define EEPROM_ADDRESS_HOSTNAME 65 // 15bytes, thru 79
#define EEPROM_ADDRESS_TOPIC 80 // 20bytes, thru 99
#define EEPROM_ADDRESS_IP 100 // 04bytes, thru 103
#define EEPROM_ADDRESS_SUBNET 104 // 04bytes, thru 107
#define EEPROM_ADDRESS_GATEWAY 108 // 04bytes, thru 111
#define EEPROM_ADDRESS_DNS 112 // 04bytes, thru 115
#define EEPROM_ADDRESS_MQTT_SERVER 116 // 04bytes, thru 119
#define EEPROM_ADDRESS_MQTT_PORT 120 // 02bytes, thru 121
// Forward declaration // Forward declaration
void virtual_interrupt_loop(); void virtual_interrupt_loop();
void virtual_interrupt_callback(int pin, int state); void virtual_interrupt_callback(int pin, int state);
@ -138,6 +153,20 @@ void eeprom_pwm_update();
void lcd_ac_refresh_fan(); void lcd_ac_refresh_fan();
void lcd_ac_refresh_mode(); void lcd_ac_refresh_mode();
void set_ip(String address);
void set_netmask(String address);
void set_dns(String address);
void set_gw(String address);
void set_mqtt_server(String address);
void eeprom_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4);
IPAddress eeprom_ip_retrieve(uint16_t rom_address);
void set_hostname(String hostname);
void eeprom_hostname_retrieve();
void set_topic(String topic);
void eeprom_topic_retrieve();
void mqtt_port_set(uint16_t port);
void eeprom_mqtt_port_retrieve();
char PWM_STATE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/state"; char PWM_STATE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/state";
char PWM_VALUE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/value"; char PWM_VALUE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/value";
char INPUTS_TOPIC[75] = MQTT_BASE_TOPIC "/input/00"; char INPUTS_TOPIC[75] = MQTT_BASE_TOPIC "/input/00";
@ -161,13 +190,14 @@ void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
panel.begin(115200); panel.begin(115200);
lcd_init(); Serial.println("ESPMega R3 Initializing");
lcd_begin();
lcd_send_command("boot_state.txt=\"Core Initializing . . .\"");
Serial.println("ESPMega R3 Initializing . . .");
ESPMega_begin(); ESPMega_begin();
io_begin(); io_begin();
eeprom_retrieve_init(); eeprom_retrieve_init();
lcd_send_stop_bit();
lcd_init();
lcd_begin();
lcd_send_command("boot_state.txt=\"Core Initializing . . .\"");
Serial.println("Initializing Infrared . . ."); Serial.println("Initializing Infrared . . .");
lcd_send_command("boot_state.txt=\"Infrared Initializing . . .\""); lcd_send_command("boot_state.txt=\"Infrared Initializing . . .\"");
IrReceiver.begin(IR_RECIEVE_PIN); IrReceiver.begin(IR_RECIEVE_PIN);
@ -202,34 +232,49 @@ void loop()
void eeprom_retrieve_init() void eeprom_retrieve_init()
{ {
// EEPROM Data Retrival // EEPROM Data Retrival
ac_mode = ESPMega_EEPROM.readByte(0); ac_mode = ESPMega_EEPROM.readByte(EEPROM_ADDRESS_AC_MODE);
ac_temperature = ESPMega_EEPROM.readByte(1); ac_temperature = ESPMega_EEPROM.readByte(EEPROM_ADDRESS_AC_TEMPERATURE);
ac_fan_speed = ESPMega_EEPROM.readByte(2); ac_fan_speed = ESPMega_EEPROM.readByte(EEPROM_ADDRESS_AC_FAN_SPEED);
// EEPROM Data Retrival Validation // EEPROM Data Retrival Validation
if (ac_mode > 2) if (ac_mode > 2)
{ {
ac_mode = 0; ac_mode = 0;
ESPMega_EEPROM.writeByte(0, ac_mode); ESPMega_EEPROM.writeByte(EEPROM_ADDRESS_AC_MODE, ac_mode);
} }
if (ac_temperature > AC_MAX_TEMPERATURE || ac_temperature < AC_MIN_TEMPERATURE) if (ac_temperature > AC_MAX_TEMPERATURE || ac_temperature < AC_MIN_TEMPERATURE)
{ {
ac_temperature = AC_MAX_TEMPERATURE; ac_temperature = AC_MAX_TEMPERATURE;
ESPMega_EEPROM.writeByte(1, ac_temperature); ESPMega_EEPROM.writeByte(EEPROM_ADDRESS_AC_TEMPERATURE, ac_temperature);
} }
if (ac_fan_speed > 3) if (ac_fan_speed > 3)
{ {
ac_fan_speed = 0; ac_fan_speed = 0;
ESPMega_EEPROM.writeByte(2, ac_fan_speed); ESPMega_EEPROM.writeByte(EEPROM_ADDRESS_AC_TEMPERATURE, ac_fan_speed);
} }
ac_set_state(ac_mode,ac_temperature,ac_fan_speed); ac_set_state(ac_mode, ac_temperature, ac_fan_speed);
ESPMega_EEPROM.readBlock(3, pwm_states_eeprom, 16); ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_PWM_STATE, pwm_states_eeprom, 16);
memcpy(pwm_states, pwm_states_eeprom, 16); memcpy(pwm_states, pwm_states_eeprom, 16);
ESPMega_EEPROM.readBlock(19, pwm_values_eeprom, 32); ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_PWM_VALUE, pwm_values_eeprom, 32);
memcpy(pwm_values, pwm_values_eeprom, 32); memcpy(pwm_values, pwm_values_eeprom, 32);
for(int i =0;i<15;i++) { for (int i = 0; i < 15; i++)
pwm_set_state(i,pwm_states[i]); {
pwm_set_value(i,pwm_values[i]); if (pwm_states[i] <= 1)
pwm_set_state(i, pwm_states[i]);
else
pwm_set_state(i, 0);
if (pwm_values[i] <= 4095)
pwm_set_value(i, pwm_values[i]);
else
pwm_set_value(i, 0);
} }
IP = eeprom_ip_retrieve(EEPROM_ADDRESS_IP);
SUBNET = eeprom_ip_retrieve(EEPROM_ADDRESS_SUBNET);
GATEWAY = eeprom_ip_retrieve(EEPROM_ADDRESS_GATEWAY);
DNS = eeprom_ip_retrieve(EEPROM_ADDRESS_DNS);
MQTT_SERVER = eeprom_ip_retrieve(EEPROM_ADDRESS_MQTT_SERVER);
eeprom_hostname_retrieve();
eeprom_mqtt_port_retrieve();
mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
} }
void io_begin() void io_begin()
@ -237,11 +282,6 @@ void io_begin()
Serial.println("Initializing I/O . . ."); Serial.println("Initializing I/O . . .");
pinMode(IR_RECIEVE_PIN, INPUT_PULLUP); pinMode(IR_RECIEVE_PIN, INPUT_PULLUP);
pinMode(IR_SEND_PIN, OUTPUT); pinMode(IR_SEND_PIN, OUTPUT);
memset(pwm_states, 0, PWM_COUNT);
for (int i = 0; i < PWM_COUNT; i++)
{
pwm_values[i] = 4095;
}
} }
void network_begin() void network_begin()
@ -725,7 +765,15 @@ void lcd_refresh()
panel.writeStr("pwm_state.txt", pwm_states[lcd_pwmAdj_id] ? "ON" : "OFF"); panel.writeStr("pwm_state.txt", pwm_states[lcd_pwmAdj_id] ? "ON" : "OFF");
panel.writeNum("pwm_value.val", pwm_values[lcd_pwmAdj_id]); panel.writeNum("pwm_value.val", pwm_values[lcd_pwmAdj_id]);
break; break;
case 6:
panel.writeStr("ip_set.txt", IP.toString());
panel.writeStr("netmask_set.txt", SUBNET.toString());
panel.writeStr("gateway_set.txt", GATEWAY.toString());
panel.writeStr("dns_set.txt", DNS.toString());
panel.writeStr("mqttsv_set.txt", MQTT_SERVER.toString());
panel.writeStr("host_set.txt", HOSTNAME);
panel.writeNum("port_set.val", MQTT_PORT);
break;
default: default:
break; break;
} }
@ -833,6 +881,19 @@ void trigger12()
void trigger13() void trigger13()
{ {
set_ip(panel.readStr("ip_set.txt"));
set_netmask(panel.readStr("netmask_set.txt"));
set_gw(panel.readStr("gateway_set.txt"));
set_dns(panel.readStr("dns_set.txt"));
set_mqtt_server(panel.readStr("mqttsv_set.txt"));
set_hostname(panel.readStr("host_set.txt"));
uint16_t port = panel.readNumber("port_set.val");
mqtt_port_set(port);
Serial.println("NEWPORT:");
Serial.println(port);
delay(100);
ESP.restart();
} }
void eeprom_pwm_update() void eeprom_pwm_update()
@ -852,3 +913,66 @@ void eeprom_pwm_update()
lcd_send_stop_bit(); lcd_send_stop_bit();
} }
} }
void set_ip(String address)
{
IP.fromString(address);
eeprom_ip_update(EEPROM_ADDRESS_IP, IP[0], IP[1], IP[2], IP[3]);
}
void set_netmask(String address)
{
SUBNET.fromString(address);
eeprom_ip_update(EEPROM_ADDRESS_SUBNET, SUBNET[0], SUBNET[1], SUBNET[2], SUBNET[3]);
}
void set_dns(String address)
{
DNS.fromString(address);
eeprom_ip_update(EEPROM_ADDRESS_DNS, DNS[0], DNS[1], DNS[2], DNS[3]);
}
void set_gw(String address)
{
GATEWAY.fromString(address);
eeprom_ip_update(EEPROM_ADDRESS_GATEWAY, GATEWAY[0], GATEWAY[1], GATEWAY[2], GATEWAY[3]);
}
void set_mqtt_server(String address)
{
MQTT_SERVER.fromString(address);
eeprom_ip_update(EEPROM_ADDRESS_MQTT_SERVER, MQTT_SERVER[0], MQTT_SERVER[1], MQTT_SERVER[2], MQTT_SERVER[3]);
}
void eeprom_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
{
uint8_t addressblock[4] = {byte1, byte2, byte3, byte4};
ESPMega_EEPROM.writeBlock(rom_address, addressblock, 4);
}
IPAddress eeprom_ip_retrieve(uint16_t rom_address)
{
uint8_t addressblock[4];
ESPMega_EEPROM.readBlock(rom_address, addressblock, 4);
return IPAddress(addressblock[0], addressblock[1], addressblock[2], addressblock[3]);
}
void set_hostname(String hostname)
{
hostname.toCharArray(HOSTNAME, 15);
ESPMega_EEPROM.writeBlock(EEPROM_ADDRESS_HOSTNAME, (uint8_t *)HOSTNAME, 15);
}
void eeprom_hostname_retrieve()
{
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_HOSTNAME, (uint8_t *)HOSTNAME, 15);
Serial.print("HOSTNAMEREAD:");
Serial.println(HOSTNAME);
}
void mqtt_port_set(uint16_t port)
{
uint8_t port_arr[2];
memcpy(port_arr, &port, 2);
ESPMega_EEPROM.writeBlock(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2);
}
void eeprom_mqtt_port_retrieve()
{
uint8_t port_arr[2];
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_MQTT_PORT, port_arr, 2);
memcpy(&MQTT_PORT, port_arr, 2);
}

13
src/user_code.cpp Normal file
View File

@ -0,0 +1,13 @@
void user_init() {
}
void user_loop() {
}
void virtual_interrupt_user_callback(int pin, int state) {
//Automation: Light Switch
}
void timer_tick_callback() {
//Automation: Night Light
}