Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADS1256 ADC Input addition #537

Closed
SAM26K opened this issue Oct 1, 2018 · 165 comments
Closed

ADS1256 ADC Input addition #537

SAM26K opened this issue Oct 1, 2018 · 165 comments

Comments

@SAM26K
Copy link

SAM26K commented Oct 1, 2018

Mycodo Issue Report:

  • Specific Mycodo Version: 6.39

Problem Description

Please list:

  • Using waveshare ADS1256 8 ch A/D converter to monitor analog soil moisture sensors.
    Since this pcb is not on the sensor list, I'm using some python code (called by some Linux bash code) to return the selected channels voltage. This is the bash code:
    sudo /etc/getch0.sh
    Above command calls the following command:
    sudo python ~/Desktop/ads1256code1/py-ads1256/read_volts_ch0.py

   read_volts_ch0.py contains the following python code:
   import ads1256       # import this lib                             

gain = 1			 # ADC's Gain parameter
sps = 25			 # ADC's SPS parameter

# Create the first list. It will receive ADC's absolute values
AllChannelValuesVolts = [0,0,0,0,0,0,0,0]       

# Create the second list. It will received absolute values converted to Volts
AllChannelValues = [0,0,0,0,0,0,0,0]		

# Initialize the ADC using the parameters
ads1256.start(str(gain),str(sps))  
#Fill the first list with all the ADC's absolute channel values
AllChannelValues = ads1256.read_all_channels()        
 for i in range(0, 8):
	# Fill the second list  with the voltage values
	AllChannelValuesVolts[i] = (((AllChannelValues[i] * 100) /167.0)/int(gain))/1000000.0   
#for i in range(0, 8):     
#    # Print all the absolute values
#    print AllChannelValues[i]              
# Print a new line
#print ("\n");							   
#for i in range(0, 8):     
    # Print all the Volts values converted from the absolute values
#    print AllChannelValuesVolts[i]
 print AllChannelValuesVolts[0]
# Stop the use of the ADC
ads1256.stop() 	
  • what were you trying to do
  • specific setup details that are involved

Errors

  • List any errors you encountered.
  • Copy and pasting crash logs, or link to any specific
    code lines you've isolated (please use GitHub permalinks for this)

Steps to Reproduce the issue:

How can this issue be reproduced?

  1. step 1
  2. step 2...
  3. etc

Additional Notes

Is there anything that should be added to make it easier
to address this issue?

@SAM26K
Copy link
Author

SAM26K commented Oct 1, 2018

Errors out when selecting "Live". Error log:
Error 500: Internal Server Error

Something bad happened but it's probably not your fault. Letting the developers know about these issues is crucial to supporting Mycodo. Please submit a new issue on GitHub with the following error traceback (copy the entire traceback):

Error (Full Traceback):
Traceback (most recent call last):
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functionsrule.endpoint
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask_login/utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "/home/pi/Mycodo/mycodo/mycodo_flask/routes_page.py", line 994, in page_live
use_unit=use_unit)
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask/templating.py", line 135, in render_template
context, ctx.app)
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/flask/templating.py", line 117, in _render
rv = template.render(context)
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/pi/Mycodo/env/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/pi/Mycodo/mycodo/mycodo_flask/templates/pages/live.html", line 3, in top-level template code
{% set help_page = ["live-measurements", ('Live Measurements')] %}
File "/home/pi/Mycodo/mycodo/mycodo_flask/templates/layout.html", line 256, in top-level template code
{%- block body %}{% endblock -%}
File "/home/pi/Mycodo/mycodo/mycodo_flask/templates/pages/live.html", line 129, in block "body"
0.0 {% if use_unit[each_input.unique_id][each_measure] and use_unit[each_input.unique_id][each_measure] in dict_units %}{{dict_units[use_unit[each_input.unique_id][each_measure]]['unit']}}{% endif %} ({{dict_measurements[each_measure]['name']}}) | <{{
('Please wait')}}>

File "/home/pi/Mycodo/env/lib/python3.5/site-packages/jinja2/environment.py", line 411, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: 'collections.OrderedDict object' has no attribute ''

@SAM26K
Copy link
Author

SAM26K commented Oct 1, 2018

Code is modified version of py-ads1256/read_volts_examply.py available on github. When executed from a terminal on the pi itself (pi3B) it works fine and returns the voltage for channel 0 only one time. If I use the "Linux command" input function, it generates above error when "Live" tab is clicked.

@SAM26K SAM26K closed this as completed Oct 1, 2018
@SAM26K SAM26K reopened this Oct 1, 2018
@SAM26K
Copy link
Author

SAM26K commented Oct 1, 2018

Sorry didn't mean to close issue.

@SAM26K
Copy link
Author

SAM26K commented Oct 1, 2018

Code borrowed from here:
https://github.com/fabiovix/py-ads1256

@SAM26K
Copy link
Author

SAM26K commented Oct 1, 2018

Sorry, this is the first time Im posting on github. Here; is second attempt to post the modified python module code to get the voltage from one channel:

import ads1256       # import this lib                             

gain = 1			 # ADC's Gain parameter
sps = 25			 # ADC's SPS parameter

# Create the first list. It will receive ADC's absolute values
AllChannelValuesVolts = [0,0,0,0,0,0,0,0]       

# Create the second list. It will received absolute values converted to Volts
AllChannelValues = [0,0,0,0,0,0,0,0]		

# Initialize the ADC using the parameters
ads1256.start(str(gain),str(sps))  

# Fill the first list with all the ADC's absolute channel values
AllChannelValues = ads1256.read_all_channels()        
                
for i in range(0, 8):
	# Fill the second list  with the voltage values
	AllChannelValuesVolts[i] = (((AllChannelValues[i] * 100) /167.0)/int(gain))/1000000.0   


#for i in range(0, 8):     
#    # Print all the absolute values
#    print AllChannelValues[i]              


# Print a new line
#print ("\n");							   


#for i in range(0, 8):     
    # Print all the Volts values converted from the absolute values
#    print AllChannelValuesVolts[i]
         
print AllChannelValuesVolts[0]

@SAM26K
Copy link
Author

SAM26K commented Oct 1, 2018

That didn't work any better than the first attempt, Don't understand the weird formatting.
the bold print is not what I pasted so ignore those bits.

@kizniche
Copy link
Owner

kizniche commented Oct 3, 2018

Thanks. I'll look into the issues and integrating the ADC

@kizniche
Copy link
Owner

kizniche commented Oct 8, 2018

I fixed this issue with the Linux Command input, however, after you've updated to the latest release (not yet released), you will have to deactivate your controller, save the settings again, then reactivate it for it to be fixed.

@kizniche
Copy link
Owner

kizniche commented Oct 8, 2018

I'll also work on adding this ADC to the builtin inputs.

@kizniche
Copy link
Owner

kizniche commented Oct 8, 2018

