commit 1dead2b8f9d7a96be46a766bd7f19c217a2ea2b5 Author: Arne Schroeder Date: Wed Sep 4 11:03:56 2019 +0200 add initial sensor software diff --git a/Software/WeatherSensor_D1mini/README.md b/Software/WeatherSensor_D1mini/README.md new file mode 100644 index 0000000..a21c157 --- /dev/null +++ b/Software/WeatherSensor_D1mini/README.md @@ -0,0 +1,20 @@ +# WeatherSensor for Wemos D1mini + +## Required Arduino Components + +### Libraries + + * WifiManager - ESP8266 WiFi Connection Manager `v0.14.0` + * PubSubClient - Client library for MQTT messaging `v2.7.0` + * Adafruit Unified Sensor Driver `v1.0.3` + * Adafruit BME280 Library `v1.0.9` + +### Board Support Package + + * esp8266 `v2.5.2` + + +# TODO + * Implement initial configuration via own wifi network with web configuration page + * measure battery voltage + diff --git a/Software/WeatherSensor_D1mini/WeatherSensor_D1mini.ino b/Software/WeatherSensor_D1mini/WeatherSensor_D1mini.ino new file mode 100644 index 0000000..7495eb5 --- /dev/null +++ b/Software/WeatherSensor_D1mini/WeatherSensor_D1mini.ino @@ -0,0 +1,217 @@ +/* + Basic ESP8266 MQTT example + + This sketch demonstrates the capabilities of the pubsub library in combination + with the ESP8266 board/library. + + It connects to an MQTT server then: + - publishes "hello world" to the topic "outTopic" every two seconds + - subscribes to the topic "inTopic", printing out any messages + it receives. NB - it assumes the received payloads are strings not binary + - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led, + else switch it off + + It will reconnect to the server if the connection is lost using a blocking + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to + achieve the same result without blocking the main loop. + + To install the ESP8266 board, (using Arduino 1.6.4+): + - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": + http://arduino.esp8266.com/stable/package_esp8266com_index.json + - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" + - Select your ESP8266 in "Tools -> Board" + +*/ + +#include +#include + +// Update these with values suitable for your network. + +const char *ssid = "AcS"; +const char *password = "67993776724373201548"; +const char* mqtt_server = "192.168.1.98"; +const String mqtt_client = "home/S000"; +const String mqtt_topic_t = mqtt_client + String("/temperatur"); +const String mqtt_topic_h = mqtt_client + String("/humidity"); +const String mqtt_topic_p = mqtt_client + String("/pressure"); +const String mqtt_topic_l = mqtt_client + String("/lux"); +const String mqtt_topic_led_state = mqtt_client + String("/led_state"); +const String mqtt_topic_led = mqtt_client + String("/led"); + +WiFiClient espClient; +PubSubClient client(espClient); +long lastMsg = 0; +char msg[50]; +int value = 0; +unsigned long t0; + + +#include +#include +#include +#include + +#define BME_SCK 13 +#define BME_MISO 12p +#define BME_MOSI 11 +#define BME_CS 10 + +#define SEALEVELPRESSURE_HPA (1013.25) + +Adafruit_BME280 bme; // I2C + +#include +Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); + +unsigned long delayTime; + + +void setup() { + //pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output + //digitalWrite(BUILTIN_LED, HIGH); + Serial.begin(115200); + setup_wifi(); + setup_sensor(); + client.setServer(mqtt_server, 1883); + //client.setCallback(callback); + + if (!client.connected()) { + reconnect(); + } + delay(500); + client.loop(); + delay(500); + process_sensor(); + delay(500); + client.loop(); + delay(500); + ESP.deepSleep(300e6); +} + + + +void setup_wifi() { + delay(10); + // We start by connecting to a WiFi network + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + + + +void setup_sensor() { + Serial.print(F("BME280 setup... ")); + + bool status; + // default settings + // (you can also pass in a Wire library object like &Wire2) + status = bme.begin(); + if (!status) { + while(1) { + Serial.println("Could not find a valid BME280 sensor, check wiring!"); + //while (1); + delay(2000); + } + } + + tsl.enableAutoRange(true); + tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); + delayTime = 1000; + Serial.println("done."); +} + + + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '1') { + digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level + // but actually the LED is on; this is because + // it is acive low on the ESP-01) + } else { + digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH + } + +} + + + +void reconnect() { + // Loop until we're reconnected + const String start_topic = mqtt_client + String("/client"); + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect(mqtt_client.c_str())) { + Serial.println("connected"); + // Once connected, publish an announcement... + client.publish(start_topic.c_str(), "hello world"); + // ... and resubscribe + client.subscribe(mqtt_topic_led.c_str()); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + + + +void process_sensor() { + unsigned long t1 = millis(); + Serial.print("ReadSensor: "); + Serial.println(t1-t0); + t0 = t1; + auto temperatur = bme.readTemperature(); + auto humidity = bme.readHumidity(); + auto pressure = bme.readPressure() / 100.0F; + + int led = !digitalRead(BUILTIN_LED); + + sensors_event_t event; + tsl.getEvent(&event); + float light = 0.0; + if (event.light) { + light = event.light; + } + + snprintf(msg, 75, "T: %0.2f *C, P: %0.2f hPa, H: %0.2f, L: %0.0f LED: %d", + temperatur, pressure, humidity, light, led); + client.publish(mqtt_topic_t.c_str(), String(temperatur).c_str()); + client.publish(mqtt_topic_h.c_str(), String(humidity).c_str()); + client.publish(mqtt_topic_p.c_str(), String(pressure).c_str()); + client.publish(mqtt_topic_l.c_str(), String(light).c_str()); + client.publish(mqtt_topic_led_state.c_str(), String(led).c_str()); + Serial.println(msg); +} + + + +void loop() { + delay(500); +}