Nedzib Sastoque Rangel
Published © GPL3+

A Crib for the Care of Babies with Android Remote Alert

A device for baby crib that measures parameters such as temperature, humidity or others, whit monitoring using firebase on Android.

IntermediateShowcase (no instructions)5 hours1,752
A Crib for the Care of Babies with Android Remote Alert

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Android device
Android device
×1

Software apps and online services

Firebase
Google Firebase
Android Things
Google Android Things

Story

Read more

Schematics

DHT Conection

Code

Android App

Java
App for supervise data of the cot
package nedzib.firebaseuno;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {
    EditText eth,ett,humin,humax,temin,temax;
    int hmi=0,hma=0,tmi=0,tma=0;
    //Notification
    NotificationCompat.Builder noti;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //
        final int notid=001;
        noti=
                (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setSmallIcon(android.R.drawable.ic_dialog_alert)
                        .setContentTitle("Baby Care")
                        .setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, 6)
                        .setContentText("Data outside of secure limits");
        //
        Intent resultIntent = new Intent(this, MainActivity.class);
        PendingIntent resultPendindIntent=
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        noti.setContentIntent(resultPendindIntent);
        //
        final NotificationManager notima=
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        eth=(EditText)findViewById(R.id.editText);
        ett=(EditText)findViewById(R.id.editText2);
        humin=(EditText)findViewById(R.id.ethmi);
        humax=(EditText)findViewById(R.id.ethma);
        temin=(EditText)findViewById(R.id.ettmi);
        temax=(EditText)findViewById(R.id.ettma);
        FirebaseDatabase  Datab=FirebaseDatabase.getInstance();
        ///Principal Lectures
        DatabaseReference  ref=Datab.getReference("Humidity");
        DatabaseReference  ref1=Datab.getReference("Temperature");
        //threshold lectures
        final DatabaseReference  tmin=Datab.getReference("tmin");
        final DatabaseReference  tmax=Datab.getReference("tmax");
        final DatabaseReference  hmin=Datab.getReference("hmin");
        final DatabaseReference  hmax=Datab.getReference("hmax");
        //Single Listen Threshold From Firebase
        hmin.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                hmi=dataSnapshot.getValue(Integer.class);
                humin.setText(Integer.toString(hmi));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("ERROR", databaseError.getMessage());
            }
        });
        hmax.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int hma=dataSnapshot.getValue(Integer.class);
                humax.setText(Integer.toString(hma));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("ERROR", databaseError.getMessage());
            }
        });
        tmin.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int tmi=dataSnapshot.getValue(Integer.class);
                temin.setText(Integer.toString(tmi));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("ERROR", databaseError.getMessage());
            }
        });
        tmax.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int tma=dataSnapshot.getValue(Integer.class);
                temax.setText(Integer.toString(tma));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("ERROR", databaseError.getMessage());
            }
        });
        //EditText Listener and Send to Firebase
        humin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                hmi=Integer.parseInt(humin.getText().toString());
                hmin.setValue(hmi);
            }
        });
        humax.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                hma=Integer.parseInt(humax.getText().toString());
                hmax.setValue(hma);
            }
        });
        temin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tmi=Integer.parseInt(temin.getText().toString());
                tmin.setValue(tmi);
            }
        });
        temax.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tma=Integer.parseInt(temax.getText().toString());
                tmax.setValue(tma);
            }
        });
        //EventListener Value read from Firebase
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int h = dataSnapshot.getValue(Integer.class);
                eth.setText(Integer.toString(h));
                if (h<hmi || h>hma){
                    notima.notify(notid, noti.build());}
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("ERROR", databaseError.getMessage());
            }
        });
        ref1.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int t = dataSnapshot.getValue(Integer.class);
                ett.setText(Integer.toString(t));
                if (t<tmi || t>tma){
                    notima.notify(notid, noti.build());}
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e("ERROR", databaseError.getMessage());
            }
        });

    }
}

NodeMCU Code

C/C++
Code for upload data to firebase
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <FirebaseArduino.h>

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
 
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor


const char* ssid = "**********";  //your ssid
const char* pass = "**********"; //your password

const char* base       = "myurl.firebaseio.com"; //your URL project Firebase
const char* auth       = "***************"; //your Auth password

const int DHTPin = 10;     // what digital pin we're connected to
 
DHT dht(DHTPin, DHTTYPE);
 
void setup() {
   Serial.begin(9600);
   dht.begin();
   Firebase.begin(base, auth);
}
 
void loop() {
    /////////////////////////////Conect to WiFi//////////
  Serial.print("Conect to: "); 
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Conected");
  
 while(true){
   delay(10);
 
   float h = dht.readHumidity();
   float t = dht.readTemperature();
 
//   if (isnan(h) || isnan(t)) {
//      Serial.println("Failed to read from DHT sensor!");
//      return;
//   }
 
   Serial.print("\nHumidity: ");
   Serial.print(h);
   Serial.print(" %\t");
   Serial.print("Temperature: ");
   Serial.print(t);
   
   Firebase.setInt("Humidity", h);
          if (Firebase.failed()) {
            Serial.print("failed");
            Serial.println(Firebase.error()); 
            return;
          }else{Serial.println("\nVar Ok ");}


      Firebase.setInt("Temperature", t);
          if (Firebase.failed()) {
            Serial.print("failed");
            Serial.println(Firebase.error()); 
            return;
          }else{Serial.println("Var Ok");}
}}

Credits

Nedzib Sastoque Rangel

Nedzib Sastoque Rangel

6 projects • 13 followers
#UDistrital Electronics student, I love programming, domotic, create prototypes and learn new things

Comments