Compare commits
3 Commits
099740938f
...
9222bf14a2
Author | SHA1 | Date | |
---|---|---|---|
|
9222bf14a2 | ||
|
1b196a09a4 | ||
|
3a5ebc06c5 |
63
include/mcp9808.h
Normal file
63
include/mcp9808.h
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
#include <Arduino.h>
|
||||||
|
/* Daten:
|
||||||
|
* Temperaturbereich -40 bis 125°C
|
||||||
|
* Genauigkeit: ± 0,25°C
|
||||||
|
* Auflösung: 0,0625°C
|
||||||
|
*/
|
||||||
|
#include <Wire.h>
|
||||||
|
#include "Adafruit_MCP9808.h"
|
||||||
|
float valTemp;
|
||||||
|
|
||||||
|
// Create MCP9808 temperature sensor object
|
||||||
|
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
|
||||||
|
|
||||||
|
char Temperature[15] = {0};
|
||||||
|
|
||||||
|
void init_MCP9808(){
|
||||||
|
// Make sure the sensor is found, you can also pass in a different i2c
|
||||||
|
// address with tempsensor.begin(0x19) for example, also can be left in blank for default address use
|
||||||
|
// Also there is a table with all addres possible for this sensor, you can connect multiple sensors
|
||||||
|
// to the same i2c bus, just configure each sensor with a different address and define multiple objects for that
|
||||||
|
// A2 A1 A0 address
|
||||||
|
// 0 0 0 0x18 this is the default address
|
||||||
|
// 0 0 1 0x19
|
||||||
|
// 0 1 0 0x1A
|
||||||
|
// 0 1 1 0x1B
|
||||||
|
// 1 0 0 0x1C
|
||||||
|
// 1 0 1 0x1D
|
||||||
|
// 1 1 0 0x1E
|
||||||
|
// 1 1 1 0x1F
|
||||||
|
if (!tempsensor.begin(0x18)) {
|
||||||
|
Serial.println("Couldn't find MCP9808! Check your connections and verify the address is correct.");
|
||||||
|
datenSave(-20);
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
Serial.println("Found MCP9808!");
|
||||||
|
tempsensor.setResolution(3); // sets the resolution mode of reading, the modes are defined in the table bellow:
|
||||||
|
// Mode Resolution SampleTime
|
||||||
|
// 0 0.5°C 30 ms
|
||||||
|
// 1 0.25°C 65 ms
|
||||||
|
// 2 0.125°C 130 ms
|
||||||
|
// 3 0.0625°C 250 ms
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float getTemperature_MCP9808(){
|
||||||
|
// Wake up MSP9808 - power consumption ~200 mikro Ampere
|
||||||
|
tempsensor.wake();
|
||||||
|
float temperature = tempsensor.readTempC();
|
||||||
|
tempsensor.shutdown();
|
||||||
|
dtostrf(temperature,7,1,Temperature);
|
||||||
|
Serial.print("Temperatur (MCP9808):\t");
|
||||||
|
Serial.print(Temperature);
|
||||||
|
Serial.println(" °C");
|
||||||
|
tempsensor.shutdown_wake(1); // shutdown MSP9808 - power consumption ~0.1 mikro Ampere, stops temperature sampling
|
||||||
|
return temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
void M2M_Temperatur_MCP9808(String deviceId = "4711") {
|
||||||
|
char topic[100];
|
||||||
|
dtostrf(valTemp,7,1,Temperature);
|
||||||
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/temperature_MCP9808");
|
||||||
|
client.publish(topic, Temperature, true);
|
||||||
|
}
|
150
include/messADS1115.h
Normal file
150
include/messADS1115.h
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
/***************************************************************************
|
||||||
|
* Example sketch for the ADS1115_WE library
|
||||||
|
*
|
||||||
|
* This sketch shows how to use the ADS1115 in single shot mode.
|
||||||
|
*
|
||||||
|
* Further information can be found on:
|
||||||
|
* https://wolles-elektronikkiste.de/ads1115 (German)
|
||||||
|
* https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English)
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include<ADS1115_WE.h>
|
||||||
|
#include<Wire.h>
|
||||||
|
#define I2C_ADDRESS 0x48
|
||||||
|
|
||||||
|
/* There are several ways to create your ADS1115_WE object:
|
||||||
|
* ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48
|
||||||
|
* ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS
|
||||||
|
* ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48
|
||||||
|
* ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together
|
||||||
|
*/
|
||||||
|
ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS);
|
||||||
|
|
||||||
|
struct {
|
||||||
|
char Akku[15] = {0};
|
||||||
|
char Solar[15] = {0};
|
||||||
|
} ADSData;
|
||||||
|
|
||||||
|
void initADS() {
|
||||||
|
Wire.begin();
|
||||||
|
if(!adc.init()){
|
||||||
|
Serial.println("ADS1115 not connected!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set the voltage range of the ADC to adjust the gain
|
||||||
|
* Please note that you must not apply more than VDD + 0.3V to the input pins!
|
||||||
|
*
|
||||||
|
* ADS1115_RANGE_6144 -> +/- 6144 mV
|
||||||
|
* ADS1115_RANGE_4096 -> +/- 4096 mV
|
||||||
|
* ADS1115_RANGE_2048 -> +/- 2048 mV (default)
|
||||||
|
* ADS1115_RANGE_1024 -> +/- 1024 mV
|
||||||
|
* ADS1115_RANGE_0512 -> +/- 512 mV
|
||||||
|
* ADS1115_RANGE_0256 -> +/- 256 mV
|
||||||
|
*/
|
||||||
|
adc.setVoltageRange_mV(ADS1115_RANGE_4096); //comment line/change parameter to change range
|
||||||
|
|
||||||
|
/* Set the inputs to be compared
|
||||||
|
*
|
||||||
|
* ADS1115_COMP_0_1 -> compares 0 with 1 (default)
|
||||||
|
* ADS1115_COMP_0_3 -> compares 0 with 3
|
||||||
|
* ADS1115_COMP_1_3 -> compares 1 with 3
|
||||||
|
* ADS1115_COMP_2_3 -> compares 2 with 3
|
||||||
|
* ADS1115_COMP_0_GND -> compares 0 with GND
|
||||||
|
* ADS1115_COMP_1_GND -> compares 1 with GND
|
||||||
|
* ADS1115_COMP_2_GND -> compares 2 with GND
|
||||||
|
* ADS1115_COMP_3_GND -> compares 3 with GND
|
||||||
|
*/
|
||||||
|
//adc.setCompareChannels(ADS1115_COMP_0_GND); //uncomment if you want to change the default
|
||||||
|
|
||||||
|
/* Set number of conversions after which the alert pin will assert
|
||||||
|
* - or you can disable the alert
|
||||||
|
*
|
||||||
|
* ADS1115_ASSERT_AFTER_1 -> after 1 conversion
|
||||||
|
* ADS1115_ASSERT_AFTER_2 -> after 2 conversions
|
||||||
|
* ADS1115_ASSERT_AFTER_4 -> after 4 conversions
|
||||||
|
* ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default)
|
||||||
|
*/
|
||||||
|
//adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); //uncomment if you want to change the default
|
||||||
|
|
||||||
|
/* Set the conversion rate in SPS (samples per second)
|
||||||
|
* Options should be self-explaining:
|
||||||
|
*
|
||||||
|
* ADS1115_8_SPS
|
||||||
|
* ADS1115_16_SPS
|
||||||
|
* ADS1115_32_SPS
|
||||||
|
* ADS1115_64_SPS
|
||||||
|
* ADS1115_128_SPS (default)
|
||||||
|
* ADS1115_250_SPS
|
||||||
|
* ADS1115_475_SPS
|
||||||
|
* ADS1115_860_SPS
|
||||||
|
*/
|
||||||
|
//adc.setConvRate(ADS1115_128_SPS); //uncomment if you want to change the default
|
||||||
|
|
||||||
|
/* Set continuous or single shot mode:
|
||||||
|
*
|
||||||
|
* ADS1115_CONTINUOUS -> continuous mode
|
||||||
|
* ADS1115_SINGLE -> single shot mode (default)
|
||||||
|
*/
|
||||||
|
//adc.setMeasureMode(ADS1115_CONTINUOUS); //uncomment if you want to change the default
|
||||||
|
|
||||||
|
/* Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will
|
||||||
|
* assert when measured values are beyond the maximum limit or outside the window
|
||||||
|
* Upper limit first: setAlertLimit_V(MODE, maximum, minimum)
|
||||||
|
* In max limit mode the minimum value is the limit where the alert pin assertion will be
|
||||||
|
* be cleared (if not latched)
|
||||||
|
*
|
||||||
|
* ADS1115_MAX_LIMIT
|
||||||
|
* ADS1115_WINDOW
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
//adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5); //uncomment if you want to change the default
|
||||||
|
|
||||||
|
/* Enable or disable latch. If latch is enabled the alert pin will assert until the
|
||||||
|
* conversion register is read (getResult functions). If disabled the alert pin assertion
|
||||||
|
* will be cleared with next value within limits.
|
||||||
|
*
|
||||||
|
* ADS1115_LATCH_DISABLED (default)
|
||||||
|
* ADS1115_LATCH_ENABLED
|
||||||
|
*/
|
||||||
|
//adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default
|
||||||
|
|
||||||
|
/* Sets the alert pin polarity if active:
|
||||||
|
*
|
||||||
|
* ADS1115_ACT_LOW -> active low (default)
|
||||||
|
* ADS1115_ACT_HIGH -> active high
|
||||||
|
*/
|
||||||
|
//adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default
|
||||||
|
|
||||||
|
/* With this function the alert pin will assert, when a conversion is ready.
|
||||||
|
* In order to deactivate, use the setAlertLimit_V function
|
||||||
|
*/
|
||||||
|
//adc.setAlertPinToConversionReady(); //uncomment if you want to change the default
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
float readChannel(ADS1115_MUX channel) {
|
||||||
|
float voltage = 0.0;
|
||||||
|
adc.setCompareChannels(channel);
|
||||||
|
adc.startSingleMeasurement();
|
||||||
|
while(adc.isBusy()){}
|
||||||
|
voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt
|
||||||
|
return voltage;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MessungADS() {
|
||||||
|
float voltage = 0.0;
|
||||||
|
|
||||||
|
voltage = readChannel(ADS1115_COMP_0_GND);
|
||||||
|
dtostrf(voltage,8,2,ADSData.Akku);
|
||||||
|
Serial.print("Akku:\t\t\t");
|
||||||
|
Serial.print(ADSData.Akku);
|
||||||
|
Serial.println(" V");
|
||||||
|
|
||||||
|
voltage = readChannel(ADS1115_COMP_1_GND);
|
||||||
|
dtostrf(voltage,8,2,ADSData.Solar);
|
||||||
|
Serial.print("Solar:\t\t\t");
|
||||||
|
Serial.print(ADSData.Solar);
|
||||||
|
Serial.println(" V");
|
||||||
|
|
||||||
|
}
|
79
include/mess_BMP280.h
Normal file
79
include/mess_BMP280.h
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
#include <SPI.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_Sensor.h>
|
||||||
|
#include <Adafruit_BMP280.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define SEALEVELPRESSURE_HPA (1002.7) // 1013.25
|
||||||
|
// Richen 219 m über NN
|
||||||
|
// Eppingem 195 m über NN
|
||||||
|
|
||||||
|
Adafruit_BMP280 bmp; // I2C
|
||||||
|
float K_BMP280 = -0.0;
|
||||||
|
|
||||||
|
bool F_BMP280;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
char temperature[15] = {0};
|
||||||
|
char pressure[15] = {0};
|
||||||
|
char approx_altitud[15] = {0};
|
||||||
|
char humity[15] = {0};
|
||||||
|
} BMP280Data;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Init_BMP280(){
|
||||||
|
bool status = bmp.begin(0x76);
|
||||||
|
F_BMP280 = true;
|
||||||
|
if (!status) {
|
||||||
|
Serial.println("Could not find a valid BME280 sensor, check wiring!");
|
||||||
|
F_BMP280 = false;
|
||||||
|
} else{
|
||||||
|
/* Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
|
||||||
|
delay(5000); */
|
||||||
|
/* Default settings from datasheet. */
|
||||||
|
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
|
||||||
|
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
|
||||||
|
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
|
||||||
|
Adafruit_BMP280::FILTER_X16, /* Filtering. */
|
||||||
|
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
|
||||||
|
|
||||||
|
//bmp_temp->printSensorDetails();
|
||||||
|
Serial.println("BMP280 gefunden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_BMP_280() {
|
||||||
|
|
||||||
|
|
||||||
|
Serial.print("Temperature (BMP280):\t");
|
||||||
|
float t = bmp.readTemperature();
|
||||||
|
t = t + K_BMP280;
|
||||||
|
dtostrf(t,7,1,BMP280Data.temperature);
|
||||||
|
Serial.print(BMP280Data.temperature);
|
||||||
|
Serial.println(" °C");
|
||||||
|
|
||||||
|
Serial.print("Pressure:\t\t");
|
||||||
|
float p = bmp.readPressure() / 100.0F;
|
||||||
|
//p = p + 22;
|
||||||
|
dtostrf(p,5,0,BMP280Data.pressure);
|
||||||
|
Serial.print(BMP280Data.pressure);
|
||||||
|
Serial.println(" hPa");
|
||||||
|
|
||||||
|
Serial.print("Approx. Altitude:\t");
|
||||||
|
float a = bmp.readAltitude(SEALEVELPRESSURE_HPA);
|
||||||
|
dtostrf(a,7,1,BMP280Data.approx_altitud);
|
||||||
|
Serial.print(BMP280Data.approx_altitud);
|
||||||
|
Serial.println(" m über NN");
|
||||||
|
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
void M2M_BMP280(String deviceId = "4711") {
|
||||||
|
char topic[100];
|
||||||
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/temperature_BMP_280" );
|
||||||
|
client.publish(topic, BMP280Data.temperature, true);
|
||||||
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/pressure" );
|
||||||
|
client.publish(topic, BMP280Data.pressure, true);
|
||||||
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/approx_altitude" );
|
||||||
|
client.publish(topic, BMP280Data.approx_altitud);
|
||||||
|
}
|
22
include/mess_Ub.h
Normal file
22
include/mess_Ub.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
ADC_MODE(ADC_VCC);
|
||||||
|
|
||||||
|
const float MinimalSpannung = 2.85;
|
||||||
|
float korectur = 9.2800899887514060742407199100112e-4;
|
||||||
|
char floatString[15] = {0};
|
||||||
|
float AKKU;
|
||||||
|
|
||||||
|
float getBattery()
|
||||||
|
{
|
||||||
|
#if (NOBATT == 1)
|
||||||
|
Serial.print("Batterie:\t\t 3.05 V\n");
|
||||||
|
return 3.05;
|
||||||
|
#endif
|
||||||
|
int Vcc = ESP.getVcc();
|
||||||
|
float VCC = Vcc * korectur ;
|
||||||
|
Serial.printf("Rohdaten: %d, ", Vcc);
|
||||||
|
dtostrf(VCC,7,2,floatString);
|
||||||
|
Serial.printf("Vcc: %s V\n", floatString);
|
||||||
|
return VCC;
|
||||||
|
}
|
24
include/mess_Ub_old.h
Normal file
24
include/mess_Ub_old.h
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
const float MinimalSpannung = 2.85;
|
||||||
|
float korectur = 1.02910;
|
||||||
|
char floatString[15] = {0};
|
||||||
|
float AKKU;
|
||||||
|
|
||||||
|
float getBattery(float kor = 1.000)
|
||||||
|
{
|
||||||
|
#if (NOBATT == 1)
|
||||||
|
Serial.print("Batterie:\t\t 3.05 V\n");
|
||||||
|
return 3.05;
|
||||||
|
#endif
|
||||||
|
float valA0 = analogRead(A0);
|
||||||
|
valA0 = valA0 * 3.7; // (R1 + r1 + r2) / r2
|
||||||
|
// r1 und r2 Spannungsteiler
|
||||||
|
// r1 = 270k, r2 = 100k
|
||||||
|
// Spannungsbereich = 5.2 Volt
|
||||||
|
valA0= valA0 / 1024;
|
||||||
|
valA0 = valA0 * kor;
|
||||||
|
dtostrf(valA0,7,2,floatString);
|
||||||
|
Serial.printf("Batterie:\t\t %s V\n", floatString);
|
||||||
|
return valA0;
|
||||||
|
}
|
49
include/mess_htu21.h
Normal file
49
include/mess_htu21.h
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_Sensor.h>
|
||||||
|
#include <Adafruit_I2CDevice.h>
|
||||||
|
#include <Adafruit_HTU21DF.h>
|
||||||
|
|
||||||
|
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
|
||||||
|
|
||||||
|
bool F_HTU_21D;
|
||||||
|
float K_HTU= -0.00;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
char temperature[15] = {0};
|
||||||
|
char humity[15] = {0};
|
||||||
|
} htuData;
|
||||||
|
|
||||||
|
void init_HTU21(){
|
||||||
|
F_HTU_21D = false;
|
||||||
|
if (!htu.begin()) {
|
||||||
|
Serial.println("Couldn't find sensor HUT21D!");
|
||||||
|
|
||||||
|
}
|
||||||
|
F_HTU_21D = true;
|
||||||
|
Serial.println("HUT21D gefunden");
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_HTU21D() {
|
||||||
|
float t = htu.readTemperature();
|
||||||
|
t = t + K_HTU;
|
||||||
|
dtostrf(t,7,1,htuData.temperature);
|
||||||
|
float h = htu.readHumidity();
|
||||||
|
dtostrf(h,7,1,htuData.humity);
|
||||||
|
Serial.print("Temperature (HTU21D):\t");
|
||||||
|
Serial.print(htuData.temperature);
|
||||||
|
Serial.println(" °C");
|
||||||
|
Serial.print("Luftfeuchtigkeit:\t");
|
||||||
|
Serial.print(htuData.humity);
|
||||||
|
Serial.println(" %");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void M2M_HTU21D(String deviceId = "4711") {
|
||||||
|
char topic[100];
|
||||||
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/temperature_Htu_21" );
|
||||||
|
client.publish(topic, htuData.temperature, true);
|
||||||
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/humity" );
|
||||||
|
client.publish(topic, htuData.humity, true);
|
||||||
|
/* Serial.printf("HTU21:\t\t %s °C\n", htuData.temperature);
|
||||||
|
Serial.printf("HTU21:\t\t %s %%\n", htuData.humity); */
|
||||||
|
}
|
|
@ -8,11 +8,58 @@
|
||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:esp07]
|
[env]
|
||||||
platform = espressif8266
|
platform = espressif8266
|
||||||
board = esp07
|
board = d1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
upload_port = /dev/ttyUSB0
|
|
||||||
monitor_port = /dev/ttyUSB0
|
monitor_port = /dev/ttyUSB0
|
||||||
monitor_speed = 115200
|
monitor_speed = 74800
|
||||||
|
monitor_filters = time
|
||||||
|
upload_port = /dev/ttyUSB0
|
||||||
|
lib_deps =
|
||||||
|
knolleary/PubSubClient @ 2.8
|
||||||
|
adafruit/Adafruit HTU21DF Library @ 1.0.5
|
||||||
|
adafruit/Adafruit BMP280 Library @ 2.6.6
|
||||||
|
wollewald/ADS1115_WE @ 1.4.3
|
||||||
|
adafruit/Adafruit MCP9808 Library @ 2.0.0
|
||||||
|
|
||||||
|
build_flags =
|
||||||
|
-DTEMPTEST33=1
|
||||||
|
-DNOBATT=0
|
||||||
|
-DMaxErrCount=30
|
||||||
|
-DMQTT=0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[env:debug] ; Entwicklungssystem
|
||||||
|
build_flags = ${env.build_flags}
|
||||||
|
-DDEBUG=0
|
||||||
|
-DNOADS=0
|
||||||
|
-DNAME=\"WETTERSTATIONBORIS\"
|
||||||
|
-DSTASSID=\"St.-Peters-Gasse\"
|
||||||
|
-DSTAPSK=\"1952994784599319\"
|
||||||
|
-DGATEWAY=\"192.168.127.1\"
|
||||||
|
-DDNS=\"192.168.127.1\"
|
||||||
|
-DKMYIP=\"192.168.127.7\"
|
||||||
|
-Dmqtt_server=\"hjkmqtt.dedyn.io\"
|
||||||
|
-Dmqtt_port=8883
|
||||||
|
-DTERROR=5
|
||||||
|
-DTLOWBATT=60
|
||||||
|
-DTINTERVAL=1
|
||||||
|
|
||||||
|
[env:release] ; Produktivsystem:
|
||||||
|
build_flags = ${env.build_flags}
|
||||||
|
-DDEBUG=0
|
||||||
|
-DEMAIL=\"koerner.c.m@gmail.com\"
|
||||||
|
-DNAME=\"WETTERSTATIONMARCEL\"
|
||||||
|
-DSTASSID=\"EasyBox-838169\"
|
||||||
|
-DSTAPSK=\"RcZmua6Xv4R4V5Kf\"
|
||||||
|
-DGATEWAY=\"192.168.2.1\"
|
||||||
|
-DDNS=\"192.168.2.1\"
|
||||||
|
-DKMYIP=\"192.168.2.81\"
|
||||||
|
-Dmqtt_server=\"hjkmqtt.dedyn.io\"
|
||||||
|
-Dmqtt_port=8883
|
||||||
|
-DTERROR=20
|
||||||
|
-DTLOWBATT=60
|
||||||
|
-DTINTERVAL=10
|
||||||
|
|
389
src/main.cpp
389
src/main.cpp
|
@ -1,9 +1,394 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <FS.h> //this needs to be first, or it all crashes and burns...
|
||||||
|
#include <LittleFS.h>
|
||||||
|
#include <string>
|
||||||
|
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
|
#define BUILTIN_LED D15
|
||||||
|
#define TRIGGER_PIN D7
|
||||||
|
#define START_STOP_PIN D6
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void reconnect();
|
||||||
|
void setup_wifi();
|
||||||
|
void datenSave(int wert);
|
||||||
|
int readDaten();
|
||||||
|
int saveKorektur(float wert);
|
||||||
|
float readKorectur();
|
||||||
|
void verifyFingerprint();
|
||||||
|
void callback(char* topic1, byte* payload, unsigned int length);
|
||||||
|
void pulse_pin(uint8_t pin);
|
||||||
|
|
||||||
|
WiFiClientSecure espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
|
#include<mess_htu21.h>
|
||||||
|
|
||||||
|
#include <mess_BMP280.h>
|
||||||
|
|
||||||
|
#include <mess_Ub_old.h>
|
||||||
|
|
||||||
|
#include <messADS1115.h>
|
||||||
|
|
||||||
|
#include <mcp9808.h>
|
||||||
|
|
||||||
|
|
||||||
|
const char* ssid = STASSID;
|
||||||
|
const char* password = STAPSK;
|
||||||
|
String hostname = NAME;
|
||||||
|
|
||||||
|
const char *MyIP = KMYIP;
|
||||||
|
IPAddress ip;
|
||||||
|
IPAddress gateway;
|
||||||
|
IPAddress subnet(255, 255, 255, 0);
|
||||||
|
IPAddress dns;
|
||||||
|
IPAddress secondaryDNS(8, 8, 8, 8);
|
||||||
|
|
||||||
|
|
||||||
|
const char* mqtt_fprint = "a3:44:1d:aa:6e:e5:c7:55:02:20:98:ea:9b:df:1a:42:a2:f3:e3:0d";
|
||||||
|
const char* mqtt_user = "mqtt";
|
||||||
|
const char* mqtt_pass = "fische";
|
||||||
|
|
||||||
|
const unsigned long interval = TINTERVAL * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode
|
||||||
|
const unsigned long intervalLowBatt = TLOWBATT * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode, Akku entladen
|
||||||
|
const unsigned long stoerung = TERROR * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode
|
||||||
|
|
||||||
|
unsigned long previousMillis = 0;
|
||||||
|
|
||||||
|
long deviceId;
|
||||||
|
char sID[16];
|
||||||
|
char clientName[30];
|
||||||
|
unsigned long startTime;
|
||||||
|
unsigned long endTime;
|
||||||
|
char topic[100];
|
||||||
|
char topic_1[50];
|
||||||
|
char topicWert[20];
|
||||||
|
char msg[20];
|
||||||
|
int SystemStatus;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
pinMode(TRIGGER_PIN, OUTPUT);
|
||||||
|
pinMode(START_STOP_PIN, OUTPUT);
|
||||||
|
digitalWrite(TRIGGER_PIN, HIGH);
|
||||||
|
digitalWrite(START_STOP_PIN, LOW);
|
||||||
|
pulse_pin(TRIGGER_PIN); // ==> 1
|
||||||
|
Serial.begin(74880);
|
||||||
|
while ( !Serial ) delay(100); // wait for native usb
|
||||||
|
Serial.println(F("BMP280 Sensor event test"));
|
||||||
|
Serial.println("HTU21D-F test");
|
||||||
|
startTime = millis();
|
||||||
|
WiFi.mode( WIFI_OFF );
|
||||||
|
WiFi.forceSleepBegin();
|
||||||
|
pulse_pin(TRIGGER_PIN); // ==> 1
|
||||||
|
Serial.println();
|
||||||
|
//Serial.println("Testpunkt 1");
|
||||||
|
/* if (!LittleFS.begin()) {
|
||||||
|
Serial.println("LittleFS mount failed");
|
||||||
|
delay(5000);
|
||||||
|
return;
|
||||||
|
} */
|
||||||
|
//Serial.println("Testpunkt 2");
|
||||||
|
/* Dir dir = LittleFS.openDir("/data");
|
||||||
|
while (dir.next()) {
|
||||||
|
Serial.print(dir.fileName());
|
||||||
|
if(dir.fileSize()) {
|
||||||
|
File f = dir.openFile("r");
|
||||||
|
Serial.println(f.size());
|
||||||
|
}
|
||||||
|
} */
|
||||||
|
//Serial.println("Testpunkt 3");
|
||||||
|
Serial.println();
|
||||||
|
Serial.println();
|
||||||
|
Serial.println();
|
||||||
|
//SystemStatus = readDaten();
|
||||||
|
//korectur = readKorectur();
|
||||||
|
//Serial.print("Korektur: "); Serial.println(korectur,6);
|
||||||
|
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
||||||
|
//digitalWrite(BUILTIN_LED, LOW);
|
||||||
|
//digitalWrite(BUILTIN_LED, HIGH);
|
||||||
|
//Serial.print("STATUS (Systemmeldung): "); Serial.println(SystemStatus);
|
||||||
|
#if (MQTT == 0)
|
||||||
|
init_HTU21();
|
||||||
|
Init_BMP280();
|
||||||
|
//initADS();
|
||||||
|
//init_MCP9808();
|
||||||
|
#endif
|
||||||
|
AKKU = getBattery(); // ca. 170 ms
|
||||||
|
// ca. 280 ms
|
||||||
|
if (F_HTU_21D == true){
|
||||||
|
read_HTU21D();
|
||||||
|
}
|
||||||
|
read_BMP_280();
|
||||||
|
//MessungADS();
|
||||||
|
setup_wifi(); // ca. 4,5 s
|
||||||
|
// ca. 12ms
|
||||||
|
// ---------------------------------
|
||||||
|
// Status ändern !!! 0
|
||||||
|
//datenSave(0);
|
||||||
|
// ---------------------------------
|
||||||
|
//digitalWrite(BUILTIN_LED, HIGH);
|
||||||
|
deviceId = ESP.getChipId();
|
||||||
|
sprintf(sID, "%010ld", deviceId);
|
||||||
|
Serial.print("ID: \t\t"); Serial.println(deviceId);
|
||||||
|
|
||||||
|
// ca. 5 s
|
||||||
|
espClient.setFingerprint(mqtt_fprint);
|
||||||
|
client.setServer(mqtt_server, mqtt_port);
|
||||||
|
client.setCallback(callback);
|
||||||
|
//----------
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
unsigned long Pause = 0;
|
||||||
|
if (!client.connected()) {
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
//client.loop();
|
||||||
|
int currentMillis = millis();
|
||||||
|
if (currentMillis - previousMillis >= 10000) {
|
||||||
|
previousMillis = currentMillis;
|
||||||
|
read_HTU21D();
|
||||||
|
read_BMP_280();
|
||||||
|
//valTemp = getTemperature_MCP9808();
|
||||||
|
dtostrf(AKKU,8,2,ADSData.Akku);
|
||||||
|
sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/battery" );
|
||||||
|
client.publish(topic, ADSData.Akku, true);
|
||||||
|
#if(MQTT == 0)
|
||||||
|
M2M_HTU21D(hostname.c_str());
|
||||||
|
M2M_BMP280(hostname.c_str());
|
||||||
|
M2M_Temperatur_MCP9808(hostname.c_str());
|
||||||
|
#endif
|
||||||
|
long int Feldstaerke = WiFi.RSSI();
|
||||||
|
sprintf(msg,"%ld", Feldstaerke);
|
||||||
|
sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/RSSI" );
|
||||||
|
client.publish(topic, msg, true);
|
||||||
|
client.loop();
|
||||||
|
delay(500);
|
||||||
|
digitalWrite(BUILTIN_LED, HIGH);
|
||||||
|
/* ESP.deepSleep(5e6);
|
||||||
|
delay(100); */
|
||||||
|
endTime = millis();
|
||||||
|
if (AKKU < MinimalSpannung){
|
||||||
|
// ---------------------------------
|
||||||
|
// Status ändern !!! -5
|
||||||
|
datenSave(-5);
|
||||||
|
// ---------------------------------
|
||||||
|
Pause = intervalLowBatt -((endTime - startTime) * 1000); // Pause ca. 60 Minuten
|
||||||
|
Serial.println("AKKU entladen!");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
Pause = interval -((endTime - startTime) * 1000); // Pause ca. 15 Minuten
|
||||||
|
}
|
||||||
|
if(Pause <=0){
|
||||||
|
Pause = 1;
|
||||||
|
}
|
||||||
|
//ESP.restart();
|
||||||
|
Serial.print("Ich gehe für ca. "); Serial.print((Pause/1000/1000/60)+1); Serial.println( " Minuten schlafen.");
|
||||||
|
//ESP.deepSleep(Pause, WAKE_RF_DISABLED); // Pause
|
||||||
|
digitalWrite(START_STOP_PIN, !LOW);
|
||||||
|
#if (DEBUG == 1)
|
||||||
|
ESP.deepSleep(10e6);
|
||||||
|
#else
|
||||||
|
ESP.deepSleep(Pause);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_wifi() {
|
||||||
|
|
||||||
|
long ErrCount = 0;
|
||||||
|
|
||||||
|
delay(10);
|
||||||
|
// We start by connecting to a WiFi network
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.print(ssid);
|
||||||
|
Serial.print(" ");
|
||||||
|
|
||||||
|
WiFi.forceSleepWake();
|
||||||
|
delay( 1 );
|
||||||
|
WiFi.persistent( false );
|
||||||
|
WiFi.mode( WIFI_STA );
|
||||||
|
//WiFi.setHostname(hostname.c_str()); //define hostname
|
||||||
|
WiFi.hostname(hostname.c_str());
|
||||||
|
WiFi.mode( WIFI_STA );
|
||||||
|
if (!ip.fromString(MyIP)) { // try to parse into the IPAddress
|
||||||
|
Serial.println("UnParsable IP");
|
||||||
|
}
|
||||||
|
if (!dns.fromString(DNS)) { // try to parse into the IPAddress
|
||||||
|
Serial.println("UnParsable DNS");
|
||||||
|
}
|
||||||
|
if (!gateway.fromString(GATEWAY)) { // try to parse into the IPAddress
|
||||||
|
Serial.println("UnParsable GATEWAY");
|
||||||
|
}
|
||||||
|
//WiFi.config( ip, dns, gateway, subnet );
|
||||||
|
if (!WiFi.config(ip, gateway, subnet, dns, secondaryDNS))
|
||||||
|
{
|
||||||
|
Serial.println("STA Failed to configure");
|
||||||
|
}
|
||||||
|
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
ErrCount ++;
|
||||||
|
if (ErrCount >= MaxErrCount){
|
||||||
|
// ---------------------------------
|
||||||
|
// Status ändern !!! -1
|
||||||
|
datenSave(-1);
|
||||||
|
// ---------------------------------
|
||||||
|
endTime = millis();
|
||||||
|
unsigned long Pause = stoerung -((endTime - startTime) * 1000); // Pause
|
||||||
|
Serial.println();
|
||||||
|
Serial.println("STÖRUNG WiFi.");
|
||||||
|
Serial.print("Ich gehe für ca. "); Serial.print(Pause/1000/1000/60); Serial.println( " Minuten schlafen.");
|
||||||
|
ESP.deepSleep(Pause, WAKE_NO_RFCAL); // Pause
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.println(" WiFi connected");
|
||||||
|
Serial.print("IP address: \t");
|
||||||
|
Serial.print(WiFi.localIP()); Serial.print("\tRESSI: "); Serial.println(WiFi.RSSI());
|
||||||
|
/* delay(2000);
|
||||||
|
ESP.deepSleep(2e6, WAKE_RF_DEFAULT);
|
||||||
|
delay(100); */
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback(char* topic1, byte* payload, unsigned int length)
|
||||||
|
{
|
||||||
|
Serial.print("Message arrived [");
|
||||||
|
Serial.print(topic1);
|
||||||
|
Serial.print("] ");
|
||||||
|
for (unsigned int i = 0; i < length; i++) {
|
||||||
|
msg[i] = (char)payload[i];
|
||||||
|
}
|
||||||
|
msg[length] = '\0';
|
||||||
|
Serial.println(msg);
|
||||||
|
if(strcmp(topic1, topic_1)== 0){
|
||||||
|
Serial.print(msg);
|
||||||
|
Serial.println();
|
||||||
|
korectur = atof(msg);
|
||||||
|
Serial.print("Korektur:\t");Serial.println(korectur, 8);
|
||||||
|
int er = saveKorektur(korectur);
|
||||||
|
if (er != 0){
|
||||||
|
Serial.println("Daten konnten nicht gespeichert werden.");
|
||||||
|
}
|
||||||
|
float test = readKorectur();
|
||||||
|
Serial.print("Korektur:\t");Serial.println(test, 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void reconnect() {
|
||||||
|
// Loop until we're reconnected
|
||||||
|
sprintf(clientName, "%s%s", "ESP8266Client", sID);
|
||||||
|
while (!client.connected()) {
|
||||||
|
Serial.print("Attempting MQTT connection...");
|
||||||
|
verifyFingerprint();
|
||||||
|
// Attempt to connect
|
||||||
|
if (client.connect(clientName)) {
|
||||||
|
Serial.println("connected");
|
||||||
|
// Once connected, publish an announcement...
|
||||||
|
client.publish("outTopic", "hello world");
|
||||||
|
// ... and resubscribe
|
||||||
|
client.subscribe("inTopic");
|
||||||
|
} else {
|
||||||
|
Serial.print("failed, rc=");
|
||||||
|
Serial.print(client.state());
|
||||||
|
Serial.println(" try again in 5 seconds");
|
||||||
|
// Wait 5 seconds before retrying
|
||||||
|
delay(5000);
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void datenSave(int wert){
|
||||||
|
File k = LittleFS.open("/status.txt", "w");
|
||||||
|
if(!k){
|
||||||
|
Serial.println("file open failed");
|
||||||
|
}
|
||||||
|
k.println(wert);
|
||||||
|
k.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
int readDaten()
|
||||||
|
{
|
||||||
|
int Error;
|
||||||
|
File k = LittleFS.open("/status.txt", "r");
|
||||||
|
if(!k){
|
||||||
|
Serial.println("file open failed");
|
||||||
|
Error = -10;
|
||||||
|
}else{
|
||||||
|
String data = k.readString();
|
||||||
|
Error = data.toInt();
|
||||||
|
k.close();
|
||||||
|
}
|
||||||
|
return Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
int saveKorektur(float wert){
|
||||||
|
int Error = 0;
|
||||||
|
File k = LittleFS.open("/Korektur.txt", "w");
|
||||||
|
if(!k){
|
||||||
|
Serial.println("file open failed");
|
||||||
|
Error = -1;
|
||||||
|
}else{
|
||||||
|
k.println(String(wert,8));
|
||||||
|
k.close();
|
||||||
|
}
|
||||||
|
return Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
float readKorectur(){
|
||||||
|
float Korektur;
|
||||||
|
File k = LittleFS.open("/Korektur.txt", "r");
|
||||||
|
if(!k){
|
||||||
|
Serial.println("file open failed");
|
||||||
|
Korektur = 1.00;
|
||||||
|
}else{
|
||||||
|
String data = k.readString();
|
||||||
|
Korektur = data.toFloat();
|
||||||
|
k.close();
|
||||||
|
}
|
||||||
|
return Korektur;
|
||||||
|
}
|
||||||
|
|
||||||
|
void verifyFingerprint() {
|
||||||
|
unsigned long Pause = 0;
|
||||||
|
if(client.connected() || espClient.connected()) return; //Already connected
|
||||||
|
|
||||||
|
Serial.print("\n\tChecking TLS @ ");
|
||||||
|
Serial.print(mqtt_server);
|
||||||
|
Serial.print("...");
|
||||||
|
|
||||||
|
if (!espClient.connect(mqtt_server, mqtt_port)) {
|
||||||
|
//Serial.println("\n\tConnection failed. Rebooting.");
|
||||||
|
Serial.println("\n\tConnection failed.");
|
||||||
|
Serial.flush();
|
||||||
|
//blink.detach();
|
||||||
|
//blink.attach(0.05, flip);
|
||||||
|
//delay(5000);
|
||||||
|
endTime = millis();
|
||||||
|
Pause = stoerung -((endTime - startTime) * 1000); // Pause ca. 15 Minuten
|
||||||
|
Serial.print("\tIch gehe für "); Serial.print((Pause/1000/1000/60)+1); Serial.println( " Minuten schlafen.");
|
||||||
|
ESP.deepSleep(Pause, WAKE_RF_DISABLED); // Pause
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
espClient.stop();
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
void pulse_pin(uint8_t pin)
|
||||||
|
{
|
||||||
|
digitalWrite(pin, LOW);
|
||||||
|
delay(1);
|
||||||
|
digitalWrite(pin, HIGH);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user