From e96f5898860aa2e31bd6b8b5d6838da2184b7818 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 19 Feb 2019 22:54:38 +0000 Subject: Moisture sensor --- cricket.ino | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'cricket.ino') diff --git a/cricket.ino b/cricket.ino index 7979c61..46430f0 100644 --- a/cricket.ino +++ b/cricket.ino @@ -8,12 +8,15 @@ #define WIFI_PASS "PASS" WiFiClient wifiClient; -#define MQTT_ID "cricket0" +#define MQTT_ID "ID" #define MQTT_BROKER "BROKER" PubSubClient mqttClient(wifiClient); #define DHTPIN 5 //Pin D1 #define DHTTYPE DHT22 //Temp and Humidity Sensor +#define ANALOG_PIN 17 + + DHT dht(DHTPIN, DHTTYPE); void setup() { @@ -21,6 +24,7 @@ void setup() { Serial.println("Initialised..."); connectWifi(); delay(1000); + pinMode(ANALOG_PIN,INPUT); dht.begin(); } @@ -60,7 +64,7 @@ void connectMqtt() { void loop() { delay(1500); - float h = dht.readHumidity(); + float h = analogRead(ANALOG_PIN); float t = dht.readTemperature(); char t_msg[20]; @@ -70,7 +74,7 @@ void loop() { Serial.println("Failed to read from DHT sensor!"); return; } - Serial.print("Humidity: "); + Serial.print("Moisture: "); Serial.print(h); Serial.print(" Temperature: "); Serial.println(t); @@ -81,5 +85,5 @@ void loop() { dtostrf(t, 1, 2, t_msg); mqttClient.publish("temperature", t_msg); dtostrf(h, 1, 2, h_msg); - mqttClient.publish("humidity", h_msg); + mqttClient.publish("moisture", h_msg); } -- cgit v1.2.3 From c4b62abe473716dc004a19bbb48b382bac80d601 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 19 Feb 2019 23:15:20 +0000 Subject: Moisture and Humidity --- cricket.ino | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'cricket.ino') diff --git a/cricket.ino b/cricket.ino index 46430f0..0166db5 100644 --- a/cricket.ino +++ b/cricket.ino @@ -8,8 +8,8 @@ #define WIFI_PASS "PASS" WiFiClient wifiClient; -#define MQTT_ID "ID" -#define MQTT_BROKER "BROKER" +#define MQTT_ID "cricket1" +#define MQTT_BROKER "192.168.0.59" PubSubClient mqttClient(wifiClient); #define DHTPIN 5 //Pin D1 @@ -64,17 +64,21 @@ void connectMqtt() { void loop() { delay(1500); - float h = analogRead(ANALOG_PIN); + float m = analogRead(ANALOG_PIN); + float h = dht.readHumidity(); float t = dht.readTemperature(); char t_msg[20]; char h_msg[20]; + char m_msg[20]; // Check if any reads failed and exit early (to try again). - if (isnan(h) || isnan(t)) { + if (isnan(h) || isnan(t) || isnan(m)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Moisture: "); + Serial.print(m); + Serial.print(" Humidity: "); Serial.print(h); Serial.print(" Temperature: "); Serial.println(t); @@ -83,7 +87,9 @@ void loop() { connectMqtt(); } dtostrf(t, 1, 2, t_msg); - mqttClient.publish("temperature", t_msg); + mqttClient.publish("cricket1/temperature", t_msg); dtostrf(h, 1, 2, h_msg); - mqttClient.publish("moisture", h_msg); + mqttClient.publish("cricket1/humidity", h_msg); + dtostrf(m, 1, 2, m_msg); + mqttClient.publish("cricket1/moisture", m_msg); } -- cgit v1.2.3 From 8a6cbc16c200faa926671a6c69df2775983410ee Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 19 Feb 2019 23:25:51 +0000 Subject: Build topic from ID --- cricket.ino | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'cricket.ino') diff --git a/cricket.ino b/cricket.ino index 0166db5..a78091d 100644 --- a/cricket.ino +++ b/cricket.ino @@ -87,9 +87,15 @@ void loop() { connectMqtt(); } dtostrf(t, 1, 2, t_msg); - mqttClient.publish("cricket1/temperature", t_msg); dtostrf(h, 1, 2, h_msg); - mqttClient.publish("cricket1/humidity", h_msg); dtostrf(m, 1, 2, m_msg); - mqttClient.publish("cricket1/moisture", m_msg); + char temp_topic[20]; + char humidity_topic[20]; + char moisture_topic[20]; + sprintf(temp_topic, "%s/temperature", MQTT_ID); + sprintf(humidity_topic, "%s/humidity", MQTT_ID); + sprintf(moisture_topic, "%s/moisture", MQTT_ID); + mqttClient.publish(temp_topic, t_msg); + mqttClient.publish(humidity_topic, h_msg); + mqttClient.publish(moisture_topic, m_msg); } -- cgit v1.2.3 From 75ee7d3101749462f130274f000339e520b4c696 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 19 Feb 2019 23:27:06 +0000 Subject: Tidy up --- cricket.ino | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'cricket.ino') diff --git a/cricket.ino b/cricket.ino index a78091d..e20a96c 100644 --- a/cricket.ino +++ b/cricket.ino @@ -8,15 +8,14 @@ #define WIFI_PASS "PASS" WiFiClient wifiClient; -#define MQTT_ID "cricket1" -#define MQTT_BROKER "192.168.0.59" +#define MQTT_ID "ID" +#define MQTT_BROKER "BROKER" PubSubClient mqttClient(wifiClient); #define DHTPIN 5 //Pin D1 #define DHTTYPE DHT22 //Temp and Humidity Sensor #define ANALOG_PIN 17 - DHT dht(DHTPIN, DHTTYPE); void setup() { -- cgit v1.2.3 From cf1837ccf7786d5455719bf83e4e3e39bfe0faf8 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Tue, 19 Feb 2019 23:27:49 +0000 Subject: Tidy up --- cricket.ino | 1 - 1 file changed, 1 deletion(-) (limited to 'cricket.ino') diff --git a/cricket.ino b/cricket.ino index e20a96c..4cf802e 100644 --- a/cricket.ino +++ b/cricket.ino @@ -15,7 +15,6 @@ PubSubClient mqttClient(wifiClient); #define DHTPIN 5 //Pin D1 #define DHTTYPE DHT22 //Temp and Humidity Sensor #define ANALOG_PIN 17 - DHT dht(DHTPIN, DHTTYPE); void setup() { -- cgit v1.2.3