Pcf8591

Hi,

I am trying to display the adc pcf8591 data to Cayenne by utilizing the “Build Your Own Thing” function. But I can’t seem to get the connection from hardware.
This is the coding that I am currently using to connect the hardware to mqtt.

import cayenne.client
import smbus
import time

MQTT_USERNAME = “3baa9320-3aa9-11ea-a38a-d57172a4b4d4”
MQTT_PASSWORD = “95a5bce532fa419bb8d44db8a048bf8df676c1df”
MQTT_CLIENT_ID = “e0cf4260-3ce8-11ea-ba7c-716e7f5ba423”

address = 0x48
A0 = 0x40
A1 = 0x41
A2 = 0x42
A3 = 0x43
bus = smbus.SMBus(1)

def on_message(message):
print("message received: " + str(message))

def read_volt():
while True:
bus.write_byte(address,A0)
value = bus.read_byte(address)
print("AOUT:%1.3f " %(value*5.0/255))
time.sleep(1)

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.read_volt = read_volt
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883)
client.loop_forever()

Forgive me for my poor coding as I am really new to this and still trying to learn more everyday.
Thanks in advance :slightly_smiling_face:

are you able to read data from the pcf8591 without the cayenne code? if so then use the below code:

#!/usr/bin/env python
import cayenne.client
import time
import logging
import smbus

# 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"
address = 0x48
A0 = 0x40
bus = smbus.SMBus(1)


# 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, loglevel=logging.INFO)
# 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

while True:
    client.loop()
    if (time.time() > timestamp + 10):
        bus.write_byte(address,A0)
        value = bus.read_byte(address)
        client.virtualWrite(1, value)
        timestamp = time.time()
        i = i+1

you can aso use th cayenne plugin for pcf8591 for cayenne raspberry pi agent https://github.com/myDevicesIoT/cayenne-plugin-pcf8591

1 Like

Yes sir, the pcf8591 code are executable without the cayenne code.I will try and run the code that you gave. Thank you so much for the assist. If the code work, I will inform you asap.
:grinning:

I’ve tried to follow the cayenne plugin method. Successfully clone the plugins but the widgets does not show up. Then, I restart the service but still nothing display as of widgets for pcf8591. Is there any way around this to make it show up?

can you share the log file tail -f /var/log/myDevices/cayenne.log

Here is the file. Hope this help in any possible ways.

cayenne.log (312.4 KB)

will check what is the issue, meanwhile did you used the code that i send?

Yes, I did try your coding but there is an error, which is the statement below.

Traceback (most recent call last):
File “/home/pi/Documents/turbiditycayenne.py”, line 2, in
import cayenne.client
ModuleNotFoundError: No module named ‘cayenne’

I thought everything include cayenne lib are already included during the installation. Did a bit of google-ing but still can’t find any solution.

try this:

git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python
cd Cayenne-MQTT-Python
python3 setup.py install

then run the above code with:

python3 file_name.py

Thank you for the fast reply and assist. I really appreciate it!
Good news and bad news.
Good news is the cayenne module error fixed. Bad news is,

Traceback (most recent call last):
File “/home/pi/turbiditycayenne.py”, line 13, in
bus = smbus.SMBus(1)
FileNotFoundError: [Errno 2] No such file or directory

Is this error related to the i2c? I’ve tried sudo pip3 install smbus but that doesn’t fix it.

go through this post https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=97314

Super super good news! Everything suddenly work perfectly including the pcf8591 plugin that you gave. Now I have both the widgets and the BYOT MQTT connection. The problem was with the i2c lib. I somehow managed to fix it.
Thank you so much sir! Can’t thank you enough for helping me solve my problems.
But I do have 1 more question regarding the output data.

From the coding you first gave me:

while True:
client.loop()
if (time.time() > timestamp + 10):
bus.write_byte(address,A0)
value = bus.read_byte(address)
client.virtualWrite(1, value)
timestamp = time.time()
i = i+1

Can I add in this code to display the data from the function?

print("AOUT:%1.3f " %(value*5.0/255))

glad to hear it is working now. you can add the code but you will need to pass it in the client.virtualWrite(1, value)
Also, don’t use both the device. delete one and keep the other.

Alright, noted! Probably going to use the plugin as it is more convenient.
Once again, thank you so much sir for the guide. Problems all solved.
Hope you have a wonderful day ahead. :smile:

1 Like