sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
227 stars 121 forks source link

Something went wrong...could not send Event to server! - No code temperature sensor DHT11 esp8266 #353

Closed abhinav4gangwar closed 6 months ago

abhinav4gangwar commented 7 months ago

Here's the complete log for the error. The device shows online in sinric pro dashboard but it doesnt seem to send any data. Code is the default no code that sinric pro provides.

21:50:43.185 -> [SinricPro:add()]: Adding device with id "6560dc13031135be133db303". 21:50:43.185 -> [SinricPro:Websocket]: Connecting to WebSocket Server using SSL (ws.sinric.pro) 21:50:43.185 -> [SinricPro:Websocket]: headers: 21:50:43.185 -> appkey:a9f12144-4054-45e2-baa1-973c5d071da9 21:50:43.185 -> deviceids:6560dc13031135be133db303 21:50:43.217 -> restoredevicestates:false 21:50:43.217 -> ip:192.168.29.32 21:50:43.217 -> mac:2C:F4:32:2E:B0:68 21:50:43.217 -> platform:ESP8266 21:50:43.217 -> SDKVersion:3.0.0 21:50:46.135 -> [SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server. 21:50:46.135 -> Something went wrong...could not send Event to server! 21:50:46.181 -> [SinricPro:Websocket]: connected 21:50:46.181 -> Connected to SinricPro 21:50:46.181 -> [SinricPro:Websocket]: receiving data 21:50:46.477 -> [SinricPro.handleReceiveQueue()]: 1 message(s) in receiveQueue 21:50:46.477 -> [SinricPro.handleReceiveQueue()]: Signature is valid. Processing message... 21:50:46.477 -> [SinricPro:extractTimestamp(): Got Timestamp 1700929246

sivar2311 commented 7 months ago
21:50:46.135 -> [SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server.
...
21:50:46.181 -> [SinricPro:Websocket]: connected

It looks like you send events before the connection to the SinricPro server has been established.

abhinav4gangwar commented 7 months ago

Can you please specify where the connection to SinricPro is being established. I tried adding some delay in the setupSinricPro function but it doesn't seem to work. Also I randomly received a temperature reading in my SinricPro dashboard (then it started saying that the event could not be sent) with the current code.

/*
 * Example for how to use SinricPro Temperaturesensor device:
 * - setup a temperature sensor device
 * - send temperature event to SinricPro server when temperature has changed
 * 
 *
 * DHT Library used in this example: https://github.com/markruys/arduino-DHT
 * 
 * If you encounter any issues:
 * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
 * - ensure all dependent libraries are installed
 *   - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
 *   - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
 * - open serial monitor and check whats happening
 * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
 * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
 */
#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
 #define DEBUG_ESP_PORT Serial
 #define NODEBUG_WEBSOCKETS
 #define NDEBUG
#endif

#include <Arduino.h>
#ifdef ESP8266 
 #include <ESP8266WiFi.h>
#endif 
#ifdef ESP32   
 #include <WiFi.h>
#endif

#include "SinricPro.h"
#include "SinricProTemperaturesensor.h"
#include "DHT.h" // https://github.com/markruys/arduino-DHT

#define WIFI_SSID         ""
#define WIFI_PASS         ""
#define APP_KEY           ""
#define APP_SECRET        ""
#define TEMP_SENSOR_ID    "" 
#define BAUD_RATE         115200              // Change baudrate to your need (used for serial monitor)
#define EVENT_WAIT_TIME   60000               // send event every 60 seconds

#define DHT_PIN           5

DHT dht;                                      // DHT sensor

bool deviceIsOn;                              // Temeprature sensor on/off state
float temperature;                            // actual temperature
float humidity;                               // actual humidity
float lastTemperature;                        // last known temperature (for compare)
float lastHumidity;                           // last known humidity (for compare)
unsigned long lastEvent = (-EVENT_WAIT_TIME); // last time event has been sent

/* bool onPowerState(String deviceId, bool &state) 
 *
 * Callback for setPowerState request
 * parameters
 *  String deviceId (r)
 *    contains deviceId (useful if this callback used by multiple devices)
 *  bool &state (r/w)
 *    contains the requested state (true:on / false:off)
 *    must return the new state
 * 
 * return
 *  true if request should be marked as handled correctly / false if not
 */
bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Temperaturesensor turned %s (via SinricPro) \r\n", state?"on":"off");
  deviceIsOn = state; // turn on / off temperature sensor
  return true; // request handled properly
}

/* handleTemperatatureSensor()
 * - Checks if Temperaturesensor is turned on
 * - Checks if time since last event > EVENT_WAIT_TIME to prevent sending too much events
 * - Get actual temperature and humidity and check if these values are valid
 * - Compares actual temperature and humidity to last known temperature and humidity
 * - Send event to SinricPro Server if temperature or humidity changed
 */
void handleTemperaturesensor() {
  // if (deviceIsOn == false) return; // device is off...do nothing
  // Serial.printf("4\r\n");
  unsigned long actualMillis = millis();
  if (actualMillis - lastEvent < EVENT_WAIT_TIME) return; //only check every EVENT_WAIT_TIME milliseconds
  delay(dht.getMinimumSamplingPeriod());
  temperature = dht.getTemperature();          // get actual temperature in °C
//  temperature = dht.getTemperature() * 1.8f + 32;  // get actual temperature in °F
  humidity = dht.getHumidity();                // get actual humidity

  if (isnan(temperature) || isnan(humidity)) { // reading failed... 
    Serial.printf("DHT reading failed!\r\n");  // print error message
    return;                                    // try again next time
  } 

  if (temperature == lastTemperature || humidity == lastHumidity) return; // if no values changed do nothing...
  // Serial.printf("Temperature: %2.1f Celsius\tHumidity: %2.1f%%\r\n", temperature, humidity);
  SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];  // get temperaturesensor device
  bool success = mySensor.sendTemperatureEvent(temperature, humidity); // send event
  if (success) {  // if event was sent successfuly, print temperature and humidity to serial
    Serial.printf("Temperature: %2.1f Celsius\tHumidity: %2.1f%%\r\n", temperature, humidity);
  } else {  // if sending event failed, print error message
    Serial.printf("Something went wrong...could not send Event to server!\r\n");
  }

  lastTemperature = temperature;  // save actual temperature for next compare
  lastHumidity = humidity;        // save actual humidity for next compare
  lastEvent = actualMillis;       // save actual time for next compare
}

// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  IPAddress localIP = WiFi.localIP();
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProTemperaturesensor& mySensor = SinricPro[TEMP_SENSOR_ID];

  // setup SinricPro
  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); 
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });

  SinricPro.begin(APP_KEY, APP_SECRET);  
  delay(10000);
}

