B4R Tutorial Reed Switch Monitor Example

rwblinn

Well-Known Member
Licensed User
B4R & B4J experiment to monitor the state of a Reed switch connected to an ESP8266 - a NodeMCU is used. If the Reed switch state changes (triggered by a magnetic contact), data is send to a B4J server and stored in a MongoDB, which can be accessed via Browser (using WebSockets with port; shown in jQuery datatable). The B4J server and the MongoDB are running on a Raspberry Pi (RPi) 2B (or higher).

This experiment shows integration of several hardware components and the B4X tools B4R & B4J.
Has been great fun building and showing the results - esp the magical updates for all connected clients.

MongoDB
The Database for this experiment uses a classical field structure:
Database: reedmonitor; Collection: reedmonitor with fields: _id (object), timestamp (string), switch (string), state (string).
The _id field is generated with a unique value by the MongoDB as not explicitly set in the B4J server.
The _id field is hidden in the displayed jQuery datatable (index.html in the Objects\www folder).
Hint:
For Database maintenance or checking purposes, use terminal commands by
  • opening a Terminal
  • login as user Pi
  • run mongo (example from RPi terminal pi@33:~ $ mongo). Check out the help commands.

RPi MongoDB Installation
Open a terminal, login as user Pi and run:
  • sudo apt-get update
  • sudo apt-get upgrade
  • sudo apt-get install mongodb-server
Note: After installation the mongodb-server is started automatically and also after reboot.

B4J MongoDB Installation
Read the B4J Tutorial.
Installation:
Ensure to copy the required libraries from the file mongodb-native.zip & jMongoDB.zip to the B4J additional libraries folder.

B4J
While developing & testing, connect to the B4J-Bridge running on the RPi.
Ensure to copy the <project>/Objects/www folder to the RPi Tempjars folder.
This has to be done every time files (like the index.html) are changed in the www folder.
To access the B4J server running on RPi enter in a webbrowser the RPi-IP-Address-RPi:51042, e.g. 192.168.0.10:51042.
When data is updated in the MongoDB, all connected webbrowsers are updated (jQuery datatable refreshed).
This is a nice feature using websockets - done by using a map of connected id for which in a loop the class is used to refresh the datatable.

B4R
During development the NodeMCU is connected via USB to a development device running Windows.
After development, the NodeMCU is connected to the RPi.

Run the Experiment on a RPi
  • Upload the final B4R program to the NodeMCU (from the development device).
  • Connect the NodeMCU to a RPi USB port.
  • Create a RPi folder /home/pi/b4x/reedmonitor.
  • Copy from the development device B4J project folder:
    1) the file reedmonitor.jar to /home/pi/b4x/reedmonitor (must be in lowercase).
    2) the objects/www folder to /home/pi/b4x/reedmonitor/www.
  • Start reedmonitor.jar from the folder /home/pi/b4x/reedmonitor: sudo java -jar reedmonitor.jar. The output is shown in the terminal.
  • Open a webbrowser with URL RPi-IP-Address:51042, like 192.168.0.19:51042.
  • Play with the Reed switch by attach and detach a magnet.
  • The webbrowser table should be updated to all connected webbrowsers with the status of the Reed switch.

-Parts Needed
1x ESP8266 [used a NodeMCU 0.9 but can be any other ESP]
1x Reed Switch [used the Reed Mini from the Sunfounder Arduino Kit (handles 3.3v and 5v)]
1x LED Red
1x Raspberry Pi 2B or 3

-Wiring
ESP8266 = Reed Switch
3.3v = 3.3v (Green) --- for NodeMCU can also use the Vin pin as 5v
GND = GND (Pink)
D2 = Signal (Yellow)

ESP8266 = LED
D4 = Plus (Red)
GND = GND (Black)

-Screenshots

Download Source Code
B4R & B4J Project Source here. The source has additional documentation.

ToDo
  • Enhancements could be: additional Reed switches, make the server front end (index.html) responsive by using material design (w3.css or other).
 
Last edited by a moderator:
Top