B4R Question nodeMCU and hc-sr505 mini PIR

giggetto71

Active Member
Licensed User
Hi guys,
I am trying to get the HC-sr505 sensor working with nodeMCU but I get aways the same value logged from the sensor, I always get "1". I have tried both pin D1 and D2.
Any idea?


B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private server As WiFiServerSocket
    Private astream As AsyncStreams
    Private timer1 As Timer
    Private ser As B4RSerializator
    Private Pins As D1Pins
    Private CurrentValueFromSensor As Boolean
    Private Pin As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)    
    Log("AppStart")
     If wifi.Connect2("Infostrada-3442ED","xxxxx") Then 'change to your network SSID (use Connect2 if a password is required).
        Log("Connected to wireless network.")
        Log("My ip: ", wifi.LocalIp)
    Else
        Log("Failed to connect.")
        Return
    End If
    Pin.Initialize(Pins.D1,Pin.MODE_INPUT_PULLUP)
    timer1.Initialize("timer1_Tick", 3000)
    timer1.Enabled = True
    server.Initialize(51042, "server_NewConnection")
    server.Listen
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("Client connected")
    astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub

Sub Timer1_Tick
    Dim SensVal As Boolean
    SensVal = Pin.DigitalRead
    Log(SensVal)
    If SensVal <> CurrentValueFromSensor Then
        CurrentValueFromSensor = SensVal
        Log(SensVal)
        If server.Socket.Connected Then
            astream.Write(ser.ConvertArrayToBytes(Array("Time here is: ", SensVal)))
        End If
    End If
    
    
    If server.Socket.Connected Then
        astream.Write(ser.ConvertArrayToBytes(Array("Time here is: ", Millis)))
    End If
End Sub


Sub AStream_NewData (Buffer() As Byte)
    Dim be(10) As Object
    Dim data() As Object = ser.ConvertBytesToArray(Buffer, be)
    Log("Received:")
    For Each o As Object In data
        Log(o)
    Next
End Sub

Sub AStream_Error
    Log("Error")
    server.Listen
End Sub
 

thetahsk

Active Member
Licensed User
Hi guys,
I am trying to get the HC-sr505 sensor working with nodeMCU but I get aways the same value logged from the sensor, I always get "1". I have tried both pin D1 and D2.
Any idea?..
The HC-sr505 Sensor has an operating voltage range: DC 4.5-20V and your ESP works with 3.3 V. Have you considered this.
For your prototyping and first testing, disable Wi-Fi on your ESP and work only with the Log
function. There are interference problems between ESP Wi-Fi and HC-sr505. For good result
always use a metall case for the sensor. Check also if you need the pull up resistor for the port.
 
Last edited:
Top