// main setup function
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  // dht.setup(DHT_PIN);
  dht.setup(DHT_PIN);

  setupWiFi();
  setupSinricPro();
}

void loop() {
  // Serial.printf("2\r\n");
  SinricPro.handle();
  // Serial.printf("3\r\n");
  handleTemperaturesensor();
}

Edit by sivar2311: Inserted Codeblocks for better readability Please use Codeblocks (three backticks ```before and after the code)

sivar2311 commented 7 months ago

Can you please specify where the connection to SinricPro is being established.

The connection is automatically established in the background by SinricPro.handle().

I tried adding some delay in the setupSinricPro...

This will not work due to the previously mentioned connection process handled by SinricPro.handle(). The library works "non-blocking".

Also I randomly received a temperature reading in my SinricPro dashboard (then it started saying that the event could not be sent) with the current code

The error message should (and probably will) only be displayed as long as there is no connection to the server.

However, you can make the sending of the event dependent on the existence of a connection:

Option 1:

void handleTemperaturesensor() {
  if (!SinricPro.isConnected()) return;

  <rest of the existing code>

Option 2:

void loop() {
  SinricPro.handle();
  if (SinricPro.isConnected()) handleTemperaturesensor();
}
sivar2311 commented 7 months ago

Additional notes:

The code you're using is outdated since version 3.x.

You can remove the following lines:

bool deviceIsOn;                              // Temeprature sensor on/off state
/* bool onPowerState(String deviceId, bool &state) 
 *
 * Callback for setPowerState request
 * parameters
 *  String deviceId (r)
 *    contains deviceId (useful if this callback used by multiple devices)
 *  bool &state (r/w)
 *    contains the requested state (true:on / false:off)
 *    must return the new state
 * 
 * return
 *  true if request should be marked as handled correctly / false if not
 */
bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Temperaturesensor turned %s (via SinricPro) \r\n", state?"on":"off");
  deviceIsOn = state; // turn on / off temperature sensor
  return true; // request handled properly
}
// if (deviceIsOn == false) return; // device is off...do nothing
github-actions[bot] commented 7 months ago

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!

github-actions[bot] commented 6 months ago

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!