GreenHouse Monitor Program

From OpenCircuits
Revision as of 21:33, 8 February 2017 by Russ hensel (talk | contribs)
Jump to navigation Jump to search

What

This is an arduino program intended to monitor the environment in a green house. It has a simple command driven interface. It is designed to run with Python Smart Terminal Documentation on the construction of a Smart Terminal extension to support the green house monitor and save its data to a csv file are also part of this project and the documentation here.

Source Code

Still working on where I will keep the source. It is open source, email me in the meantime: User:Russ_hensel The code is pretty straight forward, the heavy lifting is all left to the Python Smart Terminal. There is nothing very fancy or special about the arduino program, get the source, read it. Email me if there are issuses. Also look at this wiki's category SmartTerminal (link at bottom of this page).

Arduino GreenHouse Commands

Commands are single letter strings, in some cases followed by numbers. All end with a <cr>. All envoke responses all of which also end with <cr>

Command Purpose Send Response Comment
Get version of arduino software v GreenHouse Monitor v3 2017 01 24.01 Of course exact string changes with version. This string is used by the SmartTerminal to verify the arduino connection.
Get the Temperature t 33.0 60.0 In this version there are 2 sensors.

Smart Terminal Extension

In general when you want to make an extension you should find one that is similar to the one you want, copy it, then tweak the code. This description will not quite do that because the extension ( and arduino program ) are already written.

Copy Some Code

All the extensions that are written are in modules with names of the form xxx_processing. Look at the file headers .... and pick one to copy. If you run this copy it should start up without too much trouble. But how do you get the code to execute? Say your module ( file ) is my_processing.py and the class name ( which you will probably change when you make the copy ) is MyProcessing. You do not run your module, you run SmartTerminal. The trick is to first make some changes to the parameter file. What you need is to make sure that at the end of creating a Parameter class the final setting reflect something like:

     self.ext_processing_module      = "my_processing"
     self.ext_processing_class       = "MYProcessing"

are the setting that are made last. More documentation the on the parameters is found in Smart Terminal Parameter Examples.

SmartTerminal Code Explanation

I will explain the green house code, use this explanation to build your own code. This code is gh_csv_processing.py The really fine details are in the code comments, this documentation is at a higher level. To activate the code have your parameter file ( this is automatic in the intermediate file by setting mode = "GreenHouseCSV" )

    self.ext_processing_module      = "gh_csv_processing"
    self.ext_processing_class       = "GHCSVProcessing"

What Is the Point

It is always nice to know when code is being explained what the code is supposed to do. So here it is.

The idea is that the arduino will be set up in an environment like a green house and its job is to measure and report on variables like temperature and humidity. The job of the smart terminal is to control that data acquisition and log it to a csv file.

Details

  • The smart terminal need not be used in its smart mode, all the dumb terminal stuff still works and let you issue commands to the arduino and inspect the results.
  • In addition the smart function that are added include:
    • Automatic probe of comm ports to find the arduino.
    • Opening the comm port and logging the data to a csv file.

Lets get an overview of the methods ( for the detail read the code ):

  • __init__() builds the class, gathers references to other objects in the application, and finally defines some instance variables for data processing later
  • add_gui() builds gui components and returns a reference to the GUI components, for Tkinter this is a frame, for Kivy it will probably be a layout.

Now a complexity of the application is that it is running in 2 different threads. When the application starts up it has a single thread that after a bit of time creates the gui and runs its main method. I call that the GUI thread. The GUI thread starts another thread, ( the class SmartTerminalHelper ) which has its own methods and attributes. In a multi threaded application you have to be careful what calls what. Calls between the threads are particularly tricky once both are running. For the more difficult issues of this type communication between the threads is managed by pushing data into queues which are thread safe and allow the threads to communicate safely with each other. All this is in the code. The GHCSVProcessing class is a bit complicated since parts of it run in one thread, parts in the other. How do you tell what is what. There are two ways one is to reason it out, one is to read the comments, this discussion should help. Init is called from the GUI thread which is the one that is "in charge" of the application. It also calls __add_gui__ which since it adds to the GUI will create objects that are also executed in the GUI thread. All the functions in the class are commented so you know what class they should be called from ( run in )

Additional Functions Run in the GIU

Both of these are call back function called from the GUI we have created in add_gui, so they run in the GUI thread. However both want to call functions in the helper thread. There code is very simple, each one pushes its request into a queue which is processed by the helper thread when it get to it.

  • cb_test_test_ports( )
  • cb_end_helper( )


Rest of Functions Run in Helper

  • find_and_monitor_arduino( )
  • set_time()
  • process_temp_line()
  • process_humid_line()


  • cb_find_and_monitor_arduino()