Solar Charger Monitor

by borisqbf in Circuits > Arduino

620 Views, 1 Favorites, 0 Comments

Solar Charger Monitor

IMG_2200.JPG

Few years ago we purchased summer house located in a small village. The house is connected to the power grid, but the supply is less reliable than in the metropolitan area. When something happens to the transmission lines, our village would be the last to receive attention of the emergency crew as there is only a handful of permanently occupied homes.

 

I decided to build a small off-grid solar power source to provide electricity for the bare essentials – a fridge, microwave, gas hot water controller and a coffee machine (can’t live without it). 


I ended up with six 250W solar panels, two car batteries and a Renogy Rover 40A MPPT solar charge controller arranged in 24V system connected to 2500W inverter. 

During long summer days this setup is more than adequate but in the winter the panels do not produce enough power to charge the batteries. 


Traditional approach - feeding excess power back to the grid and consuming mains power when required defeats the purpose of building this backup power source as it is mandatory to shut down solar power generation when the mains power supply is interrupted. Also, anything to do with the mains power grid is governed by endless regulatory documents, certifications, etc. 


So, my solution was to build a monitor for solar charge controller that would detect battery voltage getting below a certain level and switch the load from the inverter to the mains power source. Likewise, when the battery is fully charged it would switch back to solar. To make it more interesting, the monitor would get the solar controller data sent to MQTT server and I would be able to monitor the process remotely. Perfect solution for the summer house! 


The controller is built on ESP8266 MCU – I used D1 mini board. It is polling Renogy Rover via Modbus interface. The controller data is sent to Hive MQTT server.

The device has two modes of operation - manual and automatic, where the power source is switched based on the battery voltage.

The major challenge I had during building the project was lack of GPIOs on D1. On the hindsight I should have used Arduino pared with some Wi-Fi shield, but I made it to work with some sacrifices in the end.

 

Another challenge was availability of information. While a number of projects found on the web were focused on implementing similar functionality, it was still a matter of trial and error. 


I hope this Instructable could be used a source of information about Renogy Rover Modbus interface and help others to build more interesting devices interfacing this solar charge controller.


Safety First - a word of warning.


This project deals with mains power which is lethal. Please do not continue if you do not feel comfortable with domestic AC power wiring. Get qualified electrician to help you with AC part of the device. Proceed at your own risk. And please do not do anything stupid like making any changes while the device is connected to the AC power.


Supplies

ESP8266 D1 Mini

