SONOFF Basic R4 LAN

Hello, I have the SONOFF Basic R4 that I’m trying to control with my own C# application, but so far, I haven’t been successful in doing so.

I have a pump that is connected to the output of my switch through a contactor (I have a snubber on the coil terminals for protection) that I control to help supply water to my apartment when the pressure is really low. So far, everything is working well until it’s time to use the washing machine. I need to start the pump whenever the washing machine draws water (or it errors out), but it’s a hassle to do since it doesn’t draw water at fixed intervals for a fixed duration all the time, which makes creating a scene for this task rather difficult.

Previously, I created a small Arduino project using a water flow sensor to detect water flow and create visual and audible alerts, so I know the washing machine is drawing water and start the pump manually. I can use it now to start the pump using my R4, but I was thinking if I can automate the whole process with something like a NodeMCU board.

Before I ordered anything, I wanted to make sure I could control the R4 using a custom application (that gets its data from the NodeMCU). First, I tried a WebSocket connection (ws://R4-IP:8081), but I kept receiving error 400 at the handshake.

I managed to get the apiKey using NetCapture on my phone when I captured an HTTP request to the SONOFF. It looked like this:

The headers:

http://IP:8081/zeroconf/switches
POST /zeroconf/switches HTTP/1.1
accept: application/json
user-agent: eWeLink_Android/v5.21.1
cache-control: no-store
Content-Type: application/json;charset=UTF-8
Content-Length: 333
Host: IP:8081
Connection: Keep-Alive
Accept-Encoding: gzip

The request:

{
	"sequence":"1763689250783",
	"deviceid":"xxxxxxxxxx",
	"selfApikey":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
	"iv":"MTU0NzgzMTgzNDgwNjQ5OQ==",
	"encrypt":true,
	"data":"GG/HPXHUD5HIlke1HBNkWEfP0OgkkNTwrPJp6b8i96C1A+jmqWdtxf5c7Thnv+OQPR/OAeHJsvVdR+g2bWZZNBX3hXDkThBssrDRLvJIrBZUYcRHrNakX0hwfdHRST3fPGgSBlEZ6OjatMPKhRLsS0lSRT9WO1KeyQ30HYk7owY="
}

I tried to replicate the same request by using the selfApikey to encrypt the payload, which I think should be like this:

{
    "params": {
        "switch": "on"
    }
}

I stripped the dashes from the selfApikey to create an AES 256-bit key (while retaining them in the request variable), and encrypted the payload using the key and IV (after converting it back to a normal string from base64). Finally, I sent the request to the HTTP address, and I got an error code 500, BUT the request would return a “success” response from time to time that looks like this:

Success response: {"error":0,"sequence":"1763748541651","deviceid":"xxxxxxxxxx","encrypt":true}

However, nothing happens.

I created the sequence using:

DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()).ToString()

So, my question now is, am I doing something wrong, or is it just not possible this way? My last resort would be a premium subscription to use IFTTT paired with Adafruit IO.

Update 1: I managed to control my device using my computer by applying to be a dev, using the provided appID and appSecret to authenticate (freakin’ confusing) and control the device. I can’t make more than 50,000 requests a month, so no constant status updates every second. I settled for 1-minute status updates and status updates upon pressing the on/off button (it’ll detect the current status and reverse it).



It took me some time to replicate SONOFF’s button look lol.

I think I can use my tokens to communicate with the device using a Wi-Fi-equipped microcontroller board.

No experience with the R4 but why don’t you just put Tasmota on it using their web installation if you have a USB to Serial device? Seems a lot easier than trying to figure out the device protocol.

And pretty sure there is a MQTT library for the Arduino ide so you could just repurpose the Arduino if it is connected to wifi.

I would equate your project to killing a mosquito with an F35fighter jet.

  1. Why can’t you just fix a low oreassure sensor to your water pump, or replace it with a small water pump with an inbuilt water preassure sensor. Those pumps are very affordable these days.

  2. Create an automation, or scene that will switch on the preassure pump using the BASIC R4, whenever the washing machine is switched on. And switch off the water pump when it is switched off, or inoperational. - (If Washing machine is ON, Switch On BASIC R4, if Washing machine is OFF switch off basic R4)- connect BASIC R4 to switch on.and off the water pump.

  3. Let tte pump activate itselfwhenever there is low water preassure whenever the washing machine starts to draw water. And will.stop pumping water when it stops to draw water.

Don’t you need to.use tge pump for other purposes beyond the washing machine?

You could connect to the washing machine water inlet solenoid valve(s) so that when the programmer switches valve(s) ON to fill with water it operates another device (Sonoff extreme Mini R4 wi-fi ?) to trigger your pump R4. It does mean making the connection inside the washing machine and there would be an implication on the washing machine’s guarantee- if it is still under warranty

I forgot to mention that I don’t want to flash it since it’s usually a one-way street process, and the device is shared with my family.

lol, the pump actually has an inline pressure monitoring device, but it’s not calibrated right and lets the pump run for way too long before it turns it off, which caused a few damaged pipes and one burst water heater. You can say I don’t have access to the pump, so I can only work from inside my apartment. Initially, I wanted to use a water pressure sensor like this, but I couldn’t find it anywhere locally, and its price is kinda ridiculous.

That’s some hardcore modification, dude :sweat_smile:. Very risky, and as you said, it would void the warranty.

I use this type of water preassure gauge (link.below). they are calibratable for both the high snd low preassures required. I replaced the factory preassure controller from the water pump and instead hooked it to this old school gauge.

My house needs to run at 2.0 bars to avoid damaging the solar geysers. These calibratable gauges were the best option.

If you have space, you can make it run even morre happier if you hooked the gauge to a 50 to 100 litre (or more)old truck.air preassure tank, get one from.the truck.scrap yard and connect it just after the water pump.

You can also use a pressure regulator that limits the maximum output pressure of the pump to protect the pipes in the house.

The problem is that I can’t place anything at the pump level right now, so I’m stuck with indoor solutions.

I’m not familiar with this kind of switch. Is it purely mechanical? I’m familiar with the crappy digital ones we have around here.

Yes, these switches are mechanical, the mechanical.components are connected to an electrical contact that switches ON/OFF the pump, depending on the preassure that has been set.

Once you.manage to.get this switch in the water supply, you will notice longer need the BASIC R4,. Unless you relegate it to activate or deactivate the system completely. Say, when you are on vacation.

1 Like

When you flash a Tasmota firmware to a device, you have the possibility of saving the original firmware.

I always do that, but, to be honest, since I never had the need to rollback to the original firmware, I can’t affirm that it works.

Maybe someone here that did it could confirm if it works.

1 Like