/Water_Feature_Controller_Hackster

Code for Water Feature Controller Project on Hackster.io

Primary LanguageCOtherNOASSERTION

Water Feature Controller Project Entry for Hackster Connect Things with Code Contest

This is a repository for the Water Feature Project. The project is built using the Modus Toolbox Eclipse IDE and was created from the WiFi-MQTT-Client example application.

This project controls the pump and light for an outdoor water fountain using a Tuya compatible dual WiFi Smart Plug. The pump and light will turn on when there is presence detected on the adjacent patio. Presence detection is done using a Doppler radar shield. The MQTT client RTOS task establishes a connection with the configured MQTT server, and creates two tasks: publisher and subscriber. The publisher task publishes messages on a topic when presence is detected. A Node-Red program passes MQTT messages from the server to control the Tuya Smart plug. In case of unexpected disconnection of MQTT or Wi-Fi connection, the application executes a reconnection mechanism to restore the connection. The project also includes a local TFT display to monitor radar detection states and ambient light.

Sequence of operation

  1. WiFi is connected to access point.
  2. MQTT client is connected to MQTT server running on RPi4 on local network.
  3. MQTT client sets up publish and subscribe on topic "presencedetected".
  4. The GPIO interrupt service routine (ISR) notifies the publisher task when "target detected" signal is received from radar shield.
  5. The publisher task publishes message on "presencedetected" topic to indicate state of "target detected" signal (true/false).
  6. The MQTT server sends back the message to the MQTT client because it is also subscribed to the same topic.
  7. A Node-Red program (also running on the RPi4) is subscribed to the topic and forwards the MQTT messages to the Tuya Smart plug.

Requirements

  • ModusToolbox™ software v3.1 or later (tested with v3.1)
  • Board support package (BSP) minimum required version: 4.0.0
  • Programming language: C

Toolchain used (make variable 'TOOLCHAIN')

  • GNU Arm® embedded compiler v11.3.1 (GCC_ARM) - Default value of TOOLCHAIN

Kit used (make variable 'TARGET')

Project hardware

  • PSoC™ 62S2 Wi-Fi BT Pioneer Kit
  • S2GO RADAR BGT60LTR11
  • CY8CKIT-028-TFT

Configuring the MQTT client

Wi-Fi and MQTT configuration macros

