0

I have this URL http://mypaint.epizy.com/relay-control.php for changing the value of 0 to 1 or reverse in http://mypaint.epizy.com/relay-status.txt

from urllib.request import urlopen
import wiringpi
import time

url = "http://mypaint.epizy.com/relay-status.txt"

wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(17,1)

while 1:
    relaystatus = urlopen(url).read()
    print(relaystatus)
    
    if relaystatus == "1":
        wiringpi.digitalWrite(17,1)
    elif relaystatus == "0":
        wiringpi.digitalWrite(17,0)
        
    time.sleep(2)

when I print out I get many HTML codes as you can see from the code I only need either 0 or 1 for me to control my relay

AfroDemoz
  • 39
  • 1
  • 7

1 Answers1

0

1. Complicated and slow way


It seems like to open http://mypaint.epizy.com/relay-status.txt you need javascript to be enabled. Selenium is one way to solve this Problem:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from bs4 import BeautifulSoup

service = Service(executable_path=ChromeDriverManager().install())
browser = webdriver.Chrome(service=service)
url = "http://mypaint.epizy.com/relay-status.txt"
browser.get(url)
html_source = browser.page_source
browser.quit()
soup = BeautifulSoup(html_source, features="html.parser")

status = int(soup.html.body.pre.text)
if status == 1:
    print("Relay is ON")
else:
    print("Relay is OFF")

This will output if the relay is on or not. Since you are on a raspberry pi which does not have a display. You have to use a virtual display look here

from pyvirtualdisplay import Display

display = Display(visible=0, size=(1600, 1200))
display.start()
...

It may not work using ChromeDriverManager and/or Service. You may have to install chromium with

sudo apt-get install chromium-chromedriver

and remove unused imports:

from selenium import webdriver
from bs4 import BeautifulSoup
from pyvirtualdisplay import Display

display = Display(visible=0, size=(1600, 1200))
display.start()
browser = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver') # path could be different
url = "http://mypaint.epizy.com/relay-status.txt"
browser.get(url)
html_source = browser.page_source
browser.quit()
soup = BeautifulSoup(html_source, features="html.parser")

status = int(soup.html.body.pre.text)
if status == 1:
    print("Relay is ON")
else:
    print("Relay is OFF")

Using wiringpi and the infinite loop:

import wiringpi
import time
from selenium import webdriver
from bs4 import BeautifulSoup
from pyvirtualdisplay import Display

display = Display(visible=0, size=(1600, 1200))
display.start()
browser = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver') # path could be different
url = "http://mypaint.epizy.com/relay-status.txt"
while 1:
    browser.get(url)
    html_source = browser.page_source
    soup = BeautifulSoup(html_source, features="html.parser")
    status = int(soup.html.body.pre.text)
    if status == 1:
        wiringpi.digitalWrite(17,1)
    else:
        wiringpi.digitalWrite(17,0)
    time.sleep(2)

browser.quit()

2. Easy and Fast way


There is a better, faster and quote "hacky" way to solve this:

import requests

url = 'http://mypaint.epizy.com/relay-status.txt'
response = requests.get(url, cookies={'__test': '308a35fb3e997dc37c3018ac966140a8'})
print(response.content.decode("utf-8"))

To find out the value you can just look at the __test cookie in the request. Or you do it the difficult and not very intelligent way like me...:


The site wants to execute the following javascript code:

function toNumbers(d) {
    var e = [];
    d.replace(/(..)/g, function(d) {
        e.push(parseInt(d, 16))
    });
    return e
}

function toHex() {
    for (var d = [], d = 1 == arguments.length && arguments[0].constructor == Array ? arguments[0] : arguments, e = "", f = 0; f < d.length; f++) e += (16 > d[f] ? "0" : "") + d[f].toString(16);
    return e.toLowerCase()
}
var a = toNumbers("f655ba9d09a112d4968c63579db590b4"),
    b = toNumbers("98344c2eee86c3994890592585b49f80"),
    c = toNumbers("e80f812fb08612bba53dc95185808773");
document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
location.href = "http://mypaint.epizy.com/relay-status.txt?i=1";

if you try to execute this in the console it says that slowAES is not defined. To fix this I copied this code and pasted it in the console. Instead of setting a cookie I printed the __test value to the console with

toHex(slowAES.decrypt(c, 2, a, b))

This gave me the value

'308a35fb3e997dc37c3018ac966140a8'

I don't know if this value will change depending on what time you check the page.

Full Code:

import time
import wiringpi
import requests

url = 'http://mypaint.epizy.com/relay-status.txt'
while 1:
    response = requests.get(url, cookies={'__test': '308a35fb3e997dc37c3018ac966140a8'})
    status = int(response.text)
    if status == 1:
        wiringpi.digitalWrite(17,1)
    else:
        wiringpi.digitalWrite(17,0)
    time.sleep(2)
noah1400
  • 1,282
  • 1
  • 4
  • 15