WeatherStation/Software/WeatherSensor_D1mini/WeatherSensor_D1mini.ino

195 lines
4.7 KiB
C++

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const int CONFIG_PIN = D0;
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 <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#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_U.h>
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);
}