Displaying solar production on a YoctoDisplay

Displaying solar production on a YoctoDisplay

This week, we created a small program that displays the current production of the building's photovoltaic panels on a Yocto-MaxiDisplay. To do this, we wrote a small Python script that retrieves production information from the building's Envoy S Metered gateway.




When a building is equipped with solar panels, the best way to make the most the infrastructure profitable is to optimize the use of electrical appliances to make the most of the electricity generated by the panels. For example, running the dishwasher in the middle of the day rather than in the evening. For this reason, we've written a small program in Python that displays the current solar production in the kitchen, as well as the current consumption of the house.

The Envoy S Metered gateway

The building's solar system uses Enphase microinverters connected to an Enphase Envoy S Metered gateway. This gateway is capable of measuring the current production of all the solar panels, as well as the building's current power consumption.

Like many current installations, this gateway is connected to the local network and can publish its data to a Cloud service (in this case https://enlighten.enphaseenergy.com/). But what's less well known is that it's possible to obtain all this information locally by connecting directly to the gateway. There's even a Python package for obtaining this information directly.

Yocto-MaxiDisplay

For the display, we used a Yocto-MaxiDisplay connected to a YoctoHub-Wireless-n. In fact, we reused the infrastructure we had set up 9 years ago to display the day's appointments in the kitchen.

The application

We wrote this application in Python, because it's easy to use and cross-platform, but above all, because we found a library that allowed us to interface easily with our Enphase gateway. This library is available on GitHub but also directly from PyPI, making it easy to install.

So we started by installing this library as well as the Yoctopuce library.

pip install Enphase-API pip install yoctopuce



For the code, we drew heavily on the sample code of the Enphase-API library. The application is divided into three main parts.

The first part is setting up the connection and authentication with the Envoy S Metered gateway, the second is the retrieval of the building's production and consumption information, and finally the third is the display of these two pieces of information on the Yocto-MaxiDisplay.

In this post, we only detail the interaction of the application with the Yocto-MaxiDisplay, but the complete sources of the application are available on GitHub along with our other example programs. As we said, the code that interacts with the gateway is very heavily inspired by the examples in the Enphase-API library and is well documented, but if you have any questions, please don't hesitate to contact us.

On startup, we begin by setting up a connection with the YoctoHub to which the Yocto-MaxiDisplay is connected, to retrieve a YDisplay object that allows us to interact with the screen.

errmsg = YRefParam()
res = YAPI.RegisterHub(config['yoctopuce_url'], errmsg)
if res != YAPI:SUCCESS:
    print('Unable to connect to %s (%s)'
       % (config['yoctopuce_url'], errmsg.value))
    sys.exit(1)

disp = YDisplay.FirstDisplay()
if disp is None:
    print('No module connected')
    sys.exit(1)



Next, we reset the screen content and save the screen dimensions. We also retrieve a pointer to layer 0 of the screen, which enables us to double-buffer the screen and avoid flickering when updating screen content.

disp.resetAll()
w = disp.get_displayWidth()
h = disp.get_displayHeight()
l0 = disp.get_displayLayer(0)



Next, we enter the application's main loop, which first retrieves the building's electricity production and consumption data. Then, on the first line, it displays the difference, i.e. the surplus energy produced by the solar panels. The second line displays the solar production.

while disp.isOnline():
    production, consumption = get_solar_stats(gateway)
    delta = production - consumption

    l0.clear()
    l0.selectFont('Large.yfm')
    l0.drawText(w / 2, h / 3, YDisplayLayer.ALIGN.CENTER,
        format_watts(delta))
    l0.selectFont('Medium.yfm')
    l0.drawText(w / 2, h * 2 / 3, YDisplayLayer.ALIGN.CENTER,
        "(" + format_watts(production) + ")")
    disp.swapLayerContent(0, 1)
    YAPI.Sleep(5000, errmsg)



Note the call to disp.swapLayerContent, which swaps the contents of layer 0 (bottom) with layer 1 (top), enabling us to perform double buffering. As layer 1 is placed above layer 0, there's no risk of the Yocto-MaxiDisplay refreshing the OLED screen with incomplete content (which would be the case between the call to clear and the calls to drawText).

In the end, the core of our application looks like this.

def main():
    with open('config.json', mode='r', encoding='utf-8') as json_file:
        config = json.load(json_file)

    gateway = get_secure_gateway_session(config)
    errmsg = YRefParam()
    res = YAPI.RegisterHub(config['yoctopuce_url'], errmsg)
    if res < 0:
        print('Unable to connect to %s (%s)'
            % (config['yoctopuce_url'], errmsg.value))
        sys.exit(1)
    disp = YDisplay.FirstDisplay()
    if disp is None:
        print('No module connected')
        sys.exit(1)
    disp.resetAll()
    w = disp.get_displayWidth()
    h = disp.get_displayHeight()
    l0 = disp.get_displayLayer(0)
    while disp.isOnline():
        production, consumption = get_solar_stats(gateway)
        delta = production - consumption

        l0.clear()
        l0.selectFont('Large.yfm')
        l0.drawText(w / 2, h / 3, YDisplayLayer.ALIGN.CENTER,
            format_watts(delta))
        l0.selectFont('Medium.yfm')
        l0.drawText(w / 2, h * 2 / 3, YDisplayLayer.ALIGN.CENTER,
            "(" + format_watts(production) + ")")
        disp.swapLayerContent(0, 1)
        YAPI.Sleep(5000, errmsg)



We then installed this little script on a Raspberry Pi and configured the Raspberry Pi to start this script at boot time, et voilà!

Yocto-MaxiDisplay is integrated into kitchen furniture
Yocto-MaxiDisplay is integrated into kitchen furniture



To conclude

Using the Enphase gateway and the Yocto-MaxiDisplay, our Python script displays solar production and power consumption in the kitchen in real time. This solution lets us know while standing in the kitchen whether it's worth starting the dishwasher, or whether it's better to wait for sunnier weather.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.