0

I have this code running on ESP32 connected to SVM30-J

#include <WiFi.h>
#include <PubSubClient.h>
#include <Arduino_JSON.h>
#include <svm30.h>

// Replace the next variables with your SSID/Password combination
const char* ssid = "xxxx";
const char* password = "xxxx";

// Add your MQTT Broker IP address, example:
const char* mqtt_server = "xxxx";
const int mqtt_port = 18712;
const char* mqtt_user = "xxxx";
const char* mqtt_password = "xxxx";

/* define driver debug
 * false : no messages
 * true : display driver debug messages
 */
#define DEBUG true

// define number of seconds between display results
#define DELAY 10


// function prototypes (sometimes the pre-processor does not create prototypes themself on ESPxx)
void read_id();
void read_featureSet();
void Errorloop(char *mess);
void read_values();
void read_baseline();
void KeepTrigger(uint8_t del);

// create instance
SVM30 svm;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

  Serial.begin(115200);

  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  //client.setCallback(callback);

  Serial.println(F("Reading information from the SVM30"));

  // enable debug messages
  svm.EnableDebugging(DEBUG);

  svm.begin();

  Serial.print(F("Driver version : "));
  Serial.println(svm.GetDriverVersion());

  // try to detect SVM30 sensors
  if (svm.probe() == false) Errorloop("could not detect SVM30 sensors");
  else Serial.println(F("SVM30 detected"));

  // read and display the ID
  read_id();

  // read SGP30 feature set
  read_featureSet();
}

void loop() {
  char chipId[13]; // Don't forget one byte for the terminating NULL...
  uint64_t mac = ESP.getEfuseMac();
  sprintf(chipId, "%012x", mac);
  Serial.println(chipId);

  JSONVar data;

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  // read measurement values
  read_values();

  // wait x seconds
  KeepTrigger(DELAY);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  WiFi.setSleep(false);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // If a message is received on the topic esp32/output, you check if the message is either "on" or "off". 
  // Changes the output state according to the message
  if (String(topic) == "esp32/output") {
    Serial.print("Changing output to ");
    if(messageTemp == "on"){
      Serial.println("on");
      //digitalWrite(ledPin, HIGH);
    }
    else if(messageTemp == "off"){
      Serial.println("off");
      //digitalWrite(ledPin, LOW);
    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
      // Subscribe
      //client.subscribe("esp32/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 10 seconds");
      // Wait 10 seconds before retrying
      delay(10000);
    }
  }
}


/*
 * @brief : keep triggering SGP30 while waiting
 * 
 * @param del : number of seconds to wait
 *
 * source datasheet SVM30:
 * The on-chip baseline compensation algorithm has been optimized 
 * for 1HZ sampling rate. The sensor shows best performance when 
 * used with this sampling rate.
 * 
 */
void KeepTrigger(uint8_t del)
{
  uint8_t w_seconds = del;
  unsigned long startMillis;

  if (w_seconds == 0) w_seconds = 1;

  while (w_seconds--)
  {
    startMillis = millis();

    if (! svm.TriggerSGP30())
      Errorloop("Error during trigger waiting");

    // this gives 1Hz /1000ms (aboutisch)
    while(millis() - startMillis < 1000);
  }
}

/*
 * @brief : read and display the values from the SVM30
 * 
 */
void read_values() {
  struct svm_values v;
  uint16_t baseline;

  if (! svm.GetValues(&v))
      Errorloop("Error during reading values");

  JSONVar data;

  Serial.print(F("CO2 equivalent : "));
  Serial.print(v.CO2eq);
  data["co2_equivalent"] = v.CO2eq;

  Serial.print(F(", TVOC : "));
  Serial.print(v.TVOC);
  data["tvoc"] = v.TVOC;

  Serial.print(F(", H2_signal : "));
  Serial.print(v.H2_signal);
  data["h2_signal"] = v.H2_signal;

  Serial.print(F(", Ethanol_signal : "));
  Serial.print(v.Ethanol_signal);
  data["ethanol_signal"] = v.Ethanol_signal;

  Serial.print(F(", Humidity : "));
  Serial.print((float) v.humidity/1000);
  data["humidity"] = (float) v.humidity/1000;

  Serial.print(F(", temperature : "));
  Serial.print((float) v.temperature/1000);
  data["temperature"] = (float) v.temperature/1000;

  Serial.print(F(", absolute humidity : "));
  Serial.print(v.absolute_hum);
  data["absolute_hum"] = v.absolute_hum;

  if (! svm.GetBaseLine_CO2(&baseline))
      Errorloop("could not read SGP30 CO2 baseline");

  Serial.print(F(" CO2 equivalent baseline : 0x"));
  Serial.print(baseline, HEX);
  data["co2_equivalent_baseline"] = (baseline, HEX);

  if (! svm.GetBaseLine_TVOC(&baseline))
      Errorloop("could not read SGP30 TVOC baseline");

  Serial.print(F(", TVOC baseline : 0x"));
  Serial.println(baseline, HEX);
  data["tvoc_baseline"] = (baseline, HEX);


  char chipId[13]; // Don't forget one byte for the terminating NULL...
  uint64_t mac = ESP.getEfuseMac();
  sprintf(chipId, "%012x", mac);
  data["chipid"] = chipId;

  Serial.print("Data = ");
  Serial.println(data);

  String data_json = JSON.stringify(data);
  char mqtt_publish_topic[] = "esp32/data";
  client.publish(mqtt_publish_topic, data_json.c_str());

}

