B4R Tutorial ESP8266 + UDP + BMP180 = Simple weather station

Discussion in 'B4R Tutorials' started by Erel, Aug 22, 2016.

Similar threads

B4A Code Snippet [B4X] Convert bitmap to BMP format
B4R Tutorial Arduino + Raspberry Pi
B4R Code Snippet Firebase Push Messages (FCM)
B4R Library BMP180 - Pressure and temperature
B4R Tutorial SD cards
  1. Erel

    Erel Administrator Staff Member Licensed User

    This is an example of how UDP can be used to broadcast the current temperature and pressure to one or more clients.

    The nice thing about it is that there is no server involved. The ESP8266 just sends the packets to the broadcast address.


    RandomAccessFile is used to create the packets. B4R doubles are 4 bytes (equivalent to B4X floats).
    It is tempting to send the data as a string. However it is more efficient and actually simpler to deal with binary encoded numeric values.

    This is the main code of this example (B4R):
    Dim buffer(8As Byte
    Dim raf As RandomAccessFile
    raf.WriteDouble32(bmp180.LastResult, raf.CurrentPosition)
    0, bmp180.LastResult)
    raf.WriteDouble32(bmp180.LastResult, raf.CurrentPosition)
    usocket.BeginPacket(ip, port)
    The clients code:
    Private Sub usocket_PacketArrived (Packet As UDPPacket)
    If Packet.Length <> 8 Then
    Log("Invalid data")
    Dim raf As RandomAccessFile
         raf.CurrentPosition = Packet.Offset
    Dim temperature, pressure As Float
         temperature = raf.ReadFloat(raf.CurrentPosition)
         pressure = raf.ReadFloat(raf.CurrentPosition)
    Log($"Temperature: $1.1{temperature}°"$)
    Log($"Pressure: $1.1{pressure} mBar"$)
    End If
    End Sub
    The broadcast address is usually the network address with 255 as the fourth byte. The B4J program prints it.

    BMP180 library is available here: https://www.b4x.com/android/forum/threads/bmp180-pressure-and-temperature.67730/

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    The B4i client was updated. Fixed a bug where the socket became invalid while the app is in the background.
    sasidhar likes this.
  3. tonga

    tonga Member Licensed User

    Hi Erel,

    I'm starting to use ESP8266 so I have a board Nodemcu Amica. I try to use this board with BMP180 with this programs but it doesn't work.
    There are problems of compatibility? Or other?

  4. Erel

    Erel Administrator Staff Member Licensed User

    As you can see in this example, it does work with ESP8266.

    Try to connect the BMP180 to an Arduino and see whether it works.
  5. tonga

    tonga Member Licensed User

    BMP180 connected to board Nodemcu Amica works on IDE Arduino and I see the read values of temperature and pressure in the serial monitor. Instead I don't read nothing in log of B4R. In the display of Android Phone I see only the label "ESP8266 Weather"
  6. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread in the questions forum. You need to first be able to read the sensor value. Start with this code.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice