Initial commit

This commit is contained in:
starcalc 2017-07-09 22:43:07 +02:00
commit 77c6e7ca8b
1 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,142 @@
// Wemos D1 board, connected to a battery box and a DS18B20 temperature sensor
//
// 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
#include <OneWire.h>
#include <DallasTemperature.h>
const char* ssid = "CTDO-IoT";
const char* password = "";
#define DEVICENAME "haus/bad"
#define TOPIC DEVICENAME"/temperature"
#define ONLINETOPIC DEVICENAME"/online"
#define MQTTSERVER IPAddress(195, 160, 169, 11) // raum.ctdo.de: 195.160.169.11
const int sleepTimeS = 300; // Reduce this value for debugging. Increase if you want more battery life
#define VCCPIN D7
#define ONE_WIRE_BUS D6
#define GNDPIN D5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float tempC;
// For WLAN & MQTT
#include <ESP8266WiFi.h>
#include <AsyncMqttClient.h>
AsyncMqttClient mqttClient;
uint16_t packetId1Pub;
bool packet1Ack = false;
bool ready = false;
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;
}
void onMqttPublish(uint16_t packetId) {
Serial.println("** Publish acknowledged **");
Serial.print(" packetId: ");
Serial.println(packetId);
// TODO: Only when both packages were acknowledged...
if (packetId == packetId1Pub) {
packet1Ack = true;
}
if (packet1Ack) {
ready = true;
}
}
void onMqttConnect(bool sessionPresent) {
char buf[7];
packetId1Pub = mqttClient.publish(TOPIC, 1, true, ftoa(tempC, buf, 2));
}
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();
}
void loop() {
// Send the command to get temperature readings
sensors.requestTemperatures();
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
tempC = sensors.getTempCByIndex(0);
// 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");
// Print the IP address
Serial.println(WiFi.localIP());
// 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();
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();
}
void initiateDeepSleep()
{
ESP.deepSleep(sleepTimeS * 1000000);
delay(100);
}