Yuanyuan
Published

Internet of Diaper

Have you ever thought of an AI diaper with monitor function? Now Using sensors we could make ELDERLY &BABY CARE even Smarter!

IntermediateShowcase (no instructions)4 hours1,497
Internet of Diaper

Things used in this project

Hardware components

SenseCAP K1100 - The Sensor Prototype Kit with LoRa® and AI
Seeed Studio SenseCAP K1100 - The Sensor Prototype Kit with LoRa® and AI
×1

Software apps and online services

Ubidots
Ubidots
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Inflatable baby
Diapers

Story

Read more

Code

io diaper

Arduino
#include <PubSubClient.h>
#include <rpcWiFi.h>
#include <Wire.h>


/*Office WiFi*/
#define WIFISSID "SEEED-MKT" // Put your WifiSSID here
#define PASSWORD "depot0518" // Put your wifi password here

#define TOKEN "BBFF-8kv6qgJsV4lPnZetlr2GuUbovcM2o3"
#define VARIABLE_LABEL1 "soil" // Assign the variable label
#define DEVICE_LABEL "wio-terminal" // Assign the device label
#define MQTT_CLIENT_NAME "WLKDJH2EHC" // MQTT client Name

char mqttBroker[] = "industrial.api.ubidots.com";

WiFiClient wifiClient;
PubSubClient client(wifiClient);

uint8_t state = 0;

int sensorPin = A0;
static int soilValue = 0;

static int cont = 0;


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);
    }
  }
}


void read_soil() //read moisture value
{
  soilValue = analogRead(sensorPin);
  Serial.print("Moisture = ");
  Serial.println(soilValue);
  if (soilValue > 500) cont = 2;
  if (soilValue < 450) cont = 1;
}

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_LABEL1); // 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() {
  Wire.begin();
  Serial.begin(9600);

  while (!Serial);


  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    WiFi.begin(WIFISSID, PASSWORD);
  }

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  WiFi.begin(WIFISSID, PASSWORD);

  delay(1000);
  client.setServer(mqttBroker, 1883);
  client.setCallback(callback);

}

void loop() {
  read_soil();
  send_data();
}

Credits

Yuanyuan

Yuanyuan

2 projects • 9 followers
Creative designer & Multimedia artist 💗👾
Thanks to Yuanyuan W.

Comments