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

'pm' was not declared in function [Blockly][Codegenerator] #269

Closed Hamlet3000 closed 5 months ago

Hamlet3000 commented 5 months ago

Block description

compiler error: {"exit":"Command failed: 1: Uncaught Fatal Exception","process":"/tmp/6335d0de8c09de8588e1a8e4cf75cb6d/sketch/sketch.ino: In function 'void publisOSM()':\n/tmp/6335d0de8c09de8588e1a8e4cf75cb6d/sketch/sketch.ino:121:33: error: 'pm' was not declared in this scope\n addMeasurement(SENSOR_IDRID,pm.pm25);\n ^~\n/tmp/6335d0de8c09de8588e1a8e4cf75cb6d/sketch/sketch.ino:121:33: note: suggested alternative: 'Pm'\n addMeasurement(SENSOR_IDRID,pm.pm25);\n ^~\n Pm\n\nError during build: exit status 1\n"}

Expected block behaviour

if you add the SDS011 block within a funcion, pm is not declared

Screenshot 2024-02-12 144533

blockly code: PmResult pm = sds.queryPm(); should not be within the void loop()


#include <senseBoxIO.h>
#include <WiFi101.h>
#include <SdsDustSensor.h> // http://librarymanager/All#Nova_Fitness_Sds_dust_sensors_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;

SdsDustSensor sds(Serial1);
const char SENSOR_IDRID[] PROGMEM = "sensorID";
static const uint8_t NUM_SENSORS = 1;
const char SENSEBOX_ID [] PROGMEM = "600085a462dbda001b4ea891";
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: 593d91b7a62fde0139d08c0c4069256be45313b573cf5ea02f6de6316bd54bf2\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)
 ;
 }
  }
/**
 * Beschreibe diese Funktion …
 */
void publisOSM() {
    addMeasurement(SENSOR_IDRID,pm.pm25);
  submitValues();
}

void setup() {

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

sds.begin();
sds.setQueryReportingMode();
ArduinoBearSSL.onGetTime(getTime);

}

void loop() {
time_startInterval = millis();

PmResult pm = sds.queryPm();

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

}
mariopesch commented 5 months ago

following implementation might fix it

float getPMValues(int type) {
PmResult pm = sds.queryPm();
if (pm.isOk()) {
    if (type == 25){
      return pm.pm25;
    } else if (type == 10) {
      return pm.pm10;
    }
else return 0;

}
}

need to test it properly!