do you know python programming?
Bassics, but do you mean the Example-03?
this code works for ads1115, you need to modify it for ads1015 (i guess you know this much basics)
#!/usr/bin/env python
import Adafruit_ADS1x15
import cayenne.client
import time
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = "MQTT_USERNAME"
MQTT_PASSWORD = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"
# The callback for when a message is received from Cayenne.
def on_message(message):
print("message received: " + str(message))
# If there is an error processing the message return an error string, otherwise return nothing.
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)
i = 0
timestamp = 0
# Simple demo of reading each analog input from the ADS1x15 and printing it to
# the screen.
# Author: Tony DiCola
# License: Public Domain
# Import the ADS1x15 module.
# Create an ADS1115 ADC (16-bit) instance.
# adc = Adafruit_ADS1x15.ADS1115()
# Or create an ADS1015 ADC (12-bit) instance.
adc = Adafruit_ADS1x15.ADS1015()
# Note you can change the I2C address from its default (0x48), and/or the I2C
# bus by passing in these optional parameters:
# adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)
# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
# - 2/3 = +/-6.144V
# - 1 = +/-4.096V
# - 2 = +/-2.048V
# - 4 = +/-1.024V
# - 8 = +/-0.512V
# - 16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1
# Main loop.
while True:
client.loop()
# Read all the ADC channel values in a list.
values = [0]*4
if (time.time() > timestamp + 10):
for i in range(4):
# Read the specified ADC channel using the previously set gain value.
values[i] = adc.read_adc(i, gain=GAIN)
client.virtualWrite(i, values[i])
timestamp = time.time()
It was already optimised for ads1015. Does not want to work.
Connecting to mqtt.mydevices.com:1883
Traceback (most recent call last):
File “ADS1015.py”, line 65, in
values[i] = adc.read_adc(i, gain=GAIN)
File “/usr/local/lib/python2.7/dist-packages/Adafruit_ADS1x15/ADS1x15.py”, line 192, in read_adc
return self._read(channel + 0x04, gain, data_rate, ADS1x15_CONFIG_MODE_SINGLE)
File “/usr/local/lib/python2.7/dist-packages/Adafruit_ADS1x15/ADS1x15.py”, line 133, in _read
result = self._device.readList(ADS1x15_POINTER_CONVERSION, 2)
File “/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO/I2C.py”, line 134, in readList
results = self._bus.read_i2c_block_data(self._address, register, length)
File “/usr/local/lib/python2.7/dist-packages/Adafruit_PureIO/smbus.py”, line 216, in read_i2c_block_data
cmdstring[i] = val
TypeError: one character string expected
ADS1015 is the file with your code. and yes, ADS1x15 is in the same folder
can you install with python3 the ads library and run the file with python3. Also make sure the ads is detected by i2cdetect -y 1
This one?
this one.
Works
I mean the program
and which program is it? can you share?
nevermind, it will just increase my work. follow this steps:
- git clone GitHub - adafruit/Adafruit_Python_ADS1x15: Python code to use the ADS1015 and ADS1115 analog to digital converters with a Raspberry Pi or BeagleBone black.
- cd Adafruit_Python_ADS1x15
- sudo python3 setup.py install
-
i2cdetect -y 1//check whether ads is detected - run this code with ads1015 selected and see whether you are getting data from the ads.
- create a new file and add the below code:
code:
#!/usr/bin/env python
import Adafruit_ADS1x15
import cayenne.client
import time
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = "MQTT_USERNAME"
MQTT_PASSWORD = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"
# The callback for when a message is received from Cayenne.
def on_message(message):
print("message received: " + str(message))
# If there is an error processing the message return an error string, otherwise return nothing.
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)
i = 0
timestamp = 0
# Simple demo of reading each analog input from the ADS1x15 and printing it to
# the screen.
# Author: Tony DiCola
# License: Public Domain
# Import the ADS1x15 module.
# Create an ADS1115 ADC (16-bit) instance.
# adc = Adafruit_ADS1x15.ADS1115()
# Or create an ADS1015 ADC (12-bit) instance.
adc = Adafruit_ADS1x15.ADS1015()
# Note you can change the I2C address from its default (0x48), and/or the I2C
# bus by passing in these optional parameters:
# adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)
# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
# - 2/3 = +/-6.144V
# - 1 = +/-4.096V
# - 2 = +/-2.048V
# - 4 = +/-1.024V
# - 8 = +/-0.512V
# - 16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1
# Main loop.
while True:
client.loop()
# Read all the ADC channel values in a list.
values = [0]*4
if (time.time() > timestamp + 10):
for i in range(4):
# Read the specified ADC channel using the previously set gain value.
values[i] = adc.read_adc(i, gain=GAIN)
client.virtualWrite(i, values[i])
timestamp = time.time()
First of all, this is my code:
from ADS1x15 import ADS1015
import time
adc = ADS1015()
channel = 0
gain = 1
t = 0
while True:
data = adc.read_adc(channel, gain)
print t, “s:”, data
t += 0.5
time.sleep(0.5)
It works and gives me good results.
I did as you said
I cloned the files and installed them using python3. I did i2ctetect and the ads was at 48 as it always does.
Then I tried your code and it responded with the error:
Connecting to mqtt.mydevices.com:1883
Traceback (most recent call last):
File “ADS1015.py”, line 65, in
values[i] = adc.read_adc(i, gain=GAIN)
File “/usr/local/lib/python2.7/dist-packages/Adafruit_ADS1x15/ADS1x15.py”, line 192, in read_adc
return self._read(channel + 0x04, gain, data_rate, ADS1x15_CONFIG_MODE_SINGLE)
File “/usr/local/lib/python2.7/dist-packages/Adafruit_ADS1x15/ADS1x15.py”, line 133, in _read
result = self._device.readList(ADS1x15_POINTER_CONVERSION, 2)
File “/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO/I2C.py”, line 134, in readList
results = self._bus.read_i2c_block_data(self._address, register, length)
File “/usr/local/lib/python2.7/dist-packages/Adafruit_PureIO/smbus.py”, line 216, in read_i2c_block_data
cmdstring[i] = val
TypeError: one character string expected
Then I tried sudo python3 <your_code> with the result:
^CTraceback (most recent call last):
File “ADS1015.py”, line 59, in
client.loop()
File “/usr/local/lib/python3.7/dist-packages/cayenne_mqtt-1.1.0-py3.7.egg/cayenne/client.py”, line 122, in loop
File “/usr/local/lib/python3.7/dist-packages/paho_mqtt-1.5.0-py3.7.egg/paho/mqtt/client.py”, line 1163, in loop
socklist = select.select(rlist, wlist, , timeout)
so can you add this code into the cayenne code and send data. you can check the code i gave above.
do you receive something?
receive what?
I’m running the program n
I’m getting those readings on the console:
PUB v1/xxxxxxxxxxxxxxxx/things/xxxxxxxxxxxxxx/data/1
-15
PUB v1/xxxxxxxxxxxxxxx/things/xxxxxxxxxxxxxx/data/1
-15
PUB v1/xxxxxxxxxxxxxxxxxxxx/things/xxxxxxxxxxxxxxxxxxxxxx/data/1
-15
PUB v1/xxxxxxxxxxxxxxxxxx/things/xxxxxxxxxxxxxxxxx/data/1
-15
Is this how this should look like?
yes, if -15 is your ads value. can you share your code also.