Macro Description
Wi-Fi Connection Configurations In configs/wifi_config.h
WIFI_SSID SSID of the Wi-Fi AP to which the MQTT client connects
WIFI_PASSWORD Passkey/password for the Wi-Fi SSID specified above
WIFI_SECURITY Security type of the Wi-Fi AP. See cy_wcm_security_t structure in cy_wcm.h file for details.
MAX_WIFI_CONN_RETRIES Maximum number of retries for Wi-Fi connection
WIFI_CONN_RETRY_INTERVAL_MS Time interval in milliseconds in between successive Wi-Fi connection retries
MQTT Connection Configurations In configs/mqtt_client_config.h
MQTT_BROKER_ADDRESS Hostname of the MQTT broker
MQTT_PORT Port number to be used for the MQTT connection. As specified by IANA, port numbers assigned for MQTT protocol are 1883 for non-secure connections and 8883 for secure connections. However, MQTT brokers may use other ports. Configure this macro as specified by the MQTT broker.
MQTT_SECURE_CONNECTION Set this macro to 1 if a secure (TLS) connection to the MQTT broker is required to be established; else 0.
MQTT_USERNAME
MQTT_PASSWORD
User name and password for client authentication and authorization, if required by the MQTT broker. However, note that this information is generally not encrypted and the password is sent in plain text. Therefore, this is not a recommended method of client authentication.
MQTT Client Certificate Configurations In configs/mqtt_client_config.h
CLIENT_CERTIFICATE
CLIENT_PRIVATE_KEY
Certificate and private key of the MQTT client used for client authentication. Note that these macros are applicable only when MQTT_SECURE_CONNECTION is set to 1.
ROOT_CA_CERTIFICATE Root CA certificate of the MQTT broker
MQTT Message Configurations In configs/mqtt_client_config.h
MQTT_PUB_TOPIC MQTT topic to which the messages are published by the Publisher task to the MQTT broker
MQTT_SUB_TOPIC MQTT topic to which the subscriber task subscribes to. The MQTT broker sends the messages to the subscriber that are published in this topic (or equivalent topic).
MQTT_MESSAGES_QOS The Quality of Service (QoS) level to be used by the publisher and subscriber. Valid choices are 0, 1, and 2.
ENABLE_LWT_MESSAGE Set this macro to 1 if you want to use the 'Last Will and Testament (LWT)' option; else 0. LWT is an MQTT message that will be published by the MQTT broker on the specified topic if the MQTT connection is unexpectedly closed. This configuration is sent to the MQTT broker during MQTT connect operation; the MQTT broker will publish the Will message on the Will topic when it recognizes an unexpected disconnection from the client.
MQTT_WILL_TOPIC_NAME
MQTT_WILL_MESSAGE
The MQTT topic and message for the LWT option described above. These configurations are applicable only when ENABLE_LWT_MESSAGE is set to 1.
MQTT_DEVICE_ON_MESSAGE
MQTT_DEVICE_OFF_MESSAGE
The MQTT messages that control the device (LED) state in this code example.
Other MQTT Client Configurations In configs/mqtt_client_config.h
GENERATE_UNIQUE_CLIENT_ID Every active MQTT connection must have a unique client identifier. If this macro is set to 1, the device will generate a unique client identifier by appending a timestamp to the string specified by the MQTT_CLIENT_IDENTIFIER macro. This feature is useful if you are using the same code on multiple kits simultaneously.
MQTT_CLIENT_IDENTIFIER The client identifier (client ID) string to be used during MQTT connection. If GENERATE_UNIQUE_CLIENT_ID is set to 1, a timestamp is appended to this macro value and used as the client ID; else, the value specified for this macro is directly used as the client ID.
MQTT_CLIENT_IDENTIFIER_MAX_LEN The longest client identifier that an MQTT server must accept (as defined by the MQTT 3.1.1 spec) is 23 characters. However, some MQTT brokers support longer client IDs. Configure this macro as per the MQTT broker specification.
MQTT_TIMEOUT_MS Timeout in milliseconds for MQTT operations in this example
MQTT_KEEP_ALIVE_SECONDS The keepalive interval in seconds used for MQTT ping request
MQTT_ALPN_PROTOCOL_NAME The application layer protocol negotiation (ALPN) protocol name to be used that is supported by the MQTT broker in use. Note that this is an optional macro for most of the use cases.
Per IANA, the port numbers assigned for MQTT protocol are 1883 for non-secure connections and 8883 for secure connections. In some cases, there is a need to use other ports for MQTT like port 443 (which is reserved for HTTPS). ALPN is an extension to TLS that allows many protocols to be used over a secure connection.
MQTT_SNI_HOSTNAME The server name indication (SNI) host name to be used during the transport layer security (TLS) connection as specified by the MQTT broker.
SNI is extension to the TLS protocol. As required by some MQTT brokers, SNI typically includes the hostname in the "Client Hello" message sent during TLS handshake.
MQTT_NETWORK_BUFFER_SIZE A network buffer is allocated for sending and receiving MQTT packets over the network. Specify the size of this buffer using this macro. Note that the minimum buffer size is defined by the CY_MQTT_MIN_NETWORK_BUFFER_SIZE macro in the MQTT library.
MAX_MQTT_CONN_RETRIES Maximum number of retries for MQTT connection
MQTT_CONN_RETRY_INTERVAL_MS Time interval in milliseconds in between successive MQTT connection retries

Document history

Version Description of change
1.0.0 Initial Commit