Share My Creation Make Project Lighthouse65

Project Lighthouse65

Intro
Lighthouse65
is a Make project which initially started off with the goal on how to control the Raspberry Pi Pico W with peripherals via Bluetooth Low Energy (BLE).
Whilst exploring, the idea came to develop a model Lighthouse, using bricks, with actuators and sensors to be BLE controlled via PC or Android device.

lh65-doc-model.png


Brief Solution
The solution runs a MicroPython script on the Raspberry Pico W, acting as a BLE server with unique MAC address.
The BLE server enables to connect a single BLE client and listens to incoming data from that BLE client containing commands to get or set Lighthouse actuators and sensors.
A command is a JavaScript Object Notation (JSON) object with two keys depending on the target actuator or sensor.
An example of a command as JSON object sent from the BLE client:
B4X:
{'dev':'lhtl', 'blink':0|1}
This command sets the Lighthouse toplight blink state on (1) or off (0).
The command is executed and a response, also a JSON object is sent back to the BLE client.

lh65-doc-communication.png


Notes
The solution developed can be applied to other Make projects using Bluetooth Low Energy communication.
The Raspberry Pi Pico W has been selected as the BLE server because want to learn MicroPython programming on micro-controllers.
Whilst developing Lighthouse65, experienced a fast learning curve in using Micropython and Python resulting in ready-to-use and easy to enhance solutions.

BLE Server
MicroPython nightly build used, like MicroPython v1.20.0-536-gabb44694d on 2023-09-29; Raspberry Pi Pico W with RP2040.
MicroPython script with external packages picozero, micropython_ssd1306 and classes ble_advertising, ble_simple_peripheral (from the MicroPython BLE examples).
Serveral MicroPython classes & subclasses (from the external packages) created for the devices used, like door, light, rgbledflasher, oleddisplay.

BLE Clients
Several BLE clients are created using various development tools.
  • Windows / Linux Console script - Test the BLE server commands (Python, Thonny).
  • Android App - Control the Lighthouse65 including own functions (B4A).
  • Windows / Linux HMI Application - Control the Lighthouse65 in a SCADA (Supervisory Control and Data Acquisition) manner (B4J) requires the BLE command handler.
  • Windows / Linux Command Handler - BLE interface between client (with no BLE) & the BLE server (Python, Thonny).
Notes
  • Several B4X Custom Views developed, like LControlOnOff, LControlLEDDisplay, LControlStatus.
  • The BLE Command Handler can also be used to test the BLE server commands.
lh65-doc-b4jclient.png

lh65-doc-b4aclient.png


BLE Server Functions (Raspberry Pi Pico W MicroPython)
  • BLE client connect (Bluetooth build-in the Raspberry Pi Pico W).
  • BLE client connect state indicator (Yellow LED).
  • Lighthouse toplight with can flash, light or blink (RGB LED).
  • Pushbutton to stop the system (all devicess off)(DFRobot Pushbutton Yellow).
  • Lighthouse OLED display for status & weather information (OLED 124*64 pixel).
  • Entrance door controlled via servo motor to set state open or closed (Servo Tower Pro Mini).
  • Pushbutton to open or close the entrance door (DFRobot Pushbutton Green)
  • Entrance door light with state on or off and set the brightness (White LED).
  • Weather info like temperature, humidity, airpressure, wind speed/direction & cloud cover either simulated or via HTTP request from the German Weather Services (DWD).
  • Windturbine with info like rotor speed, power generated & fan speed controlled from weather windspeed information (Mini DC-Motor with fan).
  • Raspberry Pi Pico W Memory & Internal Temperature information request.
  • ... and many more ...
BLE Client Functions
The BLE client controls the BLE server functions for actuators and sensors connected to the Lighthouse.
In addition, the BLE client can also create own functions which are sent to the Lighthouse65.
Examples are:
Lighthouse Toplight Flash Pattern
The lighthouse toplight has a function to flash, means turn the toplight on/off following a pattern which is a sequence with intervals.
A pattern is defined as an array with tuples (led brightness 0 .. 1 (off|full brightness), state time in seconds), i.e. (1,on_time),(0,off_time),...
For example a simple pattern is (1,1),(0,1) means turn toplight on (1 means full brightness) for 1 second and next turn the toplight off (0 means no brightness) for 1 second.
These patterns are defined for real lighthouses.
For Lighthouse65 there are 3 phases simulated.
phases1 = [(1, 0.2), (0, 1)]
phases2 = [(1,0.3), (0,2.7), (1,0.3), (0,2.7), (1,0.3), (0,8.7), (1,0.3), (0,8.7)]
phases3 = [(0.05, 0.2), (0, 1)]

Weather Data
The BLE client requests in regular intervals weather data and sents the data to the Lighthouse65 to display on its OLED display.
For the BLE client solutions, weather information is requested from the German Weather Services (DWD).

Simulation
The BLE client simulates weather data and controls the windturbine depending wind speed or sets the OLED display.

Client with no BLE support
For clients which do not have BLE support or functionality, a BLE Command Handler has been developed.
The BLE Command Handler
  • is a Python script running in a console and listens to client commands or BLE server responses.
  • is started from the client, i.e. B4J UI application.
  • accepts console input strings holding commands and sent the command to the BLE server.
    The BLE server response is sent back to the client via stderr.
Client with no BLE support - Communication Steps
  1. Client starts the BLE Command Handler.
  2. BLE Command Handler connects to the BLE server (Raspberry Pi Pico W).
  3. Client sends command as JSON string to the BLE Command Handler Input.
  4. BLE Command Handler sends command as JSON object via BLE to the BLE server.
  5. BLE server acts upon the command.
  6. BLE server sends response to the BLE Command Handler.
  7. BLE Command Handler listens to BLE server notifications and sends the BLE server response to the client via stderr.
  8. Client acts upon the response received from the BLE Command Handler.
Credits
  • Anywhere software for their suite of products
  • @klaus for the xGauges B4XLib
Source Code
Can not share the source code as rather specific & large.
 
Last edited:
Top