Tech

Intro to Home Assistant with Raspberry Pi 4

Intro to Home Assistant with Raspberry Pi 4

Installation of Home Assistant on Raspberry Pi 4

Introduction

Home Assistant is an open-source home automation service that enables local control with security. It is managed and maintained by worldwide developers community. Since the launch home assistant has come a long way with support for more than 2900+ integrations available at the time of writing. It has a robust documentation and community support which makes it an ideal home automation choice for those who would like to move away from cloud based automation or the hobbyists who would like to play around with the advanced controls home assistant provides.

In this tutorial, we will discuss about how to install home assistant OS on Raspberry Pi 4 and what are the challenges in installation.

Pre-requisites

  • Raspberry Pi 4 Model B
  • Good Micro SD card (at least 32 GB)
  • SD card reader (for flashing the SD card with home assistant OS)
  • Good internet connection (preferrably access to LAN connection for Raspberry Pi 4)
  • PC/Laptop for writing/flashing SD card

Challenges

Here I would like to mention the mistakes I made:

Cheap 10€ micro SD card:

In order to save some money (because it was a hobby project for me) I picked up a cheap micro SD card from the store. You guessed it right. While trying to write/flash the micro SD card, for some reason I managed to corrupt it. Rendering it useless. Even though micro SD is not recommended (SSD is preferred) I would suggest a good micro SD card from a reputed brand.

Faulty SD card readers:

Please make sure to use a good SD card reader for writing/flashing the cards. After the cheap SD card disaster, I realized I must check my SD card readers for their functionality. Guess what, my SD card reader was also faulty. I managed to buy a new one before I tried to flash my new SanDisk SD card.

Word of Caution:

If you are planning to use your old SD card lying around somewhere in your drawers, flashing will OVERWRITE/WIPE YOUR DATA on the card. Make sure you have your data backed up.

Setup and Installation

We will need the WNetAddConnection2A function from the Windows API to make the connection to a network resource as in our case samba server.

We need to set up 3 key components to achieving a successful connection:

  • Setup network resource
  • Samba server username
  • Samba server password

Network resource or NETRESOURCE needs attributes to be setup like:

  • local name - Q:
  • remote name - ////samba_ip_address//path//to//destination (extra / are for escape characters in the path)
  • resource type - to any

You can read more about NETRESOURCE here.

After setting up our NETRESOURCE we need to call WNetAddConnection2A function to establish a connection to the samba server. WNetAddConnection2A takes in 4 arguments:

  • net resource - that we built above
  • password - samba password
  • username - samba username
  • type of connection you would like

WNetAddConnection2A returns with an error code which can be handled as you wish. Below is a sample function showing the creation of NETRESOURCE and add a new connection using WNetAddConnection2A.

 1#include <windows.h>
 2#include <Winnetwk.h>
 3#include <system_error>
 4#include <iostream>
 5
 6// Need to link with Netapi32.lib and Mpr.lib
 7
 8bool ServerConnection::create(const QString& remoteName, const QString& username, const QString& password)
 9{
10   DWORD dwRetVal;
11
12   NETRESOURCE nr;
13   DWORD dwFlags;
14
15   // assign a drive name
16   // avoid using commonly used names like C:, D:
17   LPSTR szLocalName = "Q:";
18
19   LPSTR szRemoteName = new TCHAR[remoteName.toStdString().size() + 1]; //define
20   std::strcpy(szRemoteName, remoteName.toStdString().c_str());
21
22   // Zero out the NETRESOURCE struct
23   memset(&nr, 0, sizeof (NETRESOURCE));
24
25   // Assign our values to the NETRESOURCE structure.
26   nr.dwType = RESOURCETYPE_ANY;
27   nr.lpLocalName = szLocalName;
28   nr.lpRemoteName = szRemoteName;
29   nr.lpProvider = NULL;
30
31   // Assign a value to the connection options
32   // this flag makes sure windows doesn't remember previous connections
33   dwFlags = CONNECT_TEMPORARY;
34
35   // Call the WNetAddConnection2 function to assign
36   // a drive letter to the share.
37   dwRetVal = WNetAddConnection2A(&nr, password.toStdString().c_str(), username.toStdString().c_str(), dwFlags);
38
39   // get the message string
40   std::string message = std::system_category().message(dwRetVal);
41
42   std::cout << message << std::endl;
43
44   // handle error statements
45   switch (dwRetVal)
46   {
47       case NO_ERROR:
48       case ERROR_ALREADY_ASSIGNED:
49       case ERROR_DEVICE_ALREADY_REMEMBERED:
50           return true;
51       default:
52           return false;
53   }
54}

This implementation to communicate with samba server using Windows API has dependencies too. To make this work you will need to link your application to 2 different libraries:

  • Mpr.lib
  • NetAPI32.lib

These libs are usually found inside *C:\Program Files(x86)\Windows Kits\10*

Conclusion

To be able to communicate with the samba server beyond the authentication wall as a window client can be tricky. Using Windows API we learned how to mount the samba server locally as a network resource using WNetAddConnection2A function.

If this was helpful, please share this blog, and also feel free to add your thoughts or comments below.

Photo by FLY:D on Unsplash

comments powered by Disqus