Sam Stevens
Published © GPL3+

Hexabitz Home Automation

Universal Home Automation using modular Hexabitz PCB's and AI

IntermediateShowcase (no instructions)10 hours1,263
Hexabitz Home Automation

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Hexabitz Solid State Relay Module (H0FR60)
Hexabitz Solid State Relay Module (H0FR60)
×1
SparkFun XBee Explorer Dongle
SparkFun XBee Explorer Dongle
×1
Xbee 802.15.4
×1
AC-DC 3.3 V
×1

Software apps and online services

Maker service
IFTTT Maker service
Adafruit

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Hexabitz Relay Schematic

This is a general schematic for connecting the individual components of the hardware design

Code

Raspberry Pi Code

Python
Links the Zigbees and Adafruit accounts
from Adafruit_IO import Client, Feed, Data
import time
import serial
from digi.xbee.devices import XBeeDevice, RemoteXBeeDevice,XBee64BitAddress

# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'insert personal account key'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username and key)
ADAFRUIT_IO_USERNAME = 'insert username'

# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

regStatus= [0,0] #Registering the last value saved on pi
updatedStatus= [0,0] #The updated status from AdaFruit
nameOfDevice=['lamp','fan'] #Name of the device
hexNames=['1002','1003'] #The Hexabit serial number
checkStatus= [0,0]
counter=1 #useless for now lol


xbee = XBeeDevice('/dev/ttyUSB0',9600) #identifying the plugged Xbee to the pi
xbee.open() #Starting the Xbee network

#Now we are creating 2 Xbee objects that will only talk to a partiuler Xbee
# As we are doing Unite casting
#The first is the lamp
hexBee1 = RemoteXBeeDevice(xbee, XBee64BitAddress.from_hex_string("0013A200417F1518"))
#The second is the fan
hexBee2 = RemoteXBeeDevice(xbee, XBee64BitAddress.from_hex_string("0013A200417F10DC"))


toggle = bytearray([0x05, 0x00, 0x01, 0x75, 0xDE, 0x75])


try: # loading the acssess to the feeds
    for index in range(0,2):
        string=nameOfDevice[index]
        digital = aio.feeds(string)
        
except RequestError: # create a digital feed
    feed = Feed(name=string)
    digital = aio.create_feed(feed)
    
ticks = 0
    
while True:
    #Updating the status of the electronic switches from ada fruit (from web or AI changes)  
    for index in range(0,2):
        ReceivedData=aio.receive(nameOfDevice[index]).value
        updatedStatus[index]= ReceivedData
    
    checkStatus = [0,0]

    #To compare the status of the recorded valiue with the most updated one
    for index in range(0,2):
        if int(regStatus[index]) > int(updatedStatus[index]):
            #print('turning off the'+ (nameOfDevice[index]))
            if index ==0:
                xbee.send_data(hexBee1, toggle, 1)
            if index==1:
                xbee.send_data(hexBee2, toggle, 1)
            #After turning them off, we register the value in the regStatus list
            regStatus[index]= updatedStatus[index]
        if int(regStatus[index]) < int(updatedStatus[index]):
            #print('turning on the '+ (nameOfDevice[index]))
            if index ==0:
                xbee.send_data(hexBee1, toggle, 1)
            if index==1:
                xbee.send_data(hexBee2, toggle, 1)
            #After turning them off, we register the value in the regStatus list
            regStatus[index]= updatedStatus[index]
#### Below is the part I mean

# you can take it off because it's not tested
#That is the bone code to check the status from the switches
    
    for numberOftimes in range(0,10):
        # ______--------___________ check the syntax for reciving a message from the
        #switches to the pi, I am checking 10 messages which is more than expected
        # Check the status of each switch with a timeout of 5 seconds.
        # Each switch should send its staus every second. This loop will
        # constantly check for the status of each swtich
        messageRecived = xbee.read_data()
        if messageRecived != None:
            switchNumberDeticted = messageRecived.data.decode("utf8")[3:7]
            
            theStatusOfTheSwitch = messageRecived.data.decode("utf8")[7:10]
            if switchNumberDeticted == hexNames[0]:
                checkStatus[0]=1
            if switchNumberDeticted == hexNames[1]:
                checkStatus[1]=1
       
            
                # iterate the counter and create a time delay before checking the aio
        
        ### at this point we should have marked 1 to every pluged switch
                                
                
        
    for theValue in checkStatus:
        theIndexOfDisconnectedSwitch=  checkStatus.index(theValue)
        print (theValue)
        if theValue == 0 and updatedStatus[theIndexOfDisconnectedSwitch] == '1':       
            theDisconnectedSwitchName= nameOfDevice[theIndexOfDisconnectedSwitch]
            aio.send(theDisconnectedSwitchName, 0)
            print("bro")

Hexabitz Base Level Code

C#
Code for Hexabitz Relay
/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
 
/*
  MODIFIED by Hexabitz for BitzOS (BOS) V0.1.5 - Copyright (C) 2017-2018 Hexabitz
    All rights reserved
*/
/* Includes ------------------------------------------------------------------*/
#include "BOS.h"

/* Private variables ---------------------------------------------------------*/
 char test[13], status[13];
 char *stp = status;
 char module[5];
 char *myModID = module;
 int network, transLast;
 
/* Private function prototypes -----------------------------------------------*/
void relayTransmitCheck(void)
{
  //Check the current state of the relay
  //If OFF and the last state transmitted was ON, transmit the new status
  if(SSR_State == STATE_OFF){
   strcpy(stp,"REG");
   strcat(stp,myModID);
   strcat(stp,"OFF\n");
   HAL_UART_Transmit(GetUart(P3),(uint8_t *) status,128,100);
   transLast = 0;
  }
  //If ON and the last state transmitted was OFF, transmit the new status
  if(SSR_State == STATE_ON){
   strcpy(stp,"REG");
   strcat(stp, myModID);
   strcat(stp,"ONN\n");
   HAL_UART_Transmit(GetUart(P3),(uint8_t *) status,128,100);
   transLast = 1;
  }
}

/* Main functions ------------------------------------------------------------*/
int main(void)
{

  /* MCU Configuration----------------------------------------------------------*/
  /* Reset all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();
  /* Initialize all user peripherals */
 /* Initialize BitzOS */
 BOS_Init();
  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();
  /* Start scheduler */
  osKernelStart();
 
  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  while (1)
  {

  }

}
/*-----------------------------------------------------------*/
/* FrontEndTask function */
void FrontEndTask(void * argument)
{
 //Used to clear test button from memory, only needed for module 1
 
 //set module ID for this specific Module, unique for each Heaxbitz
 strcpy(module, "1002");
 
 //Boot up delay, used for system reset, also needed to allow Zigbee time to boot up
 Delay_s(5);
 // Initial status transmit and update (testing)
 // Replace with Network Update moethod
 //SendMessageFromPort(P3,01,00,CODE_H0FR6_TOGGLE,0);
  for(;;)
  {
  // With network established, this is the only function the module should be checking.
  relayTransmitCheck();
  
  // Stability Delay
  Delay_ms(500);
 }
}
/*-----------------------------------------------------------*/
/************************ (C) COPYRIGHT HEXABITZ *****END OF FILE****/

Credits

Sam Stevens

Sam Stevens

1 project • 1 follower
B.S. in Electrical Engineering. Hardware and IoT Enthusiast
Thanks to Jignesh Trivedi, Mohammad Almousa, and Robert Menke.

Comments