2017-07-09 20:43:07 +00:00
|
|
|
// Wemos D1 board, connected to a battery box and a DS18B20 temperature sensor
|
|
|
|
//
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
// For temperature reading
|
|
|
|
// Libraries needed:
|
|
|
|
// * OneWire
|
|
|
|
// * DallasTemperature
|
|
|
|
//
|
|
|
|
// Pinout: https://wiki.wemos.cc/products:d1:d1_mini
|
|
|
|
// D0 = GPIO16 --> Connect D0 to RST for Deep Sleep-Wakeup
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
#include <OneWire.h>
|
|
|
|
#include <DallasTemperature.h>
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2018-09-13 19:44:51 +00:00
|
|
|
const char* ssid = "CTDO-IoT";
|
2018-09-13 19:45:26 +00:00
|
|
|
const char* password = "";
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2018-09-13 19:44:51 +00:00
|
|
|
#define DEVICENAME "haus/bad"
|
2017-07-09 20:43:07 +00:00
|
|
|
#define TOPIC DEVICENAME"/temperature"
|
2018-09-13 19:44:51 +00:00
|
|
|
#define TOPICVOLTAGE DEVICENAME"/voltage"
|
2017-07-09 20:43:07 +00:00
|
|
|
#define ONLINETOPIC DEVICENAME"/online"
|
2018-09-13 19:44:51 +00:00
|
|
|
#define MQTTSERVER IPAddress(195, 160, 169, 11) // test.mosquitto.org = 37.187.106.16
|
2017-07-09 20:43:07 +00:00
|
|
|
const int sleepTimeS = 300; // Reduce this value for debugging. Increase if you want more battery life
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
#define VCCPIN D7
|
|
|
|
#define ONE_WIRE_BUS D6
|
|
|
|
#define GNDPIN D5
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
|
|
DallasTemperature sensors(&oneWire);
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
float tempC;
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
// For WLAN & MQTT
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <AsyncMqttClient.h>
|
|
|
|
AsyncMqttClient mqttClient;
|
|
|
|
uint16_t packetId1Pub;
|
|
|
|
bool packet1Ack = false;
|
2018-09-13 19:44:51 +00:00
|
|
|
uint16_t packetId2Pub;
|
|
|
|
bool packet2Ack = false;
|
|
|
|
float voltage = 0.0;
|
|
|
|
int raw = 0;
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
bool ready = false;
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
char *ftoa( double f, char *a, int precision)
|
|
|
|
{
|
|
|
|
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
|
|
|
|
|
|
|
|
char *ret = a;
|
|
|
|
long heiltal = (long)f;
|
|
|
|
itoa(heiltal, a, 10);
|
|
|
|
while (*a != '\0') a++;
|
|
|
|
*a++ = '.';
|
|
|
|
long desimal = abs((long)((f - heiltal) * p[precision]));
|
|
|
|
itoa(desimal, a, 10);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-09-13 19:44:51 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
void onMqttPublish(uint16_t packetId) {
|
2018-09-13 19:44:51 +00:00
|
|
|
// TODO: Only when both packages were acknowledged...
|
2017-07-09 20:43:07 +00:00
|
|
|
if (packetId == packetId1Pub) {
|
|
|
|
packet1Ack = true;
|
|
|
|
}
|
2018-09-13 19:44:51 +00:00
|
|
|
if (packetId == packetId2Pub) {
|
|
|
|
packet2Ack = true;
|
|
|
|
}
|
|
|
|
if (packet1Ack && packet2Ack) {
|
2017-07-09 20:43:07 +00:00
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
}
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
void onMqttConnect(bool sessionPresent) {
|
|
|
|
char buf[7];
|
|
|
|
packetId1Pub = mqttClient.publish(TOPIC, 1, true, ftoa(tempC, buf, 2));
|
2018-09-13 19:44:51 +00:00
|
|
|
packetId2Pub = mqttClient.publish(TOPICVOLTAGE, 1, true, ftoa(voltage, buf, 2));
|
2017-07-09 20:43:07 +00:00
|
|
|
}
|
2018-09-13 19:44:51 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
void setup() {
|
|
|
|
pinMode(GNDPIN, OUTPUT);
|
|
|
|
pinMode(VCCPIN, OUTPUT);
|
|
|
|
digitalWrite(GNDPIN, LOW);
|
|
|
|
digitalWrite(VCCPIN, HIGH);
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.println("ESP-Temperature-Reader-and-MQTT-Poster-via-WiFi");
|
|
|
|
// Start up the sensors library
|
|
|
|
sensors.begin();
|
|
|
|
}
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
void loop() {
|
|
|
|
// Send the command to get temperature readings
|
2017-08-12 20:23:30 +00:00
|
|
|
Serial.println("Requesting Temperature");
|
2017-07-09 20:43:07 +00:00
|
|
|
sensors.requestTemperatures();
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
// You can have more than one DS18B20 on the same bus.
|
|
|
|
// 0 refers to the first IC on the wire
|
2017-08-12 20:23:30 +00:00
|
|
|
Serial.println("Requesting Temperature from Device 0");
|
2017-07-09 20:43:07 +00:00
|
|
|
tempC = sensors.getTempCByIndex(0);
|
2018-09-13 19:44:51 +00:00
|
|
|
raw = analogRead(A0); // 2.428V = 739
|
|
|
|
// Je Tick = 0,003285521V
|
|
|
|
voltage = raw * 0.003285521;
|
2017-08-12 20:23:30 +00:00
|
|
|
|
|
|
|
Serial.println("Connecting to WIFI");
|
2017-07-09 20:43:07 +00:00
|
|
|
// Connect to WiFi
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
int timeout = 0;
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
timeout++;
|
|
|
|
if (timeout>20) {
|
|
|
|
// WIFI isn't available after 10 seconds -> abort mission, mission's a failure
|
|
|
|
initiateDeepSleep();
|
|
|
|
}
|
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
Serial.println("");
|
|
|
|
Serial.println("WiFi connected");
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
// Print the IP address
|
|
|
|
Serial.println(WiFi.localIP());
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
// Publish result to MQTT
|
|
|
|
mqttClient.onConnect(onMqttConnect);
|
|
|
|
mqttClient.onPublish(onMqttPublish);
|
|
|
|
mqttClient.setServer(MQTTSERVER, 1883);
|
|
|
|
mqttClient.setKeepAlive(5).setCleanSession(false).setWill(ONLINETOPIC, 2, true, "no"); // .setCredentials("user", "pass").setClientId(DEVICENAME);
|
|
|
|
Serial.println("Connecting to MQTT...");
|
|
|
|
mqttClient.connect();
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
timeout = 0;
|
|
|
|
while (!ready) {
|
|
|
|
delay(250);
|
|
|
|
timeout++;
|
|
|
|
if (timeout > 40)
|
|
|
|
{
|
|
|
|
// MQTT isn't available after 10 seconds -> abort mission, mission's a failure
|
|
|
|
initiateDeepSleep();
|
|
|
|
}
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
Serial.println("");
|
|
|
|
initiateDeepSleep();
|
|
|
|
}
|
2017-08-12 20:23:30 +00:00
|
|
|
|
2017-07-09 20:43:07 +00:00
|
|
|
void initiateDeepSleep()
|
|
|
|
{
|
|
|
|
ESP.deepSleep(sleepTimeS * 1000000);
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
|