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
Other
230 stars 124 forks source link

Retrieving data from database #170

Closed rcassinis closed 3 years ago

rcassinis commented 3 years ago

I am new to Sinric Pro, which I think is very useful and simple to use, yet a bit cryptic about where questions should be asked. I have two questions though: 1) is it possible to assign static addresses to the devices? 2) What is the best way to retrieve data from the database? Explanation: I have built a system that includes a NodeMCU that provides periodical data from a sensor (every minute, actually), and I have another NodeMCU that must, every minute, compare the last two data and drive a physical device. The data are in the database (the Sinric Pro web dashboard shows them9, but what is the best way to acquire them? The two ESP must be separated for wiring reasons.

Any help will be much appreciated.

sivar2311 commented 3 years ago

I am new to Sinric Pro, which I think is very useful and simple to use, yet a bit cryptic about where questions should be asked.

For questions and issues related to Arduino Library: this is the right place :)

  1. is it possible to assign static addresses to the devices?

What kind of "addresses"? Do you mean IP-Addresses like here ?

2 ...

I think this is question, is not related to the library and should be asked in the non-sdk-issues repository

rcassinis commented 3 years ago

1) Yes, I mean static IP addresses. 2) OK, i will move the question to the right place :)

riccardo

sivar2311 commented 3 years ago

There is nothing special about using static ip and this library.

I think the link from above should help you on this topic.

sivar2311 commented 3 years ago

Comment on your question number 2:

If you need the same data on ESP no. 2 that is captured by ESP no. 1, I would have both ESP's communicate directly! But that is outside the scope of this library and even outside SinricPro.

