mirror of
https://github.com/dustinbrun/Wetterstation.git
synced 2025-09-08 10:27:30 +02:00
Teil 3
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
WARNING:
|
||||
If you are not receiving the Json Messages you probably have to increase the
|
||||
MQTT_MAX_PACKET_SIZE in the PubSubClient.h file to at least 1024
|
||||
(Located on Windows at C:\Users\*YOUR_USERNAME*\Documents\Arduino\libraries\pubsubclient-master\src\PubSubClient.h)
|
||||
*/
|
||||
|
||||
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_BME280.h> //https://github.com/adafruit/Adafruit_BME280_Library
|
||||
#include <BH1750.h> //https://github.com/claws/BH1750
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
|
||||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h> //https://github.com/milesburton/Arduino-Temperature-Control-Library
|
||||
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
|
||||
#include "Adafruit_VEML6070.h" // https://github.com/adafruit/Adafruit_VEML6070
|
||||
|
||||
|
||||
|
||||
const char *ssid = "----WIFI_SSID_HERE----";
|
||||
const char *password = "----WIFI_PASSWORD_HERE----";
|
||||
|
||||
//MQTT Server
|
||||
const char *mqtt_server = "----MQTT_SERVER_IP_HERE----";
|
||||
const char *mqtt_user = "----MQTT_USERNAME_HERE----";
|
||||
const char *mqtt_pass = "----MQTT_PASSWORD_HERE----";
|
||||
|
||||
|
||||
|
||||
uint8_t ds18b20_2m[8] = {0x28, 0xB4, 0x2A, 0xA7, 0x4D, 0x20, 0x01, 0x11}; //You need to adapt these addresses
|
||||
uint8_t ds18b20_0m[8] = {0x28, 0xA9, 0x2F, 0x1C, 0x0B, 0x00, 0x00, 0xA8};
|
||||
|
||||
const int hoehe = 437; //Aufstellhoehe in m
|
||||
|
||||
//Pins
|
||||
const int dataled = D8;
|
||||
const int onewire = D3; //DS18B20
|
||||
const int regensensorpin = A0;
|
||||
const int regensensorpower = D5;
|
||||
|
||||
#define TEMPERATURE_PRECISION 11 //9-12 Bit for DS18B20
|
||||
/* 9-12 Bit
|
||||
9 - 0.5 Steps
|
||||
10 - 0.25 Steps
|
||||
11 - 0.125 Steps <- Best for this Case
|
||||
12 - 0.0625 Steps
|
||||
*/
|
||||
|
||||
Adafruit_BME280 bme;
|
||||
Adafruit_VEML6070 uvMeter = Adafruit_VEML6070();
|
||||
BH1750 lightMeter;
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
OneWire oneWire(onewire);
|
||||
DallasTemperature ds18b20(&oneWire);
|
||||
StaticJsonBuffer<1024> jsonBuffer;
|
||||
|
||||
|
||||
//Variablen
|
||||
float temp = 0;
|
||||
float bodentemp = 0;
|
||||
float druck_sensorwert = 0;
|
||||
float druck = 0;
|
||||
float feuchte = 0;
|
||||
float lux = 0;
|
||||
int uv = 0;
|
||||
float regensensorwert = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("MQTT Wetterstation");
|
||||
|
||||
pinMode(dataled, OUTPUT);
|
||||
pinMode(regensensorpin, INPUT_PULLUP);
|
||||
pinMode(regensensorpower, OUTPUT);
|
||||
digitalWrite(regensensorpower, LOW);
|
||||
|
||||
ledon();
|
||||
delay(500);
|
||||
|
||||
ds18b20.begin();
|
||||
ds18b20.setResolution(ds18b20_2m, TEMPERATURE_PRECISION);
|
||||
ds18b20.setResolution(ds18b20_0m, TEMPERATURE_PRECISION);
|
||||
|
||||
// SDA, SCL
|
||||
Wire.begin(D2, D1);
|
||||
|
||||
if (! bme.begin(0x76))
|
||||
{
|
||||
Serial.println("Could not find BME280 sensor");
|
||||
//while (1) {}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("BME280 initialized.");
|
||||
}
|
||||
|
||||
|
||||
if (! lightMeter.begin())
|
||||
{
|
||||
Serial.println("Could not find BH1750 sensor");
|
||||
//while (1) {}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("BH1750 initialized.");
|
||||
}
|
||||
|
||||
|
||||
uvMeter.begin(VEML6070_4_T);
|
||||
/*
|
||||
possible integration times: -> adapt the convert_to_risk_level-function accordingly!
|
||||
VEML6070_HALF_T ~62.5ms
|
||||
VEML6070_1_T ~125ms
|
||||
VEML6070_2_T ~250ms
|
||||
VEML6070_4_T ~500ms
|
||||
|
||||
*/
|
||||
uv = uvMeter.readUV();
|
||||
if (uv == -1 || uv == 65535)
|
||||
{
|
||||
Serial.println("Could not find VEML6070 sensor");
|
||||
//while (1) {}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("VEML6070 initialized.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("WiFi connected.");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
ledblink();
|
||||
Serial.println("-----Initialisierung beendet-----");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected())
|
||||
reconnect();
|
||||
client.loop();
|
||||
|
||||
delay(100);
|
||||
}
|
24
ESP8266/Software_MQTT_Wetterstation_json/led.ino
Normal file
24
ESP8266/Software_MQTT_Wetterstation_json/led.ino
Normal file
@@ -0,0 +1,24 @@
|
||||
void ledon() {
|
||||
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
|
||||
analogWrite(dataled, fadeValue);
|
||||
delay(7);
|
||||
}
|
||||
}
|
||||
|
||||
void ledoff() {
|
||||
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
|
||||
analogWrite(dataled, fadeValue);
|
||||
delay(7);
|
||||
}
|
||||
}
|
||||
|
||||
void ledblink() {
|
||||
ledon();
|
||||
delay(200);
|
||||
ledoff();
|
||||
delay(200);
|
||||
ledon();
|
||||
delay(200);
|
||||
ledoff();
|
||||
delay(200);
|
||||
}
|
104
ESP8266/Software_MQTT_Wetterstation_json/mqtt.ino
Normal file
104
ESP8266/Software_MQTT_Wetterstation_json/mqtt.ino
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
Json format:
|
||||
{
|
||||
"temp_2m": 22.45,
|
||||
"temp_0m": 12.45,
|
||||
"druck": 1234.56,
|
||||
"feuchte": 45.4,
|
||||
"helligkeit": 12345.56,
|
||||
"regensensor": "256",
|
||||
"uvsensor" : "1234"
|
||||
}
|
||||
*/
|
||||
|
||||
void sendmqtt() {
|
||||
|
||||
JsonObject& json = jsonBuffer.createObject();
|
||||
json["temp_2m"] = temp;
|
||||
json["temp_0m"] = bodentemp;
|
||||
json["druck"] = druck;
|
||||
json["feuchte"] = feuchte;
|
||||
json["helligkeit"] = lux;
|
||||
json["regensensor"] = regensensorwert;
|
||||
json["uvsensor"] = uv;
|
||||
|
||||
char output[1025];
|
||||
json.printTo(output, sizeof(output));
|
||||
|
||||
client.publish("wetter_außen/get", output);
|
||||
client.publish("wetter_außen/update", "1");
|
||||
|
||||
jsonBuffer.clear();
|
||||
}
|
||||
|
||||
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
Serial.print("MQTT Verbindungsversuch...");
|
||||
if (client.connect("ESP8266_Wetterstation_außen", mqtt_user, mqtt_pass)) {
|
||||
Serial.println("connected");
|
||||
ledoff();
|
||||
client.subscribe("wetter_außen/debug/#");
|
||||
client.subscribe("wetter_außen/set/#");
|
||||
}
|
||||
else {
|
||||
Serial.print("fehlgeschlagen, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println("Erneuter Versuch in 5 Sekunden");
|
||||
ledon();
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.print(F("Message arrived ["));
|
||||
Serial.print(topic);
|
||||
Serial.print(F("] "));
|
||||
|
||||
char message[length + 1];
|
||||
for (unsigned int i = 0; i < length; i++) {
|
||||
message[i] = (char)payload[i];
|
||||
}
|
||||
message[length] = '\0';
|
||||
Serial.println(message);
|
||||
|
||||
String topicStr = topic;
|
||||
|
||||
if (topicStr.startsWith("wetter_außen/set"))
|
||||
{
|
||||
readsensors();
|
||||
sendmqtt();
|
||||
return;
|
||||
}
|
||||
|
||||
if (topicStr.startsWith("wetter_außen/debug/reset")) {
|
||||
Serial.println("MQTT RESET!");
|
||||
Serial.flush();
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
if (topicStr.startsWith("wetter_außen/debug/ping")) {
|
||||
Serial.println("PING");
|
||||
client.publish("wetter_außen/debug/pong", message, false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (topicStr.startsWith("wetter_außen/debug/wifi/ping")) {
|
||||
//@author Marvin Roger - https://github.com/marvinroger/homie-esp8266/blob/ad876b2cd0aaddc7bc30f1c76bfc22cd815730d9/src/Homie/Utils/Helpers.cpp#L12
|
||||
int32_t rssi;
|
||||
rssi = WiFi.RSSI();
|
||||
uint8_t quality;
|
||||
if (rssi <= -100) {
|
||||
quality = 0;
|
||||
} else if (rssi >= -50) {
|
||||
quality = 100;
|
||||
} else {
|
||||
quality = 2 * (rssi + 100);
|
||||
}
|
||||
char qualityString[8];
|
||||
dtostrf(quality, 1, 1, qualityString);
|
||||
client.publish("wetter_außen/debug/wifi/pong", qualityString);
|
||||
return;
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
void readsensors() {
|
||||
ledon();
|
||||
digitalWrite(regensensorpower, HIGH);
|
||||
delay(50);
|
||||
|
||||
temp = 0;
|
||||
bodentemp = 0;
|
||||
druck_sensorwert = 0;
|
||||
feuchte = 0;
|
||||
lux = 0;
|
||||
regensensorwert = 0;
|
||||
uv = 0;
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
//BME280
|
||||
//temp += bme.readTemperature();
|
||||
druck_sensorwert += bme.readPressure();
|
||||
feuchte += bme.readHumidity();
|
||||
|
||||
//BH1750
|
||||
lux += lightMeter.readLightLevel();
|
||||
|
||||
//DS18B20
|
||||
ds18b20.requestTemperatures();
|
||||
bodentemp += ds18b20.getTempC(ds18b20_0m);
|
||||
temp += ds18b20.getTempC(ds18b20_2m);
|
||||
|
||||
//VEML6070
|
||||
uv += uvMeter.readUV(); //measurement takes as long as the integration time is set
|
||||
|
||||
//Regensensor
|
||||
regensensorwert += analogRead(regensensorpin);
|
||||
|
||||
delay(100);
|
||||
}
|
||||
temp = temp / 3;
|
||||
druck_sensorwert = druck_sensorwert / 3;
|
||||
feuchte = feuchte / 3;
|
||||
lux = lux / 3;
|
||||
bodentemp = bodentemp / 3;
|
||||
uv = uv / 3;
|
||||
regensensorwert = regensensorwert / 3;
|
||||
digitalWrite(regensensorpower, LOW);
|
||||
|
||||
//Rechnen
|
||||
//druck = (druck_sensorwert / 100) + (hoehe / 8.5);
|
||||
druck = druck_sensorwert / 100;
|
||||
druck = bme.seaLevelForAltitude(hoehe, druck);
|
||||
|
||||
ledoff();
|
||||
}
|
Reference in New Issue
Block a user