Humino: Accessing Soil Data With a Telegram Bot

I have written about building an Arduino-based soil monitoring system and how to predict time until next watering with linear regression. Yet, to see these results I had to shutdown the Arduino every morning, remove the SD card it was logging data to, copy that to my computer and run the analysis script. That should be wireless!

So I setup a bot for Telegram messenger that I can ask for the current measurements.

It's actually quite easy to set this up when you have an Internet-connected device that can run Python scripts. But the Arduino isn't, so I connected it to a Raspberry Pi. When connected with a USB cable, the Raspberry Pi can read messages that are sent by the Arduino on the serial line.

# pip install pyserial
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.readline()
> Hello, this is Arduino

So I've got one script running indefinitely to record humidity values from the Arduino into an Sqlite database. A second script is running once an hour to update a plot of the past days’ measurements and to write the current predictions for time-until-dry into a text file.

The first script is also starting the Telegram bot itself, which is responding to requests with the most recent plot and predictions.

def measure(bot, update):
    with open(os.path.join(config.OUT_FOLDER, "status.txt")) as f:
        status = f.read()

    update.message.reply_text(status)
    
    with open(os.path.join(config.OUT_FOLDER, "plot.png"), "rb") as f:
        bot.send_photo(chat_id=update.message.chat_id, photo=f)

updater = Updater(config.TELEGRAM_API_TOKEN)
dp = updater.dispatcher
dp.add_handler(CommandHandler('measure', measure))
updater.start_polling()

See the full code for this project on Github