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

Alexa read back other Air Quality, C02, N02, CH20, #16

Open
TPCQitek opened this issue Nov 21, 2022 · 29 comments
Open

Alexa read back other Air Quality, C02, N02, CH20, #16

TPCQitek opened this issue Nov 21, 2022 · 29 comments

Comments

@TPCQitek
Copy link

Originally posted by @TPCQitek in #14 (comment)


revised question, so much like the read back PM2.5 (as inAirQualitySensor.h) this allow me to add my Sensor reading values, I would like to have at minimum additionally (C02, N02, CH20,) as a read back.

thanks.

@kakopappa
Copy link
Contributor

kakopappa commented Nov 21, 2022 via email

@TPCQitek
Copy link
Author

TPCQitek commented Nov 21, 2022 via email

@kakopappa
Copy link
Contributor

AFAIK PM2. 5 is measured in micrograms per cubic meter of air, so you should select CubicMeters

@TPCQitek
Copy link
Author

TPCQitek commented Nov 23, 2022 via email

@kakopappa
Copy link
Contributor

Hi @TPCQitek

Can't see the code for main.ino or the images embedded. can you update the issue again? Replaying the email with images won't work.

@kakopappa
Copy link
Contributor

utterance would be

Alexa, what is the [range controller name] of [device name]

Example: I have added a range controller with the name: level and a device with the name: C02 Sensor.

You can ask Alexa

Alexa, what is the level of C02 Sensor

image
image

@sinricpro sinricpro deleted a comment from TPCQitek Nov 23, 2022
@kakopappa
Copy link
Contributor

kakopappa commented Nov 23, 2022

in the example code, you have attached I did not see you are updating the server with C02 values.

here's my example code. It will update the range value every 1 min. You can see it in Alexa and inquire about it.

I have deleted your comment because it contains secret keys.

/*
 * Example
 *
 * 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
 */

 // Custom devices requires SinricPro ESP8266/ESP32 SDK 2.9.6 or later

// Uncomment the following line to enable serial debug output
#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 "CSensor.h"

#define APP_KEY    ""
#define APP_SECRET ""
#define DEVICE_ID  ""

#define SSID       ""
#define PASS       ""

#define BAUD_RATE  9600

CSensor &cSensor = SinricPro[DEVICE_ID];

/*************
 * Variables *
 ***********************************************
 * Global variables to store the device states *
 ***********************************************/

// RangeController
std::map<String, int> globalRangeValues;

/*************
 * Callbacks *
 *************/

// RangeController
bool onRangeValue(const String &deviceId, const String& instance, int &rangeValue) {
  Serial.printf("[Device: %s]: Value for \"%s\" changed to %d\r\n", deviceId.c_str(), instance.c_str(), rangeValue);
  globalRangeValues[instance] = rangeValue;
  return true;
}

bool onAdjustRangeValue(const String &deviceId, const String& instance, int &valueDelta) {
  globalRangeValues[instance] += valueDelta;
  Serial.printf("[Device: %s]: Value for \"%s\" changed about %d to %d\r\n", deviceId.c_str(), instance.c_str(), valueDelta, globalRangeValues[instance]);
  globalRangeValues[instance] = valueDelta;
  return true;
}

/**********
 * Events *
 *************************************************
 * Examples how to update the server status when *
 * you physically interact with your device or a *
 * sensor reading changes.                       *
 *************************************************/

// RangeController
void updateRangeValue(String instance, int value) {
  cSensor.sendRangeValueEvent(instance, value);
}

/********* 
 * Setup *
 *********/

void setupSinricPro() {

  // RangeController
  cSensor.onRangeValue("rangeInstance1", onRangeValue);
  cSensor.onAdjustRangeValue("rangeInstance1", onAdjustRangeValue);


  SinricPro.onConnected([]{ Serial.printf("[SinricPro]: Connected\r\n"); });
  SinricPro.onDisconnected([]{ Serial.printf("[SinricPro]: Disconnected\r\n"); });
  SinricPro.begin(APP_KEY, APP_SECRET);
};

void setupWiFi() {
  WiFi.begin(SSID, PASS);
  Serial.printf("[WiFi]: Connecting to %s", SSID);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected\r\n");
}

