Adafruit_MCP3008 SPI_PORT = 0 SPI_DEVICE = 0 mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) while True: value = 0 # The read_adc function will get the value of the specified channel value = mcp.read_adc(0) temp = value * 100 * 3.3 / 1023 # Print the ADC values. print('Temp: {:.2f}°C'.format(temp)) time.sleep(0.5) 5 . 5
Adafruit_MCP3008 import paho.mqtt.publish as publish SPI_PORT = 0 SPI_DEVICE = 0 mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) host = 'localhost' while True: value = 0 # The read_adc function will get the value of the specified channel value = mcp.read_adc(0) temp = value * 100 * 3.3 / 1023 temp_json = json.dumps({'temperature': '{0:.2f}'.format(temp)}) # Print the ADC values. publish.single(topic='demo/temp', payload=temp_json, hostname=host) time.sleep(0.5) 6 . 3