sensebox / React-Ardublockly

This repository contains the new senseBox learn- and programming environment powered by google Blockly and React
Apache License 2.0
2 stars 6 forks source link

multiple "WiFiClient client" with mqtt and osm #268

Closed Hamlet3000 closed 6 months ago

Hamlet3000 commented 6 months ago

Describe the bug

{"exit":"Command failed: 1: Uncaught Fatal Exception","process":"/tmp/16e2ce7c6b7dc296cea2c4c605550b91/sketch/sketch.ino:26:32: error: conflicting declaration 'BearSSLClient client'\n BearSSLClient client(wifiClient);\n ^\n/tmp/16e2ce7c6b7dc296cea2c4c605550b91/sketch/sketch.ino:20:12: note: previous declaration as 'WiFiClient client'\n WiFiClient client;\n ^~\n\nError during build: exit status 1\n"}

To Reproduce Steps to reproduce the behavior:

  1. Go bo blockly
  2. add "Connect to Wifi" block to setup
  3. add "Connect to MQTT Broker" block setup
  4. add "Connect to Opensensemap" within the interval block to loop forever
  5. try to compile code

Expected behavior program code should be compilable

Screenshots Screenshot 2024-02-12 140204

Desktop (please complete the following information):

Additional context blockly code:

#include <senseBoxIO.h>
#include <WiFi101.h>
#include <Adafruit_MQTT.h> //http://librarymanager/All#Adafruit_MQTT_Library"
#include <Adafruit_MQTT_Client.h>
#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);
static const uint8_t NUM_SENSORS = 0;
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;

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);
}

ArduinoBearSSL.onGetTime(getTime);

}

void loop() {
time_startInterval = millis();

  if (time_startInterval > time_actualInterval + intervalInterval) {
  time_actualInterval = millis();
  submitValues();
}

}