Initial commit
This commit is contained in:
+145
@@ -0,0 +1,145 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ModbusIP_ESP8266.h>
|
||||
#include "config.h"
|
||||
|
||||
// network
|
||||
|
||||
IPAddress my_ip(MY_IP);
|
||||
IPAddress gateway(MY_GW);
|
||||
IPAddress subnet(MY_NET);
|
||||
|
||||
// modbus consts and works
|
||||
#define TCP_PORT 15000
|
||||
#define POLL_INTERVAL 10000
|
||||
#define T_UPDATE_INTERVAL 500
|
||||
|
||||
#define MODE_AUTO 0
|
||||
#define MODE_MANUAL 1
|
||||
|
||||
#define REGISTER_ON_TEMP 0
|
||||
#define REGISTER_OFF_TEMP 1
|
||||
#define REGISTER_MODE 2
|
||||
#define REGISTER_CUR_TEMP 3
|
||||
#define REGISTER_CUR_STATE 4
|
||||
#define REGISTER_SECONDARY_TEMP 5
|
||||
|
||||
ModbusIP mb;
|
||||
|
||||
char id[64];
|
||||
|
||||
long last_poll;
|
||||
long last_update;
|
||||
|
||||
int t_on = 100;
|
||||
int t_off = 1000;
|
||||
int cur_mode = MODE_AUTO;
|
||||
int cur_t = 0;
|
||||
int cur_state = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(D2, OUTPUT);
|
||||
Serial.begin(115200);
|
||||
Serial.println("Initialised...");
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.config(my_ip, gateway, subnet);
|
||||
WiFi.setAutoReconnect(true);
|
||||
WiFi.begin(WIFI_NAME, WIFI_PASS);
|
||||
Serial.print("Connecting");
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.print("Connected, IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
uint8_t macAddr[6];
|
||||
WiFi.macAddress(macAddr);
|
||||
sprintf(id, "%02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
|
||||
Serial.printf("Connected, mac address: %s\n", id);
|
||||
last_poll = millis();
|
||||
|
||||
mb.server(15000);
|
||||
mb.addHreg(REGISTER_ON_TEMP, t_on);
|
||||
mb.addHreg(REGISTER_OFF_TEMP, t_off);
|
||||
mb.addHreg(REGISTER_MODE, cur_mode);
|
||||
mb.addHreg(REGISTER_CUR_TEMP, cur_t);
|
||||
mb.addHreg(REGISTER_CUR_STATE, cur_state);
|
||||
mb.addHreg(REGISTER_SECONDARY_TEMP, cur_t);
|
||||
}
|
||||
|
||||
// thermistor
|
||||
#define THERMISTORPIN A0
|
||||
#define THERMISTORNOMINAL 10000
|
||||
#define TEMPERATURENOMINAL 25
|
||||
#define NUMSAMPLES 5
|
||||
#define BCOEFFICIENT 3950
|
||||
#define SERIESRESISTOR 10000
|
||||
|
||||
void UpdateCurrent()
|
||||
{
|
||||
float average = 0;
|
||||
for (int i = 0; i < NUMSAMPLES; i++)
|
||||
{
|
||||
average += analogRead(THERMISTORPIN);
|
||||
delay(10);
|
||||
}
|
||||
average /= NUMSAMPLES;
|
||||
average = 1023 / average - 1;
|
||||
average = SERIESRESISTOR / average;
|
||||
float steinhart;
|
||||
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
|
||||
steinhart = log(steinhart); // ln(R/Ro)
|
||||
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
|
||||
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
|
||||
steinhart = 1.0 / steinhart; // инвертируем
|
||||
steinhart -= 273.15; // конвертируем в градусы по Цельсию
|
||||
cur_t = int(steinhart * 100);
|
||||
last_update = millis();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
unsigned long current = millis();
|
||||
int u = 0;
|
||||
if (current < last_update || current - last_update >= T_UPDATE_INTERVAL)
|
||||
{
|
||||
UpdateCurrent();
|
||||
u = 1;
|
||||
}
|
||||
mb.task();
|
||||
t_on = mb.Hreg(REGISTER_ON_TEMP);
|
||||
t_off = mb.Hreg(REGISTER_OFF_TEMP);
|
||||
cur_mode = mb.Hreg(REGISTER_MODE);
|
||||
if (cur_mode == 0)
|
||||
{
|
||||
if (cur_t < t_on) cur_state = 1;
|
||||
else if (cur_t > t_off) cur_state = 0;
|
||||
mb.Hreg(REGISTER_CUR_STATE, cur_state);
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_state = mb.Hreg(REGISTER_CUR_STATE);
|
||||
}
|
||||
digitalWrite(D2, cur_state);
|
||||
if (u)
|
||||
{
|
||||
Serial.println("Cur state");
|
||||
Serial.println(cur_state);
|
||||
Serial.println(cur_t);
|
||||
}
|
||||
mb.Hreg(REGISTER_ON_TEMP, t_on);
|
||||
mb.Hreg(REGISTER_OFF_TEMP, t_off);
|
||||
mb.Hreg(REGISTER_MODE, cur_mode);
|
||||
mb.Hreg(REGISTER_CUR_TEMP, cur_t);
|
||||
mb.Hreg(REGISTER_SECONDARY_TEMP, cur_t);
|
||||
if (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
// no wifi
|
||||
Serial.println("No WiFi connection");
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user