esp8266-esp32-sdk
esp8266-esp32-sdk copied to clipboard
Issues with Sending Sensor Data to Sinric Pro from ESP32
I'm facing an issue with my ESP32 project where I'm unable to send temperature, humidity, and air quality data to Sinric Pro. The device connects to WiFi and Sinric Pro, but fails to send the data. Below is the code I'm using. Any guidance on what might be going wrong would be greatly appreciated.
#include <WiFi.h>
#include <SinricPro.h>
#include <SinricProTemperaturesensor.h>
#include <SinricProAirQualitySensor.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <DHT.h>
#include <TroykaMQ.h>
// Define pins for the display
#define TFT_CS 5
#define TFT_RST 16
#define TFT_DC 17
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Define pins and type for the DHT22 sensor
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Define pin for the MQ-135 sensor
#define MQ135PIN 34
MQ135 mq135(MQ135PIN);
// WiFi configuration
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// Sinric Pro configuration
const char* appKey = "YOUR_APP_KEY";
const char* appSecret = "YOUR_APP_SECRET";
const char* temperatureDeviceID = "YOUR_TEMPERATURE_DEVICE_ID";
const char* airQualityDeviceID = "YOUR_AIR_QUALITY_DEVICE_ID";
void setupWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void setupSinricPro() {
SinricProTemperaturesensor &myTemperatureSensor = SinricPro[temperatureDeviceID];
SinricProAirQualitySensor &myAirQualitySensor = SinricPro[airQualityDeviceID];
SinricPro.onConnected([](){ Serial.println("Connected to SinricPro"); });
SinricPro.onDisconnected([](){ Serial.println("Disconnected from SinricPro"); });
SinricPro.begin(appKey, appSecret);
}
void setupDisplay() {
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
}
void readAndDisplaySensors() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
float airQuality = mq135.readCO2();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
tft.fillScreen(ILI9341_BLACK);
tft.drawLine(0, 60, tft.width(), 60, ILI9341_WHITE);
tft.drawLine(0, 120, tft.width(), 120, ILI9341_WHITE);
tft.fillCircle(20, 30, 10, ILI9341_RED);
tft.setCursor(40, 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Temp: ");
tft.print(temperature, 1);
tft.println(" C");
tft.fillCircle(20, 90, 10, ILI9341_BLUE);
tft.setCursor(40, 80);
tft.print("Humidity: ");
tft.print(humidity, 1);
tft.println(" %");
tft.fillCircle(20, 150, 10, ILI9341_GREEN);
tft.setCursor(40, 140);
tft.print("Air Quality: ");
tft.print(airQuality, 1);
tft.println(" ppm");
}
void sendToSinricPro(float temperature, float humidity, float airQuality) {
SinricProTemperaturesensor &myTemperatureSensor = SinricPro[temperatureDeviceID];
SinricProAirQualitySensor &myAirQualitySensor = SinricPro[airQualityDeviceID];
if (WiFi.status() == WL_CONNECTED) {
bool tempResult = myTemperatureSensor.sendTemperatureEvent(temperature, humidity);
if (tempResult) {
Serial.println("Temperature and Humidity sent successfully");
} else {
Serial.println("Failed to send Temperature and Humidity");
}
bool airQualityResult = myAirQualitySensor.sendAirQualityEvent(airQuality);
if (airQualityResult) {
Serial.println("Air Quality sent successfully");
} else {
Serial.println("Failed to send Air Quality");
}
} else {
Serial.println("WiFi not connected");
}
}
void setup() {
Serial.begin(115200);
setupDisplay();
dht.begin();
mq135.calibrate();
setupWiFi();
setupSinricPro();
}
void loop() {
SinricPro.handle();
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
float airQuality = mq135.readCO2();
readAndDisplaySensors();
sendToSinricPro(temperature, humidity, airQuality);
delay(60000); // Wait 60 seconds before sending again
}
Issue Description:
-
The device connects to WiFi and Sinric Pro successfully.
-
Fails to send temperature, humidity, and air quality data to Sinric Pro.
-
The Sinric Pro console shows the devices are connected, but no data is being received.
Request for Assistance:
-
Are there any issues with the way I'm sending the data to Sinric Pro?
-
Is there a specific order or additional steps required to ensure the data is sent successfully?
-
Any suggestions or modifications to make the code work as expected?
Thank you in advance for your help!
Your issue is likely delay(60000); which pauses the MCU (that ends up dropping the internal connection to the server).
Try re-writting without delay like below
unsigned long lastSendTime = 0; // Stores the time of the last data send
void loop() {
SinricPro.handle();
// Check if 60 seconds have passed since the last send
if (millis() - lastSendTime >= 60000) {
lastSendTime = millis(); // Update last send time
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
float airQuality = mq135.readCO2();
readAndDisplaySensors();
sendToSinricPro(temperature, humidity, airQuality);
}
}
Ref: https://help.sinric.pro/pages/tutorials/air-quality-sensors/mq135
This issue has gone quiet. Spooky quiet. We currently close issues after 14 days of inactivity. It’s been at least 7 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks for being a part of the SinricPro community!
Hey again! It’s been 14 days since anything happened on this issue, so our friendly robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to comment on this issue or create a new one if you need anything else. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks again for being a part of the SinricPro community!