melodygong
Published

Kid's Clothing Humidity Real-Time Monitor

Connect the moisture sensor with Wio Terminal, send data to Ubidots, and monitor the kid's clothes' humidity in real time.

BeginnerShowcase (no instructions)216
Kid's Clothing Humidity Real-Time Monitor

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1
Seeed Studio Grove - Soil Moisture Sensor
×1
Seeed Studio Grove - Universal 4 Pin Buckled 5cm Cable (5 PCs Pack)
Seeed Studio Grove - Universal 4 Pin Buckled 5cm Cable (5 PCs Pack)
×1

Software apps and online services

Ubidots
Ubidots
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

children's clothes

Story

Read more

Code

Monitor the dryness of children's clothes

Arduino
#include <PubSubClient.h>
#include <rpcWiFi.h>
#include <TFT_eSPI.h>
 
//Required Information
#define WIFISSID "" // Put your WifiSSID here
#define PASSWORD "" // Put your wifi password here
#define TOKEN "" // Put your Ubidots' TOKEN
#define VARIABLE_LABEL "Humidity of clothing" // Assign the variable label
#define DEVICE_LABEL "wio-terminal" // Assign the device label
#define MQTT_CLIENT_NAME "mees2pivm1" // MQTT client Name
 
const long interval = 100;
unsigned long previousMillis = 0;
 
TFT_eSPI tft;
 
char mqttBroker[] = "industrial.api.ubidots.com";
 
WiFiClient wifiClient;
PubSubClient client(wifiClient);
 
//Soil moisture pins and sensor values
int sensorPin = A0;
static int soilValue = 0;
 
// Space to store values to send
char str_soil[6];
char payload[700];
char topic[150];
 
void callback(char* topic, byte* payload, unsigned int length){
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
}
 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
 
  // Attempt to connect
  if (client.connect(MQTT_CLIENT_NAME, TOKEN,"")) {
    Serial.println("connected");
  }
  else {
    Serial.print("failed, rc=");
    Serial.print(client.state());
    Serial.println(" try again in 2 seconds");
    // Wait 2 seconds before retrying
    delay(2000);
    }
  }
}
 
//Reading soil moisture sensor values
void read_soil()
{
  soilValue = analogRead(sensorPin);
  Serial.print("Moisture = ");
  Serial.println(soilValue);
}
 
//Sending data to Ubidots
void send_data()
{
  dtostrf(soilValue, 4, 0, str_soil);
 
  if (!client.connected()) {
    reconnect();
  }
 
  // Builds the topic
  sprintf(topic, "%s", ""); // Cleans the topic content
  sprintf(topic, "%s%s", "/v2.0/devices/", DEVICE_LABEL);
 
  //Builds the payload
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label
  sprintf(payload, "%s%s", payload, str_soil); // Adds the value
  sprintf(payload, "%s}", payload); // Closes the dictionary brackets
 
  client.publish(topic, payload);
  delay(500);
 
  client.loop();
}
 
 
void setup() {
  Serial.begin(115200);
 
  tft.begin();
  tft.setRotation(3);
  tft.setTextSize(2);
  tft.fillScreen(TFT_BLACK);
 
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
 
  tft.drawString("Connecting to WiFi...",20,120);
  WiFi.begin(WIFISSID, PASSWORD);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    WiFi.begin(WIFISSID, PASSWORD);
  }
 
  tft.fillScreen(TFT_BLACK);
  tft.drawString("Connected to the WiFi",20,120);
 
  delay(1000);
  client.setServer(mqttBroker, 1883);
  client.setCallback(callback);
}
 
void loop() {
  read_soil();   //Reading soil moisture sensor values
  send_data();   //Sending data to Ubidots
  delay(5000);
}

Credits

melodygong
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.