How do moisture sensors work? When the probes are places into the soil the sensor passes current through and measures the resistance from one probe to another. That resistance can be measured and be converted to a number. In our case 100 is going be saturated in water while upwards to 900 means that it is dry.
We will be using losants web service to transmit that number from the nodemcu to losants servers where we can visually depict the water level and send the SMS.
Let's get started and wire the moisture sensor up first. It does not matter which way you wire the probes to the LM293 board. From the board to the nodeMCU you want to connect the A0 pin which is red to the single analog pin on the nodeMCU. You can skip the D0 pin and go to the VCC pin or the blue line to 3.3vs. Finally connect the green ground pin the the ground on the NodeMCU.
Now let's wire up the 3.3v relay. Wire the ground on the relay to a ground on the breadboard, wire IN2 to GPIO4 on the board and finally VCC to 3.3v. Im using mostly closed on the relay so when power is given it will open activating the pump. The nodemcu cannot power the pump on its own so I cut a usb cable to expose the wiring and to give the pump power.
In this picture above the positive wire is going to the relay and the ground is going to the negative wire on the pump. To finish off the circuit the positive wire is going to the relay. Now that it is all wired up, let's go to the code.
We will be using losants service so first things first make an account. After you have made an account make a device and be sure to remember to copy the device ID, Access key and Secret Key.
#include <ESP8266WiFi.h>
#include <Losant.h>
#include <PubSubClient.h>
// WiFi credentials.
const char* WIFI_SSID = "1206";
const char* WIFI_PASS = "912456801";
// Losant credentials.
const char* LOSANT_DEVICE_ID = "5ae8ca1fd27ab70006315236";
const char* LOSANT_ACCESS_KEY = "d8b8e8b7-fdbd-4718-82d1-71e03963aa0d";
const char* LOSANT_ACCESS_SECRET = "ac7f25ac625a0ec3e35f6cf99e1eb7d479a3dfa5edb90b82082faebd3b2138ec";
Change the wifi credentials and your losant ones too.
double input = analogRead(A0);
Serial.println();
Serial.print("Moisture level: ");
Serial.println(input);
// Turn the sensor back off.
digitalWrite(MOISTURE_PIN, LOW);
if(input>700){
digitalWrite(relayInput, LOW); // turn relay on
delay(2000);
}
digitalWrite(relayInput, HIGH); // turn relay off
Variable named input is set to read the moister sensor. In the console it will write the moisture level from 1-999 with it being dryer the higher you get. The relay will turn on activating the pump if the water level drys to a level of 700 or more. It will run the pump for 2000ms and then turn off.
Let's head over to losant to see how to send visualize and send the SMS. Go to the workflow area in losant and drag the device button to the workflow. Then set the device as your moisture sensor.
Create two global variable and name them wet and dry and set them as 370 and 700 respectively. Then create a latch and follow what I put. It will send an sms if the moister goes higher than 370. Create an sms output and put a desired message. You have now finished the project and will never forget to water your plants again!
Comments