#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#define ONE_WIRE_BUS 13
#define TEMPERATURE_PRECISION 9
#define INFLUX_HOST "116.203.155.96"
#define INFLUX_PORT 8089
#define INFLUX_SERIES "heizung"
#define INTERVALL 4000
OneWire oneWire(13);
DallasTemperature sensors(&oneWire);
WiFiUDP udp;
typedef struct {
char measurement[20];
char tags[50];
char field[20];
DeviceAddress addr;
float temp;
} Probe;
Probe probes[] = {
{INFLUX_SERIES, "punkt=heizung", "t_vor", {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xDF}, 33.3},
{INFLUX_SERIES, "punkt=heizung", "t_rueck", {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x9F, 0x12}, 30.2},
{INFLUX_SERIES, "punkt=boiler", "t_vor", {0x10, 0x10, 0x10, 0x10, 0x10, 0x9f, 0x9d, 0x99}, 60.2},
{INFLUX_SERIES, "punkt=boiler", "t_rueck", {0x10, 0x10, 0x1f, 0xf0, 0xff, 0x10, 0x10, 0x61}, 52.1},
{INFLUX_SERIES, "punkt=kueche", "t_raum", {0x10, 0x10, 0x1f, 0xf0, 0xff, 0x10, 0x10, 0x61}, 21.1},
{INFLUX_SERIES, "punkt=studio", "t_raum", {0x10, 0x10, 0x1f, 0xf0, 0xff, 0x10, 0x10, 0x61}, 20.0}
};
void setup(void)
{
Serial.begin(9600);
sensors.begin();
scanDevices();
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop(void)
{
Serial.print("\nRequesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
for (int i=0; i<sizeof probes/sizeof probes[0]; i++) {
Probe p = probes[i];
float temp = sensors.getTempC(p.addr);
if (temp == DEVICE_DISCONNECTED_C)
{
Serial.println("Error: Could not read temperature data");
temp = p.temp;
}
Serial.print("Sending data: ");
Serial.printf("%s,%s %s=%2.1f\n", p.measurement, p.tags, p.field, temp);
udp.beginPacket(INFLUX_HOST, INFLUX_PORT);
udp.printf("%s,%s %s=%2.1f", p.measurement, p.tags, p.field, temp);
udp.endPacket();
}
delay(INTERVALL);
}
void scanDevices(void) {
Serial.print("Locating devices...");
int numberOfDevices = sensors.getDeviceCount();
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
DeviceAddress tempDeviceAddress;
// Loop through each device, print out address
for (int i = 0; i < numberOfDevices; i++)
{
if (sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}