### ESP8266 BASIC Sample DHT11

ESP8266 NodeMCU HowTo - Get Temperature and Humidity from DHT11 sensor using a timer and send the data via UDP.
Connection:
DHT11 ---- ESP8266 (NodeMCU)
GND ------ GND
VCC ------ 3.3v
Signal --- D4
UDP Communication:
NodeMCU IP 192.168.0.4 with port 3661.
B4J Application IP 192.168.0.6 with broadcast port 3662 and listener port 3661.
Code:
ESPBasic on the NodeMCU and B4J UI-Application on the PC Device.
Note:
Instead of using a B4J UI-Application, an option is to create a B4J Webserver displaying the data received via UDP:
ESPBasic Code
'Set the sensor model (DHT11) and pin (D4)
dht.setup(11, D4)
'Start the timer every 10 seconds and run branch getdata
timer 10000, [getdata]
wait
'Branch getdata
[getdata]
celsius = dht.temp()
humidity = dht.hum()
'Option 1: Send the data to the UDP client listening to port 3661
'udpwrite "192.168.0.4", 3661, "Temperature " & celsius & "C, Humidity " & humidity & "%"
'Option 2: Send the data in JSON Format to the UDP client listening to port 3661
timestamp = chr(34) & time() & chr(34)
data$ = |{|
data$ = data$ & |"timestamp":|
data$ = data$ & timestamp
data$ = data$ & |,|
data$ = data$ & |"temperature":|
data$ = data$ & celsius
data$ = data$ & |,|
data$ = data$ & |"humidity":|
data$ = data$ & humidity
data$ = data$ & |}|
print data$
udpwrite "192.168.0.4", 3661, data$
wait
B4J Code
The layout uses TextFields, TextArea, ListView and a Button (see process globals).
For this example the UDP packet send functionality is not used, although part of the code (as taken frm a B4R HowTo).
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private UDPSocket1 As UDPSocket
Private taPacketSend As TextArea
Private btnSend As Button
Private edBroadcastPort As TextField
Private edListenerPort As TextField
Private edBroadcastHost As TextField
Private lvPacketReceived As ListView
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main")
MainForm.Title = "B4R HowTo UDP Communication"
MainForm.SetFormStyle("UTILITY")
AppInit
MainForm.Show
End Sub`
`Sub AppInit
'Set the ports
'The broadcast host & port is the Arduino local IP address with port = messages will send using these parameter
edBroadcastHost.Text = "192.168.0.6"
edBroadcastPort.Text = "3662"
'The listener port for incoming messages from the Arduino - defined in the Arduino as server port
edListenerPort.Text = "3661"
'UDP socket = EventName, Port, ReceiveBuffer)
Dim Port As Int = edListenerPort.Text
UDPSocket1.Initialize("UDP", Port, 8000)
lvPacketReceived.Items.InsertAt(0, $"${DateTime.Time(DateTime.Now)}:Nothing received"$)
taPacketSend.Text = "Hello World"
End Sub`
`Sub UDP_PacketArrived (Packet As UDPPacket)
Dim msg As String
msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
lvPacketReceived.Items.InsertAt(0, $"${DateTime.Time(DateTime.Now)}:${msg}"$)
End Sub`
`Sub UDP_PacketSend
Dim Packet As UDPPacket
Dim data() As Byte
data = taPacketSend.Text.GetBytes("UTF8")
Dim Port As Int = edBroadcastPort.Text
Packet.Initialize(data, edBroadcastHost.Text, Port)
UDPSocket1.Send(Packet)
End Sub`
`Sub btnSend_Action
UDP_PacketSend
End Sub`

Changed: , Category: ESP8266, File: T6C190M1364.htm, ID: 1364