Skip to content

Commit

Permalink
Merge pull request #146 from zholdak/Add-Dallas-Temperature-Sensors-S…
Browse files Browse the repository at this point in the history
…upport

Dallas Temperature Sensors Support (DS18x20)
  • Loading branch information
softwarecrash authored Oct 9, 2024
2 parents 8dfdb07 + a7f0b7f commit 822d68f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ lib_deps =
https://github.com/dok-net/ghostl
robtillaart/CRC@^1.0.1
4-20ma/ModbusMaster@^2.0.1
paulstoffregen/OneWire@^2.3.7
milesburton/DallasTemperature@^3.11.0

[env:d1_mini]
board = d1_mini
Expand Down
2 changes: 1 addition & 1 deletion src/PI_Serial/QPIWS.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool PI_Serial::PIXX_QPIWS()
}
else
{
liveData["Fault_code"] = "None";
liveData["Fault_code"] = "Ok";
}
}
return true;
Expand Down
96 changes: 96 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Solar2MQTT Project
#include "htmlProzessor.h"
#include "PI_Serial/PI_Serial.h"

#ifdef DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
#endif

PI_Serial mppClient(INVERTER_RX, INVERTER_TX);
WiFiClient client;
PubSubClient mqttclient(client);
Expand All @@ -25,6 +30,13 @@ AsyncWebSocketClient *wsClient;
DNSServer dns;
Settings settings;

#ifdef DS18B20
OneWire oneWire(TEMPSENS_PIN);
DallasTemperature tempSens(&oneWire);
DeviceAddress tempDeviceAddress;
uint8_t numOfTempSens;
#endif

#include "status-LED.h"

// new importetd
Expand Down Expand Up @@ -415,6 +427,18 @@ void setup()
analogWrite(LED_SRV, 255);
analogWrite(LED_NET, 255);
resetCounter(false);

#ifdef DS18B20
tempSens.begin();
numOfTempSens = tempSens.getDeviceCount();
for (int i = 0; i < numOfTempSens; i++)
{
if (tempSens.getAddress(tempDeviceAddress, i))
{
tempSens.setResolution(tempDeviceAddress, 9);
}
}
#endif
}

void loop()
Expand Down Expand Up @@ -469,6 +493,12 @@ bool prozessData()
}
if (millis() - mqtttimer > (settings.data.mqttRefresh * 1000) || mqtttimer == 0)
{
#ifdef DS18B20
if (numOfTempSens > 0)
{
tempSens.requestTemperatures();
}
#endif
sendtoMQTT(); // Update data to MQTT server if we should
mqtttimer = millis();
}
Expand All @@ -490,6 +520,19 @@ void getJsonData()
deviceJson[F("runtime")] = millis() / 1000;
deviceJson[F("ws_clients")] = ws.count();
deviceJson[F("detect_protocol")] = mppClient.protocol;
#ifdef DS18B20
for (int i = 0; i < numOfTempSens; i++)
{
if (tempSens.getAddress(tempDeviceAddress, i))
{
float tempC = tempSens.getTempC(tempDeviceAddress);
if (tempC != DEVICE_DISCONNECTED_C)
{
deviceJson["DS18B20_" + String(i + 1)] = tempC;
}
}
}
#endif
}

char *topicBuilder(char *buffer, char const *path, char const *numering = "")
Expand Down Expand Up @@ -560,6 +603,21 @@ bool sendtoMQTT()
writeLog("raw command answer: ",mppClient.get.raw.commandAnswer);
mppClient.get.raw.commandAnswer = "";
}
#ifdef DS18B20
for (int i = 0; i < numOfTempSens; i++)
{
if (tempSens.getAddress(tempDeviceAddress, i))
{
float tempC = tempSens.getTempC(tempDeviceAddress);
if (tempC != DEVICE_DISCONNECTED_C)
{
char valBuffer[8];
sprintf(msgBuffer1, "%s/DS18B20_%i", settings.data.mqttTopic, (i + 1));
mqttclient.publish(msgBuffer1, dtostrf(tempC, 4, 1, valBuffer));
}
}
}
#endif
// RAW
mqttclient.publish(topicBuilder(buff, "RAW/Q1"), (mppClient.get.raw.q1).c_str());
mqttclient.publish(topicBuilder(buff, "RAW/QPIGS"), (mppClient.get.raw.qpigs).c_str());
Expand Down Expand Up @@ -690,6 +748,44 @@ bool sendHaDiscovery()
mqttclient.endPublish();
}
}
#ifdef DS18B20
// Ext Temp sensors
for (int i = 0; i < numOfTempSens; i++)
{
if (tempSens.getAddress(tempDeviceAddress, i))
{
String haDeviceDescription = String("\"dev\":") +
"{\"ids\":[\"" + mqttClientId + "\"]," +
"\"name\":\"" + settings.data.deviceName + "\"," +
"\"cu\":\"http://" + WiFi.localIP().toString() + "\"," +
"\"mdl\":\"EPEver2MQTT\"," +
"\"mf\":\"SoftWareCrash\"," +
"\"sw\":\"" + SOFTWARE_VERSION + "\"" +
"}";

String haPayLoad = String("{") +
"\"name\":\"DS18B20_" + (i + 1) + "\"," +
"\"stat_t\":\"" + settings.data.mqttTopic + "/DS18B20_" + (i + 1) + "\"," +
"\"avty_t\":\"" + settings.data.mqttTopic + "/Alive\"," +
"\"pl_avail\": \"true\"," +
"\"pl_not_avail\": \"false\"," +
"\"uniq_id\":\"" + mqttClientId + ".DS18B20_" + (i + 1) + "\"," +
"\"ic\":\"mdi:thermometer-lines\"," +
"\"unit_of_meas\":\"°C\"," +
"\"dev_cla\":\"temperature\",";
haPayLoad += haDeviceDescription;
haPayLoad += "}";
sprintf(topBuff, "homeassistant/sensor/%s/DS18B20_%d/config", settings.data.mqttTopic, (i + 1)); // build the topic

mqttclient.beginPublish(topBuff, haPayLoad.length(), true);
for (size_t i = 0; i < haPayLoad.length(); i++)
{
mqttclient.write(haPayLoad[i]);
}
mqttclient.endPublish();
}
}
#endif
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#define LED_SRV 0
#define LED_NET 4

#ifdef ARDUINO_ESP8266_WEMOS_D1MINI
#define DS18B20
#define TEMPSENS_PIN 15 // DS18B20 Pin; D8 on Wemos D1 Mini
#endif

#define DBG_BAUD 115200
#define DBG_WEBLN(...) WebSerial.println(__VA_ARGS__)
#define SOFTWARE_VERSION SWVERSION
Expand Down

0 comments on commit 822d68f

Please sign in to comment.