/*
 * @brief: read and display the id of the SGP30 and SHTC1 sensors
 */
void read_id() {

  uint16_t buf[3];    // SGP30 needs 3 words, SHTC1 is 1 word
  char  id[15];

  if ( ! svm.GetId(SGP30, buf))
      Errorloop("could not read SGP30 id");

  Serial.print(F("\nSGP30 id : "));
  sprintf(id, "%04x %04x %04x", buf[0], buf[1], buf[2]);
  Serial.println(id);

  if (svm.GetId(SHTC1, buf) == false)
      Errorloop("could not read SHTC1 id");

  Serial.print(F("SHTC1 id : "));
  // only bit 5:0 matter (source: datasheet)
  sprintf(id, "%04x", buf[0] & 0x3f);
  Serial.println(id);
}

/*
 * @brief: read and display the product feature set of the SGP30 sensor
 */
void read_featureSet(){

  char buf[2];

  if ( ! svm.GetFeatureSet(buf))
      Errorloop("could not read SGP30 feature set");

  Serial.print(F("\nSGP30 product type : "));
  Serial.print((buf[0] & 0xf0), HEX);

  Serial.print(F(", Product version : "));
  Serial.println(buf[1], HEX);
  Serial.println();
}

/**
 *  @brief : continued loop after fatal error
 *  @param mess : message to display
 */
void Errorloop(char *mess)
{
  Serial.println(mess);
  Serial.println(F("Program on hold"));
  for(;;) delay(100000);
}

This code run as expected, except get following in serial monitor

000010a4ae30
Attempting MQTT connection...connected
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x50 , Received: 0x31 0x8D 0x47 0x9D length: 4
Sending to 0x70: 0x78 0x66 , Received: 0x61 0xBD 0x7B 0xBE length: 4
CO2 equivalent : 400, TVOC : 0, H2_signal : 12685, Ethanol_signal : 18333, Humidity : 48.34, temperature : 21.81, absolute humidity : 9.28Sending to 0x58: 0x20 0x15 , Received: 0x00 0x00 0x00 0x00 length: 4
 CO2 equivalent baseline : 0x0Sending to 0x58: 0x20 0x15 , Received: 0x00 0x00 0x00 0x00 length: 4
, TVOC baseline : 0x0
Data = {"co2_equivalent":400,"tvoc":0,"h2_signal":12685,"ethanol_signal":18333,"humidity":48.33599853515625,"temperature":21.812999725341797,"absolute_hum":9.2800455093383789,"co2_equivalent_baseline":16,"tvoc_baseline":16,"chipid":"000010a4ae30"}
[D][WiFiClient.cpp:509] connected(): Disconnected: RES: 0, ERR: 128
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
000010a4ae30
Attempting MQTT connection...connected
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x50 , Received: 0x31 0x94 0x47 0xAB length: 4
Sending to 0x70: 0x78 0x66 , Received: 0x61 0xC4 0x7A 0xDC length: 4
CO2 equivalent : 400, TVOC : 0, H2_signal : 12692, Ethanol_signal : 18347, Humidity : 47.99, temperature : 21.83, absolute humidity : 9.22Sending to 0x58: 0x20 0x15 , Received: 0x00 0x00 0x00 0x00 length: 4
 CO2 equivalent baseline : 0x0Sending to 0x58: 0x20 0x15 , Received: 0x00 0x00 0x00 0x00 length: 4
