sagmill
Member
Hi,
I want to share the distance data measured by two ultrasonic sensors (hc-Sr04) by using Wemos D1 with other devices.
I launch Wemos as an access point and I can connect only one device.
but for multiple connection I have to use mqtt.
But I do not know how to set up mqtt in accesspoint mode.
this is my codes; Please help me fix it:
I want to share the distance data measured by two ultrasonic sensors (hc-Sr04) by using Wemos D1 with other devices.
I launch Wemos as an access point and I can connect only one device.
but for multiple connection I have to use mqtt.
But I do not know how to set up mqtt in accesspoint mode.
this is my codes; Please help me fix it:
B4R:
Sub Process_Globals
Public Serial1 As Serial
'server & socket & Wifi
Private server As WiFiServerSocket
Private wifi As ESP8266WiFi
Dim WiFiSocket1 As WiFiSocket
'Ultrasonic
Private Ultrasonic_astream As AsyncStreams
Private d1 As D1Pins
Private trigPin, echoPin As Pin
Private pulsduration As ULong 'ignore
Private client_astream As AsyncStreams
Private timer1 As Timer
Private timer2 As Timer
Private pin As Pin
Private bc As ByteConverter
Private Ultrasonic_pin As SoftwareSerial
Dim mqtt As MqttClient
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
If wifi.StartAccessPoint("Distance") Then
mqtt.Initialize(WiFiSocket1.Stream,bc.StringToBytes(wifi.AccessPointIp),51042,"Wemos","Mqtt_MessageArrived", "Mqtt_Disconnected")
Log("My ip: ",wifi.AccessPointIp)
Else
Log("Access Not started")
End If
'for read data from pin D6 and D7 instead tx/rx pin in first module ultrasonic distancer
Ultrasonic_pin.Initialize(9600,d1.D6,d1.D7)
Ultrasonic_astream.Initialize(Ultrasonic_pin.Stream,"Ultrasonic_astream_NewData", Null)
'for read data from pin D2 and D3 as trigger and echo pin in second ultrasonic distancer
trigPin.Initialize(d1.D2, trigPin.MODE_OUTPUT)
echoPin.Initialize(d1.D3, echoPin.MODE_INPUT)
'read data from second ultrasonic
timer2.Initialize("timer2_Tick", 1000)
timer2.Enabled = True
'for send data in a loop to a client
timer1.Initialize("timer1_Tick", 1000)
End Sub
'read data from first ultrasonic
Sub Ultrasonic_astream_NewData (Buffer() As Byte)
GlobalStore.Put(0,Buffer)
Log("data saved in slot 0")
End Sub
'read data from scond ultrasonic
Private Sub Timer2_Tick
trigPin.DigitalWrite(False)
DelayMicroseconds(2)
trigPin.DigitalWrite(True)
DelayMicroseconds(10)
trigPin.DigitalWrite(False)
RunNative("pulseins", echoPin.PinNumber)
Dim duration As Long = pulsduration
Dim distance As Long = duration / 58.2
GlobalStore.Put(1,bc.StringToBytes(distance))
Log("data saved in slot 1")
End Sub
#if C
void pulseins (B4R::Object* o) {
b4r_main::_pulsduration = pulseIn(o->toULong(),HIGH);
}
#End if
Sub Timer1_Tick
Dim firstData() As Byte
firstData=GlobalStore.Slot0
Dim secondData() As Byte
secondData=GlobalStore.Slot1
If firstData.Length>0 And secondData.Length>0 Then
mqtt.Publish("DIST",firstData)
mqtt.Publish("DIST",secondData)
Else
mqtt.Publish("DIST",bc.StringToBytes("no data"))
End If
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("Wemos", 0)
End Sub
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Log("Message arrived. Topic=", Topic, " payload: ", Payload)
End Sub
Sub Mqtt_Disconnected
Log("Disconnected")
mqtt.Close
Connect(0)
End Sub
Last edited: