Jerry Zhang
Created May 27, 2019 © CC BY

Home Monitoring System

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 hours43
Home Monitoring System

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

Main 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' # Be sure to replace the client ID from the Imgur API here
api_key = 'api_key' # Be sure to replace the API key from the Imgur API here
headers = {"Authorization": "Client-ID client_id"} 

# Use OpenCV to capture and save an image from a webcam.
cam = VideoCapture(0) 
s, img = cam.read()
if s:
    imwrite("1.jpg", img)

# Upload the image taken to Imgur
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"] # Retrieve the URL of the uploaded image

# Post the URL using MQTT to PubNub via Soracom
subprocess.Popen(["mosquitto_pub", "-h", "beam.soracom.io", "-p", "1883", "-t", "image", "-m", link], stdout=subprocess.PIPE)

Main Activity Layout (Android App Code)

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