![]() |
first steps with python3 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: first steps with python3 (/thread-3871.html) |
first steps with python3 - hunnimonstr - Jul-03-2017 Hi all i could use a few pointers in the right directions. I am trying to automate the patio plant care, and automatically water plants as they need it. using the miflora plant sensors which broadcast information via Btooth BLE and a raspberry pi Zer0-W which will use gpio pins to switch on/off water pumps. So far so good.. things sort of work ;) But i need to add in a bit more complexity and im not sure about the best way to go. I intend to run a python script via cron scheduled job every 30 mins or so firstly i want to log the data collected, local file vs iot server?? next i want to incorporate some warnings and alarms such as Low battery ?? MQTT? then the decision to water or not really needs to check the last time water was applied and raise warnings if it was applied on the last cycle, look up in logs or retain some persistent data?? from miflora.miflora_poller import MiFloraPoller, \ MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE, MI_BATTERY import datetime # for logging / timestamp import time # for sleep function def sensortest(plantid, saddr, iopin): """ my first function..""" poller = MiFloraPoller(saddr) if poller.parameter_value(MI_MOISTURE) < 30: GPIO.output(iopin, 0) # turn on pump1 time.sleep(15) GPIO.output(iopin, 1) # turn off pump1 # debug/test output print(" info on plant : " , plantid) print(" Temperature: {}".format(poller.parameter_value("temperature"))) print(" Moisture: {}".format(poller.parameter_value(MI_MOISTURE))) print(" Light: {}".format(poller.parameter_value(MI_LIGHT))) print(" Conductivity: {}".format(poller.parameter_value(MI_CONDUCTIVITY))) print("Battery: {}".format(poller.parameter_value(MI_BATTERY)))called from from miflora.miflora_poller import MiFloraPoller, \ MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE, MI_BATTERY import RPi.GPIO as GPIO import datetime import time import myfunc1 GPIO.setmode(GPIO.BCM) # set board mode to Broadcom GPIO.setup(17, GPIO.OUT) # set up pin 17 GPIO.setup(27, GPIO.OUT) # set up pin 27 GPIO.setup(22, GPIO.OUT) # set up pin 22 GPIO.setup(23, GPIO.OUT) # set up pin 23 GPIO.setup(24, GPIO.OUT) # set up pin 24 GPIO.setup(25, GPIO.OUT) # set up pin 25 GPIO.output(17, 1) # turn on pin 17 GPIO.output(27, 1) # turn on pin 27 GPIO.output(22, 1) # turn on pin 22 GPIO.output(23, 1) # turn on pin 23 GPIO.output(24, 1) # turn on pin 24 GPIO.output(25, 1) # turn on pin 25 myfunc1.sensortest("P1","C4:7C:8D:64:43:6F",17) myfunc1.sensortest("P2","C4:7C:8D:64:43:C8",27) myfunc1.sensortest("P3","C4:7C:8D:64:43:7F",22) myfunc1.sensortest("P4","C4:7C:8D:64:43:6c",23) myfunc1.sensortest("P5","C4:7C:8D:64:43:5b",24) myfunc1.sensortest("P6","C4:7C:8D:64:43:3f",25)I intend to read up on enumeration and catch/try next by enumerating the Btooth addy's i can loose the plantid variable any pointers provided regarding the best way to log data, and or retain some persistent data between runs will be greatly appreciated. if im not using a python vocabulary yet, be patient ;) RE: first steps with python3 - Larz60+ - Jul-03-2017 what does the miflora code look like? The second module can be written like: import RPi.GPIO as GPIO class Gpio: def __init__(self): GPIO.setmode(GPIO.BCM) # set board mode to Broadcom def gpio_setup(self, pins): for pin in pins: GPIO.setup(pin, GPIO.OUT) def gpio_out(selfself, pins): for pin in pins: GPIO.output(pin, 1) def main(self): gpio = Gpio() setup_seq = [27, 22, 23, 24, 25] output_seq = [17, 27, 22, 23, 24, 25] gpio.gpio_setup(setup_seq) gpio.gpio_out(output_seq) if __name__ == '__main__': main() RE: first steps with python3 - hunnimonstr - Jul-03-2017 https://github.com/open-homeautomation/miflora which i found following an openhab forum post after an evenings googling on the subject. I have a wired arduino version already in place but wired? in this day and age? I had been planning on an esp8266-12 solution, but the practical problems of waterproofing and power supply (batteries) and my very basic electronics skills/understanding would result in a relatively huge box and monthly bulk battery charging and a high cost per plant pot unit. where these things cost about a tenner are tiny and run 12months on a single 3v coin cell. https://xiaomi-mi.com/sockets-and-sensors/xiaomi-huahuacaocao-flower-care-smart-monitor/ What i could use is a few pointers as to how to best log the data i gather, my default is a csv record in a text file on a shared drive Also is there a neat mechanism to retain 'current values' between scheduled runs of my python script? again my default option is a text file. Cheers.. RE: first steps with python3 - hunnimonstr - Jul-03-2017 (Jul-03-2017, 12:19 PM)Larz60+ Wrote: what does the miflora code look like? thanks for that, I have modded the main script to reflect the suggestions, much neater.. def main(self): gpio = Gpio() iopin_seq = [17, 27, 22, 23, 24, 25] btooth_seq = ["C4:7C:8D:64:43:6F","C4:7C:8D:64:43:C8","C4:7C:8D:64:43:61","C4:7C:8D:64:47:A7","C4:7C:8D:64:43:AE","C4:7C:8D:64:43:F5"] plant_id = 1 gpio.gpio_setup(iopin_seq) gpio.gpio_out(iopin_seq) for bt in btooth_seq: myfunc1.sensortest(plant_id, bt ,iopin_seq[plant_id-1]) plant_id = plant_id + 1 GPIO.cleanup() if __name__ == '__main__': main('self') RE: first steps with python3 - nilamo - Jul-03-2017 How do you want to do logging? Do you want to log on the device locally, or do you want to push your logs to an external source? If you're not sure, then consider the device itself... once it's setup and running, how often do you want to walk over to it to check it's logs (or ssh into it)? RE: first steps with python3 - hunnimonstr - Jul-03-2017 Initially only a subset of the data available and a few previously recorded values are required for triggering action or alarms. however once collected the whole set of data may become interesting and reveal something currently unknown, or not ... :) Im erring towards an iot cloud server tho my first attempts with the ubidots walk thru are disappointingly laden with error messages.. early days.. (yes i have an a/c and used valid security token and variable id..) while i prefer to 'own' my data as a rule, small amounts of cloud storage are free and possibly more robust than the sdcard in the pi.. also failure to log in a timely fashion could itself be a warning of a problem requiering investigation. when watering knowing when last watered is crucial, as if triggered too soon due to error or problem with the water delivery (hose leak) to avoid needing to retrieve this when run on a scheduled basis, the process could run and wait idle for periods between checks, that way a list of the last few watering times for each area could be maintained. but its not an arduino is it? hmm. |