>>> Join Us in the Fight Against Air Pollution

AirGradient Forum

Integrating airgradient one(i-9psl) to thingsboard

I have been trying to get a basic install to Thingsboard with an Airgradient One.
Been trying to get it to even compile so that I can send some basic data to Thingsboard, ultimately to get the whole of the sensors to publish data to that thingsboard server.

#include "configconfig.h" 

// config file needs to include  WIFI_AP, TOKEN, HOSTIP
//#include <AVR_RTC.h>
// #include <avr/io.h>
// #include <avr/pgmspace.h>
#include <WiFiEspClient.h>
//#include <TBPubsubClient.h>
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
//#include "hardwareserial.h"
#include <ThingsBoard.h>
//#include "SCD30.h"
//#include "seeed_SHT40.h"
#include <Wire.h>
#include <Seeed_HM330X.h>
#include <Arduino.h>
#include <SensirionI2CSgp41.h>
//#include <Seeed_HM330X.h>

//new libs for airgradient ONE
#include "s8_uart.h"
#include <PMS.h>
#include "Adafruit_SHT4x.h"
// #include "driver_sht4x_basic.h"
//#include <AirGradient.h>
#include <SensirionI2cSht4x.h>
//#include "driver_sht4x_read_test.h"

//mqtt protocol
//#include <Espressif_MQTT_Client.h>

#define baud 115200

#ifdef  ARDUINO_SAMD_VARIANT_COMPLIANCE
    #define SERIAL_OUTPUT SerialUSB
#else
    #define SERIAL_OUTPUT Serial
#endif

// softwareserial check for s8

/* BEGIN CONFIGURATION */

#if (defined USE_SOFTWARE_SERIAL || defined ARDUINO_ARCH_RP2040)
  #define S8_RX_PIN 5         // Rx pin which the S8 Tx pin is attached to (change if it is needed)
  #define S8_TX_PIN 4         // Tx pin which the S8 Rx pin is attached to (change if it is needed)
#else
  #define S8_UART_PORT  1     // Change UART port if it is needed
#endif
/* END CONFIGURATION */
//removing software Serial make it use hardware Serial
#ifdef USE_SOFTWARE_SERIAL
  SoftwareSerial S8_serial(S8_RX_PIN, S8_TX_PIN);
#else
  #if defined(ARDUINO_ARCH_RP2040)
    REDIRECT_STDOUT_TO(Serial)    // to use printf (Serial.printf not supported)
    UART S8_serial(S8_TX_PIN, S8_RX_PIN, NC, NC);
  #else
    HardwareSerial S8_serial(S8_UART_PORT);   
  #endif
#endif
//class calls from header files
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
SensirionI2CSgp41 sgp41;
PMS pms(Serial);
PMS::DATA data;
//S8_UART *sensor_S8;//was  *sensor_S8
S8_sensor s8_sensor;
SensirionI2cSht4x Sht4_sensor;

// SGP41
//SensirionI2CSgp41 sgp41;
// time in seconds needed for NOx conditioning
uint16_t conditioning_s = 10;

char thingsboardServer[] = HOSTIP;
WiFiEspClient espClient;
ThingsBoard tb(espClient);

//SoftwareSerial soft(10, 11); // RX, TX

int status = WL_IDLE_STATUS;
unsigned long lastSend;


void setup() {
    Serial.begin(baud);

}

uint16_t error;
  char errorMessage[256];
  uint16_t defaultRh = 0x8000;
  uint16_t defaultT = 0x6666;
  uint16_t srawVoc = 0;
  uint16_t srawNox = 0;

void loop() {
  // put your main code here, to run repeatedly:
 sensors_event_t humidity, temp;
  uint32_t timestamp = millis();


  status = WiFi.status();
  if ( status != WL_CONNECTED) {
    while ( status != WL_CONNECTED) {
      Serial.print(F("Attempting to connect to WPA SSID: "));
      Serial.println(WIFI_AP);
      // Connect to WPA/WPA2 network
      status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
      delay(500);
    }
    Serial.println(F("Connected to AP"));
  }

  if ( !tb.connected() ) {
    reconnect();
  }

    if ( millis() - lastSend > 2000 ) { // Update and send only after 2 seconds
    getAndSendData();
    lastSend = millis();
  }
    tb.loop();


}

void reconnect() {
  // int tries = 0;
  // Loop until we're reconnected
  while (!tb.connected()) {
    Serial.print(F("Connecting to ThingsBoard node ..."));
    // Attempt to connect (clientId, username, password)
    if ( tb.connect(thingsboardServer, TOKEN) ) {
      Serial.println(F( "[DONE]" ));
    } else {
      Serial.print(F( "[FAILED]" ));
      Serial.println(F( " : retrying in 5 seconds" ));
      // Change the hostip in case it has changed
      // if (tries % 2 == 0) {
      //   tries++;
      //   strcpy(thingsboardServer, HOSTIP2);
      // } else {
      //   tries++;
      //   strcpy(thingsboardServer, HOSTIP);
      // }
      // Wait 5 seconds before retrying
      delay( 5000 );
    }
  }
}

void InitWiFi()
{
  // initialize Serial for ESP module
  //Serial.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial);
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue
    while (true);
  }

  Serial.println(F("Connecting to AP ..."));
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(WIFI_AP);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
    delay(500);
  }
  Serial.println(F("Connected to AP"));
}


void getAndSendData()
{

  sensors_event_t humidity, temp;
  uint32_t timestamp = millis();


sgp41Data();

}


void sgp41Data() {
  uint16_t error;
  char errorMessage[256];
  uint16_t defaultRh = 0x8000;
  uint16_t defaultT = 0x6666;
  uint16_t srawVoc = 0;
  uint16_t srawNox = 0;

  if (conditioning_s > 0) {
      // During NOx conditioning (10s) SRAW NOx will remain 0
      error = sgp41.executeConditioning(defaultRh, defaultT, srawVoc);
      conditioning_s--;
  } else {
      // Read Measurement
      error = sgp41.measureRawSignals(defaultRh, defaultT, srawVoc, srawNox);
  }

  if (error) {
      Serial.print("Error trying to execute measureRawSignals(): ");
      errorToString(error, errorMessage, 256);
      Serial.println(errorMessage);
  } else {
      tb.sendTelemetryData("sgp_SRAW_VOC", srawVoc);
      tb.sendTelemetryData("sgp_SRAW_NOx", srawNox);

      Serial.print(F("Data sent to ThingsBoard... "));
      Serial.println(srawVoc);
  }
}

The error I’m getting right now is the thingsboard library Version 10.2 because it was reccommended to downgrade because of another set of errors. with TBpubsubClient library version 2.9.2. Though now I am getting “error: ‘mbedtls_md_context_t’ {aka ‘struct mbedtls_md_context_t’} has no member named ‘md_info’
34 | for (size_t i = 0; i < mbedtls_md_get_size(m_ctx.md_info); i++)”
as an error with no mention of this error occuring that I found.