From 1a99342c5ac249662f8a7c3ba5e1ead089a0aa2e Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 16 Aug 2017 10:46:59 +0200 Subject: [PATCH] first working version --- sketch_aug14a.ino | 150 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 sketch_aug14a.ino diff --git a/sketch_aug14a.ino b/sketch_aug14a.ino new file mode 100644 index 0000000..aa6015c --- /dev/null +++ b/sketch_aug14a.ino @@ -0,0 +1,150 @@ +#include +#include + +#include +#include + + +// #define DEBUG + +const char* ssid = "EG-WLAN"; +const char* password = "shae3sheuthai2oluNgiqueiyahyumeiphughi8jequeil6taethooyeik1joh5"; +const char* mqttServer = "172.16.2.15"; +//const char* ssid = "WLan-KI-Pro"; +//const char* password = "sVAPHCmo"; +//const char* mqttServer = "10.11.184.91"; + +const uint16_t sleepTime = 300; + + +WiFiClient 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, 1883); + + 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 + + mqtt_connect(); + read_thermometer(); + +#ifdef DEBUG + Serial.println("Sleeping"); +#endif + ESP.deepSleep(sleepTime * 1000000); + +} + +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")) { +#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() { +}