A recurring question on some Arduino and ESP groups on Facebook goes like: "how to remotely control a relay?"
In this post I show a simple and quick way to remotely control a 2 Relay module through a mobile app using the RemoteXY service.
Pre-requisite to this post is a previous I have written : "Remote Control NodeMCU with RemoteXY" in which I introduce the development of mobile apps to control nodeMCU projects.
The Circuit
As shown in the above figure, the 2 Channel Relay module has 4 pins:
GND
IN1
IN2
Vcc
Those pins need to be wired in the following way:
Module: GND --> nodeMCU:GND
Module: IN1 --> nodeMCU:D4
Module: IN2 --> nodeMCU:D5
Module: Vcc --> nodeMCU:Vin
Note:
Those wire connections assume that the nodeMCU is powered with 5V. In such situation on the module Vin pin there will be 5V which is the right voltage to provide to the Relay module. If you power you nodeMCU with a different voltage, please make sure you adapt the voltage available on Vin to be exactly 5V.
Graphic Interface
Build this simple graphic interface by using 3 labels and 2 switch in the RemoteXY editor.
The switch si should be named:
switch_1
switch_2
Now in the configuration panel, bottom right, let's configure each switch in a way that they connect to pin D4 and D5 on the nodeMCU
Configuration
Let's configure RemoteXy in this way
Don't forget to specify your WiFi name and password
Source Code
Here the link of the shared RemoteXY project. Once on RemoteXY you can download the source code to upload on your nodeMCU
Testing
Below a short video showing the behavior of the application
Una domanda ricorrente sui gruppi Arduino e ESP di Facebook e': "come controllo un rele' da remoto?"
In questo post faccio vedere un modo semplice e veloce per controllare un modulo a 2 Rele' tramite un applicazione mobile sfruttando un servizio chiamato RemoteXY.
Come si vede in figura il modulo Relay a 2 canali espone 4 pin
GND
IN1
IN2
Vcc
Questi pin vanno collegati nel seguente modo:
Modulo: GND --> nodeMCU:GND
Modulo: IN1 --> nodeMCU:D4
Modulo: IN2 --> nodeMCU:D5
Modulo: Vcc --> nodeMCU:Vin
Nota:
Questi collegamenti assumono che il nodeMCU sia alimentato a 5 V.
Cio' significa che sul pin Vin ci saranno 5V adatti ad alimentare il modulo Releay.
Se alimentate il vostro nodeMCU con una tensione differente assicuratevi di adattare la tensione in modo che sul pin Vcc del modulo Releay ci siano 5 V
Interfaccia Grafica
Realizzate questa semplice interfaccia grafica dall'editor di RemoteXY usando 3 label e 2 switch.
Gli switch si devono chiamare:
switch_1
switch_2
Ora nel pannello di configurazione in basso a destra, configurate ognuno degli switch in modo da collegarli ai pin D4 e D5 di nodeMCU
Configurazione
Configurate RemoteXY in questo modo
Non dimenticatevi di impostare il nome della vostra rete WiFi e la password di accesso.
Codice
Qui il link del progetto condiviso su RemoteXY da cui potete scaricare il codice da caricare sul vostro nodeMCU
Funzionamento
Qui un video che dimostra il funzionamento dell'applicazione
a Web application to easily design and build a graphical interface and to generate the necessary source code for the controlling board (ESP8266, Arduino, ...)
a Mobile app for iOS and Android to remotely control over Bluetooth or WiFi the remote board
In order to create a complete application with RemoteXY it is necessary to follow 4 simple steps::
Step #1 : Graphical Interface
Building a graphical interface with a simple drag&drop editor available here
Step #2: Source Code Configuration
Configure and generate the sketch source code to be uploaded on the control board (ESP8266, Arduino)
Step #3: Upload Sketch
Compile and upload of the generated sketch on the control board (ESP8266,Arduino) through the Arduino IDE (or other supported IDE)
Step #4: Download Mobile App
Download the mobile app and install it on your client device (iOS, Android)
Configuration
After the design of the graphical interface it is necessary to configure the tool in order to generate the proper source code.
There are 4 different aspects to configure:
Connection
Four (4) type of connections are supported
Note: the iOS app only supports Bluetooth Low Energy aka BLE
Device (the control board)
The following platforms are supported
Communication Module
The following communication modules, to be paired with the control board, are supported:
Here some example of supported scenarios: Arduino Uno with Bluetooth HC-05 module, Arduino with ESP8266 or nodeMCU with ESP8266
IDE environment
Three (3) different development environment are supported:
LED control with nodeMCU
Let's now implement a simple use case: remotely control a LED through a mobile app and nodeMCU
GUI
Use the online editor to design a simple user interface by adding a label and a switch as depicted below
Now selecting the switch (ON/OFF) it is possible to configure some simple parameters in the Element section
For the sake of simplicity let's stick to the default parameters
Configuration
Now let's configure the code generator in the following way
This way we are telling to RemoteXY that we want to use a nodeMCU board as controller and the on-board ESP8266 as communication module and that our programming environment is Arduino.
The Connection is set to WiFi Access Point (AP) hence we are telling to RemoteXY that we want our nodeMCU to behave as a WiFi Access Point.
In such case it is necessary to configure the Module Interface section by specifying the SSID which will be broadcasted by the nodeMCU AP.
Let's also specify the TCP port on which the nodeMCU server will be listening for incoming connections.
Again for sake of simplicity let's check Open point meaning that we want an open WiFi network with no authentication.
Source Code
Now press the following button
RemoteXY will redirect us to a new page in which the generated source code is available for the user.
The source code and the RemoteXY library are available for download as zip files.
Let's now download the RemoteXY library and install it on Arduino IDE through the proper menu.
The download the source code
As we can see in the generated code some information from the previous config phase are present:
SSID of AP WiFI
Listening port
The RemoteXY_CONF[] section contains a description of the graphic interface. It will be transfer to the requesting mobile app once a connection with the nodeMCU is established.
Circuits
Now let's build the simple circuit depicted below by connecting a LED on the D4 pin of nodeMCU. Let's not forget the 100-220 Ohm limiting resistor.
Testing
Now let's compile the code and upload the binary to nodeMCU through the Arduino IDE.
Now on your mobile device (smartphone o tablet) join the new WiFi network called LedWiFi
It is now time to run the RemoteXY app on mobile and then click on the top tight + sign
Now select the WiFi point option
Now press the Connect button and the app will connect to the nodeMCU and it will download the GUI description
Now you are all set to remotely control the LED wired to the nodeMCU control board
un applicazione Web che permette di disegnare un interfaccia grafica e di generare il codice da far girare sulla scheda di controllo (ESP8266, Arduino, ...)
un applicazione mobile per iOS e Android che permette di controllare da remoto tramite bluetooth, wifi la scheda di controllo
Per creare un'applicazione completa con RemoteXY bisogna eseguire 4 semplici passi:
Passo #1 : Interfaccia Grafica
Disegnare l'interfaccia grafica con una comodo editor drag&drop disponibile qui
Passo #2: Configurazione Codice Sorgente
Configurare e generare il codice sorgente da caricare sul modulo di controllo (ESP8266, Arduino)
Passo #3: Upload Sketch
Compilare e caricare il codice sulla scheda di controllo (ESP8266,Arduino) tramite l'IDE Arduino (o tramite gli altri IDE supportati)
Passo #4: Download App mobile
Scaricare l'applicazione mobile ed installarla sul dispositivo client (iOS, Android)
Configurazione
Dopo aver disegnato l'interfaccia grafica della propria applicazione e' necessario configurare il tool per generare il codice in maniera corretta
Ci sono 4 aspetti da configurare:
La Connessione (Connection)
Sono supportate 4 tipi diversi di connessioni
Nota: l'applicazione per iOS supporta solo Bluetooth a bassa energia (BLE)
Il Device (la scheda di controllo)
Sono supportate le seguenti piattaforme
Il Modulo di Comunicazione usato
Sono supportati i seguenti moduli di comunicazione da accoppiare con le piattaforme supportate
Quindi per esempio e' supportato Arduino Uno con Bluetooth HC-05, oppure Arduino con ESP8266, oppure nodeMCU con ESP8266
L'IDE che si utilizza
Sono supportati 3 diversi ambienti di sviluppo
Accensione LED con nodeMCU
Vediamo ora un semplice esempio di come controllare lo stato di un LED tramite una semplice applicazione mobile e nodeMCU
GUI
Disegniamo tramite l'editor online una semplicissima interfaccia con una label e con uno switch
Ora cliccando sullo switch (ON/OFF) e' possibile configurare alcuni semplici parametri ad esso correlati nella sezione Element
Per semplicita' lasciamo i valori di default che vanno benissimo
Configurazione
Configuriamo il codice nel seguente modo
In questo modo stiamo dicendo a RemoteXY che vogliamo usare una scheda nodeMCU con ESP8266 per controllare il nostro led e che la vogliamo programmare con l'IDE Arduino.
La connessione e' di tipi Wi-Fi access point e quindi stiamo dicendo a RemoteXY che vogliamo che il nostro nodeMCU si comporti come un WiFi Access Point
Quindi e' necessario anche configurare la sezione Module Interface inserendo lo SSID della rete WiFi che verra' implementata da nodeMCU
Specifichiamo anche la porta su cui il nostro server,1 che gira su nodeMCU, rimarra' in ascolto.
Infine per semplicita' impostiamo il tutto come un Open point cioe' senza autenticazione e senza password
Codice
Premendo ora il pulsante
siamo rediretti ad una pagina in cui e' visualizzato il codice per nodeMCU e dove e' possibile scaricare il codice come file zip e anche la libreria RemoteXY in formato zip
A questo punto scarichiamo la libreria ed installiamola nell'IDE Arduino tramite l'apposito menu'.
Poi scarichiamo il codice
Come possiamo vedere nel codice generato ci sono le informazioni che abbiamo inserito in fase di configurazione:
SSID del AP WiFI
Porta del server
La sezione RemoteXY_CONF[] contiene una descrizione dell'interfaccia grafica che verra' inviata all'applicazione mobile una volta stabilita la connessione tra device mobile ed il nodeMCU
Circuito
Ora creiamo il semplicissimo circuito andando ad connettere un LED sul pin D4 del nodeMCU tramite una resistenza da 100-220 Ohm
Test
Ora compiliamo e carichiamo lo sketch sul nodeMCU tramite l'IDE Arduino
Ora tramite il vostro dispositivo mobile (smartphone o tablet) collegatevi alla nuova rete WiFi chiamata LedWiFi
A questo punto lanciate l'app RemoteXY e cliccate sul segno + in alto a destra.
Poi selezionate l'opzione WiFi point
Ora premete su Connect e l'applicazione si connettera' al vostro nodeMCU e scarichera' la definizione dell'interfaccia
Ora potete pilotare il led dal vostro telefono/tablet