Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect values. #1

Open
shojkeee opened this issue Apr 23, 2022 · 9 comments
Open

Incorrect values. #1

shojkeee opened this issue Apr 23, 2022 · 9 comments

Comments

@shojkeee
Copy link

Hello. I came across your library and an example for taking readings from the dsm501 sensor. Your example is the best. But here's the problem when I add mqtt to your example. All indications become incorrect. I guess the problem is in time. But I can't figure out how to solve it.

@karser
Copy link
Owner

karser commented Apr 23, 2022

Do you use delays? Can you maybe show the code? What device is it btw?

@shojkeee
Copy link
Author

shojkeee commented Apr 23, 2022

#include "AGS02MA.h"
#include <Wire.h>
#include <WiFi.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMqttClient.h>

#define WIFI_SSID "NETGEAR"
#define WIFI_PASSWORD "14117687"

// Raspberry Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, 31, 145)
// For a cloud MQTT broker, type the domain name
//#define MQTT_HOST "example.com"
#define MQTT_PORT 1883

// Temperature MQTT Topics
#define MQTT_PUB_TEMP "esp32/ags02ma/tvoc"

float ppm25;
#define DUST_SENSOR_PIN_PM10 5 //Must be the pins that
#define DUST_SENSOR_PIN_PM25 2 //support interrupts

#define INTERVAL_COUNTDOWN 1000
#define INTERVAL_READ 30000

#include <KarserDSM501.h>
// ISRs forward declaration
void pm10_handleInterrupt();
void pm25_handleInterrupt();
// init pm10 and pm25 instances
KarserDSM501 pm10(DUST_SENSOR_PIN_PM10, pm10_handleInterrupt);
KarserDSM501 pm25(DUST_SENSOR_PIN_PM25, pm25_handleInterrupt);
// handle ISRs
void pm10_handleInterrupt() { pm10.handleInterrupt(); }
void pm25_handleInterrupt() { pm25.handleInterrupt(); }

unsigned long timer = 0;
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 1000; // Interval at which to publish sensor readings

void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttPublish(uint16_t packetId) {
Serial.print("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}

void setup() {
Serial.begin(115200);
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

WiFi.onEvent(WiFiEvent);

mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
//mqttClient.onSubscribe(onMqttSubscribe);
//mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// If your broker requires authentication (username and password), set them below
mqttClient.setCredentials("mqtt", "mqtt");
connectToWifi();
}

void loop() {

if (!pm10.isReady() && (millis() >= timer + INTERVAL_COUNTDOWN)) {
Serial.println("DSM501 warm up: " + String(pm10.getReadyCountdown()));
timer += INTERVAL_COUNTDOWN;

} else if (millis() >= timer + INTERVAL_READ) {
timer += INTERVAL_READ;
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(pm25.readPM()).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", MQTT_PUB_TEMP, packetIdPub1);
Serial.printf("Message: %.2f \n", String(pm25.readPM()));
}
}

@shojkeee
Copy link
Author

I'm not selenium in programming. I have one sample, I connect different sensors in the image and likeness.

@shojkeee
Copy link
Author

Are you Russian? then you can use Russian, it will be easier this way

@karser
Copy link
Owner

karser commented Apr 23, 2022

Let's stick to English since this might help somebody else.

Try commenting out this line
//uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(pm25.readPM()).c_str());
and check if the pm25 value is correct in console.
So that you will figure out if the issue happens during publishing or setup.

@shojkeee
Copy link
Author

I've done it before. It doesn't help. I completely removed everything from the "void loop", leaving only your code. And still the values are not correct. I think the problem is connecting to wi-fi and to the mqtt server. Somewhere in this place there is a delay that causes the pulses to be counted incorrectly.

@shojkeee
Copy link
Author

I have now tried another example. And the same thing, after I add the code to send to the mqtt server, the values begin to be incorrect. Apparently I turned to the wrong address(

@karser
Copy link
Owner

karser commented Apr 23, 2022

If interrupts don't work during wi-fi connection you probably should split this task into 2 boards so that pulses will be counted uninterruptedly. I'm wondering if ATTINY13 can handle this task.

@shojkeee
Copy link
Author

in general, have examples on esp8266 with sending to the mqtt server. But they are too sophisticated. They are on platform.io and there is a lot of excess, for example, temperature sensors. It takes a very long time to clean all this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants