Remote invoke methods on your micro controller
Tags: esp32, mqtt, home, automation
Published: 2017-10-03 08:42:00

---
title: Remote invoke methods on your micro controller
published: true
description: When I've been started to do some home automation once again observed how hard is to write logic on micro controllers because of steps required for debugging. Also after installing device somewhere in the wall when you want to change something in the code you have to remove it to burn the cod. I decided to move all logic on a more powerful machine but still using micro-controllers for gpio/usart/i2c...
cover_image: https://camo.githubusercontent.com/1e51d4534b9495c44e059dbdc6a4ffbd55733ea0/687474703a2f2f692e696d6775722e636f6d2f337432587a7a4f2e706e67
tags: esp32, mqtt, home, automation
---

Few months ago I started to create a small device that will turn on/off the lights in my room, I needed this feature because in the evening I like to read, but turning off lights is a little bit painful because I have to stand up of my bed.

I've been started with ESP8266 controller because of incorporated Wifi and low price. First version of code used HTTP requests and executed actions based on GET calls (ex: put HIGH value on pin 13 was /action?pin=13&output=1 ), after first version I understood that controlling lights in other rooms will not be possible using the same code, project wasn't extendable in a easy way, I had to tune code for each room, adding additional parameter in request wasn't a good solution because I had to write source code on micro-controller customized for each device/room), also I had to remember IP addresses for calling actions. 

Imagine a mobile application where you have to add IP addresses for each device you want to control and you have to handle situation when your IP addresses changes - it's not that easy and UX will be a bad one.

Knowing drawbacks and writing new requirements, my decision was to rewrite the project from scratch using MQTT as data transfer protocol.

After reading some articles about MQTT, I've decided to write code that will allow to control unlimited devices and as additional requirement I wanted to use the same code to control other things from my house ( like reading temperature from sensors or writing/reading from analog pins ) with easy and minimal setup, without editing the source code for each micro controller.

I started with idea that a small "core" should execute any methods that it knows about and they should be callable over MQTT. At this moment you can use MetalIO, add your methods in /src/functions and just register them in /src/main.cpp:

```
//create new instance
auto digitalWriteFunction = new DigitalWriteFunction("digitalWrite");
//register to metalio core
metal->put(digitalWriteFunction);
```

Now you can call your method over MQTT as:
publish a message to your configured topic ( ex: /actions/room1 )

````
digitalWrite:13,1
````


One important thing is that MetalIO is fully configurable over Wifi, when you start device and click on BTN1 ( or any button that you can choose at compilation time, "TRIGGER_PIN" parameter in platformio.ini ), your device will create a named AP where you can connect and see config interface ( default address: 192.168.1.4 )

<img src="https://camo.githubusercontent.com/26f41e1c89ff7d0dde3f1fff8972b19618878e10/687474703a2f2f692e696d6775722e636f6d2f56596a477163542e706e67" width="200px" />

At this moment MetalIO software can be written on any ESPx, Arduino or Intel Galileo Board, configured over Wifi and any application/dashboard that supports MQTT can be used to communicate with hardware devices.


Project can be found on GitHub: https://github.com/daniftodi/metalio