Jerry Zhang
Created May 22, 2019 © CC BY

Smart Home Monitor

Did you turn off the stove, close the garage door, get the mail, or lock your front door? Check remotely via an Android app!

IntermediateFull instructions provided4 hours29
Smart Home Monitor

Things used in this project

Hardware components

SORACOM Air Global IoT SIM
SORACOM Air Global IoT SIM
×1
Huawei 3G USB Dongle (MS2131i)
×1
Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
Any Raspberry Pi model with wifi can work as well. I installed Raspbian onto a 16GB microSD card to run on this Raspberry Pi.
×1
Webcam, Logitech® HD Pro
Webcam, Logitech® HD Pro
Any AVC Compatible Webcam should work in this project
×1

Software apps and online services

SORACOM Beam - Data Transfer Support
SORACOM Beam - Data Transfer Support
PubNub Publish/Subscribe API
PubNub Publish/Subscribe API
Android Studio
Android Studio
OpenCV
OpenCV
Imgur API

Story

Read more

Code

Python Script

Python
This is the main script to run to take images, publish the images to Imgur and send the URL to PubNub.
import base64
import json
import requests
import sys
import subprocess
import time
import signal
from base64 import b64encode
from cv2 import *

client_id = 'client_id'
headers = {"Authorization": "Client-ID client_id"}
api_key = 'api_key'

cam = VideoCapture(0)
s, img = cam.read()
if s:
    imwrite("1.jpg", img)

url = "https://api.imgur.com/3/upload.json"
j1 = requests.post(
    url,
    headers = headers,
    data = {
        'key': api_key,
        'image': b64encode(open('1.jpg', 'rb').read()),
        'type': 'base64',
        'name': '1.jpg',
        'title': 'p1'
    }
)
link = j1.json()["data"]["link"]

subprocess.Popen(["mosquitto_pub", "-h", "beam.soracom.io", "-p", "1883", "-t", "image", "-m", link], stdout=subprocess.PIPE)

Main Activity Layout

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        app:srcCompat="@drawable/image1" />

    <ImageView
        android:id="@+id/image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        app:srcCompat="@drawable/image2" />

    <ImageView
        android:id="@+id/image3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        app:srcCompat="@drawable/image3" />

</RelativeLayout>

Credits

Jerry Zhang

Jerry Zhang

11 projects • 15 followers
Thanks to PubNub.

Comments