Relays are electronic switches that allow low power (voltage) devices to control high power circuits.
In this example the ESP8266 board controls a regular 220v light bulb.
*** High power electricity can be dangerous. Handle with care. ***
The relay and the wires must be boxed. Touching any element can be lethal.
(The video was removed, it showed an ESP8266 connected to a light bulb through the relay, and controlled by an Android device.)
Controlling the relay is simple.
The right side is connected to the high power circuit.
COM = Common line
NC = Normally close
NO = Normally open
The relay will connect or disconnect the COM and NO connections (and disconnect or connect the COM and NC connections).
The left side is connected to the board. Note that I'm using a 3.3v relay as I'm using a 3.3v board.
VCC - 3.3v pin
IN - The logic pin (d6 in the example)
GND
Setting the IN pin to high will connect the switch.
The Android is connected to the board with MQTT.
B4R code (libraries: rESP8266, rESP8266WiFi and rMQTT)
The B4A project is attached. I'm running an external MQTT broker. You can run it in your B4A project if you like.
In this example the ESP8266 board controls a regular 220v light bulb.
*** High power electricity can be dangerous. Handle with care. ***
The relay and the wires must be boxed. Touching any element can be lethal.
(The video was removed, it showed an ESP8266 connected to a light bulb through the relay, and controlled by an Android device.)
Controlling the relay is simple.
The right side is connected to the high power circuit.
COM = Common line
NC = Normally close
NO = Normally open
The relay will connect or disconnect the COM and NO connections (and disconnect or connect the COM and NC connections).
The left side is connected to the board. Note that I'm using a 3.3v relay as I'm using a 3.3v board.
VCC - 3.3v pin
IN - The logic pin (d6 in the example)
GND
Setting the IN pin to high will connect the switch.
The Android is connected to the board with MQTT.
B4R code (libraries: rESP8266, rESP8266WiFi and rMQTT)
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private d1pins As D1Pins
Private d6 As Pin
Private mqtt As MqttClient
Private wifi As ESP8266WiFi
Private client As WiFiSocket
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
d6.Initialize(d1pins.D6, d6.MODE_OUTPUT)
d6.DigitalWrite(False)
If wifi.Connect("dlink") = False Then
Log("Error connecting to router!")
Return
End If
'broker address is: 192.168.0.6:51042. Change as needed.
mqtt.Initialize(client.Stream, Array As Byte(192, 168, 0, 6), _
51042, "esp", "Mqtt_MessageArrived", "Mqtt_Disconnected")
Connect(0)
End Sub
Sub Connect(unused As Byte)
If mqtt.Connect = False Then
Log("trying to connect again")
CallSubPlus("Connect", 1000, 0)
Return
End If
Log("Connected to broker")
mqtt.Subscribe("esp", 0)
End Sub
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Log("Message arrived. Topic=", Topic, " payload: ", Payload)
If Topic = "esp" Then
Dim b As Boolean
b = Payload(0) = 1
d6.DigitalWrite(b)
End If
End Sub
Sub Mqtt_Disconnected
Log("Disconnected")
mqtt.Close
Connect(0)
End Sub
The B4A project is attached. I'm running an external MQTT broker. You can run it in your B4A project if you like.
Attachments
Last edited: