Compare commits
6 Commits
8bc1861024
...
MQTT-Testa
| Author | SHA1 | Date | |
|---|---|---|---|
| 66953310ad | |||
|
|
5316dba469 | ||
| b6e43131a6 | |||
| 2623b3c97c | |||
| 23d4f8d2ec | |||
|
|
0ae0e76f68 |
@@ -4,8 +4,21 @@
|
||||
* For project specific settings. Copy to config.h and place your settings there.
|
||||
**/
|
||||
|
||||
// MQTT-Configuration
|
||||
const char *MQTT_BROKER = "192.168.178.74";
|
||||
const char *MQTT_SENSOR_TOPIC = "living/sensor2";
|
||||
const char *MQTT_CLIENT_USER = "NULL";
|
||||
const char *MQTT_CLIENT_PW = "NULL";
|
||||
const char *MQTT_CLIENT_ID = "living_mobile_sensor";
|
||||
|
||||
const char *MQTT_SENSOR_TOPIC = "living/sensor2";
|
||||
const char *MQTT_LAST_WILL_TOPIC = "living/sensor2/status";
|
||||
const char *MQTT_LAST_WILL_MSG = "offline";
|
||||
uint8_t MQTT_WILL_QOS = 1; // 0, 1 or 2
|
||||
boolean MQTT_WILL_RETAIN = true;
|
||||
|
||||
|
||||
// Configuration for Soil-Sensor
|
||||
const int AirValue = 700; //replace the value with value when placed in air (800)
|
||||
const int WaterValue = 150; //replace the value with value when placed in water (345)
|
||||
|
||||
#endif
|
||||
@@ -36,9 +36,8 @@ uint8_t rh_buf[RH_BUF_LEN];
|
||||
* */
|
||||
#include "config.h"
|
||||
|
||||
|
||||
const int AirValue = 800; //replace the value with value when placed in air
|
||||
const int WaterValue = 345; //replace the value with value when placed in water
|
||||
// const int AirValue = 800; //replace the value with value when placed in air
|
||||
// const int WaterValue = 345; //replace the value with value when placed in water
|
||||
|
||||
volatile boolean new_value = false;
|
||||
|
||||
@@ -55,6 +54,7 @@ RH_ASK driver(RH_SPEED, RH_RX_PIN);
|
||||
#define MSG_BAT 0x20
|
||||
#define MSG_TEMPHUM 0x30
|
||||
#define MSG_ERR 0x40
|
||||
#define MSG_HUM 0x50
|
||||
|
||||
char buff[20];
|
||||
|
||||
@@ -170,6 +170,14 @@ void loop()
|
||||
Serial.println(SoilHumidity);
|
||||
new_value = true;
|
||||
break;
|
||||
case MSG_HUM:
|
||||
Serial.println("Soil Humidity");
|
||||
memcpy(&soil, &rh_buf[1], 2);
|
||||
Serial.println(soil);
|
||||
SoilHumidity = (float)(map(soil, AirValue, WaterValue, 0, 100));
|
||||
Serial.println(SoilHumidity);
|
||||
new_value = true;
|
||||
break;
|
||||
case MSG_BAT:
|
||||
// battery data
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
@@ -215,9 +223,12 @@ void reconnect()
|
||||
{
|
||||
Serial.println("INFO: Attempting MQTT connection...");
|
||||
// Attempt to connect
|
||||
if (mqttClient.connect(MQTT_CLIENT_ID))
|
||||
|
||||
// Should work without authentication, if credentials are NULL
|
||||
if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_CLIENT_USER, MQTT_CLIENT_PW, MQTT_WILL_TOPIC, MQTT_WILL_QOS, MQTT_WILL_RETAIN, MQTT_WILL_MSG))
|
||||
{
|
||||
Serial.println("INFO: connected");
|
||||
mqttClient.publish(MQTT_WILL_TOPIC, "online", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -274,6 +285,7 @@ void publishData(float temp, float humid, float soil, long battery)
|
||||
char data[200];
|
||||
serializeJson(doc, data);
|
||||
mqttClient.publish(MQTT_SENSOR_TOPIC, data, true);
|
||||
|
||||
yield();
|
||||
}
|
||||
|
||||
|
||||
@@ -14,4 +14,7 @@
|
||||
// Sleep this many again before waging up. Sleep time in s = WATCHDOG_WAKEUP s * WATCHDOG_SLEEP_FURTHER, e.g. 8*8s = 64s
|
||||
#define WATCHDOG_SLEEP_FURTHER 8
|
||||
|
||||
// define wether to use a DHT-Sensor or not
|
||||
#define USE_DHT_SENSOR true
|
||||
|
||||
#endif
|
||||
@@ -68,7 +68,10 @@ void setup()
|
||||
pinMode(SOIL_PIN, INPUT);
|
||||
|
||||
// init the DHT22
|
||||
dht_init(&dht, DHT_PIN);
|
||||
if (USE_DHT_SENSOR)
|
||||
{
|
||||
dht_init(&dht, DHT_PIN);
|
||||
}
|
||||
|
||||
uint8_t num_blink = 3;
|
||||
|
||||
@@ -112,24 +115,35 @@ void loop()
|
||||
memcpy(&rh_buf[1], &battery, 4);
|
||||
rh_send(rh_buf,RH_BUF_LEN);
|
||||
}
|
||||
else if (dht_read_data(&dht, &t, &h) == 1)
|
||||
else if (USE_DHT_SENSOR)
|
||||
{
|
||||
rh_buf[0] = MSG_TEMPHUM;
|
||||
memcpy(&rh_buf[1], &t, 4);
|
||||
memcpy(&rh_buf[5], &h, 4);
|
||||
soil = analogRead(SOIL_PIN);
|
||||
memcpy(&rh_buf[9], &soil, 2);
|
||||
if (dht_read_data(&dht, &t, &h) == 1)
|
||||
{
|
||||
rh_buf[0] = MSG_TEMPHUM;
|
||||
memcpy(&rh_buf[1], &t, 4);
|
||||
memcpy(&rh_buf[5], &h, 4);
|
||||
soil = analogRead(SOIL_PIN);
|
||||
memcpy(&rh_buf[9], &soil, 2);
|
||||
|
||||
rh_send(rh_buf,RH_BUF_LEN);
|
||||
rh_send(rh_buf,RH_BUF_LEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
// error message
|
||||
blink(4);
|
||||
digitalWrite(TRANSISTOR_BASE_PIN, HIGH);
|
||||
delay(5);
|
||||
rh_buf[0] = MSG_ERR;
|
||||
rh_send(rh_buf,1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// error message
|
||||
blink(4);
|
||||
digitalWrite(TRANSISTOR_BASE_PIN, HIGH);
|
||||
delay(5);
|
||||
rh_buf[0] = MSG_ERR;
|
||||
rh_send(rh_buf,1);
|
||||
rh_buf[0] = MSG_HUM;
|
||||
soil = analogRead(SOIL_PIN);
|
||||
memcpy(&rh_buf[1], &soil, 2);
|
||||
|
||||
rh_send(rh_buf,RH_BUF_LEN);
|
||||
}
|
||||
counter++;
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
#define MSG_BAT 0x20
|
||||
#define MSG_TEMPHUM 0x30
|
||||
#define MSG_ERR 0x40
|
||||
#define MSG_HUM 0x50 // case, sensor just reads soil-humidity
|
||||
|
||||
// for watchdog wakeup
|
||||
// https://gist.github.com/dwhacks/8055287
|
||||
|
||||
Reference in New Issue
Block a user