Loading ...
Home » Spaces » Other Products, Topics, & Discussions » discussion » General » Read value from Arduino web server

Read value from Arduino web server

Discussion in Other Products, Topics, & Discussions started by mozo , 8 days ago
Login to follow, share, and participate in this space.
Not a member?Join now
  • mozobata
    mozo
    New Member New Member
    mozo 8 days ago

    I am trying to write Python code to read a simple temperature value from the local Arduino server.

    Here is the datesheet of ds18b20

    This is my source page:

    <font color=black size=7>Senzorji DS18B20<font color=black size=7><br />
    <font color=black size=7>Kurnica: [26.00] &#176;
    C
    <font color=green size=7><font color=black size=7><br />
    <font color=black size=7>Zunaj: [33.13] &#176;
    C
    <font color=green size=7><font color=black size=7><br />
    <font color=black size=7>Garaza: [27.62] &#176;
    C
    <font color=black size=7><br />
    <br />
    Senzor DHT11 - Garaza<font color=black size=7><br />
    <font color=black size=7>Temperatura v garazi: [24.00] &#176;
    C
    <font color=green size=7><font color=black size=7><br />
    <font color=black size=7>Vlaga v garazi: [17.00] %
    <br />
    <br />
    Senzor DHT11 - Stara kurilnica<font color=black size=7><br />
    <font color=black size=7>Temperatura v stari kurilnici: [23.00] &#176;
    C
    <font color=green size=7><font color=black size=7><br />
    <font color=black size=7>Vlaga v stari kurilnici: [14.00] %
    <font color=green size=7>

    I want to read the value in square brackets with Beautiful soup but no luck. I cannot find my case. All examples are more complex; mine is simple but I do not know.

    I'm using Raspberry Pi.

    With PHP, I wrote simple code. It changed for me and it worked. This is my PHP code:

    function get_string_between($string, $start, $end){
            $string = " ".$string;
            $ini = strpos($string,$start);
            if ($ini == 0) return "";
            $ini += strlen($start);   
            $len = strpos($string,$end,$ini) - $ini;
            return substr($string,$ini,$len);
    }
    
    $arduino91 = file_get_contents("http://192.168.5.91/");
    $radiator = get_string_between($arduino91, "(povratek): [", "]");
    $garaza = get_string_between($arduino91, "Garaza: [", "]");
    $garazadht = get_string_between($arduino91, "Temperatura v garazi: [", "]");
    $garazavl = get_string_between($arduino91, "Vlaga v garazi: [", "]");
    $kurnica = get_string_between($arduino91, "Kurnica: [", "]");
    
    echo "Temperatura v kurilnici: $kuriltemp"."<br>";
    echo "Pec: $pec1"."<br>";

    How would I write such code in Python?

  • voidstar
    voidstar
    Expert Expert
    voidstar 7 days ago (in response to mozo)

    This is really a forum for APC products. Perhaps stackoverflow.com would be better?

    You could use regular expressions:

    import re
    str = "blah\nblahGaraza: [27.62] blah\nblah"
    matcher = re.compile("Garaza:\s*\[([^\]]*)]")
    match = matcher.search(str)
    if match:
    number = float(match.group(1))
    else:
    number = None

    Or you could do a direct translation of your PHP function. Instead of strpos, you'd use Python's str.find() and instead of substr, you'd use python's list slicing str[start:end]. To look at the list of functions a string supports, I did:

    dir("")

    and to see how to use the find function:

    help("".find)

  • Page 1 of 1 (2 items)
Choose your language:  
powered by Communifire
Version 5.2.6402.40375