Page 1 of 1

API and Python requests

Posted: 30 Mar 2018, 15:36
by jayrock
I'm struggling to remotely deactivate sleep mode. My idea is to let a Phython script wait for the ESP to wake and then issue "NoSleep" via a rule.

The rule I have created is here:

Code: Select all

 on disableSleep do
   NoSleep //Sleep disabled
When I hardware-deactivate sleep and issue http://192.168.2.203/control?cmd=event,disableSleep, "OK" is returned and "Sleep enabled" is deactivated in the Config page. So far so good. However a scripted version behaves oddly.

Code: Select all

#
# This is a Python3 script
#

import requests

ESP32_ip = "192.168.2.203"

while True:
  r = requests.get("http://" + ESP32_ip + "/control?cmd=event,disableSleep")
  if (r.status_code != 504):
    print("Code: " + str(r.status_code))
    print("Text: " + r.text)
    if(r.text == "OK"):
      break
The script output looks good:

Code: Select all

Code: 200
Text: OK
But as a matter of fact "Sleep enabled" is not deactivated. When I log back in this is clearly shown in the GUI, and the unit indeed goes to sleep when I connect D0 back to RST.

Can someone explain please?

Thank you.

Re: API and Python requests

Posted: 03 Apr 2018, 15:11
by linuxnico
try to use params of requests.get function

payload = {'cmd': 'event,disableSleep'}
r=requests.get('http://192.168.2.203/control', params=payload)

like resquest module exemple

http://docs.python-requests.org/en/mast ... rs-in-urls

Re: API and Python requests

Posted: 03 Apr 2018, 19:08
by jayrock
Thanks, will check it but it will take until the weekend.

Cheers,
jayrock

Re: API and Python requests

Posted: 06 Apr 2018, 20:50
by jayrock
I checked your suggestion, linuxnico. It does uncheck "Sleep enabled", so thanks for you help here.

However this seemingly only works when sleep is disabled on the HW side (ie D0 -RST disconnected). More precsise, I never get an HTTP response even during the few seconds when the units wakes up.

I played with (retained) MQTT messages and here the behaviour is the same. The rule is only fired when D0 and RST are disconnected. I looks like the MQTT message is never fetched when the unit wakes up.

Is this by design? Ie., when being awake, is it intended that neither HTTP requests nor MQTT incoming messages are process by ESP Easy?

Cheers,
jayrock