173 lines
3.3 KiB
C++
173 lines
3.3 KiB
C++
#define ESP8266
|
|
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
|
|
|
|
#define DEBUG
|
|
// #define SLEEP
|
|
|
|
//const char* ssid = "EG-WLAN";
|
|
//const char* password = "shae3sheuthai2oluNgiqueiyahyumeiphughi8jequeil6taethooyeik1joh5";
|
|
const char* ssid = "iPhone";
|
|
const char* password = "wollud123456";
|
|
//const char* ssid = "WLan-KI-Pro";
|
|
//const char* password = "sVAPHCmo";
|
|
|
|
const char* mqttServer = "broker.hottis.de";
|
|
const uint16 mqttPort = 8883;
|
|
const char* mqttUsername = "esp1";
|
|
const char* mqttPassword = "test1234";
|
|
//const char* mqttServer = "172.16.2.15";
|
|
//const char* mqttServer = "10.11.184.91";
|
|
|
|
|
|
const uint16_t sleepTime = 300;
|
|
|
|
|
|
WiFiClientSecure espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
|
|
#define ONE_WIRE_BUS 2
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
DallasTemperature sensors(&oneWire);
|
|
|
|
|
|
uint32_t startTime = 0;
|
|
|
|
void setup() {
|
|
startTime = millis();
|
|
#ifdef DEBUG
|
|
Serial.begin(115200);
|
|
Serial.println("Starting ...");
|
|
#endif
|
|
|
|
setup_wifi();
|
|
client.setServer(mqttServer, mqttPort);
|
|
|
|
sensors.begin();
|
|
uint8_t num = sensors.getDeviceCount();
|
|
#ifdef DEBUG
|
|
Serial.print("device count: ");
|
|
Serial.println(num);
|
|
#endif
|
|
|
|
sensors.setResolution(12);
|
|
|
|
#ifdef DEBUG
|
|
Serial.println("Started.");
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
void setup_wifi() {
|
|
|
|
delay(10);
|
|
// We start by connecting to a WiFi network
|
|
#ifdef DEBUG
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
#endif
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
#ifdef DEBUG
|
|
Serial.print(".");
|
|
#endif
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
#endif
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
#ifdef DEBUG
|
|
Serial.print("Attempting MQTT connection...");
|
|
#endif
|
|
// Attempt to connect
|
|
if (client.connect("ESP8266Client2", mqttUsername, mqttPassword)) {
|
|
#ifdef DEBUG
|
|
Serial.println("connected");
|
|
#endif
|
|
// Once connected, publish an announcement...
|
|
//client.publish("IoT/espThermometer/status", "hello world");
|
|
} else {
|
|
#ifdef DEBUG
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
#endif
|
|
// Wait 5 seconds before retrying
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void mqtt_connect() {
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}
|
|
client.loop();
|
|
}
|
|
|
|
|
|
void read_thermometer() {
|
|
sensors.requestTemperatures();
|
|
|
|
float t = sensors.getTempCByIndex(0);
|
|
#ifdef DEBUG
|
|
Serial.print(t);
|
|
Serial.println();
|
|
#endif
|
|
|
|
int16_t t1 = (int)t;
|
|
int16_t t2 = (int)((t - t1)*100);
|
|
|
|
uint32_t duration = millis() - startTime;
|
|
|
|
|
|
char topic[128];
|
|
snprintf(topic, 127, "IoT/espThermometer2/%s/measurement", WiFi.macAddress().c_str());
|
|
char payload[128];
|
|
snprintf(payload, 127, "%d.%d %ld", t1, t2, duration);
|
|
client.publish(topic, payload, true);
|
|
|
|
|
|
}
|
|
|
|
void loop() {
|
|
static uint32_t lastMillis = 0;
|
|
mqtt_connect();
|
|
|
|
uint32_t currentMillis = millis();
|
|
if (currentMillis - lastMillis > 1000) {
|
|
lastMillis = currentMillis;
|
|
|
|
read_thermometer();
|
|
|
|
#ifdef SLEEP
|
|
#ifdef DEBUG
|
|
Serial.println("Sleeping");
|
|
#endif
|
|
ESP.deepSleep(sleepTime * 1000000);
|
|
#endif
|
|
}
|
|
|
|
}
|