Arrinew / SWA1 WiFi power switch

With home automation more early than later this cheap WiFi power switches come into the game. I use (and build up continuously) a home automation system based on Home assistant (hass) and was looking for a possibility to switch on and off two lamps for plants (yes, also for my Chili plantage 😉 ).

This Wifi switches usually connect to their producers cloud service. All commands go first from the house over the internet to the server and than back over the internet to the switch. This is an easy to establish but very ugly thing. First, nobody else has any right to know, what in my house is controled and second, if the Internet connection or the producers server is down, the system doesn’t work. And beleave me, this is more often the case as someone may assume.

But fortunately nearly all WiFi power switches use the same chip and they can usually programed with an own or alternative firmware so they never have to see their cloud service in their whole life. So, my own home automation system takes over the role of the producers cloud.

At the German Amazon site I found a cheap pair of such WiFi power switches which are sold unter the label “Arrinew”. I think, their may be sold also under various other labels. Therefore, more unique is the model “SWA1” and the label on the internal PCB board 101-SWA1351-361

This power switches have realy a good mechanical quality. Also the electronics inside looks very significant. Unbeleaveable what they sell for under 10,-€/piece.

Who want’s to read further should know what an ESP8266 is and also MQTT should be a known concept.

The objective of this article is to replace the firmware of the power switch with an own program to integrate it into my own hosted home automation system. There are principially two ways: Use of one of the free operating systems for ESP-family (Takoma, EasyESP) or write an own software with the Arduino libraries and environment. Because I want learn a little bit, I like transparency and doesn’t like to struggle with various problems of the ESP-operating systems and finaly because I found a good example for such a firmware, I choose the second way and set up an own firmware.

The Arrinew power switches are prepared to be programmed in such a way that the required contacts are internally available. But they are not proper labeled and the ESP-chip is covered by a metal plate, so that the contacts cannot be followed visually or a continuitiy check. Luckily by googling the PCB board label I found this page where somebody has done great work by hacking these information.

An interesting detail is further that there is also an unused GPIO-pin available. Because there are never enough temperature sensors spreaded in the house this pin asks for beeing used to connect a digital one wire temperatur sensor. But this is a topic for another post.

As mentioned, the program for the power switch I have also stolen from Matt and have it a little bit modified. Now, it looks so:


// Flash mode: DOUT
// Flash Frequency: 40MHz
// Flash Size: 1MB (512kB SPIFFS)
// Reset method: None
// Crystal Frequency: 26MHz
// CPU frequency: 160MHz
// Upload speed: 921600

// Button: GPIO13
// Blue LED: GPIO4
// Red LED + Relay: GPIO5
// Extra Pin: GPIO14, used foor one-wire temperaure sensor

#include <ESP8266WiFiMulti.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <DebouncedInput.h>
#include <OneWire.h>
#include <DallasTemperature.h>


ESP8266WiFiMulti wifiMulti;
WiFiClient espClient;
PubSubClient client(espClient);

#define ONE_WIRE_BUS 14  // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


bool powerState = false;
unsigned long previousMillis=0;

#define NAME "name for OTA"
#define OTAPW "password for OTA updates"
#define BROKERUSER "MQTT broker user"
#define BROKERPASS "MQTT broker password"

// define here your MQTT topics
#define SUBTOPIC "/powerplugs/switch02/command"
#define PUBTOPIC "/powerplugs/switch02/status"
#define TEMPTOPIC "/powerplugs/switch02/temp"

#define POWERPIN 5
#define BUTTONPIN 13
#define LEDPIN 4
#define TEMP_INTERVALL 10000  // temperature is measured every 10s

DebouncedInput button(BUTTONPIN, 10, true);

void callback(char *topic, byte *payload, unsigned int length) {
    if (!strcmp(topic, SUBTOPIC)) {
        if (!strncmp((char *)payload, "on", length)) {
            digitalWrite(POWERPIN, HIGH);
            powerState = true;
            client.publish(PUBTOPIC, "on", true);
        } else if (!strncmp((char *)payload, "off", length)) {
            digitalWrite(POWERPIN, LOW);
            powerState = false;
            client.publish(PUBTOPIC, "off", true);
        }
    }
}

void setup() {
    button.begin();

    WiFi.mode(WIFI_STA);

    pinMode(POWERPIN, OUTPUT);
    pinMode(LEDPIN, OUTPUT);
    digitalWrite(POWERPIN, LOW);
    digitalWrite(LEDPIN, LOW);

    client.setServer("IP of your MQTT broker", 1883);
    client.setCallback(callback);

    // Add more of these for access to more APs.
    wifiMulti.addAP("WiFi name", "WiFi password");

    ArduinoOTA.setHostname(NAME);
    ArduinoOTA.setPassword(OTAPW);
    ArduinoOTA.begin();

    DS18B20.begin();
}

void loop() 
{
    static uint32_t ts = millis();
    ArduinoOTA.handle();
    client.loop();

    if (wifiMulti.run() != WL_CONNECTED) {
        delay(100);
    } else {
        if (!client.connected()) {
            delay(100);
            if (client.connect(NAME, BROKERUSER, BROKERPASS)) {
                client.subscribe(SUBTOPIC);
            }
        } else {
            if (millis() - ts >= 5000) {
                ts = millis();
                client.publish(PUBTOPIC, powerState ? "on" : "off", true);
            }
        }
    }

    if (button.changedTo(LOW)) {
        powerState = !powerState;
        digitalWrite(POWERPIN, powerState);
        if (client.connected()) {
            client.publish(PUBTOPIC, powerState ? "on" : "off", true);
        }
    }

    digitalWrite(LEDPIN, !client.connected());

  unsigned long currentMillis = millis();
 
  // if you don't use the temperature sensor, comment out the following block
  if(client.connected() && (currentMillis - previousMillis) > TEMP_INTERVALL){
    char h[20];
    float temp;
    DS18B20.requestTemperatures(); 
    temp = DS18B20.getTempCByIndex(0);
    sprintf(h,"%.1f",temp);
    client.publish(TEMPTOPIC,h,true);
    previousMillis=currentMillis;
  }

}

To compile and program this software the Arduino-IDE can be used. There are countless instructions and tutorials. Thanks of the used libraries the code is fairly short.

The following things are worth to be to be mentioned:
Folgende Sachen sind mir aufgefallen:

  • There are two LEDs available. The blue LED is on if the power switch has a connection to the MQTT broker (WiFi works, MQTT broker is available). This is also a good indicator, if the home automation runs. the second LED is red an is on if the power of the plug is switched on.
  • The Arduino library has at least in some conditions problems to resolve local hostnames (from the Fritzbox router) . I had to enter the IP-number of the MQTT broker instead their hostname.
  • The WiFi-Name it case sensitiv
  • The power switch needs as MPTT payload “on” and “off”. But this can be changed in the source code.
This entry was posted in home automation and tagged , , . Bookmark the permalink.

One Response to Arrinew / SWA1 WiFi power switch

  1. Peter says:

    Thank you ! With your diagram showing the pin names, I successfully flashed tasmota to my smart plug.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.