Spivey
Published © MIT

Measuring Soil Moisture With The Wemos ESP32 And Wia

Learn when your plant needs to be watered with a Wemos ESP32, a mh soil moisture sensor known as the flying fish and Wia.

BeginnerFull instructions provided1 hour3,996
Measuring Soil Moisture With The Wemos ESP32 And Wia

Things used in this project

Hardware components

thingSoC ESP32 WiFi Module
thingSoC ESP32 WiFi Module
We actually Used a Wemos board but that wasn't an option on here.
×1
SparkFun Soil Moisture Sensor (with Screw Terminals)
SparkFun Soil Moisture Sensor (with Screw Terminals)
Again we used a different brand but any soil sensor will work
×1

Software apps and online services

Wia
Wia

Story

Read more

Schematics

Wemos ESP 32

Code

Publish Soil Moisture to Wia

Arduino
#include <ArduinoJson.h>
#include <WiFi.h>
#include <ArduinoHttpClient.h>
#include <Arduino.h>


const char* ssid     = "your-wifi-ssid"; // Your WiFi ssid
const char* password = "your-wifi-password"; //Your Wifi password

// Get this sccret key from the wia dashboard. It should start with `d_sk`
const char* device_secret_key = "your-device-secret-key";

// Wia API parameters
char server[] = "api.wia.io";
char path[] = "/v1/events";
int port = 80;

WiFiClient client;
int status = WL_IDLE_STATUS;

StaticJsonBuffer<200> jsonBuffer;
HttpClient httpClient = HttpClient(client, server, port);
JsonObject& root = jsonBuffer.createObject();

const int INPUT_PIN = 23; 
const int ANALOG_PIN = 35;

void setup() {
  
  pinMode(INPUT_PIN, INPUT);
  
  // initialize serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  WiFi.begin(ssid, password);
  Serial.print("Attempting to connect to SSID: ");
  Serial.print(ssid);
  // attempt to connect to WiFi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    // Connect to WPA/WPA2 network. Change this line if using open or WEP  network:
    // wait 5 seconds for connection:
    delay(5000);
  }

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("connecting...");
}

// Thing function runs continiously
void loop() {
  
  root["name"] = "moisture";
  root["data"] =  analogRead (ANALOG_PIN);

  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {

    sendToWia(root);

  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
   delay(1000*15*60); // Wait for 15 minutes to post again
}

// Adds the correct headers for HTTP and sends Data to Wia
void sendToWia(JsonObject& data) {
  size_t len = data.measureLength();
  size_t size = len + 1;
  char json[size];
  httpClient.beginRequest();
  httpClient.post(path);
  httpClient.sendHeader("Content-Type", "application/json");
  httpClient.sendHeader("Content-Length", data.measureLength());
  httpClient.sendHeader("Authorization", "Bearer " + String(device_secret_key));
  httpClient.beginBody();
  data.printTo(httpClient);
  Serial.print("Posting ");
  data.printTo(Serial);
  Serial.println(" to Wia");
  httpClient.endRequest();

}

Credits

Spivey

Spivey

82 projects • 59 followers
Tourist in a Tutu || US Born || Melbourne/Mexico/California Raised || New Yorker at ❤️ || SF to Dublin to be COO of Wia the best IoT startup
Thanks to Alan Donoghue.

Comments