BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Rasperry Pi Python blynklib - use different auth token for different script

I have two Blynk script created using Python and blynklib version 0.2.6 on Raspberry Pi 2B.
First script will control some RF Plug, second script will report temperature and humidity using a DHT22 sensor.

Every python script will connect to my local Blynk Server on same subnet, using different Token.
Eech script will init the Blynk server using same function. A Init script sample is the following:


    def initBlynk(self):

        # blynk main object
        blynk = Blynk(
            token=self.cfg.get("blynk", "token"),
            server=self.cfg.get("blynk", "server"),
            port=self.cfg.get("blynk", "port"))

        # Blynk timer
        timer = BlynkTimer()

        """
        Assign switch write and read events:
        - write events triggered  to set value
        - read event sent sensor value
        """

        def readEventHandler(pin):
            self.log.debug("READ EVENT FOR VPIN {}".format(pin))
            self.sendSensorValue(pin)

        def writeEventHandler(pin, value):
            # we does not set anythings
            self.log.debug("WRITE EVENT VPIN {} VALUE {}".format(pin, value))
            self.sendSensorValue(pin)

        # decorating
        for vpin in self.cfg["sensor"]["vpins"].split(','):
            self.log.debug("BLYNK Decorating READ/WRITE FOR VPIN {}".format(vpin))
            # Read Event
            Blynk.handle_event(blynk, "read v{}".format(vpin))(readEventHandler)
            # Write Event
            Blynk.handle_event(blynk, "write v{}".format(vpin))(writeEventHandler)
            # Timer Event
            BlynkTimer.register(
                timer, pin=vpin, interval=BLYNK_UPDATE_INTERVAL, run_once=False)(
                    readEventHandler)

        #On connect Event
        Blynk.handle_event(blynk, "connect")(self.onConnectHandler)
        #On Disconnect Event
        Blynk.handle_event(blynk, "disconnect")(self.onDisconnectHandler)


        # Globally saving the handlers
        self._handlers.append(writeEventHandler)
        self._handlers.append(readEventHandler)

        # Saving blynk and timer object
        self._blynk = blynk
        self._timer = timer

        return self.isConneted()

The first script will control some RF Plug, the second script will read DHT22 Sensor (blynk_dht.py).

The main loop of blynk_dht.py is the following:


    def run(self):
        self.sensor = self.getSensor()
        self.db = self.getInflux()
        self._isConnected = self.initBlynk()

    def run(self):
        self.sensor = self.getSensor()
        self.db = self.getInflux()
        self._isConnected = self.initBlynk()

        while True:
            if  self._isConnected:
                self._blynk.run()
                self._timer.run()
            else:
                self.log.warning("BLYNK OR DATABASE DISCONNECTED")
                self.updateSensorValue()
                self.db = self.getInflux()
                self._blynk.disconnect()
                self._isConnected = self._blynk.connect(timeout=CONN_TIMEOUT)

After a while the Blynk Server reports that DHT Sensor is offiline, but READ funcition in Timer will continue to update Blynk Server. No Disconnection handler is called.

Someone can help me?

It has been ages since I used Blynk’s Python libraries… but I seem to recall you need to set each timer’s interval. I see nothing like this in your code…

timer.set_interval(10, blink)

def blink();
     print("Blinking here, every 10 seconds")

EDIT:

Perhaps this is it? Hard to follow your code… so perhaps I will leave this one alone, sorry.

Hi, thank you for your feedback. I’ll try to explain better.

I wrote two different python script on Raspberry Pi. The two script are very different, first one control some RF outlets using an RF transmitter, second one read temp and humidity from a DHT22 sensor. Each scripts will connect to internal Blynk server using different tokens. In this way I can identify the Raspberry PI as two different sensors on blynk server. The DHT script will also write temperature and humidity on Influx DB (but this not concerning this problem).

Real problem isn’t timer, rater the fact that the script that read DHT sensor, after some time (a few mitues or more), result as “disconneted” on Blynk server.

When disconnection will happen, the event handler ondisconnect in the script is not called (so none can inform the script that disconnection happened); even more strange is the fact that the Blynk server is still updated with the temperature and humidity sent by readEventHandler timely called by BlynkTimer