I'm running a telegram bot on my Raspberry Pi 3. When I run it in my console or using Thonny, everything runs smoothly.
python3 /home/pi/Documents/telegrambot.py
The problem occurs when I set the program to start on boot.
sudo crontab -e
[last line] @reboot sudo python3 /home/pi/Documents/telegrambot.py &
I know that the code is running because I set the bot to send me "I'm online" and it answer me if the reply isn't related to sending images... but when I send to it /tempgraph, it should answer with the the picture of its readings. But I only get the sensor's name and no image at all.
...
chatHist = readCommandHistory(db,chat_id,1)
lastCommand = chatHist[0]['msg']
choice = command.split(".")
if command in lastCommand and len(choice) <= 2:
Fonte = re.search('(\s|\n)'+str(command.split('.')[0])+'\)\s(.*)', lastCommand).group(2)
bot.sendMessage(chat_id, Fonte) #this works
if len(choice) == 1:
table = sdc.readTableServer(db, senstable, 'Fonte', Fonte)
for sensor in table.Sensor.unique():
bot.sendMessage(chat_id, sensor)
tbl = table[table.Sensor == sensor]
#until here everything is ok
for ts in tiposensores:
try:
gop.plotSetup(tbl[['Data',ts]])
#save the image in the folter ... Images/Imagem.png,
#but only works when not on stratup...
bot.sendPhoto(chat_id,open('/home/pi/Documents/Images/Imagem.png','rb'))
pif.cleanImages()
except OSError:
pass
...
@edit:
I found out that the problem happens after i call
gop.plotSetup(tbl[['Data',ts]])
inside the plotSetup function, in this step:
f= plt.figure(figsize=(12, 9))
This is the first time that i use the variable f inside the function.
@edit2
As I said before, the error happens in the plt.figure.
THis is the gpo.plotSetup(), with some changes to catch the error as instructed:
def plotSetup(Data):
...[some code here]...
with open("/home/pi/Desktop/log.csv","a+") as o:
fop.writeCSVFile(o, ['before first plt.figure'])
try:
f= plt.figure(figsize=(12, 9))
except IOError as e:
fop.writeCSVFile(o, e)
except:
for e in sys.exc_info():
fop.writeCSVFile(o, e)
...[some code here]...
f.savefig("/home/pi/Documents/Images/Imagem.png", bbox_inches="tight")
and i got this errors:
<class '_tkinter.TclError'>
no display name and no $DISPLAY environment variable
<traceback object at 0x6e3c8d50>
@edit3
That error led me to this post:
_tkinter.TclError: no display name and no $DISPLAY environment variable
this post suggested use this in the very beginning of the code:
import matplotlib
matplotlib.use('Agg')
and then VOILA!! It's alive.
Really appreciate all the help that you guys gave me, thanks! ;p
gop.PlotSetup
generate a picture and store it on disk?$ crontab -e