Skip to content

Instantly share code, notes, and snippets.

@kylejohnson
Created April 26, 2018 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylejohnson/912334bfe71cd06b6528cbc0664e704c to your computer and use it in GitHub Desktop.
Save kylejohnson/912334bfe71cd06b6528cbc0664e704c to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
int val = 0; // moisture value
int soilPin = A0;
byte host[] = {192, 168, 11, 17}; //influxdb host
int port = 8089; //influxdb port
int soilPower = 5;//Variable for Soil moisture Power
int sleepTime = 1800;//seconds
#define WLAN_SSID "fu"
#define WLAN_PASS "bar"
WiFiUDP udp;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.print(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
pinMode(soilPower, OUTPUT);
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor by default
setup_wifi();
}
void loop() {
String soil, line;
int v;
v = readSoil();
Serial.println(v);
v = map(v, 90, 361, 0, 10);//calibrated values
soil = String(v);
//https://docs.influxdata.com/influxdb/v1.5/write_protocols/line_protocol_tutorial/
line = String("environment,location=plant_1 soil=" + soil + "i");
Serial.println(line);
//send line to influxdb
udp.beginPacket(host, port);
udp.print(line);
udp.endPacket();
delay(1000);
Serial.println("Going to a deep sleep...");
ESP.deepSleep(sleepTime * 1000000);
}
int readSoil(){
digitalWrite(soilPower, HIGH);
delay(10);
val = analogRead(soilPin);
digitalWrite(soilPower, LOW);
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment