Xiaomi Non-Zigbee WiFi Socket Raspberry Pi Zero

Home Assistant

I got home assistant up and running on my Raspberry Pi Zero last night along with a small python program I created. This will allow me to control several light bulbs and power points and read data from other Xiaomi hardware I have in the house from my own code by using their Web API.

The first piece of code I have written for it is in Python, and basically checks to see if my crypto miner is awake and hasn’t tripped over it self. If the system has crashed, my program will turn its power point off, wait 60 seconds and turn it back on. My miner with auto boot on AC Power will then start up and kick off the equilhash and ethereum miners, saving me to always check up on it.



Python code:



#!/usr/local/bin/python3

#

# To run: nohup server-check.py &



import requests

import json

import os

import time

import sys



count = 0

while True:

hostname = '192.168.1.999'

response = os.system('ping -c 1 ' + hostname)



if response == 0:

count = 0

time.sleep(60)

else:

if count == 2:

sys.exit(1)

count += 1



urlOff = 'http://localhost:8123/api/services/switch/turn_off'

urlOn = 'http://localhost:8123/api/services/switch/turn_on'



headers = {'content-type':'application/json', 'x-ha-access':'password'}

payload = {}



response = requests.post(urlOff, json=payload, headers=headers)

time.sleep(60)

response = requests.post(urlOn, json=payload, headers=headers)

time.sleep(150)



Issues: Getting Home Assistant on the Zero to run was harder then on a normal Pi, as I ran into issues where Home Assistant wouldn’t load, so I had to compile it myself. Then the python 3 environment it came setup with had to be upgraded from 3.4.2 to 3.5.3 because a library required it.

Links:

https://www.home-assistant.io/

https://www.home-assistant.io/blog/2017/05/01/home-assistant-on-raspberry-pi-zero-in-30-minutes/