void setup() {
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

/********
 * Loop *
 ********/

unsigned long lastMillis = 0;

void loop() {
  SinricPro.handle();

  if (millis() - lastMillis > 60000) {
    lastMillis = millis();
    Serial.printf("updateRangeValue ..\r\n");
    updateRangeValue("rangeInstance1", random(1,2000));
  }
}

image

@kakopappa
Copy link
Contributor

kakopappa commented Nov 24, 2022

@sivar2311 has created a much-simplified version of my code.

  • Callbacks aren't used here (RangeValue is read only)
  • std::map isn't used either
  • The sketch will start sending CO2 values as soon it is connected to the server, then periodically every minute.

https://gist.github.com/sivar2311/84d2ee09b6e18d16f2670492dca7f3bc

#include <Arduino.h>
#include <SinricPro.h>

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif

#include "CSensor.h"

#define APP_KEY    ""
#define APP_SECRET ""
#define DEVICE_ID  ""

#define SSID ""
#define PASS ""

#define BAUD_RATE 115200

CSensor &cSensor = SinricPro[DEVICE_ID];

int getCO2_level() {
    // Implement your CO2 Sensor reading here and return the co2 value
    // For now this function only return a dummy value between 1 and 20000
    return random(1, 2000);
}

void updateCO2Level() {
    if (SinricPro.isConnected() == false) return;

    static unsigned long last_millis;
    unsigned long        current_millis = millis();
    if (last_millis && current_millis - last_millis < 60000) return;
    last_millis = current_millis;

    int CO2_level = getCO2_level();
    cSensor.sendRangeValueEvent("level", CO2_level);
}

void setup() {
    WiFi.begin(SSID, PASS);
    Serial.begin(BAUD_RATE);
    SinricPro.begin(APP_KEY, APP_SECRET);
}

void loop() {
    SinricPro.handle();
    updateCO2Level();
}

@TPCQitek
Copy link
Author

TPCQitek commented Nov 24, 2022 via email

@TPCQitek
Copy link
Author

TPCQitek commented Nov 24, 2022 via email

@TPCQitek
Copy link
Author

TPCQitek commented Nov 25, 2022 via email

@TPCQitek
Copy link
Author

TPCQitek commented Nov 25, 2022 via email

@kakopappa
Copy link
Contributor

the global CSensor &cSensor = SinricPro[DEVICE_ID]; can be changed to a local just before using the cSensor object

to something like below;

const char *deviceId = "whatever";
CSensor &cSensor = SinricPro[deviceId];
cSensor.sendRangeValueEvent(instance, value);

[C02 Sensor] set to 0 2022-11-25 18:55:36 101.178.35.49

According to the event log, your ESP is sending the value 0. Have you taken a look at the Arduino serial monitor to confirm this? The code looks fine though.

@kakopappa
Copy link
Contributor

I tried your code. it seems to work fine
image

One thing I noticed was the time difference between the two events is too close almost as if 2 device sending events

@TPCQitek
Copy link
Author

TPCQitek commented Nov 26, 2022 via email

@TPCQitek
Copy link
Author

TPCQitek commented Nov 26, 2022 via email

@sivar2311
Copy link

Hi @TPCQitek !

my point was that placing the statement *CSensor &cSensor = SinricPro[deviceId]; in the void setup() caused an error, as the cSensor is not declared, *

Can you post your code and the error message, please?
There should be no error using a local variable.
I think the error must be somewhere else.

Please use codeblocks!
This keeps the formatting of your code and helps us to read your code more clearly.

@TPCQitek
Copy link
Author

TPCQitek commented Nov 28, 2022 via email

@kakopappa
Copy link
Contributor

the app key and secret are not associated with this device id: 638464f5b8a7fefbd64c6dfc is wrong. According to your device settings, you have to put the appkey/secret associated with the new appkey name "IAQ"

@TPCQitek
Copy link
Author

TPCQitek commented Nov 28, 2022 via email

@kakopappa
Copy link
Contributor

Your account has 3 devices.

6384xxx00df29 - QiTek MIDI
6384xxx00e7c8 - C02 Sensor
6384xxx00f582 - QiTek Sensors

637efe47b8a7fefbd647b3f2 << Is not one of them. Likely you need to update your sketch / config in EEPROM

@TPCQitek
Copy link
Author

TPCQitek commented Nov 29, 2022 via email

@TPCQitek
Copy link
Author

TPCQitek commented Nov 29, 2022 via email

@kakopappa
Copy link
Contributor

1. 11:04:24.860 -> [SinricPro]: Device "63848db5333d12dd2a00f582" does not exist. Creating new devices

this refers to the internal list of device ids SDK holds. It's not creating devices on the server.

2) AppSecert not found for 637efe47b8a7fefbd647b3f2. Is device id correct?

When your ESP connects it sends the following details.

Device Ids: "637xxxx7fefbd647b3f2;638479xxxxxa00df29,"
AppKey: cc82ac77-xxxx-0ed047434f91

may be you delete these devices on the server but ESP reconnected with the above appkey. since the server could not find the appkey it just logs every time.

You can enable the SDK logs so you can see these ids are being sent. To enable the SDK logs, add this code to top of the ino file.

#define ENABLE_DEBUG

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

@TPCQitek
Copy link
Author

TPCQitek commented Nov 29, 2022 via email

@TPCQitek
Copy link
Author

TPCQitek commented Nov 29, 2022 via email

@sivar2311
Copy link

You can put the statement CSensor &cSensor = SinricPro[DEVICE_ID]; anywhere in your code and use it as a local variable!
If it is not a global variable, you have to put this line of code in all your functions that use this device.

Example:

void updateCO2Level() {
    CSensor &cSensor = SinricPro[DEVICE_ID]; // <- cSensor as local variable
    if (SinricPro.isConnected() == false) return;

    static unsigned long last_millis;
    unsigned long        current_millis = millis();
    if (last_millis && current_millis - last_millis < 60000) return;
    last_millis = current_millis;

    int CO2_level = getCO2_level();
    cSensor.sendRangeValueEvent("level", CO2_level);
}

@TPCQitek
Copy link
Author

TPCQitek commented Nov 29, 2022 via email

@sivar2311
Copy link

There is nothing to solve.
As kakopappa said: This message comes from the SDK running on the ESP.

When a device is used for the first time in your Sketch, e.g. CSensor& cSensor = SinricPro[DEVICE_ID];, the SDK checks if this device is already created (on the ESP) and set up in the SDK.
If the device has not yet been created (on the ESP), it will be created so that your Sketch can use the device.

This does not mean that the device is created server-side!

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

3 participants