I just added the ADC module. You will be able to test it once I make the 6.4.0 release, which could be any day now (maybe even today; I'm not sure how much progress I'll make today toward a release, but I've had a pretty successful bug hunt so far).

@SAM26K
Copy link
Author

SAM26K commented Oct 10, 2018 via email

@kizniche
Copy link
Owner

You can upgrade to the master branch with the instructions at https://github.com/kizniche/Mycodo/wiki/Updating-to-Github-Master-Branch

When the new release comes out, you will be able to upgrade from this version in the normal way via the upgrade page.

@SAM26K
Copy link
Author

SAM26K commented Oct 11, 2018 via email

@kizniche
Copy link
Owner

kizniche commented Oct 11, 2018

What was the error encountered during the fresh install? Your log excerpt is only from the upgrade.

@SAM26K
Copy link
Author

SAM26K commented Oct 11, 2018 via email

@kizniche
Copy link
Owner

So your fresh install works (fresh installs install from the latest release, not the master branch). Have you attempted an upgrade to the master branch after the fresh install?

@SAM26K
Copy link
Author

SAM26K commented Oct 11, 2018 via email

@kizniche
Copy link
Owner

Can you paste the contents of the upgrade log?

@SAM26K
Copy link
Author

SAM26K commented Oct 11, 2018 via email

@kizniche
Copy link
Owner

Sorry, but you'll have to log on to github.com to attach files. You can't do it by replying to the email notification. Thanks.

@SAM26K
Copy link
Author

SAM26K commented Oct 11, 2018 via email

@kizniche
Copy link
Owner

From your log, above, you were performing an upgrade from 6.2.2 to 6.3.9, not upgrading to the master branch from a fresh install of the latest release (v6.3.9).

[2018-09-30 23:25:38] Installed version: 6.2.2 
[2018-09-30 23:25:38] Latest version: 6.3.9

@kizniche
Copy link
Owner

When upgrading to the master branch, the first line of the upgrade log (excerpt) will be:

Upgrade script executed with the 'force-upgrade-master' argument. Upgrading from github repo master.

@kizniche
Copy link
Owner

You happened to have caught me right as I am making the 6.4.0 release. It's now released, so you can either upgrade normally or perform a fresh install to get it.

@Theoi-Meteoroi
Copy link
Contributor

Theoi-Meteoroi commented Oct 18, 2018

I have one of these waveshare AD boards so I thought I would give the new code a try - I downloaded a zip of the github repo a short while ago.

On activation - the UI error is:
Error: Could not activate Input controller with ID 71ef8ad3-788f-42d7-b2a4-6dd707668fec: No module named 'ADS1256_definitions'

the log stack is:

2018-10-18 15:59:06,702 - mycodo.daemon - ERROR - Could not activate Input controller with ID 71ef8ad3-788f-42d7-b2a4-6dd707668fec: No module named 'ADS1256_definitions'
Traceback (most recent call last):
  File "/var/mycodo-root/mycodo/mycodo_daemon.py", line 506, in controller_activate
    ready, cont_id)
  File "/var/mycodo-root/mycodo/controller_input.py", line 182, in __init__
    self.adc = input_loaded.ADCModule(self.input_dev)
  File "/home/pi/Mycodo/mycodo/inputs/ads1256_input.py", line 95, in __init__
    from ADS1256_definitions import POS_AIN0
ImportError: No module named 'ADS1256_definitions'
  • I modified ads1256_input.py to add one line so we get a sys.path and it activates.

import sys

I'm getting readings but will need to work with this a bit more.

Thanks Kyle! Nice addition. I have quite a few analog sensors I want to work with, mostly UV sensors.

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@kizniche
Copy link
Owner

@Theoi-Meteoroi fixed some issues with the module, namely the incorrect channel sequence, which should fix issues with channels 5, 6, and 7. You'll have to upgrade to the repo master to get the new code.

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@kizniche
Copy link
Owner

No, that's not necessary for this code change.

@Theoi-Meteoroi
Copy link
Contributor

Theoi-Meteoroi commented Oct 19, 2018

Looking through the issues against PiPyADC - it becomes clear that the higher sample rates are not going to work with a python driver - at least not directly. As integrated into Mycodo, we are actually taking occasional samples (determined by a Mycodo setting) and not attempting to store every sample the ADC takes. Higher sample rates affect the precision (fewer effective bits) but have much shorter calibration cycles. Cycling between channels requires a settling time lowering the effective throughput, anyway ( see page 21 in the rev. K datasheet ).
The upshot is that delta-sigma converters can be very precise ( this is the converter type used in Digital Multimeters ) they have some quirks that can affect accuracy if not addressed or avoided. A thorough reading (and understanding) of the datasheet is necessary to get optimal results.

