Fehler beim kompilieren in Blockly (WiFi Client)

Hallo,

ich möchte meine Sensebox gerne so einrichten, dass sie die Daten sowohl an openSenseMap, als auch via MQTT im lokalen Netzwerk veröffentlich. Leider bekomme ich beim Kompilieren die Fehlermeldung:

{„exit“:„Command failed: 1: Uncaught Fatal Exception“,„process“:"/tmp/a8fe5f89952bdf67cabf1dad0880ba3a/sketch/sketch.ino:29:32: error: conflicting declaration ‚BearSSLClient client‘\n BearSSLClient client(wifiClient);\n ^\n/tmp/a8fe5f89952bdf67cabf1dad0880ba3a/sketch/sketch.ino:21:12: note: previous declaration as ‚WiFiClient client‘\n WiFiClient client;\n ^~~~~~\n\nError during build: exit status 1\n"}

Hier noch der Blockly-Code:

#include <senseBoxIO.h>
#include <WiFi101.h>
#include <Adafruit_MQTT.h> //http://librarymanager/All#Adafruit_MQTT_Library"
#include <Adafruit_MQTT_Client.h>
#include <Adafruit_HDC1000.h> // http://librarymanager/All#Adafruit_HDC1000_Library
#include <ArduinoBearSSL.h>
#include <ArduinoECCX08.h>

char ssid[] = "SSID";
char pass[] = "Password";
int status = WL_IDLE_STATUS;

  const long intervalInterval = 10000;
  long time_startInterval = 0;
  long time_actualInterval = 0;

#define SERVER      "io.adafruit.com"
#define SERVERPORT      1883
#define USERNAME      "Username"
#define PASS      "Password"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, PASS);
Adafruit_HDC1000 hdc = Adafruit_HDC1000();
const char SENSOR_IDRID[] PROGMEM = "sensorID";
static const uint8_t NUM_SENSORS = 1;
const char SENSEBOX_ID [] PROGMEM = "senseBox ID";
const char server [] PROGMEM ="ingress.opensensemap.org";
WiFiClient wifiClient;
BearSSLClient client(wifiClient);
typedef struct measurement {
      const char *sensorId;
      float value;
    } measurement;
char buffer[750];
measurement measurements[NUM_SENSORS];
    uint8_t num_measurements = 0;
const int lengthMultiplikator = 35;
Adafruit_MQTT_Publish Name = Adafruit_MQTT_Publish(&mqtt, USERNAME "/feeds/Name");

// Function to connect and reconnect as necessary to the MQTT server.
    // Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
      int8_t ret;
      // Stop if already connected.
      if (mqtt.connected()) {
        return;
      }
      while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
           mqtt.disconnect();
           delay(5000);  // wait 5 seconds
      }
}


unsigned long getTime() {
      return WiFi.getTime();
    }

    void addMeasurement(const char *sensorId, float value) {
    measurements[num_measurements].sensorId = sensorId;
    measurements[num_measurements].value = value;
    num_measurements++;
    }

    void writeMeasurementsToClient() {
    // iterate throug the measurements array
    for (uint8_t i = 0; i < num_measurements; i++) {
      sprintf_P(buffer, PSTR("%s,%9.2f\n"), measurements[i].sensorId,
                measurements[i].value);
      // transmit buffer to client
      client.print(buffer);
    }
    // reset num_measurements
    num_measurements = 0;
  }

  void submitValues() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.disconnect();
delay(1000); // wait 1s
WiFi.begin(ssid, pass);
delay(5000); // wait 5s
}
  if (client.connected()) {
      client.stop();
      delay(1000);
    }
  bool connected = false;
  char _server[strlen_P(server)];
  strcpy_P(_server, server);
  for (uint8_t timeout = 2; timeout != 0; timeout--) {
    Serial.println(F("connecting..."));
    connected = client.connect(_server, 443);
    if (connected == true) {
      // construct the HTTP POST request:
      sprintf_P(buffer,
                PSTR("POST /boxes/%s/data HTTP/1.1\nAuthorization: access_token\nHost: %s\nContent-Type: "
                     "text/csv\nConnection: close\nContent-Length: %i\n\n"),
                SENSEBOX_ID, server, num_measurements * lengthMultiplikator);
      // send the HTTP POST request:
      client.print(buffer);
      // send measurements
      writeMeasurementsToClient();
      // send empty line to end the request
      client.println();
      uint16_t timeout = 0;
      // allow the response to be computed
      while (timeout <= 5000) {
        delay(10);
        timeout = timeout + 10;
        if (client.available()) {
          break;
        }
      }

      while (client.available()) {
        char c = client.read();
        // if the server's disconnected, stop the client:
        if (!client.connected()) {
          client.stop();
          break;
        }
      }

      num_measurements = 0;
      break;
    }
    delay(1000);
  }

  if (connected == false) {
  delay(5000);
  noInterrupts();
 NVIC_SystemReset();
 while (1)
 ;
 }
  }


void setup() {

if (WiFi.status() == WL_NO_SHIELD) {
    while (true);
}
while (status != WL_CONNECTED) {
    status = WiFi.begin(ssid, pass);
    delay(5000);
}

hdc.begin();
ArduinoBearSSL.onGetTime(getTime);



}


void loop() {
time_startInterval = millis();

MQTT_connect();

  if (time_startInterval > time_actualInterval + intervalInterval) {
  time_actualInterval = millis();
    addMeasurement(SENSOR_IDRID,hdc.readHumidity());
  submitValues();
  Name.publish(hdc.readHumidity());}

}

sollte hier behoben sein: https://deploy-preview-272--blockly-react.netlify.app/

1 Like

Hallo,

sollte das inzwischen klappen?
Ich bekomme immer noch eine Fehlermeldung.

Gruss Macro77

Hallo

ich habe es vor eine Jahr aufgegeben mit MQTT und opensensemap.
Heute habe ich es wieder mal versucht, aber es gibt immer noch eine Fehlermeldung.
Hat jemand eine Lösung für MQTT lokal und Daten an opensensemap zu senden?

Gruss Macro77

Hallo Marco,
Mit der Änderung von Mario hat es bei mir dann funktioniert. Seitdem sendet meine Sensebox durchgehend, sowohl mit MQTT als auch an Opensensemap.
Vielleicht wäre eine Fehlermeldung hilfreich?

Hallo Hamlet

ich erhalte immer diese Fehlermeldung:


Command failed: 1: Uncaught Fatal Exception

/tmp/mltmf4fi-aybxo1z9/sketch/sketch.ino:29:28: error: ‚client‘ was not declared in this scope
Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, PASS);
^~~~~~
/tmp/mltmf4fi-aybxo1z9/sketch/sketch.ino:29:28: note: suggested alternative: ‚uint‘
Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, PASS);
^~~~~~
uint
Error during build: exit status 1

Als Unterschied im Blockly Code sehe ich bei mir

WiFiClient wifiClient;

und oben im Post steht

WiFiClient client;

Ansonsten sehe ich ausser den Passwörtern und IDs keinen Unterschied.

Gruss Macro77

Hier noch der Blockly Code:

// Code generated by senseBox Blockly on Fri Feb 20 2026 09:10:06 GMT+0100 (Mitteleuropäische Normalzeit)

#include <senseBoxIO.h>
#include <WiFi101.h>
#include <Adafruit_MQTT.h> //http://librarymanager/All#Adafruit_MQTT_Library"
#include <Adafruit_MQTT_Client.h>
#include <Adafruit_HDC1000.h> // http://librarymanager/All#Adafruit_HDC1000_Library
#include <ArduinoBearSSL.h>
#include <ArduinoECCX08.h>

char ssid[] = "test1";
char pass[] = "test2";
int status = WL_IDLE_STATUS;

const long intervalInterval1 = 30000;
long time_startInterval1 = 0;
long time_actualInterval1 = 0;

#define SERVER      "192.168.60.230"
#define SERVERPORT      1883
#define USERNAME      "test3"
#define PASS      "test4"
WiFiClient wifiClient;
Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, PASS);
Adafruit_HDC1000 hdc = Adafruit_HDC1000();
const char SENSOR_ID890[] PROGMEM = "1234567890";
static const uint8_t NUM_SENSORS = 1;
const char SENSEBOX_ID [] PROGMEM = "123456";
const char server [] PROGMEM ="ingress.opensensemap.org";
BearSSLClient client(wifiClient);
typedef struct measurement {
  const char *sensorId;
  float value;
} measurement;
char buffer[750];
measurement measurements[NUM_SENSORS];
uint8_t num_measurements = 0;
const int lengthMultiplikator = 35;
Adafruit_MQTT_Publish temperaturSensebox = Adafruit_MQTT_Publish(&mqtt, "sensebox/temperatur");

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;
  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
  mqtt.disconnect();
  delay(5000);  // wait 5 seconds
}
}

unsigned long getTime() {
  return WiFi.getTime();
}

void addMeasurement(const char *sensorId, float value) {
  measurements[num_measurements].sensorId = sensorId;
  measurements[num_measurements].value = value;
  num_measurements++;
}

void writeMeasurementsToClient() {
  // iterate throug the measurements array
  for (uint8_t i = 0; i < num_measurements; i++) {
    sprintf_P(buffer, PSTR("%s,%9.2f\n"), measurements[i].sensorId,
    measurements[i].value);
    // transmit buffer to client
    client.print(buffer);
  }
  // reset num_measurements
  num_measurements = 0;
}

void submitValues() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.disconnect();
    delay(1000); // wait 1s
    WiFi.begin(ssid, pass);
    delay(5000); // wait 5s
  }
  if (client.connected()) {
    client.stop();
    delay(1000);
  }
  bool connected = false;
  char _server[strlen_P(server)];
  strcpy_P(_server, server);
  for (uint8_t timeout = 2; timeout != 0; timeout--) {
    Serial.println(F("connecting..."));
    connected = client.connect(_server, 443);
    if (connected == true) {
      // construct the HTTP POST request:
      sprintf_P(buffer,
      PSTR("POST /boxes/%s/data HTTP/1.1\nAuthorization: 12345678\nHost: %s\nContent-Type: "
      "text/csv\nConnection: close\nContent-Length: %i\n\n"),
      SENSEBOX_ID, server, num_measurements * lengthMultiplikator);
      // send the HTTP POST request:
      client.print(buffer);
      // send measurements
      writeMeasurementsToClient();
      // send empty line to end the request
      client.println();
      uint16_t timeout = 0;
      // allow the response to be computed
      while (timeout <= 5000) {
        delay(10);
        timeout = timeout + 10;
        if (client.available()) {
          break;
        }
      }

      while (client.available()) {
        char c = client.read();
        // if the server's disconnected, stop the client:
        if (!client.connected()) {
          client.stop();
          break;
        }
      }

      num_measurements = 0;
      break;
    }
    delay(1000);
  }

  if (connected == false) {
    delay(5000);
    noInterrupts();
    NVIC_SystemReset();
    while (1)
    ;
  }
}

void setup() {

  if (WiFi.status() == WL_NO_SHIELD) {
    while (true);
  }
  while (status != WL_CONNECTED) {
    status = WiFi.begin(ssid, pass);
    delay(5000);
  }

  hdc.begin();
  ArduinoBearSSL.onGetTime(getTime);

}

void loop() {
  time_startInterval1 = millis();

  MQTT_connect();

  if (time_startInterval1 > time_actualInterval1 + intervalInterval1) {
    time_actualInterval1 = millis();
    addMeasurement(SENSOR_ID890,hdc.readTemperature());
    submitValues();

  }
  temperaturSensebox.publish(hdc.readTemperature());
}

Hm,
hast du mal versucht „wifiClient“ durch „client“ zu ersetzen?

error: ‚client‘ was not declared in this scope

liest sich fast so, als könnte das der Fehler sein. Mehr kann ich dir da vermutlich auch nicht weiterhelfen :frowning:

Hallo Hamlet,

ich habe mal client mit wifiClient ersetzt, dann kann ich es in der Arduino IDE compilieren ohne Fehler.
Allerdings kommen dann keine Daten mehr an, weder im MQTT Broker noch auf der openSensemap.
Ich werde nun die nur MQTT Version verwenden welche ja funktioniert und dann die Daten in Homeassistant integrieren. Vielleicht werde ich dann die Daten per NodeRed an openSensemap übermitteln.
Danke jedenfalls für deine Hilfe.

Gruss Macro77

Hi @Macro77,

komisch, dass der Fehler auftaucht, kannst du mir dazu einmal die Block Struktur als XML schicken? Oder über „Projekt teilen“?

Das Problem ist, dass BearSSLClient client(wifiClient); erst nach Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, PASS); deklariert wird. Wenn du die Zeile hochziehst und kompilierst müsste es gehen. Kompilieren kannst du auch online über den Code-Editor: senseBox Blockly

Beste Grüße,

Mario