Last active
July 19, 2020 13:20
-
-
Save adegard/abf4c6c3e1710001696d71e40c39639b to your computer and use it in GitHub Desktop.
Soil Moisture ESP 32 Wifi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
// Capacitive Soil Moisture Sensor with ESP32 &write data on Thinspeak via Wifi | |
// 2020, v3.2 | |
// tested on ESP WROOM 32 DEV KIT | |
// NOTE: | |
// use pin 32 (ADC2 only) | |
// ADC1 is used by wifi (see https://live.staticflickr.com/4764/40089095211_ec1fee0087_b.jpg) | |
// change apikey, adress of channel, wifi ssid and password below | |
// Calibration Capacitive Soil Moisture Sensor with ESP32 | |
// use Serial Plotter of Arduino per determinare wet/dry values | |
// Reduse sensor tension to 1,63 volts : I used 2 resistors of 100 Ohms see diagram in post | |
// https://forum.micropython.org/viewtopic.php?t=4487 | |
// 2020 | |
*/ | |
#include <Arduino.h> | |
#include "secrets.h" | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <WiFi.h> | |
#define SENSOR_POWER 13 // Connect the power for the soil sensor here. // Connect the power for the soil sensor here. | |
#define SLEEP_TIME_SECONDS 3 //1800 30 min | |
const int AirValue = 795; //you need to replace this value with Value_1 | |
const int WaterValue = 820; //you need to replace this value with Value_2 | |
const int SensorPin = 32; | |
int soilMoistureValue = 0; | |
int soilmoisturepercent =0; | |
int numMeasure = 10; | |
String tsfield1Name; | |
String request_string; | |
WiFiClient client; | |
// This function reads the soil moisture sensor numAve times and returns the average. | |
long readSoil(int numAve) | |
{ | |
long ADCValue = 0; | |
for ( int i = 0; i < numAve; i++ ) { | |
digitalWrite( SENSOR_POWER, HIGH ); // Turn power to device on. | |
delay(250); // Wait milliseconds for sensor to settle. | |
ADCValue += analogRead( SensorPin ); // Read the value from sensor. | |
digitalWrite( SENSOR_POWER, LOW ); // Turn power to device off. | |
} | |
ADCValue = ADCValue / numAve; | |
return ADCValue; | |
} | |
void setup() { | |
Serial.begin(115200); // open serial port, set the baud rate to 9600 bps | |
WiFi.disconnect(); | |
delay(3000); | |
Serial.println("START"); | |
WiFi.begin(ssid,password); | |
while ((!(WiFi.status() == WL_CONNECTED))){ | |
delay(300); | |
Serial.print(".."); | |
} | |
Serial.println("Connected"); | |
Serial.println("Your IP is"); | |
Serial.println((WiFi.localIP())); | |
} | |
void loop() | |
{ | |
soilMoistureValue = readSoil( numMeasure ); //put Sensor insert into soil | |
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); | |
if(soilmoisturepercent > 100) { soilmoisturepercent =100;} | |
if(soilmoisturepercent < 0) { soilmoisturepercent =0;} | |
Serial.println(soilMoistureValue); | |
Serial.println(soilmoisturepercent); | |
delay(250); | |
if (client.connect("api.thingspeak.com",80)) { | |
Serial.println("sending data"); | |
request_string = thingSpeakAddress; | |
request_string += "&field1="; | |
request_string += (soilMoistureValue); | |
request_string += "&field2="; | |
request_string += (soilmoisturepercent); | |
client.print("POST /update HTTP/1.1\n"); | |
client.print("Host: api.thingspeak.com\n"); | |
client.print("Connection: close\n"); | |
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n"); | |
client.print("Content-Type: application/x-www-form-urlencoded\n"); | |
client.print("Content-Length: "); | |
client.print(request_string.length()); | |
client.print("\n\n"); | |
client.print(request_string); | |
} else { | |
Serial.println("failed"); | |
} | |
// delay(10000); //use sleep time | |
delay( 250 ); | |
Serial.print( "Goodnight for "+String( SLEEP_TIME_SECONDS ) + " Seconds" ); | |
ESP.deepSleep( SLEEP_TIME_SECONDS * 1000000 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment