Finding board's IP address

Sunmachine uses DHCP protocol to obtain an IP address that may be assigned randomly. Fortunately, Sunmachine has implemented the zeroconf protocol, so finding the IP address of the board is very easy with this python script.

#!/usr/bin/env python3

import socket
from zeroconf import *

def address_lookup(zeroconf, service_type, name, state_change):
    service_info = zeroconf.get_service_info(service_type, name)
    if state_change == ServiceStateChange.Added and service_info.port == 8404:
        print(socket.inet_ntoa(service_info.address))
        
try:
    zeroconf = Zeroconf()
    ServiceBrowser(zeroconf, '_phm._tcp.local.', handlers=[address_lookup])
    input()
except KeyboardInterrupt:
    zeroconf.close()

RestAPI request function

You can use this short function to make calls to the Sunmachine boards. The first three parameters are mandatory, fourth and fifth are optional.

#!/usr/bin/env python3

from tornado import httpclient, escape

async def request(client, address, uri, auth=None, data=None):
    url = "http://%s:8404/%s" % (address, uri)
    method = 'PUT' if data else 'GET'
    headers = { 'Authorization': 'Bearer ' + auth } if auth else None
    data = escape.json_encode(data) if data else None
    req = httpclient.HTTPRequest(url, method, headers, data)
    resp = await client.fetch(req)
    return escape.json_decode(resp.body)
Parameters:

To make this function synchronous, remove async and await keywords and use httpclient.HTTPClient() instead of httpclient.AsyncHTTPClient().

RestAPI routines

Most routes can be called using GET and PUT methods, but there are some exceptions. Responses are always JSON encoded. board routine is the only routine that may be called without JWT token when authentication is enabled.

board GET only

Get basic information about the board.

GET — no request params.

Response object:

power GET only

Get current LEDs power or 0 when the light is turned off.

GET — no request params.

Response object:

mode GET & PUT

Get or change operating mode.

GET — no request params.

PUT — request params: Response object:

brightness GET & PUT

Get or change the brightness of the light.

GET — no request params.

PUT — request params & response object:

color_hue GET & PUT

Get or change the color hue of the light (only for color diodes).

GET — no request params.

PUT — request params & response object:

timeout GET & PUT

Get or change the light timeout for auto mode.

GET — no request params.

PUT — request params & response object:

light GET & PUT

Get the ambient light intensity and the light intensity threshold. Or change the light intensity threshold for automatic mode.

GET — no request params.

PUT — request params: Response object:

alarm_silent GET & PUT

Get or change the silent alarm setting.

GET — no request params.

PUT — request params: Response object:

alarm_group GET & PUT

Get or change the alarm group setting. The alarm is activated on all grouped devices from single device.

GET — no request params.

PUT — request params & response object:

timezone GET & PUT

Get or change timezone setting.

GET — no request params.

PUT — request params & response object:

sched_new PUT only

Schedule a new task.

PUT — request params: Response object:

sched_del PUT only

Delete the task.

PUT — request params: Response object:

sched_state PUT only

Enable or disable the task.

PUT — request params: Response object:

sched_list GET only

Get a list of scheduled tasks.

GET — no request params.

Response object:

led_type GET & PUT

Get or change the type of LEDs.

GET — no request params.

PUT — request params & response object:

led_freq GET & PUT

Get or change PWM frequency for dimming LEDs (only for white diodes).

GET — no request params.

PUT — request params & response object:

led_length GET & PUT

Get or change the number of addressable LEDs (only for color diodes).

GET — no request params.

PUT — request params & response object:

wifi GET & PUT

Get details or connect to the WiFi network.

GET — no request params.

PUT — request params: Response object:

wifi_list GET only

Get a list of WiFi networks found nearby.

GET — no request params.

Response object:

trust PUT only

Add the phone to trusted devices. The alarm can be deactivated without pin code only by trusted phones.

PUT — request params: Response object:

untrust PUT only

Remove the phone from trusted devices.

PUT — request params: Response object:

auth PUT only

Turn on or off the authentication feature.

PUT — request params: Response object:

mqtt GET & PUT

Turn on or off sending logs to the MQTT broker.

GET — no request params.

PUT — request params: Response object:

MQTT subscription script

This script connects to the MQTT broker, listens to 'Sunmachine' topic and dumps every message recieved to console.

#!/usr/bin/env python3

from paho.mqtt.client import Client
import msgpack

def on_connect(client, userdata, flags, rc):
    client.subscribe('Sunmachine', 1) # topic, qos

def on_message(client, userdata, msg):
    print(msgpack.unpackb(msg.payload, raw=False))

client = Client('Subscriber', True) # client_id, clean_session
client.on_connect = on_connect
client.on_message = on_message
client.connect('localhost', 1883, 60) # host, port, keepalive
try:
    client.loop_forever()
except:
    client.disconnect()

MQTT messages

Every message is msgpack encoded and contain four members:

motion event

Emitted when motion is detected.

Data list:

ambient_light event

Emitted when ambient light intensity was changes.

Data list:

power event

Emitted when the LEDs power changes.

Data list:

alarm event

Emitted when motion is detected in the alarm mode.

No data.

mode event

Emitted when the operating mode changes.

Data list:

color_hue event

Emitted when the color hue changes.

Data list:

timeout event

Emitted when the light timeout changes.

Data list:

threshold event

Emitted when the light intensity threshold is changed or recalculated.

Data list: