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

Functional library? #4

Open
FernandoGarcia opened this issue Jun 14, 2021 · 0 comments
Open

Functional library? #4

FernandoGarcia opened this issue Jun 14, 2021 · 0 comments

Comments

@FernandoGarcia
Copy link

FernandoGarcia commented Jun 14, 2021

Hi!

I found this library in your post on Reddit.

I'm looking for a library to create a broker on ESP32.

Can this library work with Arduino IDE?

After add the cloned files to libraries folder the Arduino IDE says:

Invalid library found in /home/fernandogarcia/Arduino/libraries/gnat: no headers files (.h) found in /home/fernandogarcia/Arduino/libraries/gnat

Also is not possible compile this code available in your gist.

// Responsible for taking all of the data and serving it to any listeners.
// A simple key-value store over mqtt using gnat.

#include "WiFi.h"
#include "Arduino.h"

#include <memory>


// Enables debug messages in gnat-arduino.
//#define ARDUINO_DEBUG 1
#include <gnat/arduino-connection.h>
#include <gnat/packets.h>
#include <gnat/server.h>

  
constexpr uint16_t kPort = 0xe7c;

struct {
  uint8_t connected_clients = 0;
} status;

WiFiServer tcp_server(kPort);

void setup() {
  tcp_server.begin(); 
}

arduino::Clock ard_clock;
gnat::DataStore<std::string> data_store;
gnat::Server<arduino::Connection, gnat::DataStore<std::string>, arduino::Clock> server(&data_store, &ard_clock);

std::list<WiFiClient> active_clients;
void loop() {
  WiFiClient new_client = tcp_server.available();
  if (new_client) {
    active_clients.push_back(new_client);
    status.connected_clients++;
  }
  
  for (auto itr = active_clients.begin(); itr != active_clients.end(); itr++) {
    auto& client = *itr;
    if (!client) {
      // If disconnected remove.
      Serial.println("Lost client, dropping.");
      status.connected_clients--;
      if (itr == active_clients.begin()) {
        active_clients.pop_front();
        break; // go to next itr in this case as the loop can't continue;
      } else {
        // copy iterator, decrement then erase current.
        auto itr_tmp = itr;
        itr--;
        active_clients.erase(itr_tmp);
      }
    }
    
    if(!client.available()) continue;
    
    auto packet = gnat::Packet<Connection>::ReadNext(arduino::Connection(&client));
    if (packet) {
      const auto result = server.HandleMessage(&*packet);
      if (!result.IsOk()) {
        Serial.print("Failed to handle packet:");
        Serial.println(result.message().c_str());
      }
    } else {
      Serial.println("Failed to read packet.\n");
    }
  }
}

The compiling error is:

gnat/arduino-connection.h: No such file or directory

Best regards.

@FernandoGarcia FernandoGarcia changed the title Function library? Functional library? Jun 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant