Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MQTT support. #62

Merged
merged 10 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)


project(fanpico
VERSION 1.5.2
VERSION 1.6.0
LANGUAGES C CXX ASM
)
set(CMAKE_C_STANDARD 11)
Expand All @@ -19,7 +19,11 @@ set(FANPICO_BOARD 0804D CACHE STRING "Fanpico Board Model")
#set_property(CACHE FANPICO_BOARD PROPERTY STRINGS 0804 0803D)
set(FANPICO_CUSTOM_THEME 0 CACHE STRING "Fanpico LCD Custom Theme")
set(FANPICO_CUSTOM_LOGO 0 CACHE STRING "Fanpico LCD Custom Logo")
set(TLS_SUPPORT 0 CACHE STRING "TLS Support")

set(TLS_SUPPORT 1 CACHE STRING "TLS Support")
# Generate some "random" data for mbedtls (better than nothing...)
set(EXTRA_ENTROPY_LEN 64)
string(RANDOM LENGTH ${EXTRA_ENTROPY_LEN} EXTRA_ENTROPY)

#set(CMAKE_BUILD_TYPE Debug)

Expand All @@ -28,6 +32,7 @@ message(" FANPICO_BOARD: ${FANPICO_BOARD}")
message(" PICO_BOARD: ${PICO_BOARD}")
message("FANPICO_CUSTOM_THEME: ${FANPICO_CUSTOM_THEME}")
message(" FANPICO_CUSTOM_LOGO: ${FANPICO_CUSTOM_LOGO}")
message(" TLS_SUPPORT: ${TLS_SUPPORT}")
message(" CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message("---------------------------------")

Expand Down Expand Up @@ -90,6 +95,7 @@ add_executable(fanpico
src/square_wave_gen.c
src/pulse_len.c
src/util.c
src/util_rp2040.c
src/log.c
src/crc32.c
src/default_config.s
Expand Down Expand Up @@ -149,11 +155,13 @@ if (PICO_CYW43_SUPPORTED) # set by PICO_BOARD=pico_w
target_sources(fanpico PRIVATE
src/syslog.c
src/httpd.c
src/mqtt.c
)
target_link_libraries(fanpico PRIVATE
pico_cyw43_arch_lwip_threadsafe_background
pico_lwip_sntp
pico_lwip_http
pico_lwip_mqtt
)
if (TLS_SUPPORT)
target_link_libraries(fanpico PRIVATE
Expand All @@ -176,9 +184,12 @@ target_compile_definitions(fanpico PRIVATE USBD_DESC_STR_MAX=32)
target_compile_definitions(fanpico PRIVATE PARAM_ASSERTIONS_ENABLE_ALL=1)
target_compile_definitions(fanpico PRIVATE PICO_MALLOC_PANIC=0)
target_compile_definitions(fanpico PRIVATE PICO_DEBUG_MALLOC=0)
target_compile_definitions(fanpico PRIVATE PICO_DEOPTIMIZED_DEBUG=0)
target_compile_definitions(fanpico PRIVATE PICO_STACK_SIZE=0x1000)
#target_compile_definitions(fanpico PRIVATE PICO_DEOPTIMIZED_DEBUG=1)
target_compile_definitions(fanpico PRIVATE PICO_STACK_SIZE=0x2000)
target_compile_definitions(fanpico PRIVATE PICO_CORE1_STACK_SIZE=0x1000)
target_compile_definitions(fanpico PRIVATE PICO_USE_STACK_GUARDS=1)
target_compile_definitions(fanpico PRIVATE PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1)

pico_set_linker_script(fanpico ${CMAKE_SOURCE_DIR}/src/memmap_custom.ld)

# eof :-)
261 changes: 261 additions & 0 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ Fanpico supports following commands:
* [SYStem:LED](#systemled)
* [SYStem:LED?](#systemled-1)
* [SYStem:MBFANS?](#systemmbfans)
* [SYStem:MQTT:SERVer](#systemmqttserver)
* [SYStem:MQTT:SERVer?](#systemmqttserver-1)
* [SYStem:MQTT:PORT](#systemmqttport)
* [SYStem:MQTT:PORT?](#systemmqttport-1)
* [SYStem:MQTT:USER](#systemmqttuser)
* [SYStem:MQTT:USER?](#systemmqttuser-1)
* [SYStem:MQTT:PASSword](#systemmqttpassword)
* [SYStem:MQTT:PASSword?](#systemmqttpassword-1)
* [SYStem:MQTT:INTerval](#systemmqttinterval)
* [SYStem:MQTT:INTerval?](#systemmqttinterval-1)
* [SYStem:MQTT:SCPI](#systemmqttscpi)
* [SYStem:MQTT:SCPI?](#systemmqttscpi-1)
* [SYStem:MQTT:STATus](#systemmqttstatus)
* [SYStem:MQTT:STATus?](#systemmqttstatus-1)
* [SYStem:MQTT:COMMand](#systemmqttcommand)
* [SYStem:MQTT:COMMand?](#systemmqttcommand-1)
* [SYStem:MQTT:RESPonse](#systemmqttresponse)
* [SYStem:MQTT:RESPonse?](#systemmqtresponse-1)
* [SYStem:MQTT:TLS](#systemmqtttls)
* [SYStem:MQTT:TLS?](#systemmqtttls-1)
* [SYStem:NAME](#systemname)
* [SYStem:NAME?](#systemname-1)
* [SYStem:SENSORS?](#systemsensors)
Expand Down Expand Up @@ -1607,6 +1627,247 @@ SYS:MBFANS?
4
```

### SYStem:MQTT Commands
FanPico has MQTT Client that can be confiugred to publish (send) periodic status
updates to a topic.
Additionally MQTT Client support subscribing to a "command" topic to listen for commands.
This allows remotely controlling BrickPico.

To enable MQTT at minimum server must be configured. To explicitly disbable MQTT set server
to empty string.


#### SYStem:MQTT:SERVer
Set MQTT server to connect to. This parameter expects a DNS name as argument.

Default: <empty> (when this setting is empty string, MQTT is explicitly disabled)

Example (configure MQTT server name):
```
SYS:MQTT:SERVER io.adafruit.com
```

Example (disable MQTT):
```
SYS:MQTT:SERVER
```

#### SYStem:MQTT:SERVer?
Query currently set MQTT server name.

Example:
```
SYS:MQTT:SERVER?
io.adafruit.com
```


#### SYStem:MQTT:PORT
Set MQTT server (TCP) port. This setting is needed when MQTT server is not using standard port.
If this setting is not set (value is left to default "0"), then standard MQTT port is used.

- Secure (TLS) Port = 8883
- Insecure Port = 1883

Default: 0 (when this setting is 0 use default MQTT ports)

Example:
```
SYS:MQTT:PORT 9883
```


#### SYStem:MQTT:PORT?
Query currently set MQTT (TCP) port.

If return value is zero (0), then default MQTT port is being used.

Example:
```
SYS:MQTT:PORT?
0
```


#### SYStem:MQTT:USER
Set MQTT username to use when connecting to MQTT server.

Default: <empty>

Example:
```
SYS:MQTT:USER myusername
```


#### SYStem:MQTT:USER?
Query currently set MQTT username.

Example:
```
SYS:MQTT:USER?
myusername
```


#### SYStem:MQTT:PASS
Set MQTT password to use when connecting to MQTT server.

Default: <empty>

Example:
```
SYS:MQTT:PASS mymqttpassword
```


#### SYStem:MQTT:PASS?
Query currently set MQTT password.

Example:
```
SYS:MQTT:PASS?
mymqttpassword
```


#### SYStem:MQTT:STATus
Configure topic to publish unit status information periodically.
If this is left to empty (string), then no status information is published to MQTT server.

Default: <empty>

Example:
```
SYS:MQTT:STATUS musername/feeds/brickpico1
```


#### SYStem:MQTT:STATus?
Query currently set topic for publishing unit status information to.

Example:
```
SYS:MQTT:STATUS?
myusername/feeds/fanpico1
```


#### SYStem:MQTT:INTerval
Configure how often unit will publish (send) status message to status topic.
Set this to 0 (seconds) to disable publishing status updates.
Recommended values are 60 (seconds) or higher.

Default: 600 (every 10 minutes)

Example:
```
SYS:MQTT:INTERVAL 3600
```


#### SYStem:MQTT:INTerval?
Query currently set topic for publishing unit status information to.

Example:
```
SYS:MQTT:INTERVAL?
3600
```


#### SYStem:MQTT:SCPI
Configure if SCPI all commands will be accepted via MQTT.
If this is not enabled then only "WRITE" commands are allowed.

This is potentially "dangerous" feature, so only enable if you understand
the potential risks allowing device to be remotely configured.

Default: OFF

Example:
```
SYS:MQTT:SCPI ON
```


#### SYStem:MQTT:SCPI?
Query whether all SCPI commands are allowed via MQTT.


Example:
```
SYS:MQTT:SCPI?
OFF
```


#### SYStem:MQTT:COMMand
Configure topic to subscribe to to wait for commands to control outputs.
If this is left to empty (string), then unit won't subcrible (and accept) any commands from MQTT.

Default: <empty>

Example:
```
SYS:MQTT:STATUS musername/feeds/cmd
```


#### SYStem:MQTT:COMMand?
Query currently set topic for subscribing to wait for commands.

Example:
```
SYS:MQTT:STATUS?
myusername/feeds/cmd
```


#### SYStem:MQTT:RESPonse
Configure topic to publish responses to commands received from the command topic.
If this is left to empty, then unit won't send response to any commands.

Default: <empty>

Example:
```
SYS:MQTT:STATUS musername/feeds/response
```


#### SYStem:MQTT:RESPonse?
Query currently set topic for publishing reponses to commands.

Example:
```
SYS:MQTT:STATUS?
myusername/feeds/response
```


#### SYStem:MQTT:TLS
Enable/disable use of secure connection mode (TLS/SSL) when connecting to MQTT server.
Default is TLS on to protect MQTT credentials (usename/password).

Default: ON

Example:
```
SYS:MQTT:TLS OFF
```


#### SYStem:MQTT:TLS?
Query whether TLS is enabled or disabled for MQTT.

Example:
```
SYS:MQTT:TLS?
ON
```



#### SYStem:NAME
Set name of the system. (Default: fanpico1)
Expand Down
39 changes: 39 additions & 0 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh
#
# build.sh
#

BUILD_DIR=./build

error() { echo "`basename $0`: $*"; }
fatal() { echo "`basename $0`: $*"; exit 1; }


# asm hack (CMake cant seem to handle .incbin in .s file)
for s_file in src/*.s; do
d_file=$(echo $s_file | sed -e 's/\.s/.json/')
if [ -s "$d_file" ]; then
if [ "$d_file" -nt "$s_file" ]; then
echo "$d_file updated: touch $s_file"
touch "$s_file"
fi
fi
done


[ -d "${BUILD_DIR}" ] || mkdir -p "${BUILD_DIR}" || fatal "failed to create: $BUILD_DIR"

cd "${BUILD_DIR}" || fatal "cannot access: $BUILD_DIR"

[ -s Makefile ] || cmake .. || fatal "cmake failed"

make -j
if [ $? -eq 0 ]; then
echo "Build successful."
picotool info -a fanpico.uf2
else
echo "Build failed."
exit 1
fi

# eof :-)
Loading