Keep Me Clean

A sensor That detects when a diaper needs changing.

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1

Story

Schematics

Code

client

Arduino

#include <SPI.h>
#include <WiFi101.h>

int sensorPin = A1;    // select the input pin for the potentiometer
int sensorValue = 0; 
int sentcounter = 0;
char ssid[] = "myssid"; //  your network SSID (name)
char pass[] = "mykey";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
char server[] = "my.server.com";  
WiFiClient client;

void setup() {
  sentcounter = 0;
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();
  
}

void loop() {

  
  sensorValue = analogRead(sensorPin);
  if(sensorValue<500){
    sentcounter = 0;
    
      Serial.print("Low: ");
      Serial.print(sensorValue);
      Serial.println();
  }
  else{
    Serial.print("Still up but no post ");
      Serial.print(sensorValue);
      Serial.println();
    sentcounter +=1;
    if (sentcounter == 10)
    {
      Serial.print("Post: ");
      Serial.print(sensorValue);
      Serial.println();
      postData();
      sentcounter = 0;
    }
    
  }
  delay(1000);
  
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}


void postData() {
  // Combine yourdatacolumn header (yourdata=) with the data recorded from your arduino
  // (yourarduinodata) and package them into the String yourdata which is what will be
  // sent in your POST request
  String yourdata = "poop=poop";

  // If there's a successful connection, send the HTTP POST request
  if (client.connect(server, 80)) {
    Serial.println("connecting...");

    // EDIT: The POST 'URL' to the location of your insert_mysql.php on your web-host
    client.println("POST /path/poop.php HTTP/1.1");

    // EDIT: 'Host' to match your domain
    client.println("Host: my.server.com");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    client.print("Content-Length: ");
    client.println(yourdata.length());
    client.println();
    client.println(yourdata); 
  } 
  else {
    // If you couldn't make a connection:
    Serial.println("Connection failed");
    Serial.println("Disconnecting.");
    client.stop();
  }
}

Source : Keep Me Clean


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top