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

Interact with the eWelink API from .NET with eWelinkNET

  • 3 min

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

eWelinkNET is compatible with Windows, Linux, MAC, Android, and iOS, and allows performing actions on devices or retrieving measurements (temperature, humidity, power consumption…) from 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)
  • Cross-platform

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();
Copied!

Get credentials

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

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

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

ewelink.StoreCredenditalsFromFile();
Copied!

Later, we can retrieve the credentials by doing.

ewelink.RestoreCredenditalsFromFile();
Copied!

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();
Copied!

The devices are converted into the following classes.

  • SwitchDevice
  • MultiSwitchDevice
  • ThermostatDevice
  • RFBridgeDevice
  • CurtainDevice

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

Interact with devices

Each class has its own methods to perform the actions allowed by the device type. 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 to device changes

Changes in the device state are obtained through a WebSocket connection. The device triggers the appropriate events upon a state change.

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

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

Zeroconf (LAN mode)

It is possible to interact with eWelink devices via 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, an ARP table (a mapping of MAC-IP relationships) must be provided to allow finding the device IP. 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();
Copied!

Download the code

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