Skip to content

Commit

Permalink
MQTT support in lvgl simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausMu committed Mar 18, 2024
1 parent 73caf51 commit 665b690
Show file tree
Hide file tree
Showing 29 changed files with 4,672 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-platformio-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ jobs:
working-directory: ./Platformio
run: pio run -e esp32

- name: Build PlatformIO env:windows_linux_64bit
- name: Build PlatformIO env:linux_64bit
working-directory: ./Platformio
run: pio run -e windows_linux_64bit
run: pio run -e linux_64bit
8 changes: 4 additions & 4 deletions .github/workflows/build-platformio-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ jobs:
working-directory: ./Platformio
run: pio run -e esp32

- name: Build PlatformIO env:windows_linux_64bit
- name: Build PlatformIO env:windows_64bit
if: ${{matrix.sys == 'mingw64'}}
working-directory: ./Platformio
run: pio run -e windows_linux_64bit
run: pio run -e windows_64bit

- name: Build PlatformIO env:windows_linux_32bit
- name: Build PlatformIO env:windows_32bit
if: ${{matrix.sys == 'mingw32'}}
working-directory: ./Platformio
run: pio run -e windows_linux_32bit
run: pio run -e windows_32bit
10 changes: 5 additions & 5 deletions Platformio/hardware/ESP32/infrared_receiver_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ uint8_t IR_VCC_GPIO = 25; // IR receiver power

bool irReceiverEnabled = false;

