Language: EN

interactua-con-el-api-de-ewelink-desde-net-con-ewelinknet

Interact with the eWelink API from .NET with eWelinkNET

eWelinkNET is an API written in .NET Standard that allows us to interact directly with the eWelink API using the user credentials used by the official application.

With eWelinkNET, it is compatible with Windows, Linux and MAC, Android and iOS, and allows you to perform actions on devices or obtain measurements (temperature, humidity, power consumption…) from those devices that have such functionality.

Some of the key features of eWelinkNET are:

  • Turn devices on and off
  • Read measurements (humidity, temperature…)
  • Listen to device events via Websocket
  • Work in online and offline mode (also known as LAN mode or Zeroconf)
  • Multiplatform

Basic usage

Here is a basic example of how to use eWelinkNET.

var ewelink = new Ewelink(Email, Password, Region);
await ewelink.GetCredentials();
await ewelink.GetDevices();

var device = ewelink.Devices.First(x=> x.deviceid == deviceId) as SwitchDevice;
device.TurnOn();

Get Credentials

We can obtain the credentials necessary to perform the necessary actions using our Email, Password, and Region.

var ewelink = new Ewelink(Email, Password, Region);
var credentials = await ewelink.GetCredentials();

Alternatively, you can save the obtained credentials to avoid having to log in later.

ewelink.StoreCredenditalsFromFile();

Later, we can retrieve the credentials by doing.

ewelink.RestoreCredenditalsFromFile();

Get Devices

We can get the devices registered in your eWelink account.

var ewelink = new Ewelink(Email, Password, Region);
await ewelink.GetCredentials();
await ewelink.GetDevices();

The devices become the following classes.

  • SwitchDevice
  • MultiSwitchDevice
  • ThermostatDevice
  • RFBridgeDevice
  • CurtainDevice

All of them derive from the base class ‘Device’.

Interact with the devices

Each class has its own methods to perform the actions allowed by the type of device. Each class provides there own methods to perform actions or retrieve measurement.

For example, ‘ThermostatDevice’ provides,

  • TurnOn()
  • TurnOff()
  • Toggle()
  • GetTemperature()
  • GetHumidity()

While ‘MultiSwitchDevice’ provides,

  • TurnOn()
  • TurnOn(int channel)
  • TurnOff()
  • TurnOff(int channel)

Listen for changes in the device

Changes in the device’s state are obtained through a Websocket connection. The device launches the appropriate events when the state changes.

ewelink.websocket.OnMessage += (s, e) => Console.WriteLine(e.AsJson());
ewelink.OpenWebsocket();

In addition to launching the necessary events, the devices are updated internally according to the new state.

Zeroconf (LAN mode)

It is possible to interact with eWelink devices through LAN mode (also called Zeroconf), without the need for an internet connection or access to the eWelink cloud.

  • TurnOnLAN()
  • TurnOffLAN()

For LAN mode to work, a ArpTable (and list or the Mac - Ip relationship) has to be provided, to allow find the device Ip.

ewelink.RestoreArpTableFromFile();

Download the code

eWelink is an OpenSource development. The code for this post is available for download on GitHub. github-full