Sofar2mqtt - Remote Control for Sofar Solar Inverters
by cmcgerty in Circuits > Microcontrollers
25446 Views, 20 Favorites, 0 Comments
Sofar2mqtt - Remote Control for Sofar Solar Inverters
![Sofar2MQTT.jpg](/proxy/?url=https://content.instructables.com/FWX/WDZP/KLUVPOAR/FWXWDZPKLUVPOAR.jpg&filename=Sofar2MQTT.jpg)
A smart home interface for Sofar solar and battery inverters.
Supported models:
ME3000SP and HYD-xx00
Sofar2mqtt is a remote control interface for Sofar solar and battery inverters. It allows remote control of the inverter and reports the invertor status, power usage, battery state etc for integration with smart home systems such as Open Energy Monitor, Home Assistant and Node-Red.
As a prerequisite, you will need to be running an MQTT broker on your network. A Step 10 details the options for this.
For read only mode, it will send inverter status messages via MQTT with the inverter in any run mode.
To control the inverter, place it in passive mode and send messages to sofar2mqtt/set.
It runs on an ESP8266 microcontroller with a TTL to RS485 module such as MAX485 or MAX3485.
Designed to work with TTL modules with or without the DR and RE flow control pins. If your TTL module does not have these pins then just ignore the wire from D5.
Parts List
![parts.jpg](/proxy/?url=https://content.instructables.com/F6D/HTVH/KLUVPOB9/F6DHTVHKLUVPOB9.jpg&filename=parts.jpg)
- ESP8266 Microcontroller
- MAX3485 or MAX485 TTL to RS485 board*
- Wemos 64x48 OLED Screen (optional)
- A small project board
- A few wires and a little solder
*The MAX3485 (which is red, not blue like the MAX485 shown here) is preferred as it is much more stable because it uses 3.3v logic, just like the ESP8366. The MAX485 uses 5v logic but is somewhat tolerant of 3.3v and is generally cheaper and more widely available. I use a MAX485 but many people have reported problems with this and if you can find a MAX3485 then you should use that. MAX3485 boards do not have DR and RE flow control pins, so just skip the wire from pin D5 in the wiring diagram below.
Cut the Project Board to a Convenient Size.
![board.jpg](/proxy/?url=https://content.instructables.com/FJ9/MMGD/KLUVPOB6/FJ9MMGDKLUVPOB6.jpg&filename=board.jpg)
Optional step. Arrange the components on the board and see what the final size will be. Trim the board if you like.
Wiring
![diagram.jpg](/proxy/?url=https://content.instructables.com/FZ6/Y1LW/KLUVPOB7/FZ6Y1LWKLUVPOB7.jpg&filename=diagram.jpg)
Wire the components according to this circuit diagram.
More Wiring
![wiring.jpg](/proxy/?url=https://content.instructables.com/FND/U7Q0/KLUVPOBD/FNDU7Q0KLUVPOBD.jpg&filename=wiring.jpg)
I tend to keep the wires on top of the board, poke them through and solder underneath. Your approach may be better and your soldering will almost certainly be better than mine!
Finished Wiring
![short.jpg](/proxy/?url=https://content.instructables.com/FU1/M2EJ/KLUVPOBB/FU1M2EJKLUVPOBB.jpg&filename=short.jpg)
Make sure you connect the DR and RE pins together. The red arrow shows where a single wire from D5 connects to both DR and RE. (If you are using a TTL board without the DR and RE pins, ignore this step.)
Add the OLED Screen
![ICs.jpg](/proxy/?url=https://content.instructables.com/FFK/CXCF/KLUVPOB8/FFKCXCFKLUVPOB8.jpg&filename=ICs.jpg)
The OLED screen is optional but is a nice addition to monitor the run mode of your inverter.
Use long pinned mounts on your ESP8266 if you are stacking the OLED on top. Trim the legs so they fit comfortably into another pair of sockets on the circuit board below.
I don't recommend soldering the ESP8266 directly to your circuit board as it may make debugging more difficult.
Finishing
![Sofar2MQTT_completed.jpg](/proxy/?url=https://content.instructables.com/FLM/MSFA/KLUVPOBC/FLMMSFAKLUVPOBC.jpg&filename=Sofar2MQTT_completed.jpg)
Here's how it looks when completed.
Flashing
![arduinoIde.jpg](/proxy/?url=https://content.instructables.com/FX4/IQN4/KLUVPP90/FX4IQN4KLUVPP90.jpg&filename=arduinoIde.jpg)
Download and unzip the code from https://github.com/cmcgerty/Sofar2mqtt
Open either ME3000SP.ino or HYBRID.ino (as appropriate for your inverter model) in the Arduino IDE.
Edit it with your wifi network name and password and your mqtt server details. If you need more than one Sofar2mqtt on your network, make sure you give them unique device names.
You'll need the libraries for the ESP8266. Follow this guide if you haven't completed that step before.
Add a few more libraries using the Manage Libraries menu:
- PubSubClient
- Adafruit GFX
- Adafruit SSD1306 Wemos Mini OLED
(Even if you are not using the OLED screen, you should install the Adafruit libraries or it will not compile.)
...and upload.
Testing
Run it on the desktop, not connected to you invertor, to test that wifi and mqtt are connected and see some messages in the serial monitor. The OLED screen should show "Online" to indicate a connection to both WiFi and MQTT. It will alternate between "RS485 Error" and "CRC-FAULT" while the inverter is not connected.
Send some test messages to your MQTT broker such as "true" to sofar2mqtt/set/auto and they should be displayed in the serial monitor. A complete list of MQTT messages in in the next step.
MQTT
You need to be running an MQTT broker on your network to connect Sofar2mqtt to your automation system. Mosquitto is the most common. It comes bundled with Open Energy Monitor or as an integration for Home Assistant. But you can very easily set up your own MQTT broker and much more.
Subscribe your MQTT client to:
- sofar2mqtt/state
Which provides:
- running_state
- grid_voltage
- grid_current
- grid_freq
- battery_power
- battery_voltage
- battery_current
- batterySOC
- battery_temp
- battery_cycles
- grid_power
- consumption
- solarPV
- today_generation
- today_exported
- today_purchase
- today_consumption
- inverter_temp
- inverterHS_temp
- solarPVAmps
With the inverter in Passive Mode, send MQTT messages to:
- sofar2mqtt/set/standby - send value "true"
- sofar2mqtt/set/auto - send value "true" or "battery_save"
- sofar2mqtt/set/charge - send values in the range 0-3000 (or higher if your inverter goes above 3000 watts)
- sofar2mqtt/set/discharge - send values in the range 0-3000 (or higher if your inverter goes above 3000 watts)
battery_save is a hybrid auto mode that will charge from excess solar but not discharge. This is unique to Sofar2mqtt as passive mode does not natively provide this function in the inverter.
Node-Red
![format.jpg](/proxy/?url=https://content.instructables.com/FHD/FW5U/KLUVPQ5C/FHDFW5UKLUVPQ5C.jpg&filename=format.jpg)
Also highly recommended is to use Node-Red to format the output of your inverter before passing the data to your automation system. In the code you downloaded earlier there is a "Node-Red Flows" folder containing a flow file called "Format_Sofar2mqtt.json". You can import this to Node-Red and it provides a single node called "Format Sofar2mqtt".
Connect it to the MQTT input of your Sofar2mqtt unit and send the output to your home automation system. The images show that I use Open Energy Monitor.
Connect to Inverter
![485s.jpg](/proxy/?url=https://content.instructables.com/FGG/CRVV/KLUVPP3G/FGGCRVVKLUVPP3G.jpg&filename=485s.jpg)
Connect the Sofar2mqtt unit to a 5v micro USB power supply. Now connect wires A and B to the two wire RS485 input of your inverter, which is marked as 485s on the image of the ME3000SP.
Troubleshooting
Nothing on the OLED screen? Make sure you solder all the pins on the OLED and ESP8266, not just those with wires attached.
No communication with the inverter? Make sure the slave IDs match. Sofar2mqtt assumes slave ID 1 by default. You can change this around line 93 or in the inverter user interface. But they must be the same.
Here's what the various things on the OLED screen tell you:
Line 1 is the device name, nothing else.
Line 2 will display "Connecting" during start up and lines 3 and 4 will show WIFI and MQTT getting connected.
Line 2 also has a dot that slowly flashes, once every few seconds. This is when a heartbeat message is being sent to the inverter.
If a message is read from the inverter that fails the CRC checksum, line 3 will display "CRC-FAULT". This could be caused by a loose or bad RS485 wire or by unsupported features. A few of these is normal, a lot could indicate a problem.
If no response is received to a heartbeat message, lines 3 and 4 show "RS485 ERROR". This could be caused by disconnected or reversed RS485 wires.
During start-up, line 4 shows the Sofar2mqtt software version. Check that you have the latest version at https://github.com/cmcgerty/Sofar2MQTT
In normal operation, line 2 shows "Online" which indicates that both WIFI and MQTT are still connected.
In normal operation, line 3 shows the inverter run state, Standby, Charging, Discharging etc.
In normal operation, line 4 shows the power in Watts in or out of the batteries when charging or discharging.