@nihal.behal
Thank you for your patience on this. I wanted to share what I made too, though Iām sure its very similar to what @adam linked.
First, I just wanted to make sure I could read values coming off the MCP3008 chip. For this I followed the instruction at this page: MCP3008 | Raspberry Pi Analog to Digital Converters | Adafruit Learning System in order to print the output for each of the 8 MCP3008 pins to my command line. If you followed the wiring instructions in the Cayenne MCP3008 tutorial, you can use the āHardware SPIā option in that article.
pi@raspberrypi:~/Adafruit_Python_MCP3008/examples $ sudo python simpletest.py
Reading MCP3008 values, press Ctrl-C to quit...
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---------------------------------------------------------
| 797 | 642 | 766 | 47 | 51 | 55 | 67 | 103 |
| 797 | 643 | 766 | 49 | 53 | 57 | 69 | 106 |
| 798 | 647 | 768 | 50 | 54 | 58 | 70 | 106 |
| 799 | 649 | 769 | 46 | 53 | 56 | 68 | 102 |
| 799 | 649 | 769 | 46 | 51 | 53 | 62 | 92 |
| 799 | 646 | 768 | 27 | 28 | 28 | 31 | 43 |
| 797 | 642 | 767 | 8 | 9 | 8 | 8 | 10 |
| 797 | 642 | 766 | 2 | 0 | 0 | 0 | 0 |
| 797 | 645 | 767 | 0 | 0 | 0 | 0 | 0 |
| 800 | 642 | 767 | 36 | 0 | 0 | 0 | 0 |
| 797 | 642 | 767 | 2 | 2 | 1 | 0 | 4 |
| 797 | 644 | 767 | 0 | 0 | 0 | 0 | 0 |
| 798 | 647 | 768 | 0 | 0 | 0 | 0 | 1 |
| 799 | 649 | 769 | 1 | 1 | 1 | 1 | 4 |
| 799 | 649 | 769 | 3 | 4 | 4 | 6 | 12 |
| 798 | 646 | 767 | 21 | 24 | 28 | 36 | 59 |
| 797 | 642 | 765 | 38 | 43 | 47 | 59 | 93 |
| 797 | 641 | 765 | 47 | 51 | 56 | 68 | 104 |
| 797 | 643 | 766 | 50 | 54 | 58 | 70 | 107 |
| 798 | 646 | 767 | 49 | 53 | 57 | 70 | 108 |
| 798 | 648 | 768 | 49 | 52 | 56 | 67 | 100 |
In my case, since I donāt have these Hall effect current sensors, I have 3 photoresistors on MCP3008 pins 0, 1 and 2, and you can see the raw value they generate in that table. I have no sensor on pins 3-7, the numbers you see there are likely electrical noise.
I suggest you do this part first, to see if youāre getting values for your sensors on the pins where they are connected. If you arenāt, it could be a wiring issue with the MCP3008 or your sensors themselves.
Next, I installed the Cayenne Python MQTT Library with:
git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python
cd Cayenne-MQTT-Python
python setup.py install
Once thatās installed, there will be an example MQTT Client in an examples subfolder. The example client will create an MQTT device on your Cayenne Dashboard after you plug in your values for these placeholders:
MQTT_USERNAME = "MQTT_USERNAME"
MQTT_PASSWORD = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"
You can get these values by going to Add New > Device/Widget > Bring Your Own Thing. The example client will also publish a handful of dummy sensor data to show how writing sensor values to the Cayenne dashboard works. Since you want actual values here, I just merged in the part of the Adafruit simpletest.py file that reads the 3 pins that my sensors are on, and publishes to widgets on channels 1, 2 and 3. Here is my completed file:
#!/usr/bin/env python
import cayenne.client
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
# Hardware SPI configuration:
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = "MQTTUSERNAME"
MQTT_PASSWORD = "MQTTPASS"
MQTT_CLIENT_ID = "CLIENTID"
# 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)
i=0
timestamp = 0
while True:
client.loop()
if (time.time() > timestamp + 10):
client.virtualWrite(1, mcp.read_adc(0))
client.virtualWrite(2, mcp.read_adc(1))
client.virtualWrite(3, mcp.read_adc(2))
timestamp = time.time()
i = i+1
Presuming you were able to read values using simpletest.py
, this should work for you too. Just remember that you need to plug in your own MQTT Username/Password/Client ID, and if your sensors arenāt on pins 0, 1 and 2 (or if you have more or less than 3 sensors) youāll need to edit the client.virtualWrite()
statements to match what MCP3008 channels you want read.
One last thing: those values are just the raw readings coming off the ADC pins for each of my photoresistors, and sent to the Cayenne dashboard as generic numeric values with no data type. If I wanted them to be converted to lux (or in your case, volts), Iād have to write a bit of additional code to convert each reading to those units. If you consult the documentation for your sensors, you should be able to find how they encode their data and make this conversion. I can help you with code to do that, but I would need the specifics of what reading from the ADC = however many volts.
Phew . I hope all of this makes sense, tried to make it easy for a non-coder. If you have any questions let me know!