Skip to content

Instantly share code, notes, and snippets.

@Elfelsoufim
Created June 1, 2021 06:39
Show Gist options
  • Save Elfelsoufim/f9ef715755d385cce8258ba149ab7861 to your computer and use it in GitHub Desktop.
Save Elfelsoufim/f9ef715755d385cce8258ba149ab7861 to your computer and use it in GitHub Desktop.
/*
Smooth - A C++ framework for embedded programming on top of Espressif's ESP-IDF
Copyright 2019 Per Malmberg (https://gitbub.com/PerMalmberg)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "mqtt.h"
#include "smooth/core/task_priorities.h"
#include "smooth/core/network/Wifi.h"
//#include "wifi_creds.h"
#define ESP32_WIFI_SSID ""
#define ESP32_WIFI_PASSWORD ""
using namespace smooth;
using namespace smooth::core;
using namespace smooth::core::logging;
using namespace std::chrono;
using namespace smooth::application::network::mqtt;
App* app;
extern "C" {
void app_main(void);
}
static const char* broker = "";
//#ifdef ESP_PLATFORM
static const char* client_id = "ESP32";
//#else
// static const char* client_id = "Linux";
//#endif
App::App()
: Application(APPLICATION_BASE_PRIO, seconds(1)),
mqtt_data(MQTTDataQueue::create(10, *this, *this)),
client(client_id, seconds(10), 8192, 10, mqtt_data)
{
}
void App::init()
{
Application::init();
Log::info("App::Init", "Starting wifi...");
network::Wifi& wifi = get_wifi();
wifi.set_host_name("Smooth-ESP");
wifi.set_auto_connect(true);
wifi.set_ap_credentials(ESP32_WIFI_SSID, ESP32_WIFI_PASSWORD);
wifi.connect_to_ap();
esp_err_t error = esp_wifi_set_ps(WIFI_PS_NONE);
ESP_ERROR_CHECK(error);
client.connect_to(std::make_shared<smooth::core::network::IPv4>(broker, 1883), true);
client.subscribe("network_test", QoS::AT_LEAST_ONCE);
client.subscribe("$SYS/broker/uptime", QoS::AT_LEAST_ONCE);
send_message();
}
void App::event(const smooth::application::network::mqtt::MQTTData& event)
{
std::stringstream ss{};
std::for_each(event.second.begin(), event.second.end(), [&ss](auto c) { ss << static_cast<char>(c);});
Log::info("Rec", "T:{}, M:{}", event.first, ss.str());
send_message();
}
void App::tick()
{
client.publish("network_test", "Message", QoS::EXACTLY_ONCE, false);
}
void App::send_message()
{
static uint32_t len = 0;
std::string dataToSend = "Sample #" + std::to_string(len);
if(!client.publish("network_test", dataToSend, QoS::AT_MOST_ONCE, false)){
printf("Message could not be queued.\n");
}
len++;
}
static void mqtt_com(void* arg) {
app->start();
}
static void transmitter(void* arg) {
app->init();
while(true){
app->send_message();
vTaskDelay(100 / portTICK_RATE_MS);
}
}
void app_main(void)
{
app = new App();
xTaskCreate(mqtt_com, "mqtt_com", 16384, NULL, 10, NULL);
//xTaskCreate(transmitter,"transmitter", 4096, NULL, 10, NULL);
int counter = 0;
while(true){
printf(" Counter: %d.\n", counter);
vTaskDelay(1000 / portTICK_RATE_MS);
counter++;
}
}
/*
Smooth - A C++ framework for embedded programming on top of Espressif's ESP-IDF
Copyright 2019 Per Malmberg (https://gitbub.com/PerMalmberg)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include "smooth/core/Application.h"
#include "smooth/core/ipc/IEventListener.h"
#include "smooth/core/ipc/TaskEventQueue.h"
#include "smooth/application/network/mqtt/MqttClient.h"
#include <random>
//#ifdef ESP_PLATFORM
#include "smooth/core/io/Output.h"
//#endif
class App
: public smooth::core::Application,
smooth::core::ipc::IEventListener<smooth::application::network::mqtt::MQTTData>
{
public:
App();
void init() override;
void event(const smooth::application::network::mqtt::MQTTData& event) override;
void tick() override;
void send_message();
private:
using MQTTDataQueue = smooth::core::ipc::TaskEventQueue<smooth::application::network::mqtt::MQTTData>;
std::shared_ptr<MQTTDataQueue> mqtt_data;
smooth::application::network::mqtt::MqttClient client;
std::random_device rand{};
std::mt19937 gen{ rand() };
std::uniform_int_distribution<> dis{ 1, 3 };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment