API and Python requests

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
jayrock
New user
Posts: 3
Joined: 30 Mar 2018, 15:23

API and Python requests

#1 Post by jayrock » 30 Mar 2018, 15:36

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.

linuxnico
New user
Posts: 1
Joined: 03 Apr 2018, 15:07

Re: API and Python requests

#2 Post by linuxnico » 03 Apr 2018, 15:11

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

jayrock
New user
Posts: 3
Joined: 30 Mar 2018, 15:23

Re: API and Python requests

#3 Post by jayrock » 03 Apr 2018, 19:08

Thanks, will check it but it will take until the weekend.

Cheers,
jayrock

jayrock
New user
Posts: 3
Joined: 30 Mar 2018, 15:23

Re: API and Python requests

#4 Post by jayrock » 06 Apr 2018, 20:50

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests