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.

IOT image

Code Below Follows the video to help set IOT : 

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. 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 a 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. It 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. 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.

Categorized in: