IoT World Part 5: Measuring soil moisture over time using Soil Hygrometer Module and Losant

Needed components

 

Creating device and setting up dashboard

Log in your account, in case of not having one, sing up, it’s really fast and easy

Create a device and set Device attributes to

  • Data Type – Number and Name –  humidity

or you can use my recipe

Create e dashboard and Time Series Graph with a segment

Humidity

The graph

Here are some tutorials on how to get started – Walkthrough, Dashboard



Wiring the ESP8266 and the Soil Hygrometer Module

Connect (Soil Hygrometer Module) to (ESP8266)

  • D0 to D0
  • VCC to VIN
  • GND to GND
  • A0 is not used

 



Flashing the code on the ESP8266

Create a file inside ProjectName/src/ called main.cpp and inside put

#include <ESP8266WiFi.h>
#include <Losant.h>

// WiFi credentials.
const char *WIFI_SSID = "wifi-name";
const char *WIFI_PASS = "wifi-password";

// Losant credentials.
const char *LOSANT_DEVICE_ID = "device-id"; // Device id can be found under
                                            // Applications > Devices > Your
                                            // device > Right corner
const char *LOSANT_ACCESS_KEY = "access-key";
const char *LOSANT_ACCESS_SECRET =
    "access-secret"; // To generate Key & Secret go to Applications > Security >
                     // Right corder + Add access key

WiFiClientSecure wifiClient;

// For an unsecure connection to Losant.
// WiFiClient wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

const int hygrometer = D0;

void connect() {
  // Connect to Wifi.
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Connect to Losant.
  Serial.println();
  Serial.print("Connecting to Losant...");

  device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  // For an unsecure connection.
  // device.connect(wifiClient, ACCESS_KEY, ACCESS_SECRET);

  while (!device.connected()) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("Connected successfully");
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    // wait until Serial is initialized
  }
  // connects to WiFi
  connect();
}

void loop() {
  bool toReconnect = false;

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected from WiFi");
    toReconnect = true;
  }

  if (!device.connected()) {
    Serial.println("Disconnected from Losant");
    Serial.println(device.mqttClient.state());
    toReconnect = true;
  }

  if (toReconnect) {
    connect();
  }

  device.loop();

  // Losant uses a JSON protocol. Construct the simple state object.
  // { "button" : true }
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject &root = jsonBuffer.createObject();

  int value = analogRead(hygrometer);  // Read analog value
  value = constrain(value, 400, 1023); // Keep the ranges!
  value = map(value, 400, 1023, 100,
              0); // Map value : 400 will be 100 and 1023 will be 0

  Serial.print("Soil humidity: ");
  Serial.print(value);
  Serial.println("%");

  root["humidity"] = value;

  // Send the state to Losant.
  device.sendState(root);

  // Pause 10 minutes between measurements
  delay(600000);
}

Project is available in GitHub

git clone https://github.com/beastovest/soil-hygrometer.git

Connected to your ESP8266, on the right in Platform IO there is an arrow button > with tooltip Upload, press it, the upload should take few seconds

In case you get similar message

/dev/ttyUSB0: Access denied

You need to give rights to your current user to read ttyUSB0 port

If you have any questions or suggestions, feel free to leave a comment.

Become a Patron!