, TVOC baseline : 0x0
Data = {"co2_equivalent":400,"tvoc":0,"h2_signal":12692,"ethanol_signal":18347,"humidity":47.991001129150391,"temperature":21.830999374389648,"absolute_hum":9.223388671875,"co2_equivalent_baseline":16,"tvoc_baseline":16,"chipid":"000010a4ae30"}
[D][WiFiClient.cpp:509] connected(): Disconnected: RES: 0, ERR: 128
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x98 0x00 0x05 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x03 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x92 0x00 0x03 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x96 0x00 0x01 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
000010a4ae30
Attempting MQTT connection...connected
Sending to 0x58: 0x20 0x08 , Received: 0x01 0xA1 0x00 0x0D length: 4
Sending to 0x58: 0x20 0x50 , Received: 0x31 0x94 0x47 0xA8 length: 4
Sending to 0x70: 0x78 0x66 , Received: 0x61 0xBD 0x7A 0xCC length: 4
CO2 equivalent : 417, TVOC : 13, H2_signal : 12692, Ethanol_signal : 18344, Humidity : 47.97, temperature : 21.81, absolute humidity : 9.21Sending to 0x58: 0x20 0x15 , Received: 0x86 0x6C 0x8C 0x58 length: 4
 CO2 equivalent baseline : 0x8C58Sending to 0x58: 0x20 0x15 , Received: 0x86 0x6C 0x8C 0x58 length: 4
, TVOC baseline : 0x866C
Data = {"co2_equivalent":417,"tvoc":13,"h2_signal":12692,"ethanol_signal":18344,"humidity":47.966999053955078,"temperature":21.812999725341797,"absolute_hum":9.20920181274414,"co2_equivalent_baseline":16,"tvoc_baseline":16,"chipid":"000010a4ae30"}
[D][WiFiClient.cpp:509] connected(): Disconnected: RES: 0, ERR: 128
Sending to 0x58: 0x20 0x08 , Received: 0x01 0xA6 0x00 0x0F length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x98 0x00 0x05 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x9E 0x00 0x0F length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x98 0x00 0x0C length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x9C 0x00 0x0D length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x9A 0x00 0x11 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x9A 0x00 0x0F length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x9D 0x00 0x09 length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x96 0x00 0x0A length: 4
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x06 length: 4

Here ESP try to connect to MQTT because it looses wifi connection

Attempting MQTT connection...connected
Sending to 0x58: 0x20 0x08 , Received: 0x01 0x90 0x00 0x00 length: 4
Sending to 0x58: 0x20 0x50 , Received: 0x31 0x8D 0x47 0x9D length: 4
Sending to 0x70: 0x78 0x66 , Received: 0x61 0xBD 0x7B 0xBE length: 4
CO2 equivalent : 400, TVOC : 0, H2_signal : 12685, Ethanol_signal : 18333, Humidity : 48.34, temperature : 21.81, absolute humidity : 9.28Sending to 0x58: 0x20 0x15 , Received: 0x00 0x00 0x00 0x00 length: 4
 CO2 equivalent baseline : 0x0Sending to 0x58: 0x20 0x15 , Received: 0x00 0x00 0x00 0x00 length: 4
, TVOC baseline : 0x0
Data = {"co2_equivalent":400,"tvoc":0,"h2_signal":12685,"ethanol_signal":18333,"humidity":48.33599853515625,"temperature":21.812999725341797,"absolute_hum":9.2800455093383789,"co2_equivalent_baseline":16,"tvoc_baseline":16,"chipid":"000010a4ae30"}
[D][WiFiClient.cpp:509] connected(): Disconnected: RES: 0, ERR: 128

I am not using deep sleep yet, which would have disconnected from wifi.

Update : I had ESP32-CAM connected to same AP, when I turn off ESP32-CAM then this code works without any error (ERR: 128)

Dont know how ESP32-CAM WiFi connection affects ESP32 WiFi connectivity ?

  • what happens if you set DELAY to a lower value – Jaromanda X Jan 4 at 17:55
  • For DELAY 0, DELAY 1, DELAY 5, DELAY 10 getting same [D][WiFiClient.cpp:509] connected(): Disconnected: RES: 0, ERR: 128 – roy Jan 4 at 19:52
  • perhaps this post in an issue on github sheds some light - I don't know - it seems your not disconnecting from wifi though - nothing in the log suggests that, does it? – Jaromanda X Jan 5 at 0:49
  • If you are actually disconnectin gfrom Wifi, I know some ESP8266's had trouble keeping a wifi connection with a small set of wifi routers in an older version of the ESP8266 library - not sure if the ESP32 suffered the same issue, but out of interest, what make/model is the access point/wifi router you are connecting to? – Jaromanda X Jan 5 at 0:51
  • I have esp32-cam which connect to same AP with no issue. – roy Jan 5 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.