Header Ads

IOT with Firebase show Sensor Data to Android App

IOT is very hot topic when talking of current technology although it started long ago in 1999 by Kevin Ashton for the company P&G. Kevin cool name na? It reminds me of me ! he-he. Now back to IOT, so basic idea of IOT is to connect dumb things that are around us to a network to make things smart. We can’t make people around us smart but we can makes things smarter irony! You can say you are implementing IOT when you have some sort of (small) processor in your device that has interaction with environment human or animal , device can be multiple there is no limit to numbers here, when these devices are connected to internet of network of any sort that you are doing something with IOT.

Code Below Follows the video to help : 




Sole motive of IOT is to collect and transfer data from one end to another for clarity I would like to include terms like sensors, actuators, processors/controllers, storage. Now sensor is some device that will collect data it will require a local processor to process data or more importantly send data to some storage that will be firebase database in our case. The processor/controllers here will be Arduino NodeMCU, Sensor we are using here is Ultrasonic sensor and one more device LED that will work as are in-house testing device will share processor with ultrasonic sensor. Work of LED will be very simple glow or not glow our ultrasonic sensor will sense nearby object, we can use any sensor depending upon what we would like to monitor or work with here we will sense object and notify user.

Let's See the code for Getting Data from Sensor to Arduino and update it to firebase as Real-time Database:
#include <ESP8266WiFi.h> #include <FirebaseArduino.h>
// Set these to run example.
#define FIREBASE_HOST "esri-eea51.firebaseio.com"
#define FIREBASE_AUTH "qULRQbnuzJ3qkAdYunMasgcLlKtpXic1UQ1FxHvM"
#define WIFI_SSID "kavin"
#define WIFI_PASSWORD "1123581321"
const int trigPin = 15; //D4
const int echoPin = 12; //D3
long duration;int distance;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(16, OUTPUT); //D0
pinMode(0, INPUT); //D3
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.set("LED_STATUS", 0);
Firebase.setInt("distance", 0);
}
int n = 0;
void loop() {
/*
* For Utlrasonic sensor distance measurement
*/
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Firebase.setInt("distance",distance);
delay(2000);
/*
* Led Control Home Automation
*/
n = Firebase.getInt("LED_STATUS");
// handle error
if (n==1) {
Serial.println("LED ON");
digitalWrite(16,HIGH);
return;
//delay(10);
}
else {
Serial.println("LED OFF");
digitalWrite(16,LOW);
}
}

Notifying user will require an interface through which user will get to know what our sensor has to tell us for this we will build an very simple Android app that we have built once for home automation. This app will let display the distance of the object to the sensor and can also be used to generate some kind of alert if it is required. The app will collect data from the firebase that is our cloud storage and this cloud storage is being updated on real time by our Arduino NodeMCU with the help of our Ultrasonic Sensor.

Android App Code:


Main Activity.java :
import android.os.Bundle; package com.example.root.fire_major_notification;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
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 {
Button on;
Button off;
TextView distance;
DatabaseReference dref;
String status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
on = (Button) findViewById(R.id.on);
off = (Button) findViewById(R.id.off);
distance = (TextView) findViewById(R.id.textView3);
dref= FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
status=dataSnapshot.child("distance").getValue().toString();
distance.setText(status);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
on.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("LED_STATUS");
myRef.setValue(1);
}
});
off.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("LED_STATUS");
myRef.setValue(0);
}
});
}
}


MainActivity.xml :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.fire_major_notification">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

So, that's all for mapping your sensor data to Android app using firebase database and Arduino NodeMCU.

5 comments:

  1. Access to computers and other control systems which might provide you with information about the way the world revolves around technology should be unlimited and total. All information should be free and accessible to all. Seeing how you are being monitored by most of these tech companies. And also needing cyber help in many areas of your day to day activities, we at INTEGRATEDHACKS have come come up with a team of highly motivated and dedicated hackers to help you get access to information you are being deprived of and also protect you when necessary. Our services include and are not limited to hacking of social media accounts,email accounts, tracking of phones hacking of bank cards and many more.
    Have you ever been hacked? Need to recover your stolen account, Want to monitor your kids,spouse or partner, Change your school results, track messages from an email or mobile number, secure your email accounts, phones, bank accounts and many more, INTEGRATEDHACKS is the one for you. Hundreds of our clients have their jobs done consistently and efficiently in no time. Our professional hackers for hire team is highly qualified and can hack anything or device you desire without giving the target any form of notification which makes us one of the best.
       Are you having doubts about your partner ? Worry no more. All your doubts would be cleared.


    ★ Contact Us today For Your Desired Hacking services via : integratedhacks@protonmail.com and experience cyber hacking like never before.

    ReplyDelete
  2. I know a professional hacker named james who has worked for me this week. He offers very legitimate services such

    as clearing of bad records online without being traced back to you, He clone/hack mobile phones, hack Facebook

    account, instagram, WhatsApp, emails, Twitter, bank accounts, Skype, FIXES CREDIT REPORTs, track calls. He also

    help retrieve accounts that have been taking by hackers. His charges are affordable, reliable and 100% safe. For

    his job well done this is my own way to show appreciation, Contact him via address below...
    Email...hackintechnology@gmail. com
    Text no..+1(669) 225-2253

    ReplyDelete
  3. Hello there, i am on here for those interested in hack related services like phone taps to catch cheating spouse, email hacks, website hack, credit score repair, funding programs,clear criminal records , proof to help win court cases, DMV or any problem you feel no one can resolve. I met Andrew Alan a hacker who helped me with countless projects that came out succesfully and so i decided to vouch for his service and help increase his client base. If you feel you have anything disturbing you and you neeed someone discreet to help you out??? Contact icefoghackersorg@gmail.com or text +1-616-422-7025. I hope this helps you all on here.

    ReplyDelete
  4. I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed... odzyskiwanie danych z uszkodzonego telefonu

    ReplyDelete
  5. Hello all
    am looking few years that some guys comes into the market
    they called themselves hacker, carder or spammer they rip the
    peoples with different ways and it’s a badly impact to real hacker
    now situation is that peoples doesn’t believe that real hackers and carder scammer exists.
    Anyone want to make deal with me any type am available but first
    I‘ll show the proof that am real then make a deal like

    Available Services

    ..Wire Bank Transfer all over the world

    ..Western Union Transfer all over the world

    ..Credit Cards (USA, UK, AUS, CAN, NZ)

    ..School Grade upgrade / remove Records

    ..Spamming Tool

    ..keyloggers / rats

    ..Social Media recovery

    .. Teaching Hacking / spamming / carding (1/2 hours course)

    discount for re-seller

    Contact: 24/7

    fixitrogers@gmail.com

    ReplyDelete

Powered by Blogger.