diff options
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); } |