Solar Energy Monitoring with Raspberry Pi and Node-Red

Mitch Ross
7 min readJul 5, 2022
Node Red Solar Panel Monitoring Dashboard
Two Renogy 100 Watt Panels

I’ve always been curious about solar energy, so a few years ago I started dabbling with it by throwing a 100-watt panel on top of my backyard shed. The goal was not to save money, as I could have quickly run a power line out to the shed, but rather to experiment and learn more about lithium batteries, solar energy, and DC circuits. One of the challenges of running solar is monitoring the input/output of energy in and out. If you have a build system, the only way to view this data is by walking up to the solar charge controller and reading it on the small LCD screen. Recently, Solar Charge Controllers have dropped in price and several offer a Serial port to read the raw data. This is particularly interesting because we can set-up a Raspberry Pi to serve this data up, making it possible to view it on our phone. Some more expensive solar charge controllers offer Bluetooth or WiFi monitoring with an app, but where’s the fun in that? Plus, you likely have a Raspberry Pi sitting in a drawer somewhere.

At a minimum, a set-up involves panels and a Charge Controller for taking solar power and converting it to charge a battery set. Everything you need to know about solar and batteries can be found on this excellent Youtube channel: https://www.youtube.com/c/WillProwse.

This article won't focus on the physical wiring/set-up of the solar panel setup, but there are a few specific parts that are required for set-up.

Solar Charge RJ485 USB https://www.amazon.com/gp/product/B08X6B9112/ref=ppx_yo_dt_b_asin_title_o09_s00?ie=UTF8&psc=1

EPEver solar charge controller
https://www.amazon.com/gp/product/B08X4DDNFV/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1

Charge Controller USB to Serial
USB to Serial Cable In Solar Charge Controller

Now let's get to the software setup!

I set this up from a freshly formatted SD card and chose Raspbian Lite OS 64-bit. The steps below should help you get a fully set-up Raspberry Pi for data logging the Solar Charge Controller. There can be a lot of gotchas with the installation that I had to piece together through Github issue threads. I cover all these issues in the post so you can avoid banging your head against the wall troubleshooting. I’ll assume you have a base-level understanding of linux, ssh-ing, and updating/installing packages.

First things first, let’s ensure the Raspberry Pi OS is up to date.

Sudo apt-get update

Sudo apt-get upgrade

then let's reboot ( sudo reboot now )

Next, plug in the USB to serial cable to the raspberry pi (and your solar charge controller, although if setting up the Pi inside, you can still plug in the USB cable without the charge controller for the first part of the setup).

Type ‘lsusb’ this will show USB devices connected. You should see Exar Corp XR21B1411.

output of lsusb

Now that we are about to install the driver for this USB device, we are going to need to install Linux kernel headers (since we will be compiling the driver).

sudo apt-get install raspberrypi-kernel-headers

Now we can begin installing the USB driver.

Get the usb driver from here
git clone https://github.com/kasbert/epsolar-tracer.git

Compile the driver

We are going to do a few things
1. Build the driver
2. ‘Install’ the driver
3. Make the driver persist after reboot. ( remember how I said there are a few gotchas, this is one of them )


