Servus Peter,
nett das du mich nicht mehr vom Haken lässt
Der Raspi hat 2 IP weil meines WIssens mqtt / Raspberry / Linux ein Problem mit Sonderzeichen in Passwörtern hat. Deswegen funkt mein JojoBroker im Gäste Wlan. Mein "normales" WLAN hat eine Sonderzeichen Passwort.
Das ich die ESP´s flashen kann wusste ich nicht,
Sketch:
Code: Alles auswählen
/*
* Send temperature from ESP8266 with multiple DS18B20 to MQTT server.
* A simple Sketch to read the Temperature from multiple DS18B20 and publish them to a MQTT-Server using a ESP8266.
* Compiles in the Arduino IDE for the ESP8266
*
* For deep sleep support uncomment 'deep sleep' part
* For DHT22 support uncomment 'dht22' part
* OTA currently does not work.
*
*
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Streaming.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
//#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
//SSD1306 display(0x3C,D1, D2);
/* dht22
#include <DHT.h>
*/
/* deep sleep
#define SLEEP_DELAY_IN_SECONDS 30
*/
// data cable connected to D4 pin
#define ONE_WIRE_BUS D4
//wifi
const char* ssid = "HAL9000Gast";
const char* password = "WGSHAL9000";
//mqtt
const char* mqtt_server = "192.168.189.27";
//
const char* mqtt_username = "";
const char* mqtt_password = "";
const char* host = "/home/data/out";
WiFiClient espClient;
PubSubClient client(espClient);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay (500);
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void setup() {
// setup serial port
Serial.begin(115200);
// setup WiFi
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// setup OneWire bus
DS18B20.begin();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
//if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/*void displayData() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_10);
display.drawString(64, 0, "Office Temp/Humidty");
display.setFont(ArialMT_Plain_24);
display.display();
}*/
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// ds18b20
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
//Loop through all DS1820
while(oneWire.search(addr))
{
//Serial.print("ROM =");
//Topic is built from a static String plus the ID of the DS18B20
String romcode = "/home/data/ESP8266_3";
String Nummer = "Nummer";
//Serial.print( addr[i]);
for( i = 0; i < 8; i++) {
//Serial.write(' ');
//Serial.print(addr[i], HEX);
//romcode = romcode + String(addr[i], HEX);
Nummer = Nummer + String(addr[i]);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
//Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
//Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
//Serial.println("Device is not a DS18x20 family device.");
return;
}
//oneWire.depower();
oneWire.reset();
oneWire.select(addr);
oneWire.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a oneWire.depower() here, but the reset will take care of it.
present = oneWire.reset();
oneWire.select(addr);
oneWire.write(0xBE); // Read Scratchpad
//Serial.print(" Data = ");
//Serial.print(present, HEX);
//Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = oneWire.read();
//Serial.print(data[i], HEX);
//Serial.print(" ");
}
//Serial.print(" CRC=");
//Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
//convert RAW Temperature to String
String raw_temp = String(raw, DEC);
//convert RAW Temperature to celsius
double temp = raw * 0.0625;
Serial.print(temp);
//convert to string
char tempString[6];
dtostrf(temp, 2, 2, tempString);
if (client.publish((char*) romcode.c_str(), tempString)) {
client.publish (host,tempString);
// client.publish((char*) romcode.c_str(), romcode);
// display.drawString(66, 13, tempString );
// display.drawString(66, 40, display_humid + "%");
Serial.println();
Serial.println("Publish ok : ");
}
else {
Serial.println("Publish failed");
}
}
//End of the OneWire-Devices, reset Loop
Serial.println("End of Onewire Bus");
//oneWire.depower();
oneWire.reset_search();
delay(10000);
return;
// deep sleep
//Serial << "Closing MQTT connection..." << endl;
//client.disconnect();
//Serial << "Closing WiFi connection..." << endl;
//WiFi.disconnect();
delay(100);
// deep sleep
//Serial << "Entering deep sleep mode for " << SLEEP_DELAY_IN_SECONDS << " seconds..." << endl;
//ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
//ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
//delay(500);
}
Selber geschrieben trifft es nicht so ganz
Läuft ohne Batch, ESP einstecken und los gehts ( wenns einmal drauf ist ). Das fand ich bisher nicht dramatisch. Werde mal einen mit Tasmota flashen.
Der Vorteil am "Übersetzen" ist, dass ich den Sensor umhängen kann ohne den Sketch neu zu schreiben. Ich finde es einfacher die Batch Datei auf dem Raspi zu ändern ( komme nicht per WLAN auf die ESP´s )
Batch:
Code: Alles auswählen
#!/usr/bin/python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
def on_message(client, userdata, message):
msg = str(message.payload.decode("utf-8"))
print("message received: ", msg)
print("message topic: ", message.topic)
if message.topic == ("/home/data/ESP8266_2"):
msg2 = ": "+ msg
msg3 = time.strftime("%H:%M ") + msg2
print (msg3)
publish.single("/home/data/ESP/Buero", msg3)
elif message.topic == ("/home/data/ESP8266_1"):
msg2 = ": " + msg
msg3 = time.strftime("%H:%M ") +msg2
print (msg3)
publish.single("/home/data/ESP/Terasse", msg3)
elif message.topic == ("/home/data/ESP8266_3"):
msg2 = ": " + msg
msg3 = time.strftime("%H:%M ") +msg2
print (msg3)
publish.single("/home/data/ESP/Kellerabgang", msg3)
elif message.topic == ("/home/data/ESP8266_6a"):
msg2 = ": " + msg
msg3 = time.strftime("%H:%M ") + msg2
print (msg3)
publish.single("/home/data/ESP/Esszimmer", msg3)
elif message.topic == ("/home/data/ESP8266_6b"):
msg2 = ": " + msg
msg3 = time.strftime("%H:%M ") + msg2
print (msg3)
publish.single("/home/data/ESP/Esszimmer", msg3)
elif message.topic == ("/home/data/feuchte"):
msg2 =": " +msg
msg3 = time.strftime("%H:%M ") + msg2
publish.single("/home/data/ESP/feuchte" , msg3)
else:
#print("Sacklzement")
print (msg)
#publish.single("home/data/temp/",msg2)
#print(message.topic)
#publish.single("home/data/","msg2")
#print (msg2)x
def on_publish(mosq, obj, mid):
print("mid: " + str(msg2))
def on_connect(client, userdata, flags, rc):
client.subscribe('/home/data/#')
BROKER_ADDRESS = "192.168.189.27"
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(BROKER_ADDRESS)
print("Connected to MQTT Broker: " + BROKER_ADDRESS)
client.loop_forever()
Hat natürlich den Nachteil das die Batch laufen muss ...
Beim MQTTfx tut sich nicht viel:
Bildschirmfoto 2020-09-28 um 17.53.36.png
Die Jungs senden nur alle 30min
Und das senden sie an den Broker, respektive die Batch und die schickt dann mit dem Entsprechenden Zimmer Topic nochmal die umd die Zeit ergänzte Nachricht raus. Und dieses Topic ist der Channel im openHAB. Klappt aber nicht
Hoffnungsfrohe Grüße