SparkFun MAX3232 Transceiver Breakout (https://www.sparkfun.com/products/11189)

DB9 male/female connectors

Two Relay module - 30A (https://www.aliexpress.com/item/4000109289135.html)

Double Colour LED Metal Push Button Switch (https://www.aliexpress.com/item/1005004432296986.htm)

Prototyping PCB

Panel mounted Mains sockets

Red, Green and Blue LEDs

Various general purpose electronic components

Prototyping PCB 60X40 mm

FT232RL FTDI USB to Serial Adapter Module (https://www.aliexpress.com/item/32273550144.html)

Implement AC Power Connections

IMG_2202.JPG
IMG_2203.JPG
SolarMonitor.png

Plan and place all panel mounted components - power sockets, connectors, buttons, etc.

Connect AC power components with the relay module as per attached schematics.

Make sure that there are no exposed wires - use heat shrink tubing to insulate every contact carrying dangerous voltage.

Test and re-test all of the AC wiring. This is most important component of the instructable.

Build MCU Module

SolarMonitor.png
IMG_2212.JPG
IMG_2213.JPG
IMG_2211.JPG

Using prototyping PCB build MCU module according to the schematics

I used JST connectors to connect to all external components but it is not necessary.

I also used 90 degrees PCP header to connect MAX3232 - it could make the device footprint smaller.


Upload the Sketch

The code for the project can be downloaded from the following link: https://www.dropbox.com/sh/q4ruaggwptlzq15/AADtVk4ZbmTAM0sztbZH1umDa?dl=0


Few words about the sketch.

  1. The code is written in C++
  2. The code uses the following libraries: ESP8266WiFi, ModbusMaster, PubSubClient, LittleFS, CertStoreBearSSL
  3. You would need to set up your SSID password in WiFiController.h and your MQTT server credentials in MqttController.h
  4. Before uploading the code, open jumper JP1 to prevent interference from MAX3232. After the code is uploaded, close the jumper.
  5. The code assumes that you use HiveMQ as MQTT server. It is not terribly important but I believe every MQTT server has it's own peculiarities, especially in the are of secure communications. After you sign up with HiveMQTT you would have a page with customised samples on how to connect to their server. In essence you would need to install LittleFS to your ESP8266 to store the SSL certificates and then upload the certificates using Python AR utility. Please note - you would need to do it only once when you have brand new ESP.

Following is an extract from HiveMQ getting started:

PubSubClient library

To use HiveMQ Cloud an MQTT library has to be added first. In this example the PubSubClient is used but there are a lot of other alternatives that provide similar functionalities. Download the PubSubClient from GitHub as a .zip file. Then go to the Arduino IDE, open Sketch>Include Library>Add .ZIP Library and select the downloaded .zip file.


NTPClient library

The next library that is required is the NTPClient. Install this library by opening Tools>Manage Libraries... and searching for NTPClient. This library provides functionality for date and time. Here it is used to update the internal clock of the ESP8266 board, in order to use TLS certificates.


LittleFS Filesystem Uploader

The LittleFS Filesystem Uploader is needed to upload certificates to your board. This enables you to connect securely with HiveMQ Cloud. Download the ESP8266LittleFS-X.zip. Go to the Arduino IDE directory and open the tools folder. Unzip the downloaded .zip folder to the tools folder. The result will be a folder called ESP8266LittleFS-2.6.0 or it can have a different version. This folder contains the ESP8266LittleFS folder. Cut the ESP8266LittleFS folder and paste it into the tools folder. Afterwards, you can delete the ESP8266LittleFS-2.6.0 folder. The resulting folder structure should look like this:


<home_dir>/Arduino-<version>/tools/ESP8266LittleFS/tool/esp8266littlefs.jar



Upload certificates to the board

  1. The next step is to get the certificates that are needed for an encrypted connection to HiveMQ Cloud. For this purpose use this script by downloading the repository as a .zip folder. Only the file certs-from-mozilla.py is needed, you can delete the rest. Open this script in a Python IDE of your choice, for example PyCharm or Visual Studio Code. Execute the script in your IDE. A file will be generated, that is called certs.ar. Move this file into the directory of your Arduino IDE sketch. To find it you can go to Sketch>Show Sketch Folder in Arduino IDE. The folder where your sketch is saved should open. Inside this folder, create a new folder called data. Put your generated certs.ar into this data folder. In the Arduino IDE, in the Tools menu, you may want to select a bigger flash size, this depends on the size of your files. Then open Tools>ESP8266 LittleFS Data Upload. This will upload the files located in data to your board’s flash memory. After a few seconds, you should get the message LittleFS Image Uploaded.

Test the Code

Congratulations on getting that far.

You should be able to test most of the functionality without even connecting to Renogy Rover.

  1. Your device should be able to connect to your WiFi network. You should see Blue LED (LED2) solid lit.
  2. You should be able to switch to manual mode (S21 connected to D0 pin) - and use S1 connected to D5 pin to alternate between Solar and Grid source of power for the load. You would see toggle between LEDS S2D1 and S2D2 being lit up and the hear the relay making reassuring sound.
  3. You should see Red LED (LED3) blinking indicating communication error with Renogy Rover. It is totally expected as we haven't connected to it yet.
  4. You should be able to connect to the HiveMQ web client and observe various messages from the device.

If some of it is missing, please read troubleshooting section. Also you could post a question if you are stuck, I'll try to help.

Build Renogy Cable

IMG_2227.JPG
IMG_2225.JPG

The cable pinout is clearly visible in the photos. This is standard Renogy RS232 cable pinout minus the colour code.

Green wire is the ground. It is connected to Pin 5 of DB9 and to Pin 3 of RJ11

Yellow wire is controller's output signal (Tx) - connected to Pin 3 of DB9 and Pin 2 of RJ11

Blue wire is device's Rx - connected to Pin 2 of DB9 and Pin 1 of Rj11

The Renogy pinouts found on the web are sometimes confusing, e.g. which pin is 1? is it male or female connector? is it solder side or front view? is Rx/Tx relevant to the controller or Renogy, and so on. I hope the images of the working cable help...

The easiest way to build the cable is to find a length of old 4-wire telephone cable already crimped to 6P6C RJ11/RJ12. We only need three wires - Ground, Tx and Rx.

Unfortunately most of the new phone cables are 2-wire variety. If you are like me and could not find the 4-wire one, get 6P6C RJ11/RJ12 connector (make sure that it is not 6P2C or 6P4C, these ones do not have all pins that Renogy uses), a crimping tool and with a bit of luck you would be able to get Renogy end sorted.

DB9 end is easy. You do not need to follow Renogy standards, especially if you use DB9 with MX3232 as a shield.

Troubleshooting

putty1.PNG
putty2.PNG
renogy-1.png
renogy-2.png

If you device works straight away after you have built it - something is wrong :)

For most of us there are some problems at least.

I will try to list problems I had and approach I used to rectify them.


  1. No connection to WiFi - Blue LED is blinking. Check WiFi signal strength with your phone. Check SSID and Password for spelling mistakes.
  2. No messages received by MQTT server. Check Mqtt server credentials. Make sure you have installed LittleFS library to your new ESP8266 device. Make sure you have uploaded SSL client certificates using Python script as detailed on HiveMQ website
  3. No communications with Renogy Rover - Red LED is blinking. Check that you have closed the jumper J1 after uploading the sketch. Check the RS232 cable. Make sure you use good quality MAX 3232 chip. I have started with the cheap clones - some of them simply do not work. I bought one from SparkFun and it costed me more that all other components combined.
  4. Everything looks fine but relays are not switching. The device is built on ESP8266 which is 3.3V MCU. When connected to 5V relay module I used a simple shortcut - a diode that adds approximately 1V to HIGH value. It may or may not work in your case depending on the input circuitry of the relay module. You may need to implement a proper solution - a transistor to amplify the signal or use ESP's GPIO as a current sink if the relay module have a separate power pin for switching circuit.

BTW If you could count how many times Red LED blinks you could get some idea of what's going on. Number of blinks corresponds to the error codes, as per MqttController.h and RenogyController.h. This is not ideal but better than nothing.

If steps above do not help....

  1. Connect FT232RL USB to serial adapter to Serial1 pins of the controller (D4 and Ground pin)
  2. Connect the adapter to PC.
  3. Run PuTTY terminal utility on the PC
  4. Start new COM port PuTTY session - choose the right port on PC (putty1 screenshot)
  5. You should be able to see diagnostic output similar to putty2 screenshot
  6. After a while you should be able to observe full dump of Renogy registers:

Sending telemetry

Telemetry sent

Polling Renogy

Info Registers

Voltage rating: 24

Amp rating: 40

Discharge Current rating: 20

Product type: 0

Controller name:    LM4204

Software version: 4768

Hardware version: 5123

Serial number: 32100

Modbus address: 1

Battery voltage: 27.20

Battery charge current: 8.53

Percent battery charge level : 100

Battery temp: 35

Controller temp: 45

Panel voltage: 58.50

Panel current: 4.13

Panel power: 242

Min Battery voltage today: 23.50

Maxn Battery voltage today: 28.70

Max charging current today: 27.75

Max discharging current today: 0.00

Max charging power today: 795

Max discharging power today: 0

Charging amphours today: 79

Discharging amphours today: 0

Power generated today: 2179

Power consumption today: 0

Controller uptime (days): 367

Total Battery fullcharges: 452

Total Battery overDischarges: 5

Total charging amphours: 13705

Total discharging amphours: 0

Cumulative Power Generation: 261892

Cumulative Power Consumption: 0

Load State: 0

Charging State: 5

Controller Faults: 0


My data is different from what I could read from the Rover itself - what's wrong?

There is a document floating around called ROVER MODBUS.DOCX. I can't upload it here as it is copyrighted content but you could Google it and you would find it - no doubts. The document specifies Modbus registers used and how to interpret the values.

Also you could purchase Renogy to USB cable like this one from Aliexpress, connect it to PC and run free Modbus Master Simulator (RMMS) utility.

Configure the simulator as per attached screenshots and you would be able to get the values of Modbus registers independent from your sketch.


This is it and thankyou for reading.