Hello!
I have a python script running continuously on a RPI continously. The RPI is connected to 2 different arduinos on different serial ports to control a LED sign with command codes. The RPI sends and receives small codes such as <1000> or <1001> though usb/serial to change the LED message embedded in the RAM of the arduino. The arduino only sends data when asked by the rpi, so there is no extraneous data coming over the com ports.
Everything is working fine. However, what is strange is that from time to time, the device goes offline in BLYNK, but the python script does not break and there is no errors. I don’t know what is causing this disconnection to blynk. I need to control my LED sign from the blynk app remotely, so this is really annoying. I need this to maintain connection. Any ideas??
Here is my python code running on my rpi that communicates with Blynk through wired ethernet and the arduinos though the serial ports…
import datetime
import serial
import BlynkLib
from BlynkTimer import BlynkTimer
BLYNK_AUTH_TOKEN = 'XXXXXXX'
#Initialize Serial ports
ser1 = serial.Serial('/dev/ttyACM0', 2400, timeout=0.1)
ser1.write(b"< >")
ser2 = serial.Serial('/dev/ttyACM1', 2400, timeout=0.1)
ser2.write(b"< >")
def serialmessagerec1():
while ser1.in_waiting > 0:
line1 = ser1.readline()#.decode('Ascii').rstrip()
x=(line1.decode('Ascii').rstrip())
print(x)
blynk.virtual_write(3,"{:%m/%d/%Y-%H:%M}".format(datetime.datetime.now()),":A1: ",x)
def serialmessagerec2():
while ser2.in_waiting > 0:
line2 = ser2.readline()#.decode('Ascii').rstrip()
x=(line2.decode('Ascii').rstrip())
print(x)
blynk.virtual_write(4,"{:%m/%d/%Y-%H:%M}".format(datetime.datetime.now()),":A2: ",x)
# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN)
# Led control through V0 virtual pin
@blynk.on("V0")
def v0_write_handler(value):
# global led_switch
if int(value[0]) != 0:
print('Current Message')
#blynk.virtual_write(3,"Current Message")
ser1.write(b"<1000>")
ser2.write(b"<1000>")
# Led control through V0 virtual pin
@blynk.on("V1")
def v1_write_handler(value):
# global led_switch
if int(value[0]) != 0:
print('Original Message Received!')
#blynk.virtual_write(3,"Original Message Received!")
ser1.write(b"<1001>")
ser2.write(b"<1001>")
@blynk.on("V2")
def v2_write_handler(value):
# global led_switch
if int(value[0]) != 0:
print('Sale Message Received!')
#blynk.virtual_write(3,"Sale Message Received!")
ser1.write(b"<1002>")
ser2.write(b"<1002>")
#function to sync the data from virtual pins
@blynk.on("connected")
def blynk_connected():
print("Raspberry Pi Connected to New Blynk")
#clear status display
blynk.virtual_write(3," ")
blynk.virtual_write(4," ")
while True:
blynk.run()
serialmessagerec1()
serialmessagerec2()