Timers / Alarms



  • My timers runs once or twice then just stops.
    The program is running fine.
    It is a webserver and every x min it does a HTTP get to my server .
    howver it just stops sending and I dont know how to debug it

    Below is the timer

    class Clock:
    
        def __init__(self):
            self.seconds = 0
            self.__alarm = Timer.Alarm(self._seconds_handler, 1 , periodic=True)
    
        def _seconds_handler(self, alarm):
            self.seconds += 1
            #print("%02d seconds have passed" % self.seconds)
            if self.seconds == 30:
                alarm.callback(dd("runnung timer ")) # stop counting after 10 seconds
    
    
    
    
    def dd(msg):
        print(msg)
        senddattoweb("go")
        #abc = Clock()
    


  • @roy-Taylor That's good. Just a hint for forum postings:
    If you enclose code in lines consisting of three backticks ```, it will be displayed properly.



  • i think i got it working

    will let it run all day and see
    i took your advice swith it off then enable it again

    class Clock:
    
        def __init__(self):
            self.seconds = 0
            self.__alarm = Timer.Alarm(self._seconds_handler, 1 , periodic=True)
    
        def _seconds_handler(self, alarm):
            self.seconds += 1
            #print("%02d seconds have passed" % self.seconds)
            if self.seconds == 30:
                alarm.callback(dd()) # stop counting after 10 seconds
                alarm.cancel()
    
    abc = Clock()
    
    def dd():
        print("dd running ")
        pin_handler5("dd")
        abc = Clock()
    


  • I really appreciate the help I'm a PHP programmer so this is all to me .
    We are building a 200 VDC lithium ion battery and this will send battery data to out server and collect data from CAN BUS

    here is my main.py
    i still need a second interrupt to get the CANBUS data

    import usocket as socket
    from network import WLAN
    import machine
    from machine import Pin
    import os
    import pycom
    from utils import is_wifi_connected
    import functions as fn

    red = 0xFF5733
    green = 0x93D51B
    blue = 0x106FE9
    yellow = 0xFBF963
    orange = 0xE59F1A
    pink = 0xE911E1

    pycom.heartbeat(False)
    print("heara 1")

    from machine import Timer

    class Clock:

    def __init__(self):
        self.seconds = 0
        self.__alarm = Timer.Alarm(self._seconds_handler, 1 , periodic=True)
    
    def _seconds_handler(self, alarm):
        self.seconds += 1
        #print("%02d seconds have passed" % self.seconds)
        if self.seconds == 30:
            alarm.callback(dd) # stop counting after 10 seconds
            alarm.cancel()
    

    abc = Clock()

    def dd(msg):
    print(msg)
    pin_handler5("dd")
    abc = Clock()
    return True

    def cc(msg):
    print(msg)

    is_wifi_connected()

    html = """<!DOCTYPE html>
    <html>
    <head> <title>ESP8266 Pins</title> </head>
    <body> <h1>ESP8266 Pins</h1>
    <table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
    </body>
    </html>
    """

    addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
    s = socket.socket()
    s.bind(addr)
    s.listen(5)

    print('listening on', addr)

    #used to test if data is sent with interupts - WORKS OK
    def pin_handler5(arg):
    pycom.rgbled(green)
    global button_event_count
    button_event_count += 1
    #log("Button event at %s" % (arg.id()))
    url1 =ureq2()
    print()
    #print (url1)
    print()
    http_get(url1)
    pycom.rgbled(red)

    p_in = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP)
    p_in.callback(Pin.IRQ_RISING, pin_handler5)
    button_event_count = 0

    #'http://solrai-log.mobi/hyp/data4.php?jsdata= { "timestamp" : "2020-10-#10 12:40", SOC" : 55 , "SOH" : 99 ,"PV" : 100 ,"PI" : 100 ,"Status" : "Charging" ,"HC" : 100 , "LC" : 100 ,"AC" : 100 , "Cells" : [123,345,678,123,345,678,123,345,678,123,345,678,123,345,678,123,345,678,123,345,678,123,345,678,123,345,678] , "cell_temps" : [234,235,678,555], "pcb_temps" : [234,235,678,555] }&Serial=3c71bf878418&cpu_TEMP=1002020-10-10 18:00'

    this gets the data ready to send . Still to do - fetch the data from the canbus

    canbus program works on its own will have to do another timer to fect data from the canbus

    def ureq2():
    dates = "&LCTIME=" + "2020-10-10 12:40"
    dta = ' { "timestamp" : "2020-10-10 12:40", SOC" : 55 , "SOH" : 99 ,"PV" : 100 ,"PI" : 100 ,"Status" : "Charging" ,"HC" : 100 , "LC" : 100 ,"AC" : 100 , "Cells" : [123,345,678,123,345,678,123,345,678,123,345,678,123,345,678,123,345,678,123,345,678,123,345,678,123,345,678] , "cell_temps" : [234,235,678,555], "pcb_temps" : [234,235,678,555] }'

    #replace spaces with %20
    news = fn.replace(dta)
    dates  = fn.replace("2020-10-10 18:00")
    serno = "&Serial="+str(Serial)+"&cpu_TEMP="+str(100)
    serno = fn.replace(serno)
    print (serno)
    #return b'dta'
    print()
    print ("data4.php?jsdata="+news+serno+dates)
    #LOGDATA(news+serno+dates)
    print()
    url = 'http://solrai-log.mobi/hyp/data4.php?jsdata='+news+serno+dates
    headers = {'X-AIO-Key': 'xxxxxxxxxxxxxxxxxxx',  'Content-Type': 'application/json'}
    dat = '#"value": "50"#'
    #r = requests.post(url, data=dta, json=None, headers=headers)
    #print (r.text)
    return (url)
    

    print()
    print( ureq2())
    print()

    this send the data to the server and works well

    def http_get(url):

    _, _, host, path = url.split('/', 3)
    addr = socket.getaddrinfo(host, 80)[0][-1]
    s = socket.socket()
    s.connect(addr)
    s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
    while True:
        data = s.recv(100)
        if data:
            print(str(data, 'utf8'), end='')
        else:
            break
    s.close()
    

    http = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection:close \r\n\r\n" #HTTP response

    def listen(clientsocket):

    # Receive maxium of 12 bytes from the client
    
    
    r = clientsocket.recv(4096)
    
    
    # If recv() returns with 0 the other end closed the connection
    if len(r) == 0:
        clientsocket.close()
        return
    else:
        # Do something wth the received data...
        print("Received: {}".format(str(r))) #uncomment this line to view the HTTP request
        pycom.rgbled(orange)
    
    http = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection:close \r\n\r\n" #HTTP response
    
    if "GET / " in str(r):
        #this is a get response for the page
        # Sends back some data
        clientsocket.send(http + "<html><body><h1> You are connection "+ str(99) + "</h1><br> Your browser will send multiple requests <br> <a href='/hello'> hello!</a><br><a href='/color'>change led color!</a></body></html>")
    
    elif "GET /hello "in str(r):
    
        clientsocket.send(http + "<html><body><h1> Hello to you too! </h1><br> <a href='/'> go back </a></body></html>")
    
    elif "GET /color" in str(r):
        pycom.rgbled(0xFFFFFF)
        clientsocket.send(http + "<html><body><h1> You are connection "+ str(99) + "</h1><br> Your browser will send multiple requests <br> <a href='/hello'> hello!</a><br><a href='/color'>change led color!</a></body></html>")
    
    elif "GET /favicon.ico" in str(r):
    

    pycom.rgbled(0xFFFFFF)

        clientsocket.send(http )
    
    # Close the socket and terminate the thread
    
    clientsocket.close()
    pycom.rgbled(blue) # socket closed
    

    print("here 1 ")
    c = 0
    x = True
    while True:

    (clientsocke, address) = s.accept()
    c = c+ 1
    print ("counter = " , c)
    # Start a new thread to handle the client
    listen(clientsocke)
    pycom.rgbled(green)


  • @roy-Taylor You can do that, but it will replace the callback handler. Then, after a second, this other callback handler will be called, withe the same problem. What you could do is temporarily stopping callbacks, run the function and then activating the alarm again.
    It may as well be that a callback is not called again until the last execution of it finished. I could look into the code, but you may simply try out.



  • @robert-hh said in Timers / Alarms:

    dd

    thanks for replying

    it can take longer than 1 second as it is a http get sending data to webserver .
    like this below ?
    if self.seconds == 30:
    alarm.callback(dd)



  • @roy-Taylor In _seconds_handler() you replace the callback of the alarm by calling the function dd(). You wrote dd(..) instead of simply dd. Since the result of calling the function dd() is None, the callback is set to None and therefore is disabled.
    If you want to get the function dd being called every 30 seconds, you may simply call it. However, it the execution takes longer than a second, you may get other problems.


Log in to reply
 

Pycom on Twitter