While checking all the electronics stuff I own, I've found a soil moisture sensor (https://www.amazon.com/Moisture-Humidity-Compatible-Atomic-Market/dp/B00TMD43BS) and I decided to use it in a simple project just to refresh my basic Arduino knowledge.
The idea was to check soil moisture of a plant and send a message on twitter if it needs water. As a result you can see one of my plants in the flat tweeting happily during the day: https://twitter.com/plant_tweeting
I had many ideas to accomplish this and I'm sure this is not the optional solution, but I had some concerns. The simplest solution would be to connect moisture sensor to Raspberry Pi Zero and read the digital trigger when moisture level drops below a certain level. However I wanted to read the moisture level in analog to be able to keep it in a database or IoT cloud. That's why I needed a ADC converter which I didn't own. That's why I used an atTiny85 as an ADC.
So I came up with this solution: Read analog data from moisture sensor using atTiny85, send this value to Raspberry Pi Zero through serial connection so that a python script on Raspberry Pi Zero can save this value to a database and send a tweet. Again, I'm using atTiny85 as a ADC converter cause I don't own one.
Here's the sketch of this solution:
First I had to remember how to program atTiny85 through an Arduino Uno and this article helped me a lot : https://create.arduino.cc/projecthub/arjun/programming-attiny85-with-arduino-uno-afb829
Just a quick note: everything in this article works fine only if you program your Arduino Uno as ISP in the beginning.
atTiny85 has so little to do, that's why it has a very simple code. It just reads the moisture sensor analog input value and sends this value to a serial connection which goes to Raspberry Pi Zero:
#include <SoftwareSerial.h> #define RX 1 // D1, Pin 6 #define TX 2 // D2, Pin 7 #define sensorPin 3 // D3, Pin 3
SoftwareSerial toRaspberry(RX, TX);
void setup() { pinMode(sensorPin, INPUT); toRaspberry.begin(9600); }
void loop() { int sensorValue = analogRead(sensorPin); toRaspberry.println(sensorValue); delay(10000); }
To setup Raspberry Pi Zero Mini UART(which is available on GPIO pins) I followed this document's "Using the Mini UART port" section : https://www.abelectronics.co.uk/kb/article/1035/raspberry-pi-3--4-and-zero-w-serial-port-usage
The easiest way that I found to send tweets was the command line program twidge. It has a very simple setup and easy to use command line interface : https://jgoerzen.github.io/twidge/twidge-html/twidge.html
Here's the python script running on Raspberry Pi Zero which reads the serial input value from GPIO pins coming from atTiny85(value is between 0-1023 and it represents the moisture of the soil--bigger value means less moisture), saves it to an embedded sqlite database and sends a tweet using twidge command line:
#!/usr/bin/env python import time import serial import os import random import sqlite3 as sl
def main(): ser = serial.Serial( port='/dev/serial0', baudrate = 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) x = ser.readline(); while (x == ''): x = ser.readline(); print x checkSituation(x.rstrip());
def checkSituation(x): waterLevel = 1024 - int(x); tweetText = '{} --water level:{}'; if (waterLevel < 200): tweetText = tweetText.format(getHelpTweet(), waterLevel); else: tweetText = tweetText.format(getFineTweet(), waterLevel); tweet(tweetText); log(waterLevel);
def log(waterLevel): con = sl.connect('plant.db'); sql = 'INSERT INTO PLANT_DATA (water_level, timestamp) values(?, ?)'; data = (waterLevel, int(time.time())); con.execute(sql, data); con.commit(); con.close();
def tweet(text): command = 'twidge update "{}"'.format(text); os.system(command); print(command);
def getFineTweet(): fines = ["I'm doing fine today :)", "It's a beautiful day ;)", "I love my pot :)"]; return random.choice(fines);
def getHelpTweet(): helps = ["I need water!! :(", "Somebody give me water, please", "Anyone there to water me? :("] return random.choice(helps);
if __name__ == "__main__": main()
I've set up a cron job to run this script everyday on Raspberry Pi Zero and I receive tweets about the happiness of my plant.
Comments