Aula πŸ’‘πŸ•ŠοΈ
Published © MIT

Raspberry Pi Home Security System with Hexabitz Modules

In this project, we will be combining the power of Raspberry Pi, Hexabitz, and a Pi camera to create a motion detection camera system.

IntermediateFull instructions provided2 hours786

Things used in this project

Story

Read more

Schematics

Block Diagram

Hexabitz Array Block Diagram

Code

H0AR9x-main code

C/C++
/*
 BitzOS (BOS) V0.2.9 - Copyright (C) 2017-2023 Hexabitz
 All rights reserved

 File Name     : main.c
 Description   : Main program body.
 */
/* Includes ------------------------------------------------------------------*/
#include "BOS.h"

/* Private variables ---------------------------------------------------------*/
uint16_t motion;
char data_to_send[10];



/* Main function ------------------------------------------------------------*/

int main(void){

	Module_Init();		//Initialize Module &  BitzOS



	//Don't place your code here.
	for(;;){}
}

/*-----------------------------------------------------------*/

/* User Task */
void UserTask(void *argument){



	// put your code here, to run repeatedly.
	while(1){
		SampleDistance(&motion);

		sprintf(data_to_send,"%f", motion);
		messageParams[0] = 8; //length
		memcpy(&messageParams[1],&data_to_send,8);
		SendMessageToModule(2,CODE_H1DR5_Ethernet_Send_Data,9);
		Delay_ms(50);
		IND_blink(100);


	}
}

/*-----------------------------------------------------------*/

topology.h

C/C++
/*
 BitzOS (BOS) V0.2.9 - Copyright (C) 2017-2023 Hexabitz
 All rights reserved

 File Name     : topology.h
 Description   : Array topology definition.

 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __topology_H
#define __topology_H
#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32g0xx_hal.h"

#define __N	2					// Number of array modules

// Array modules
#define _mod1	1<<3
#define _mod2	2<<3

// Topology
static uint16_t array[__N ][7] ={
	{_H0AR9,  _mod2 | P5, 0, 0, 0, 0, 0}, 								 // Module 1
	{_H1DR5,0, 0,0, 0, _mod1 | P1, 0},					    			 // Module 2
};

// Configurations for duplex serial ports
#if ( _module == 1 )
	#define	H0AR9	1
	#define	_P1pol_normal	1
	#define	_P2pol_normal	1
	#define	_P3pol_normal	1
	#define	_P4pol_normal	1
	#define	_P5pol_normal	1
	#define	_P6pol_normal	1
#endif

#if ( _module == 2 )
	#define	H1DR5			1
	#define	_P1pol_normal	1
	#define	_P2pol_normal	1
	#define	_P3pol_normal	1
	#define	_P4pol_normal	1
	#define	_P5pol_reversed	1
	#define	_P6pol_normal	1

#endif


#ifdef __cplusplus
}
#endif
#endif /*__ topology_H */

/************************ (C) COPYRIGHT HEXABITZ *****END OF FILE****/

H1DR5x-main code

C/C++
/*
 BitzOS (BOS) V0.2.9 - Copyright (C) 2017-2023 Hexabitz
 All rights reserved

 File Name     : main.c
 Description   : Main program body.
 */
/* Includes ------------------------------------------------------------------*/
#include "BOS.h"


/* Private variables ---------------------------------------------------------*/
    uint8_t myGateway[4]={192,168,1,100};
	uint8_t mySubnet[4]={255,255,255,0};
	uint8_t myIP[4]={192,168,1,101};
	uint8_t Mac_addres[6]={0x07,0x6,0x2,0x3,0x4,0x5};

/* Private function prototypes -----------------------------------------------*/
	//uint8_t Mac_addres[6]={0xb8,0x27,0xeb,0xfd,0x80,0x64};

/* Main function ------------------------------------------------------------*/

int main(void){
	Set_Local_mac_addr(Mac_addres); // Mac_addr ethernet

	Module_Init();		//Initialize Module &  BitzOS

	//Don't place your code here.
	for(;;){}
}

/*-----------------------------------------------------------*/

/* User Task */
void UserTask(void *argument){
	Set_Local_IP(myIP);  // ip ethernet
	Set_SubnetMask(mySubnet);	// SubnetMask laptop
	Set_Remote_IP(myGateway); //ip laptop
	Set_Local_PORT(35);// port ethernet
	Set_Remote_PORT(34);//port laptop
    Set_reseve_mac_and_ip_Remote();

	// Defalt_Value();
		// put your code here, to run repeatedly.
	while(1){


	}
}

/*-----------------------------------------------------------*/

Test code to receive UDP messages in Python 3

Python
import socket
UDP_IP = "198.1.168.100"
UDP_PORT = 34

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
    data, addr = sock.recvfrom(1024) 
    print("received message: %s" % data)

Home Security Camera with Hexabitz Modules Code

Python
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server
import socket

UDP_IP = "192.168.1.100"
UDP_PORT = 34

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))


PAGE="""\
<html>
<head>
<title>Raspberry Pi - Home Security Camera with Hexabitz Modules</title>
</head>
<body>
<center><h1>Raspberry Pi - Home Security Camera with Hexabitz Modules</h1></center>
<center><img src="stream.mjpg" width="1280" height="720"></center>
</body>
</html>
"""

class StreamingOutput(object):
    def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = Condition()

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            #new frame, copy the existing buffer's content and
            #notify all clients it's available
            self.buffer.truncate()
            with self.condition:
                self.frame = self.buffer.getvalue()
                self.condition.notify_all()
            self.buffer.seek(0)
        return self.buffer.write(buf)
global data,a,addr
class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        global data,a,addr
        if self.path == '/':
            self.send_response(301)
            self.send_header('Location', '/index.html')
            self.end_headers()
        elif self.path == '/index.html':
            content = PAGE.encode('utf-8')
            self.send_response(200)
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
            self.end_headers()
            self.wfile.write(content)
        elif self.path == '/stream.mjpg':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type','multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    
                    with output.condition:
                        output.condition.wait()
                        frame = output.frame
                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e:
                logging.warning(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()

class StreamingServer(socketserver.ThreadingMixIn,server.HTTPServer):
    allow_reuse_address = True
    daemon_threads = True

with picamera.PiCamera(resolution='1280x720', framerate=24) as camera:
    camera.rotation = 180
    output = StreamingOutput()
    camera.start_recording(output, format='mjpeg')
    try:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print(addr)
        print("received message: %s" % data)
        a = data.decode()
        print(a)        
        address = ('192.168.1.104',24)
        if (len(a) >0):
            server = StreamingServer(address, StreamingHandler)
            server.serve_forever()
    finally:
        camera.stop_recording()

H0AR9x-Firmware

H1DR5x-Firmware

Credits

Aula πŸ’‘πŸ•ŠοΈ

Aula πŸ’‘πŸ•ŠοΈ

54 projects β€’ 215 followers
Electronic Engineering

Comments