(notes — https://github.com/kasbert/epsolar-tracer/tree/master/xr_usb_serial_common-1a)

Build the driver and install
cd into xr_urb_serial_common-1a folder
sudo make
sudo insmod ./xr_usb_serial_common.ko

driver install steps

Make driver reboot after login

(please note this is full path to your xr_usb_serial_common.ko followed by a “space” then /lib/modules/`uname -r` )

Please go slow here, look carefully at each step and triple-check your paths!

sudo ln -s /home/vanillax/epsolar-tracer/xr_usb_serial_common-1a/xr_usb_serial_common.ko /lib/modules/`uname -r`

sudo depmod -a

sudo modprobe xr_usb_serial_common ( note no .ko extension )

( notes https://github.com/kasbert/epsolar-tracer/issues/26 )
( notes
https://www.computerhope.com/unix/modprobe.htm#examples )

We are getting there! The USB driver is complete

To check run “ ls /dev/tty* “

you should see /dev/ttyXRUSB0 in the list

Congrats! The USB driver is installed.

Dashboard Prep

Here we are going to get some software dependencies installed. We need to install a few python libraries. The most important is pyserial and pymodbus which allow us to read from the USB Serial cable.

sudo apt-get install pip

pip install pyserial

pip install pymodbus

In the git project where we downloaded the USB driver from either, there are a few sample/demo python scripts to test the cable. At this point, we need to make sure we are plugged into the solar charge controller.

This next part is optional, however, it's nice for confirming the python decencies are ok and that you can communicate with the charge controller. You can run ‘python readall.py’ to test that you get output data. Before you do that, you need to open the file and edit where it’s reading the data from. Replace the lines where it says “ chose the serial client “ with the following below. We need to do this because the original code does not locate the right USB device, not sure why, but if you purchase the cable listed above, these steps will work.

# choose the serial client

client = ModbusClient(method=’rtu’, port=’/dev/ttyXRUSB0', baudrate=115200, stopbits = 1, bytesize = 8, timeout=1)

client.connect()

rs485_mode = serial.rs485.RS485Settings(delay_before_tx = 0, delay_before_rx = 0, rts_level_for_tx=True, rts_level_for_rx=False, loopback=False)

Run ‘ python readall.py
Again this part above is optional but recommended.

Node-Red

The next thing we are going to do is install node-red which is going to be what hosts/runs/displays our solar dashboard graphs.

‘Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways.

It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click.” — https://nodered.org/


To install node-red on Raspberry Pi

run “bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Here is another ‘gotcha’ let’s make sure node-red runs after a reboot!

sudo systemctl enable nodered.service

Node Red to work on reboot

Finally, we need to actually start node-red, we should only have to do this once after installation….

run “node-red-start

Now you should be able to connect to the node-red admin page at http://yourRapsberryIP:1880

Node-Red Dashboard Setup

Luckily for setting up a dashboard, someone has already done all the hard work, huge shout out to Adam Welch for creating a premade dashboard for monitoring solar! https://github.com/AdamWelchUK/NodeRedEPEverDashboard

I am not a node-red enthusiast or expert, but ill do my best to explain setup.

We need a few ‘modules’ installed for this node-red dashboard to work. Just think of these as libraries/packages in other software languages. Hit the hamburger menu and go to settings > manage pallet. Hit the install button and search and install these one by one.

  • node-red-dashboard
  • node-red-contrib-modbus
  • node-red-contrib-influxdb
  • node-red-contrib-moment

After you finish installing the dependencies, go to the menu again and hit import. You need to upload this file or paste in its raw contents. https://github.com/AdamWelchUK/NodeRedEPEverDashboard/blob/master/EPEverDashboardv0.2.json

You should see something like this.

Now we need to deal with gotchas again.

We need to update some config variables in the imported node-red dashboard to read from our USB cable. On the right look for a gear icon for settings. Here double click USB0 and TracerA12. You will need to change the line serial port over to ‘/dev/ttyXRUSB0’, don't forget to hit update. I'm not a node-red user so there might be an easier way. I alsocould have updated the JSON from Adam, but this is all for learning so you can have a better understanding of what going on.

Finally hit deploy

Now go to HTTP://yourraspberryip:1880/ui and you should see

Congrats! You are now monitoring your solar setup!

Additional Reading. If you are curious how values are read from the USB serial cable. Checkout https://www.developpez.net/forums/attachments/p196506d1451307310/systemes/autres-systemes/automation/probleme-com-modbus-pl7-pro/controllerprotocolv2.3.pdf/

--

--

Mitch Ross

Principle Software Engineer with a passion for development and computers. https://github.com/mitchross