Page 1 of 1

my script gets blocked when there's a watchdog

Posted: Sat Aug 10, 2019 9:02 am
by palouf34
I have a recurring problem with my script despite the watchdog found on one of the topics on this forum.

I'm trying to do the following operations:

this is my script:

Code: Select all

#station meteo
#import standard library
import machine
import time
import urequests

# import special library
import bme280
import veml7700

#start configuration machine

#machine.freq(160000000) # set the CPU frequency to 160 MHz

# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)

#end of configurartion machine

# address  of external captor
bme280_addr = 0x76
vmle7700_addr = 0x10

# variable pour jeedom

apiKey = "ezrz"
idPa = "1551"
idTemp = "1552"
idHumidity = "1553"
idLux = "1562"
host = ""
nbrReset = "1713"
port = 80
baseurl = "http://"+host+"/core/api/jeeApi.php?apikey=" 
baseurl += apiKey
baseurl += "&type=virtual&id="

#variable  des mesure
t_old = 0;
lux_old = 0.0;
h_old = 0;
pa_old = 0.0;

#mise en place du watchdog
# issus de
# second exemple de watchdod
# issus de
class WDT:
    def __init__(self, id=0, timeout=600):
        self._timeout = timeout / 10
        self._counter = 0
        self._timer = machine.Timer(id)

    def _wdt(self, t):
        self._counter += self._timeout
        if self._counter >= self._timeout * 10:

    def feed(self):
        self._counter = 0

    def init(self, timeout=None):
        timeout = timeout or self._timeout
        self._timeout = timeout
        self._timer.init(period=int(self._timeout * 1000),
                         mode=machine.Timer.PERIODIC, callback=self._wdt)

    def deinit(self):  # will not stop coroutine

# fin du watch dog

## END Simple software WDT implementation
# activate i2c port
i2c = machine.I2C(scl=machine.Pin(5),sda=machine.Pin(4))

#create  capteur "object"
capteur2_lumiere = veml7700.VEML7700(vmle7700_addr,
capteur_bme = bme280.BME280(i2c=i2c)
wdt = WDT()

#start software
def main():
  #start scan i2c buss
  print('Scan i2c bus...')
  devices = i2c.scan()

  if len(devices) == 0:
    print("No i2c device !")
    print('i2c devices found:',len(devices))

    for device in devices:  
      print("Decimal address: ",device," | Hexa address: ",hex(device))
  #end scan i2c bus
  cptReset = 0
  print("debut calcul")
  #while boucle<5:
  while True:
    if machine.reset_cause() == machine.DEEPSLEEP_RESET:
        print("s'est réveillé d'un profond sommeil")
    while i<50:
      # programmation du capteur
      #lecture de la valeur du capteur   
      mesure_lux = capteur2_lumiere.read_lux()
      #print("valeur multiplicateur: ",capteur2_lumiere.confValues)
      #print("valeur gain : ", capteur2_lumiere.gain)
      #print("Valeur VLME7700 : ")
      #print("la mesure est  de ", mesure_lux, " lux")
      # affichage des infos BME280
      #print("température ,  pression, higronometrie :")
      temperature =  capteur_bme.values[0]
      temperature = temperature[:-1]
      pression = capteur_bme.values[1]
      pression = pression[:-3]
      humidite = capteur_bme.values[2]
      humidite= humidite[:-3]
      print("Fin boucle de prise de mesure, tour numero :", i)
    #print("Valeurs deniere mesure")
    #print("la mesure est  de luminosité est de  ", mesure_lux, " lux")
    print("J'envoie les données à jeedom")
    print("Tempreature envoyé",temperature)
    print("Pression envoyé",pression[:-3])
    print("Taux d'humidité envoyé",humidite[:-1])
    print("taux de lumiére envoyé",mesure_lux)
    # constitution base du get  pour envoie a jeedom
    #baseurl = "http://"+host+"/core/api/jeeApi.php?apikey=" 
    #baseurl += apiKey
    #baseurl += "&type=virtual&id=" 
    # constitution  url d'envoie
    url = baseurl + idTemp # id jeedom  objet
    url += "&value=" 
    url += temperature; # valeur récuperer  du capteur
    response = urequests.get(url)
    url = baseurl + idPa # id jeedom  objet
    url += "&value=" 
    url += pression[:-3]; # valeur récuperer  du capteur
    response = urequests.get(url)
    url = baseurl + idHumidity # id jeedom  objet
    url += "&value=" 
    url += humidite[:-1] # valeur récuperer  du capteur
    response = urequests.get(url)
    # lux
    url = baseurl + idLux # id jeedom  objet
    url += "&value=" 
    url += str(mesure_lux); # valeur récuperer  du capteur
    response = urequests.get(url)
    print("transmission faite")
    #print("je m'endors pour quelques minutes")
    #mise en place du deepsleep
    # set RTC.ALARM0 to fire after 10 seconds (waking the device)
    #print("nous en sommes a la boucle: ",boucle )
    wdt_timer = machine.Timer(-1)
    wdt_timer.init(period=275000, mode=machine.Timer.PERIODIC,
                callback=lambda t: wdt_callback())
if __name__ =="__main__":

relaunch my script like this

Code: Select all

Meteo import >>> Meteo import                                                                                                                                
>>> Meteo.main                                                                                                                                  
Traceback (most recent call last):                                                                                                              
  File "<stdin>", line 1, in <module>                                                                                                           
AttributeError:'module' object has no attribute' main                                                                                         
I reboot the esp, I do lose some ping but the scipt does not restart.

I have to physically restart the nodemcu.

I have however the rst pin connected with the D0 pin.

Do you have any idea where my problem comes from.

Re: my script gets blocked when there's a watchdog

Posted: Sun Aug 11, 2019 3:26 am
by manseekingknowledge
Are you asking about this error?

Code: Select all

AttributeError:'module' object has no attribute' main 
If so then you need to call main as a function. and make sure you use the correct import syntax. Instead of:

Code: Select all

Metro import

Code: Select all

import Metro

Re: my script gets blocked when there's a watchdog

Posted: Sun Aug 11, 2019 12:44 pm
by palouf34
sorry a see you response so later, I'm reboot my nodemcu before.

but I restet this for futur problem.