Allgemeinere Version fuer die Allgemeinheit
This commit is contained in:
parent
77c6e7ca8b
commit
074afda3f0
|
@ -1,6 +1,6 @@
|
||||||
// Wemos D1 board, connected to a battery box and a DS18B20 temperature sensor
|
// Wemos D1 board, connected to a battery box and a DS18B20 temperature sensor
|
||||||
//
|
//
|
||||||
|
|
||||||
// For temperature reading
|
// For temperature reading
|
||||||
// Libraries needed:
|
// Libraries needed:
|
||||||
// * OneWire
|
// * OneWire
|
||||||
|
@ -8,37 +8,37 @@
|
||||||
//
|
//
|
||||||
// Pinout: https://wiki.wemos.cc/products:d1:d1_mini
|
// Pinout: https://wiki.wemos.cc/products:d1:d1_mini
|
||||||
// D0 = GPIO16 --> Connect D0 to RST for Deep Sleep-Wakeup
|
// D0 = GPIO16 --> Connect D0 to RST for Deep Sleep-Wakeup
|
||||||
|
|
||||||
#include <OneWire.h>
|
#include <OneWire.h>
|
||||||
#include <DallasTemperature.h>
|
#include <DallasTemperature.h>
|
||||||
|
|
||||||
const char* ssid = "CTDO-IoT";
|
const char* ssid = "ENTER_YOUR_SSID_HERE";
|
||||||
const char* password = "";
|
const char* password = "ENTER_YOUR_WLAN_PASS_HERE";
|
||||||
|
|
||||||
#define DEVICENAME "haus/bad"
|
#define DEVICENAME "maintopic/devicename"
|
||||||
#define TOPIC DEVICENAME"/temperature"
|
#define TOPIC DEVICENAME"/temperature"
|
||||||
#define ONLINETOPIC DEVICENAME"/online"
|
#define ONLINETOPIC DEVICENAME"/online"
|
||||||
#define MQTTSERVER IPAddress(195, 160, 169, 11) // raum.ctdo.de: 195.160.169.11
|
#define MQTTSERVER IPAddress(37, 187, 106, 16) // test.mosquitto.org = 37.187.106.16
|
||||||
const int sleepTimeS = 300; // Reduce this value for debugging. Increase if you want more battery life
|
const int sleepTimeS = 300; // Reduce this value for debugging. Increase if you want more battery life
|
||||||
|
|
||||||
#define VCCPIN D7
|
#define VCCPIN D7
|
||||||
#define ONE_WIRE_BUS D6
|
#define ONE_WIRE_BUS D6
|
||||||
#define GNDPIN D5
|
#define GNDPIN D5
|
||||||
|
|
||||||
OneWire oneWire(ONE_WIRE_BUS);
|
OneWire oneWire(ONE_WIRE_BUS);
|
||||||
DallasTemperature sensors(&oneWire);
|
DallasTemperature sensors(&oneWire);
|
||||||
|
|
||||||
float tempC;
|
float tempC;
|
||||||
|
|
||||||
// For WLAN & MQTT
|
// For WLAN & MQTT
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <AsyncMqttClient.h>
|
#include <AsyncMqttClient.h>
|
||||||
AsyncMqttClient mqttClient;
|
AsyncMqttClient mqttClient;
|
||||||
uint16_t packetId1Pub;
|
uint16_t packetId1Pub;
|
||||||
bool packet1Ack = false;
|
bool packet1Ack = false;
|
||||||
|
|
||||||
bool ready = false;
|
bool ready = false;
|
||||||
|
|
||||||
char *ftoa( double f, char *a, int precision)
|
char *ftoa( double f, char *a, int precision)
|
||||||
{
|
{
|
||||||
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
|
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
|
||||||
|
@ -52,7 +52,7 @@ char *ftoa( double f, char *a, int precision)
|
||||||
itoa(desimal, a, 10);
|
itoa(desimal, a, 10);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void onMqttPublish(uint16_t packetId) {
|
void onMqttPublish(uint16_t packetId) {
|
||||||
Serial.println("** Publish acknowledged **");
|
Serial.println("** Publish acknowledged **");
|
||||||
Serial.print(" packetId: ");
|
Serial.print(" packetId: ");
|
||||||
|
@ -65,13 +65,13 @@ void onMqttPublish(uint16_t packetId) {
|
||||||
ready = true;
|
ready = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onMqttConnect(bool sessionPresent) {
|
void onMqttConnect(bool sessionPresent) {
|
||||||
char buf[7];
|
char buf[7];
|
||||||
packetId1Pub = mqttClient.publish(TOPIC, 1, true, ftoa(tempC, buf, 2));
|
packetId1Pub = mqttClient.publish(TOPIC, 1, true, ftoa(tempC, buf, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(GNDPIN, OUTPUT);
|
pinMode(GNDPIN, OUTPUT);
|
||||||
pinMode(VCCPIN, OUTPUT);
|
pinMode(VCCPIN, OUTPUT);
|
||||||
|
@ -82,15 +82,18 @@ void setup() {
|
||||||
// Start up the sensors library
|
// Start up the sensors library
|
||||||
sensors.begin();
|
sensors.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// Send the command to get temperature readings
|
// Send the command to get temperature readings
|
||||||
|
Serial.println("Requesting Temperature");
|
||||||
sensors.requestTemperatures();
|
sensors.requestTemperatures();
|
||||||
|
|
||||||
// You can have more than one DS18B20 on the same bus.
|
// You can have more than one DS18B20 on the same bus.
|
||||||
// 0 refers to the first IC on the wire
|
// 0 refers to the first IC on the wire
|
||||||
|
Serial.println("Requesting Temperature from Device 0");
|
||||||
tempC = sensors.getTempCByIndex(0);
|
tempC = sensors.getTempCByIndex(0);
|
||||||
|
|
||||||
|
Serial.println("Connecting to WIFI");
|
||||||
// Connect to WiFi
|
// Connect to WiFi
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
int timeout = 0;
|
int timeout = 0;
|
||||||
|
@ -105,10 +108,10 @@ void loop() {
|
||||||
}
|
}
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
|
|
||||||
// Print the IP address
|
// Print the IP address
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
// Publish result to MQTT
|
// Publish result to MQTT
|
||||||
mqttClient.onConnect(onMqttConnect);
|
mqttClient.onConnect(onMqttConnect);
|
||||||
mqttClient.onPublish(onMqttPublish);
|
mqttClient.onPublish(onMqttPublish);
|
||||||
|
@ -116,7 +119,7 @@ void loop() {
|
||||||
mqttClient.setKeepAlive(5).setCleanSession(false).setWill(ONLINETOPIC, 2, true, "no"); // .setCredentials("user", "pass").setClientId(DEVICENAME);
|
mqttClient.setKeepAlive(5).setCleanSession(false).setWill(ONLINETOPIC, 2, true, "no"); // .setCredentials("user", "pass").setClientId(DEVICENAME);
|
||||||
Serial.println("Connecting to MQTT...");
|
Serial.println("Connecting to MQTT...");
|
||||||
mqttClient.connect();
|
mqttClient.connect();
|
||||||
|
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
while (!ready) {
|
while (!ready) {
|
||||||
delay(250);
|
delay(250);
|
||||||
|
@ -129,11 +132,9 @@ void loop() {
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
|
|
||||||
|
|
||||||
initiateDeepSleep();
|
initiateDeepSleep();
|
||||||
}
|
}
|
||||||
|
|
||||||
void initiateDeepSleep()
|
void initiateDeepSleep()
|
||||||
{
|
{
|
||||||
ESP.deepSleep(sleepTimeS * 1000000);
|
ESP.deepSleep(sleepTimeS * 1000000);
|
||||||
|
|
Loading…
Reference in New Issue