rickyh89
Posts: 55
Joined: Mon Jun 18, 2018 11:01 pm

Miflora plant sensor GUI & Python test script

Fri Sep 14, 2018 3:50 am

Figured i would make something quick to test and display output of Milfora sensors. I only worked on it about an hour so if connection is not good there may be acouple bugs. Feel free to do whatever with it...

EDIT: Your obviously going to need miflora download and btlewrap, also this is with python 3 (needed for miflora module)

Code: Select all

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from miflora.miflora_poller import MiFloraPoller, \
    MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE, MI_BATTERY

from btlewrap import GatttoolBackend

class Test(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.pack()
        self.labels()
        self.entry()
        self.treeview()
        self.buttons()
        self.listbox()

    def labels(self):
        main_label = Label(self, text='Miflora Sensor Application')
        mac_label = Label(self, text='Enter Mac address :')
        
        main_label.grid(row=0, column=0, columnspan=2)
        mac_label.grid(row=1,column=0,sticky=W)
        
    def entry(self):
        self.mac_entry_var = StringVar()
        mac_entry = Entry(self, textvariable=self.mac_entry_var)
        mac_entry.grid(row=1,column=1,sticky=E+W)
        Grid.columnconfigure(self, 1, weight=1)

    def treeview(self):
        scrollbar = Scrollbar(self, orient="vertical")
        self.new_lb = ttk.Treeview(self, columns=('Sensor Name','Battery','Moisture','Temp','Light','EC'))
        self.new_lb['show']='headings'
        self.new_lb.heading('#1', text= 'Sensor Name')
        self.new_lb.column('#1', width=125, stretch=NO)
        self.new_lb.heading('#2', text= 'Battery')
        self.new_lb.column('#2', width=75, stretch=NO)
        self.new_lb.heading('#3', text= 'Moisture')
        self.new_lb.column('#3', width=75, stretch=NO)
        self.new_lb.heading('#4', text= 'Temp')
        self.new_lb.column('#4', width=75, stretch=NO)
        self.new_lb.heading('#5', text= 'Light')
        self.new_lb.column('#5', width=75, stretch=NO)
        self.new_lb.heading('#6', text= 'EC')
        self.new_lb.column('#6', width=75, stretch=NO)
        self.new_lb.configure(yscroll = scrollbar.set, selectmode="browse")
        scrollbar.config(command=self.new_lb.yview)
        self.new_lb.grid(row=2,column=0, columnspan=2)

    def buttons(self):
        poll_sensor = Button(self, text='Add Sensor',
                             command=lambda:self.get_sensors())
        self.add_sensor = Button(self, text='Refresh Sensors',
                            command=lambda:self.refresh_sensors(),state='disabled')
        poll_sensor.grid(row=3,column=1,sticky=E)
        self.add_sensor.grid(row=3,column=0,sticky=E+W)

    def listbox(self):
        self.listboxs = Listbox(self)
        
    def get_sensors(self):
        if self.mac_entry_var.get() != '' and re.match("C4:7C:8D:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}", self.mac_entry_var.get()):
            poller = MiFloraPoller(mac=self.mac_entry_var.get() ,backend=GatttoolBackend,retries=2)
            readings = [self.mac_entry_var.get(),poller.parameter_value(MI_BATTERY),poller.parameter_value(MI_MOISTURE),poller.parameter_value(MI_TEMPERATURE),
                        poller.parameter_value(MI_LIGHT),poller.parameter_value(MI_CONDUCTIVITY)]
            self.new_lb.insert('','end', values=(readings[0],readings[1],readings[2],readings[3],
                                                 readings[4],readings[5]))
            self.listboxs.insert(END,self.mac_entry_var.get())
            self.mac_entry_var.set('')
            self.add_sensor.config(state='normal')
        else:
            messagebox.showwarning('ERROR', "Input is wrong format\nCheck bluetooth working\n'sudo hcitool lescan'")

    def refresh_sensors(self):
        sensors = self.listboxs.get(0,END)
        self.new_lb.delete(*self.new_lb.get_children())
        for macs in sensors:
            poller = MiFloraPoller(mac=macs ,backend=GatttoolBackend)
            readings = [poller.parameter_value(MI_BATTERY),poller.parameter_value(MI_MOISTURE),poller.parameter_value(MI_TEMPERATURE),
                        poller.parameter_value(MI_LIGHT),poller.parameter_value(MI_CONDUCTIVITY)]
            self.new_lb.insert('','end', values=(macs,readings[0],readings[1],readings[2],
                                                 readings[3],readings[4]))

        
root = Tk()
app=Test()
app.mainloop()
Here is simple python code to print out sensor details...

Code: Select all

from miflora.miflora_poller import MiFloraPoller, \
    MI_CONDUCTIVITY, MI_MOISTURE, MI_LIGHT, MI_TEMPERATURE, MI_BATTERY

from btlewrap import GatttoolBackend

poller = MiFloraPoller(mac='ENTER MAC HERE' ,backend=GatttoolBackend)

print ('Temperature : ' ,poller.parameter_value(MI_TEMPERATURE))
print ('Moisture : ' ,poller.parameter_value(MI_MOISTURE))
print ('Light : ' ,poller.parameter_value(MI_LIGHT))
print ('Conductivity : ' ,poller.parameter_value(MI_CONDUCTIVITY))
print ('Battery : ' ,poller.parameter_value(MI_BATTERY))

Return to “Automation, sensing and robotics”