Another workaround (could) be (if the capturing ESP no. 1 can make the comparison on it's own): Use a SinricPro contact sensor in ESP no. 1 and set the contact to "close" if you have a match. This could be used in alexa routines, to trigger ESP no. 2. However, this is only a "could be workaround", and not very efficient.

rcassinis commented 3 years ago

Tank you Sivar for the prompt response. I'll try to explain better, although I think you already understood well my problem. I have a wind speed meter, connected to an ESP 32, which in turn provides every minute a floating point value that is sent via Sinric Pro to Alexa. This works fine. Now, the same values must also be sent to another ESP 32 that will further process them and decide if a sun tent should be closed to avoid being destroyed. Tis ESP also receives commands from Alexa, via Sinric Pro, which drive the sun tent and various other devices. In the future, the same data will also be sent to a raspi that will plot them on a screen. The idea of reading data from the database comes from the fact that in the brokering systems I am familiar with (mainly ROS, as I am a retired roboticist) communication is bidirectional, and once a topic has been published it can be subscribed by any member of the system. My mistake was to think that such mechanism should be available also in Sinric pro, while from what you wrote I understand is not so. Obviously I thought of making a direct connection, but it's a solution I don't like, as it spoils the "cleanliness" of the whole system, and it's not so easy to implement with dynamic IP addresses. That's why I asked about static IPs.

The last point is that, if I need old data, I must retrieve them from the database, or I will be compelled to implement another database to store them, duplicating something that already exists in the system.

I hope this time I was more clear

sivar2311 commented 3 years ago

Since you (will) use a RaspberryPI anyway: Why not use it as MQTT broker? This could take care of the data storage and the communication between the devices.

SinricPro works quite differently than an MQTT broker. There is no channel on which multiple devices can listen or exchange commands.

But thanks for the description. Now the system becomes a little clearer. Did you create a custom device or are you using a temperature sensor that actually transmits the wind data?

rcassinis commented 3 years ago

Since you (will) use a RaspberryPI anyway: Why not use it as MQTT broker? This could take care of the data storage and the communication between the devices. Yes, but i'd prefer the system to havee a graceful degradation if non-essential components go out of order.

SinricPro works quite differently than an MQTT broker. There is no channel on which multiple devices can listen or exchange commands.

This is why I like it. On the other hand, I thought that if one can write mySensor.sendTemperatureEvent(temperature, humidity), a method like mySinricPro.getTemperatureEvent(mySensor, &temperature, &humidity); or something similarcould also exist. Now that I know, I'll probably resort to the direct communication solution. But first i must find out why my program doesn't work with static IP addresses.

Did you create a custom device or are you using a temperature sensor that actually transmits the wind data?

So dar, i've been using the temperatureSensor example, but I plan to build a custom device in the future.

kakopappa commented 3 years ago

We do not grant direct access to the database. If you need to access data that belongs to your account, you can use the API. However, accessing API via ESP32 is a bit difficult due to the authentication and querying data are two API calls. https://apidocs.sinric.pro/

If I want to send data from one device to another I would do following:

  1. Use ESP-NOW
  2. Setup an MQTT Broker or use a public broker
  3. UDP broadcasting
rcassinis commented 3 years ago

I am sorry, but I still have an issue I cannot understand. The program i am using for the sensor coes from the example provided by the library (TempertureSensor.h), and its significant part is as follows:

`

define ENABLE_DEBUG

ifdef ENABLE_DEBUG

define DEBUG_ESP_PORT Serial

define NODEBUG_WEBSOCKETS

define NDEBUG

endif

include

//#ifdef ESP8266

include

//#endif //#ifdef ESP32 //#include //#endif

include "SinricPro.h"

include "SinricProTemperaturesensor.h"

define WIFI_SSID "TANAORSO"

define WIFI_PASS "XXXXXXXXX"

define APP_KEY "663022a1-7039-4478-8756-xxxxxxxxxxxxxxx"

define APP_SECRET "f31c538e-5f9e-410d-9b7f-6fe4730b1245-c9628df7-e134-419d-b2d7-xxxxxxxxxx"

define TEMP_SENSOR_ID "60639e1541a7a97d3ef50c76"

define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor)

define EVENT_WAIT_TIME 60000 // send event every 60 seconds

//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 }

/* handleTemperatureSensor()

// setup function for WiFi connection void setupWiFi() { Serial.printf("\r\n[Wifi]: Connecting"); IPAddress ip(10, 0, 1, 101); IPAddress gateway (10, 0, 1, 1); IPAddress subnet (255, 255, 255, 0); // WiFi.config(ip, gateway, subnet); //. <-------------------------------------------------------------- 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]; mySensor.onPowerState(onPowerState); // 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); SinricPro.restoreDeviceStates(true); // get latest known deviceState from server (is device turned on?) }

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

setupWiFi(); setupSinricPro(); }

void loop() { SinricPro.handle(); handleTemperaturesensor(); } ` This program works perfectly for me, generating every minute random temperature data and constant humidity data that can be received by the Sinric Pro app. But, if I uncomment the line indicated by the arrow, thus imposing a static IP address, the program doesn't work anymore, even if the dhcp-generated address and the static one are the same. I am enclosing here the full output of the two versions of the program, the first one being the working one, and the second one the version using static IP address.

`DHCP VERSION:

09:55:13.823 -> rl�d⸮⸮|�⸮l⸮| ⸮l⸮c|⸮⸮⸮⸮;⸮c⸮c⸮⸮gn⸮dog⸮⸮⸮c8⸮⸮lrl;lx⸮o⸮ ............connected! 09:55:17.861 -> [WiFi]: IP-Address is 10.0.1.193 09:55:17.861 -> [SinricPro]: Device "60639e1541a7a97d3ef50c76" does not exist. Creating new device 09:55:17.861 -> [SinricPro:add()]: Adding device with id "60639e1541a7a97d3ef50c76". 09:55:17.861 -> [SinricPro:Websocket]: Connecting to WebSocket Server using SSL (ws.sinric.pro) 09:55:17.861 -> [SinricPro:Websocket]: headers: 09:55:17.861 -> appkey:663022a1-7039-4478-8756-xxxxxxxxxxxxxxxxxxx 09:55:17.896 -> deviceids:60639e1541a7a97d3ef50c76 09:55:17.896 -> restoredevicestates:true 09:55:17.896 -> ip:10.0.1.193 09:55:17.896 -> mac:D8:BF:C0:14:B6:46 09:55:17.896 -> platform:ESP8266 09:55:17.896 -> version:2.9.2 09:55:19.149 -> [SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server. 09:55:19.149 -> Something went wrong...could not send Event to server! 09:55:19.182 -> [SinricPro:Websocket]: connected 09:55:19.182 -> Connected to SinricPro 09:55:19.182 -> [SinricPro:Websocket]: headers: 09:55:19.182 -> appkey:663022a1-7039-4478-8756-xxxxxxxxxxxxxxxxxx 09:55:19.182 -> deviceids:60639e1541a7a97d3ef50c76 09:55:19.182 -> restoredevicestates:false 09:55:19.182 -> ip:10.0.1.193 09:55:19.182 -> mac:D8:BF:C0:14:B6:46 09:55:19.182 -> platform:ESP8266 09:55:19.182 -> version:2.9.2 09:55:19.182 -> [SinricPro:Websocket]: receiving data 09:55:19.216 -> [SinricPro.handleReceiveQueue()]: 1 message(s) in receiveQueue 09:55:19.216 -> [SinricPro.handleReceiveQueue()]: Signature is valid. Processing message... 09:55:19.216 -> [SinricPro:extractTimestamp(): Got Timestamp 1617177319 09:55:20.178 -> [SinricPro:Websocket]: receiving data 09:55:20.178 -> [SinricPro.handleReceiveQueue()]: 1 message(s) in receiveQueue 09:55:20.178 -> [SinricPro.handleReceiveQueue()]: Signature is valid. Processing message... 09:55:20.178 -> [SinricPro:extractTimestamp(): Got Timestamp 1617177320 09:55:20.216 -> [SinricPro.handleRequest()]: handling request 09:55:20.216 -> { 09:55:20.216 -> "header": { 09:55:20.216 -> "payloadVersion": 2, 09:55:20.216 -> "signatureVersion": 1 09:55:20.216 -> }, 09:55:20.216 -> "payload": { 09:55:20.216 -> "action": "setPowerState", 09:55:20.216 -> "clientId": "portal", 09:55:20.216 -> "createdAt": 1617177320, 09:55:20.216 -> "deviceId": "60639e1541a7a97d3ef50c76", 09:55:20.216 -> "replyToken": "f2b30f5e-5d37-4873-af29-e32c92cd2234", 09:55:20.216 -> "type": "request", 09:55:20.216 -> "value": { 09:55:20.216 -> "state": "Off" 09:55:20.216 -> } 09:55:20.216 -> }, 09:55:20.216 -> "signature": { 09:55:20.216 -> "HMAC": "2PnUI18Ccl+mb83IuwRvOqxx3kKmFlsW3X3i6lnbLDc=" 09:55:20.254 -> } 09:55:20.254 -> }Temperaturesensor turned off (via SinricPro) 09:55:20.254 -> [SinricPro:handleSendQueue()]: 1 message(s) in sendQueue 09:55:20.254 -> [SinricPro:handleSendQueue()]: Sending message... 09:55:20.254 -> { 09:55:20.254 -> "header": { 09:55:20.254 -> "payloadVersion": 2, 09:55:20.254 -> "signatureVersion": 1 09:55:20.254 -> }, 09:55:20.254 -> "payload": { 09:55:20.254 -> "action": "setPowerState", 09:55:20.254 -> "clientId": "portal", 09:55:20.254 -> "createdAt": 1617177320, 09:55:20.254 -> "deviceId": "60639e1541a7a97d3ef50c76", 09:55:20.290 -> "message": "OK", 09:55:20.290 -> "replyToken": "f2b30f5e-5d37-4873-af29-e32c92cd2234", 09:55:20.290 -> "success": true, 09:55:20.290 -> "type": "response", 09:55:20.290 -> "value": { 09:55:20.290 -> "state": "Off" 09:55:20.290 -> } 09:55:20.290 -> }, 09:55:20.290 -> "signature": { 09:55:20.290 -> "HMAC": "r7yMGoIcNdms7CMGF+e2DQMJEQm2rxufqvZ8TK+U76U=" 09:55:20.290 -> } 09:55:20.290 -> } 09:55:20.290 -> [SinricPro:handleSendQueue]: Sending to websocket 09:55:20.434 -> [SinricPro:handleSendQueue()]: message sent.

STATIC IP VERSION

09:53:45.540 -> ;l�d⸮⸮|�⸮$⸮| ⸮ $⸮c|ǃ⸮⸮{⸮c⸮c⸮⸮o'⸮lno⸮⸮⸮ bx⸮l;l{lx⸮n⸮ ⸮ d ⸮⸮  cg⸮|⸮$⸮⸮#⸮⸮no⸮�$⸮⸮l ⸮gn dgs⸮ۓn #$;⸮⸮g cl ⸮c ⸮⸮$r⸮`⸮⸮n⸮ 09:53:45.645 -> 09:53:45.645 -> 09:53:45.645 -> [Wifi]: Connecting.connected! 09:53:45.883 -> [WiFi]: IP-Address is 10.0.1.193 09:53:45.883 -> [SinricPro]: Device "60639e1541a7a97d3ef50c76" does not exist. Creating new device 09:53:45.883 -> [SinricPro:add()]: Adding device with id "60639e1541a7a97d3ef50c76". 09:53:45.883 -> [SinricPro:Websocket]: Connecting to WebSocket Server using SSL (ws.sinric.pro) 09:53:45.917 -> [SinricPro:Websocket]: headers: 09:53:45.917 -> appkey:663022a1-7039-4478-8756-40736d733e40 09:53:45.917 -> deviceids:60639e1541a7a97d3ef50c76 09:53:45.917 -> restoredevicestates:true 09:53:45.917 -> ip:10.0.1.193 09:53:45.917 -> mac:D8:BF:C0:14:B6:46 09:53:45.917 -> platform:ESP8266 09:53:45.917 -> version:2.9.2 09:53:45.917 -> [SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server. 09:53:45.917 -> Something went wrong...could not send Event to server!

`

sivar2311 commented 3 years ago

This means, the ESP cannot connect to SinricPro and is not connected. Looks like your static IP setup is wrong. You should avoid using IP addresses from DHCP pool!

sivar2311 commented 3 years ago

On a 10.x.x.x. network, the network mask usually is 255.0.0.0 Check your router settings

rcassinis commented 3 years ago

I am afraid your guesses are both wrong. I have checked a static IP outside the router DHCP pool, and the result is the same. Alèthere is nothing wrong in defining a subnet mask 255.255.255.0 for a 10.0.1.x network. Actually, my network works perfectly since several years, and the same instructions I used for thisprogram work well for other nodeMCU programs I have written without using Alexa and SinricPro. Obviously, the problem must be elsewhere...

sivar2311 commented 3 years ago

Alèthere is nothing wrong in defining a subnet mask 255.255.255.0 for a 10.0.1.x network.

I didn't say that it is wrong... but standard on 10.x network is 255.x mask and i don't know your router configuration.

If i have time, i will do a test sketch with static IP on my side...

sivar2311 commented 3 years ago

Ah wait The bug is in your code ;)

You need to define at least one primaryDNS.... otherwise the sketch is not able to resolve SinricPro's IP address ;)

sivar2311 commented 3 years ago

I did a test on my side... works perfect!

rcassinis commented 3 years ago

Yeah, it works. My fault for having taken instructions from a program that uses a modified library that automatically provides name server address - Thank you so much for finding the culprit for me!

So, for now I will stick to the static IP - direct communication solution. If I find anything better I'll let you know. Best,

Riccardo

sivar2311 commented 3 years ago

You don't need static IP for direct communication. Give your ESP a hostname: ESP8266: WiFi.hostname("mycooldevice"); ESP32: WiFi.setHostname("mycooldevice");

C:\>ping mycooldevice

Ping wird ausgeführt für mycooldevice.fritz.box [192.168.2.37] mit 32 Bytes Daten:
Antwort von 192.168.2.37: Bytes=32 Zeit=86ms TTL=255
Antwort von 192.168.2.37: Bytes=32 Zeit=98ms TTL=255
Antwort von 192.168.2.37: Bytes=32 Zeit=15ms TTL=255
Antwort von 192.168.2.37: Bytes=32 Zeit=35ms TTL=255

Ping-Statistik für 192.168.2.37:
    Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0
    (0% Verlust),
Ca. Zeitangaben in Millisek.:
    Minimum = 15ms, Maximum = 98ms, Mittelwert = 58ms
rcassinis commented 3 years ago

I had not considered modifying the hostname because it is well known that it has countless problems on the ESP. But the static solution will work for me, I think. By the way, I have another question for you as a maintainer of the library: my software also uses SinricProBlinds.h, but, if i try to compile the blinds example, I get errors from version 2.9.x, and had to resort to V 2.8.0. Do you have any idea of what can be wrong?

In the meantime, I thank you for your kind assistance

Riccardo

On 31 Mar 2021, at 12:34, Boris Jäger @.***> wrote:

You don't need static IP for direct communication. Give your ESP a hostname: ESP8266: WiFi.hostname("mycooldevice"); ESP32: WiFi.setHostname("mycooldevice");

C:>ping mycooldevice

Ping wird ausgeführt für mycooldevice.fritz.box [192.168.2.37] mit 32 Bytes Daten: Antwort von 192.168.2.37: Bytes=32 Zeit=86ms TTL=255 Antwort von 192.168.2.37: Bytes=32 Zeit=98ms TTL=255 Antwort von 192.168.2.37: Bytes=32 Zeit=15ms TTL=255 Antwort von 192.168.2.37: Bytes=32 Zeit=35ms TTL=255

Ping-Statistik für 192.168.2.37: Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0 (0% Verlust), Ca. Zeitangaben in Millisek.: Minimum = 15ms, Maximum = 98ms, Mittelwert = 58ms — You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/170#issuecomment-810962074, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATO4HMCRKD7DA5KI37DSMJDTGL3EZANCNFSM42CQ2CAQ.


Prof. Ing. Riccardo Cassinis Via Ampère 15 - 20131 Milano

Tel. (+39) 023 652 4023 Cell. (+39) 328 075 0952 Fax (+39) 029 998 4872 URL: http://www.cassinis.it http://www.cassinis.it/


sivar2311 commented 3 years ago

I had not considered modifying the hostname because it is well known that it has countless problems on the ESP

Which ones exactly? I don't know any... I even use the hostname to upload my sketches via OTA! -> I never ran into problems.

I get errors from version 2.9.x

Damn... where is my crystall ball ;) (means: "Which errors do you get?") Make sure to use

sivar2311 commented 3 years ago

Okay, i think i found the bug about Blinds example.... I had forgotten to change the example code.

I will update the example and let you know when it's done.

rcassinis commented 3 years ago

I apologise for having been obscure. Actually, I was just in a terrible hurry.

the errors I get compiling the blinds example are

'class SinricProBlinds' has no member named 'onSetPosition'

and

'class SinricProBlinds' has no member named 'onAdjustPosition'

Riccardo

On 31 Mar 2021, at 18:33, Boris Jäger @.***> wrote:

Okay, i think i found the bug about Blinds example.... I had forgotten to change the example code.

I will update the example and let you know when it's done.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/170#issuecomment-811219940, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATO4HME2BSLFLI25GOHTZULTGNFF3ANCNFSM42CQ2CAQ.


Prof. Ing. Riccardo Cassinis Via Ampère 15 - 20131 Milano

Tel. (+39) 023 652 4023 Cell. (+39) 328 075 0952 Fax (+39) 029 998 4872 URL: http://www.cassinis.it http://www.cassinis.it/


sivar2311 commented 3 years ago

Yes, the code base has changed a bit and I haven't adjusted the examples yet. I will do this in the next few days.

sivar2311 commented 3 years ago

Blinds example have been fixed

rcassinis commented 3 years ago

Thank you very much Boris: works perfectly for me.

Re. the hostname issue, just try to google “hostname not working” and you’ll see. I myself gave it a try on the MCU8266 last night, and it didn’t work. I wonder what trick you are using…

Once again, thank you very much for your cooperation!

Riccardo

On 1 Apr 2021, at 06:18, Boris Jäger @.***> wrote:

Blinds example have been fixed

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/170#issuecomment-811626354, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATO4HMDXTFR5YGLY6UUNUC3TGPX2LANCNFSM42CQ2CAQ.


Prof. Ing. Riccardo Cassinis Via Ampère 15 - 20131 Milano

Tel. (+39) 023 652 4023 Cell. (+39) 328 075 0952 Fax (+39) 029 998 4872 URL: http://www.cassinis.it http://www.cassinis.it/


sivar2311 commented 3 years ago

I wonder what trick you are using…

Are you on a windows machine, like me?

rcassinis commented 3 years ago

No, i tried with a Mac. Later I’ll try with Windows and let you know

Riccardo

On 1 Apr 2021, at 07:36, Boris Jäger @.***> wrote:

I wonder what trick you are using…

Are you on a windows machine, like me?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/170#issuecomment-811654528, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATO4HMGGKLLHQ572FNE3FUTTGQA5VANCNFSM42CQ2CAQ.


Prof. Ing. Riccardo Cassinis Via Ampère 15 - 20131 Milano

Tel. (+39) 023 652 4023 Cell. (+39) 328 075 0952 Fax (+39) 029 998 4872 URL: http://www.cassinis.it http://www.cassinis.it/


rcassinis commented 3 years ago

You guessed it right. It works from Windows, but from Linux and mac OS it doesn’t. Now I’ll have to find out if it works from another ESP8266.

Riccardo


Prof. Ing. Riccardo Cassinis Via Ampère 15 - 20131 Milano

Tel. (+39) 023 652 4023 Cell. (+39) 328 075 0952 Fax (+39) 029 998 4872 URL: http://www.cassinis.it http://www.cassinis.it/


sivar2311 commented 3 years ago

setHostname("mycooldevice") / hostname("mycooldevice) will send the hostname "mycooldevice" to your router (if DHCP is used and your router accept the name).

Here is a screenshot from my router "FritzBox 7590": image

Otherwise you can setup and use mDNS which will allow you to use hostnames like "mycooldevice.local" on your network.