// _ ___ __ _ _
// | || \ \ / / | | (_)
// | || |\ \ /\ / /__ | |__ _
// |__ _\ \/ \/ / _ \| '_ \| |
// | | \ /\ / (_) | |_) | |
// |_| \/ \/ \___/|_.__/|_|.com
//
// inverterBatteryTemp
// ESP8266 - OneWireBus - MQTT(noCallback)
//
// /*TODO*/
// UPTIME
// MQTT SUB FOR UPTIME AND ALIVE request
// LED_BUILTIN if active(now it's always on...)
//
#include
#include
#include
#include
#define oneWireBus_1 D2
const char* ssid = "";
const char* password = "";
const char* mqtt_user = "";
const char* mqtt_password = "";
const char* mqtt_server = "";
const unsigned int mqtt_port = 1883;
char* mqtt_awaketopic = "inverterBatteryTemp-awake";
char* mqtt_awakemsg = "awake-inverterBatteryTemp";
char* mqtt_alivetopic = "inverterBatteryTemp-alive";
char* mqtt_alivemsg = "inverterBatteryTemp-%ld";
unsigned int mqtt_aliveInterval = 1800; //0.5h
char* mqtt_oneWireBus_1_topic = "inverterBatteryTemp/storage/oneWireBus1";
unsigned int mqtt_oneWireBus_message_interval = 30000; // ms
unsigned long mqtt_oneWireBus_lastsent_message = 0;
unsigned int analogReadInterval = 5000; // ms
unsigned long lastAnalogReadInterval = 0;
int value_C_oneWireBus_1 = 0;
int value_F_oneWireBus_1 = 0;
OneWire oneWire(oneWireBus_1);
DallasTemperature sensors(&oneWire);
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
void setup_wifi() {
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ClosetHandler-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
client.publish(mqtt_awaketopic, mqtt_awakemsg);
Serial.print("Awake message on: ");
Serial.print(mqtt_awaketopic);
Serial.print(" with message: ");
Serial.println(mqtt_awakemsg);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
delay(10);
Serial.println(F("__________________________________________"));
Serial.println(F(""));
Serial.println(F(" _ _ __ v0.1 __ _ _ "));
Serial.println(F(" | || |\\ \\ / / | | (_)"));
Serial.println(F(" | || |_\\ \\ /\\ / /___ | |__ _ "));
Serial.println(F(" |__ _|\\ \\/ \\/ // _ \\ | '_ \\ | |"));
Serial.println(F(" | | \\ /\\ /| (_) || |_) || |"));
Serial.println(F(" |_| \\/ \\/ \\___/ |_.__/ |_|"));
Serial.println(F(""));
Serial.println(F("__________________________________________"));
Serial.println(F(""));
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
sensors.begin();
Serial.println(F("__________________________________________"));
Serial.println(F(""));
Serial.println(F("inverterBatteryTemp - Initialized"));
Serial.println(F("__________________________________________"));
Serial.println(F(""));
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > (mqtt_aliveInterval * 1000)) {
lastMsg = now;
++value;
snprintf (msg, MSG_BUFFER_SIZE, mqtt_alivemsg, value);
Serial.print("Publish alive message: ");
Serial.println(msg);
client.publish(mqtt_alivetopic, msg);
}
/* AnalogRead */
now = millis();
if (now - lastAnalogReadInterval > analogReadInterval) {
lastAnalogReadInterval = now;
// oneWireBus 1
sensors.requestTemperatures();
value_C_oneWireBus_1 = sensors.getTempCByIndex(0);
value_F_oneWireBus_1 = sensors.getTempFByIndex(0);
Serial.print(value_C_oneWireBus_1);
Serial.println("ºC");
Serial.print(value_F_oneWireBus_1);
Serial.println("ºF");
unsigned long analogReadTime = millis() - now;
Serial.print("ANALOG READ TIME: ");
Serial.println(analogReadTime);
/* PUBLISH MSGS */
// oneWireBus 1
if (now - mqtt_oneWireBus_lastsent_message > mqtt_oneWireBus_message_interval) {
mqtt_oneWireBus_lastsent_message = now;
snprintf (msg, MSG_BUFFER_SIZE, "%ld", value_C_oneWireBus_1);
client.publish(mqtt_oneWireBus_1_topic, msg);
}
}
}