diff options
author | Joe Robinson <joe@lc8n.com> | 2019-02-19 23:28:29 +0000 |
---|---|---|
committer | Joe Robinson <joe@lc8n.com> | 2019-02-19 23:28:29 +0000 |
commit | 550486080b51cbdee41af5a12ae835cc0d8ace01 (patch) | |
tree | 0d0010d5a3c5ad3d3a80fb798b7c9e50cbb2f4d0 /cricket.ino | |
parent | 8d8d3d7db1fe66b3392d9dbbf1e364aca216da35 (diff) | |
parent | cf1837ccf7786d5455719bf83e4e3e39bfe0faf8 (diff) |
Add moisture sensor
See merge request wjoe/cricket!1
Diffstat (limited to 'cricket.ino')
-rw-r--r-- | cricket.ino | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/cricket.ino b/cricket.ino index 7979c61..4cf802e 100644 --- a/cricket.ino +++ b/cricket.ino @@ -8,12 +8,13 @@ #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 +22,7 @@ void setup() { Serial.println("Initialised..."); connectWifi(); delay(1000); + pinMode(ANALOG_PIN,INPUT); dht.begin(); } @@ -60,17 +62,21 @@ void connectMqtt() { void loop() { delay(1500); + 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("Humidity: "); + Serial.print("Moisture: "); + Serial.print(m); + Serial.print(" Humidity: "); Serial.print(h); Serial.print(" Temperature: "); Serial.println(t); @@ -79,7 +85,15 @@ void loop() { connectMqtt(); } dtostrf(t, 1, 2, t_msg); - mqttClient.publish("temperature", t_msg); dtostrf(h, 1, 2, h_msg); - mqttClient.publish("humidity", h_msg); + dtostrf(m, 1, 2, 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); } |