2019年2月10日 星期日

IOT with Firebase : Control Led with Android App

源自於https://www.hackanons.com/2018/03/iot-with-firebase-control-led-with.html

IOT with Firebase : Control Led with Android App #Part3

I continue teaching IOT with Firebase through problem solving. In this video I'll show step by step guide to create Android App using Android studio for controlling LED which is connected to Arduino NodeMCU.  

I think learning through doing is the best and the problems will keep getting more complex. The code follows the video below to help.




If you like videos like this consider donating $1, or simply turn off AdBlocker. Either helps me to continue making tutorials


Transcript / Cheat Sheet

First set the internet permission for our app for which need to add this code after tag .

Manifest.xml



<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.led_control" >
<uses-permission android:name="android.permission.INTERNET" />
<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>
After setting the permission we need to create layout for our application so let's check out XML code.

activity_main.xml


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.root.led_control.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:text="IOT CONTROL LED"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/textView" />
<Button
android:id="@+id/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp"
android:text="ON" />
<Button
android:id="@+id/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OFF"
android:layout_alignBaseline="@+id/on"
android:layout_alignBottom="@+id/on"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
</RelativeLayout>

Now, at last here is our java code i.e Main_Activity.

Main Activity.java



package com.example.root.led_control;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
Button on;
Button off;
@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);
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);
}
});
}
}

At last we are end up with great tutorial on IOT home automation using Firebase , NodeMCU & Android App.

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...