tShowNewIRmessage_cb thisShowNewIRmessage_cb = NULL;
void set_showNewIRmessage_cb_HAL(tShowNewIRmessage_cb pShowNewIRmessage_cb) {
thisShowNewIRmessage_cb = pShowNewIRmessage_cb;
tAnnounceNewIRmessage_cb thisAnnounceNewIRmessage_cb = NULL;
void set_announceNewIRmessage_cb_HAL(tAnnounceNewIRmessage_cb pAnnounceNewIRmessage_cb) {
thisAnnounceNewIRmessage_cb = pAnnounceNewIRmessage_cb;
}

// The Serial connection baud rate.
Expand Down Expand Up @@ -190,8 +190,8 @@ void infraredReceiver_loop_HAL() {
message += typeToString((&results)->decode_type, (&results)->repeat);
message += " ";
message += resultToHexidecimal(&results);
if (thisShowNewIRmessage_cb != NULL) {
thisShowNewIRmessage_cb(std::string(message.c_str()));
if (thisAnnounceNewIRmessage_cb != NULL) {
thisAnnounceNewIRmessage_cb(std::string(message.c_str()));
}

yield(); // Feed the WDT (again)
Expand Down
4 changes: 2 additions & 2 deletions Platformio/hardware/ESP32/infrared_receiver_hal_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ void infraredReceiver_loop_HAL(void);
bool get_irReceiverEnabled_HAL();
void set_irReceiverEnabled_HAL(bool aIrReceiverEnabled);

typedef void (*tShowNewIRmessage_cb)(std::string message);
void set_showNewIRmessage_cb_HAL(tShowNewIRmessage_cb pShowNewIRmessage_cb);
typedef void (*tAnnounceNewIRmessage_cb)(std::string message);
void set_announceNewIRmessage_cb_HAL(tAnnounceNewIRmessage_cb pAnnounceNewIRmessage_cb);
69 changes: 61 additions & 8 deletions Platformio/hardware/ESP32/mqtt_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ WiFiClient espClient;
PubSubClient mqttClient(espClient);
bool isWifiConnected = false;

showWiFiconnected_cb thisShowWiFiconnected_cb = NULL;
void set_showWiFiconnected_cb_HAL(showWiFiconnected_cb pShowWiFiconnected_cb) {
thisShowWiFiconnected_cb = pShowWiFiconnected_cb;
tAnnounceWiFiconnected_cb thisAnnounceWiFiconnected_cb = NULL;
void set_announceWiFiconnected_cb_HAL(tAnnounceWiFiconnected_cb pAnnounceWiFiconnected_cb) {
thisAnnounceWiFiconnected_cb = pAnnounceWiFiconnected_cb;
}

tAnnounceSubscribedTopics_cb thisAnnounceSubscribedTopics_cb = NULL;
void set_announceSubscribedTopics_cb_HAL(tAnnounceSubscribedTopics_cb pAnnounceSubscribedTopics_cb) {
thisAnnounceSubscribedTopics_cb = pAnnounceSubscribedTopics_cb;
}

bool getIsWifiConnected_HAL() {
Expand All @@ -30,12 +35,12 @@ void WiFiEvent(WiFiEvent_t event){
// Set status bar icon based on WiFi status
if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP || event == ARDUINO_EVENT_WIFI_STA_GOT_IP6) {
isWifiConnected = true;
thisShowWiFiconnected_cb(true);
thisAnnounceWiFiconnected_cb(true);
Serial.printf("WiFi connected, IP address: %s\r\n", WiFi.localIP().toString().c_str());

} else if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
isWifiConnected = false;
thisShowWiFiconnected_cb(false);
thisAnnounceWiFiconnected_cb(false);
// automatically try to reconnect
Serial.printf("WiFi got disconnected. Will try to reconnect.\r\n");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Expand All @@ -44,7 +49,7 @@ void WiFiEvent(WiFiEvent_t event){
// e.g. ARDUINO_EVENT_WIFI_STA_CONNECTED or many others
// connected is not enough, will wait for IP
isWifiConnected = false;
thisShowWiFiconnected_cb(false);
thisAnnounceWiFiconnected_cb(false);

}
}
Expand All @@ -57,29 +62,77 @@ void init_mqtt_HAL(void) {
WiFi.setSleep(true);
}

std::string subscribeTopicOMOTEtest = "OMOTE/test";

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
std::string topicReceived(topic);
std::string strPayload(reinterpret_cast<const char *>(payload), length);

if (topicReceived == subscribeTopicOMOTEtest) {
Serial.printf("MQTT: received topic %s with payload %s\r\n", subscribeTopicOMOTEtest.c_str(), strPayload.c_str());

}

thisAnnounceSubscribedTopics_cb(topicReceived, strPayload);
}

void mqtt_subscribeTopics() {
mqttClient.setCallback(&callback);

mqttClient.subscribe(subscribeTopicOMOTEtest.c_str());
Serial.printf(" Successfully subscribed to MQTT topic %s\r\n", subscribeTopicOMOTEtest.c_str());

}

bool checkMQTTconnection() {

if (WiFi.isConnected()) {
if (mqttClient.connected()) {
return true;
} else {
// try to connect to mqtt server
mqttClient.setBufferSize(512); // default is 256
//mqttClient.setKeepAlive(15); // default is 15 Client will send MQTTPINGREQ to keep connection alive
//mqttClient.setSocketTimeout(15); // default is 15 This determines how long the client will wait for incoming data when it expects data to arrive - for example, whilst it is in the middle of reading an MQTT packet.
mqttClient.setServer(MQTT_SERVER, MQTT_SERVER_PORT); // MQTT initialization
if (mqttClient.connect(MQTT_CLIENTNAME, MQTT_USER, MQTT_PASS)) {

std::string mqttClientName = std::string(MQTT_CLIENTNAME) + "_esp32_" + std::string(WiFi.macAddress().c_str());
if (mqttClient.connect(mqttClientName.c_str(), MQTT_USER, MQTT_PASS)) {
Serial.printf(" Successfully connected to MQTT broker\r\n");

mqtt_subscribeTopics();

} else {
Serial.printf(" MQTT connection failed (but WiFi is available). Will try later ...\r\n");

}
return mqttClient.connected();
}
} else {
Serial.printf(" No connection to MQTT server, because WiFi ist not connected.\r\n");
// Serial.printf(" No connection to MQTT server, because WiFi ist not connected.\r\n");
return false;
}
}

unsigned long reconnectInterval = 100;
// in order to do reconnect immediately ...
unsigned long lastReconnectAttempt = millis() - reconnectInterval - 1;
void mqtt_loop_HAL() {
if (!mqttClient.connected()) {
unsigned long currentMillis = millis();
if ((currentMillis - lastReconnectAttempt) > reconnectInterval) {
lastReconnectAttempt = currentMillis;
// Attempt to reconnect
checkMQTTconnection();
}
}

if (mqttClient.connected()) {
mqttClient.loop();
}
}

bool publishMQTTMessage_HAL(const char *topic, const char *payload){

if (checkMQTTconnection()) {
Expand Down
8 changes: 5 additions & 3 deletions Platformio/hardware/ESP32/mqtt_hal_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

void init_mqtt_HAL(void);
bool getIsWifiConnected_HAL();
void mqtt_loop_HAL();
bool publishMQTTMessage_HAL(const char *topic, const char *payload);
void wifiStop_HAL();

typedef void (*showWiFiconnected_cb)(bool connected);
void set_showWiFiconnected_cb_HAL(showWiFiconnected_cb pShowWiFiconnected_cb);
typedef void (*tAnnounceWiFiconnected_cb)(bool connected);
void set_announceWiFiconnected_cb_HAL(tAnnounceWiFiconnected_cb pAnnounceWiFiconnected_cb);
typedef void (*tAnnounceSubscribedTopics_cb)(std::string topic, std::string payload);
void set_announceSubscribedTopics_cb_HAL(tAnnounceSubscribedTopics_cb pAnnounceSubscribedTopics_cb);

#endif

Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ void set_irReceiverEnabled_HAL(bool aIrReceiverEnabled) {
irReceiverEnabled = aIrReceiverEnabled;
}

void set_showNewIRmessage_cb_HAL(tShowNewIRmessage_cb pShowNewIRmessage_cb) {}
void set_announceNewIRmessage_cb_HAL(tAnnounceNewIRmessage_cb pAnnounceNewIRmessage_cb) {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ void infraredReceiver_loop_HAL(void);
bool get_irReceiverEnabled_HAL();
void set_irReceiverEnabled_HAL(bool aIrReceiverEnabled);

typedef void (*tShowNewIRmessage_cb)(std::string message);
void set_showNewIRmessage_cb_HAL(tShowNewIRmessage_cb pShowNewIRmessage_cb);
typedef void (*tAnnounceNewIRmessage_cb)(std::string message);
void set_announceNewIRmessage_cb_HAL(tAnnounceNewIRmessage_cb pAnnounceNewIRmessage_cb);
21 changes: 21 additions & 0 deletions Platformio/hardware/windows_linux/lib/MQTT-C/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Liam Bindle

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
106 changes: 106 additions & 0 deletions Platformio/hardware/windows_linux/lib/MQTT-C/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<p align="right">
<a href="https://github.com/LiamBindle/MQTT-C/stargazers"><img src="https://img.shields.io/github/stars/LiamBindle/MQTT-C.svg?style=social&label=Star" style="margin-left:5em"></a>
<a href="https://github.com/LiamBindle/MQTT-C/network/members"><img src="https://img.shields.io/github/forks/LiamBindle/MQTT-C.svg?style=social&label=Fork"></a>
</p>

<p align="center">
<img width="70%" src="docs/mqtt-c-logo.png"><br>
<a href="https://liambindle.ca/MQTT-C"><img src="https://img.shields.io/badge/docs-passing-brightgreen.svg"></a>
<a href="https://github.com/LiamBindle/MQTT-C/issues"><img src="https://img.shields.io/badge/Maintained%3F-yes-green.svg"></a>
<a href="https://GitHub.com/LiamBindle/MQTT-C/issues/"><img src="https://img.shields.io/github/issues/LiamBindle/MQTT-C.svg"></a>
<a href="https://github.com/LiamBindle/MQTT-C/issues"><img src="https://img.shields.io/github/issues-closed/LiamBindle/MQTT-C.svg"></a>
<a href="https://github.com/LiamBindle/MQTT-C/blob/master/LICENSE"><img src="https://img.shields.io/badge/License-MIT-blue.svg"></a>
</p>

MQTT-C is an [MQTT v3.1.1](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html)
client written in C. MQTT is a lightweight publisher-subscriber-based messaging protocol that is
commonly used in IoT and networking applications where high-latency and low data-rate links
are expected. The purpose of MQTT-C is to provide a **portable** MQTT client, **written in C**,
for embedded systems and PC's alike. MQTT-C does this by providing a transparent Platform
Abstraction Layer (PAL) which makes porting to new platforms easy. MQTT-C is completely
thread-safe but can also run perfectly fine on single-threaded systems making MQTT-C
well-suited for embedded systems and microcontrollers. Finally, MQTT-C is small; there are only
two source files totalling less than 2000 lines.

#### A note from the author
It's been great to hear about all the places MQTT-C is being used! Please don't hesitate
to get in touch with me or submit issues on GitHub!

## Getting Started
To use MQTT-C you first instantiate a `struct mqtt_client` and initialize it by calling
@ref mqtt_init.
```c
struct mqtt_client client; /* instantiate the client */
mqtt_init(&client, ...); /* initialize the client */
```
Once your client is initialized you need to connect to an MQTT broker.
```c
mqtt_connect(&client, ...); /* send a connection request to the broker. */
```
At this point the client is ready to use! For example, we can subscribe to a topic like so:
```c
/* subscribe to "toaster/temperature" with a max QoS level of 0 */
mqtt_subscribe(&client, "toaster/temperature", 0);
```
And we can publish to a topic like so:
```c
/* publish coffee temperature with a QoS level of 1 */
int temperature = 67;
mqtt_publish(&client, "coffee/temperature", &temperature, sizeof(int), MQTT_PUBLISH_QOS_1);
```
Those are the basics! From here the [examples](https://github.com/LiamBindle/MQTT-C/tree/master/examples) and [API documentation](https://liambindle.ca/MQTT-C/group__api.html) are good places to get started.

## Building
There are **only two source files** that need to be built, `mqtt.c` and `mqtt_pal.c`.
These files are ANSI C (C89) compatible, and should compile with any C compiler.

Then, simply <code>\#include <mqtt.h></code>.

Alternatively, you can build MQTT-C with CMake or the provided Makefile. These are provided for convenience.

## Documentation
Pre-built documentation can be found here: [https://liambindle.ca/MQTT-C](https://liambindle.ca/MQTT-C). Be sure to check out the [examples](https://github.com/LiamBindle/MQTT-C/tree/master/examples) too.

The @ref api documentation contains all the documentation application programmers should need.
The @ref pal documentation contains everything you should need to port MQTT-C to a new platform,
and the other modules contain documentation for MQTT-C developers.

## Testing and Building the Tests
The MQTT-C unit tests use the [cmocka unit testing framework](https://cmocka.org/).
Therefore, [cmocka](https://cmocka.org/) *must* be installed on your machine to build and run
the unit tests. For convenience, a simple `"makefile"` is included to build the unit tests and
examples on UNIX-like machines. The unit tests and examples can be built as follows:
```bash
$ make all
```
The unit tests and examples will be built in the `"bin/"` directory. The unit tests can be run
like so:
```bash
$ ./bin/tests [address [port]]
```
Note that the \c address and \c port arguments are both optional to specify the location of the
MQTT broker that is to be used for the tests. If no \c address is given then the
[Mosquitto MQTT Test Server](https://test.mosquitto.org/) will be used. If no \c port is given,
port 1883 will be used.
## Portability
MQTT-C provides a transparent platform abstraction layer (PAL) in `mqtt_pal.h` and `mqtt_pal.c`.
These files declare and implement the types and calls that MQTT-C requires. Refer to
@ref pal for the complete documentation of the PAL.
## Contributing
Please feel free to submit issues and pull-requests [here](https://github.com/LiamBindle/MQTT-C).
When submitting a pull-request please ensure you have *fully documented* your changes and
added the appropriate unit tests.
## License
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT). See the
`"LICENSE"` file for more details.
## Authors
MQTT-C was initially developed as a CMPT 434 (Winter Term, 2018) final project at the University of
Saskatchewan by:
- **Liam Bindle**
- **Demilade Adeoye**
Loading

0 comments on commit 665b690

Please sign in to comment.