So my suggestion is to use a lower or the lowest speed setting since we cannot sample fast enough with Mycodo to obtain all the samples captured by the converter. The only benefit I can see from the higher sampling rates is a lower settling time and lower calibration cycle time however those are all sub-second periods anyway. If you want 24 bit precision - you gotta go slow.

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@kizniche
Copy link
Owner

shorten the default text for the graph labels. Cant see the user channel name in the legends.

I'm not sure what you mean. You can set the names to whatever you like, which will be reflected in the legend labels.

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@kizniche
Copy link
Owner

  1. Select Setup -> Data
  2. Change the name of the input, then save
  3. Reload dashboard
  4. Profit

@Theoi-Meteoroi
Copy link
Contributor

There is quite a bit of interaction between settings for the ADC - Increasing the PGA multiplier reduces the full scale voltage range - and that needs to be changed in the Mycodo input settings.

Great the hear it is working for you @SAM26K
Awesome work from @kizniche to get it integrated without actual hardware to pound on.

I can see this addition will probably evolve over time with more use and feedback. The PiPyADC library has a number of features that seem worthwhile to explore further.

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@Theoi-Meteoroi
Copy link
Contributor

@SAM26K Are you using calibration? What sample rate setting?
I set my calibration to "Self Offset + Self Gain"

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@kizniche
Copy link
Owner

kizniche commented Oct 19, 2018

I'm currently in the middle of a huge refactoring of the Function section. Specifically, splitting conditionals into Triggers and Conditionals, and improving the conditional functionality (#493).

Anyway, why I mention it is because the next thing I'll be tackling after finishing this (listed in #550) is refactoring the ADC measurement system. This will include the ability to, instead of having to add a new Input for each channel, only one Input will need to be added, and the channels you wish to measure and store values of can be selected. Each channel will be able to have the voltages/units set from this one Input. This will allow the Input modules to be restructured to allow:

  • A drastic increase of measurement speed because there is only one Input attempting to read the ADC.
  • All measurements will be acquired at the same time and have the same timestamp (which will also allow all channels on the graphs to align, that is, you can view all measurements at the same time in the tooltip).
  • Make the use of a lockfile unnecessary (there's a lockfile issue where occasionally all modules will get stuck unable to acquire a lock; it's a nasty bug that I have no idea what the cause is).

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@kizniche
Copy link
Owner

Standalone Input module functionality was very recently added, so I haven't had the time to cover it in detail, but there is a bit of information in the manual.

@SAM26K
Copy link
Author

SAM26K commented Oct 19, 2018 via email

@Theoi-Meteoroi
Copy link
Contributor

ran into the dreaded lock bug after adding all channels in a tight loop. Will take a look at the code to see if I can assist.

2018-10-19 16:43:00,105 - mycodo.input_82897384 - ERROR - Error while attempting to read adc: [Errno 2] No such file or directory: '/var/lock/mycodo_adc_uart-ADS1256'
Traceback (most recent call last):
File "/var/mycodo-root/mycodo/controller_input.py", line 358, in read_adc
adc_lock.acquire()
File "/var/mycodo-root/env/lib/python3.5/site-packages/locket/init.py", line 99, in acquire
lock.acquire()
File "/var/mycodo-root/env/lib/python3.5/site-packages/locket/init.py", line 135, in acquire
path=self._path,
File "/var/mycodo-root/env/lib/python3.5/site-packages/locket/init.py", line 86, in _acquire_non_blocking
raise LockError("Couldn't lock {0}".format(path))
locket.LockError: Couldn't lock /var/lock/mycodo_adc_uart-ADS1256

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/mycodo-root/mycodo/controller_input.py", line 362, in read_adc
os.remove(self.adc_lock_file)
FileNotFoundError: [Errno 2] No such file or directory: '/var/lock/mycodo_adc_uart-ADS1256'

@SAM26K
Copy link
Author

SAM26K commented Oct 20, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 20, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 20, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 20, 2018 via email

@SAM26K
Copy link
Author

SAM26K commented Oct 20, 2018 via email

@kizniche kizniche closed this as completed Dec 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants