Skip to content

Instantly share code, notes, and snippets.

@jdc-cunningham
Last active March 9, 2018 11:20
Show Gist options
  • Save jdc-cunningham/4be0f67374e0cbe0eb18158bd2c28dab to your computer and use it in GitHub Desktop.
Save jdc-cunningham/4be0f67374e0cbe0eb18158bd2c28dab to your computer and use it in GitHub Desktop.
Example analog data grab from a solar panel with Adafruit's library on the MCP3008 with a POST request to a posttestserver endpoint
# this code is based on the Adafruit MCP3008 library being installed and the ADC being properly wired to your Raspberry Pi
# and source of analog data
# for math division
from __future__ import division
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
# for POST request
import requests
# Software SPI configuration:
CLK = 18
MISO = 23
MOSI = 24
CS = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
# Get current value from pin 0
analog_val = mcp.read_adc(0)
# multiplier
multiplier = (5 / 1024) # 5V divided by 1024 analog steps
computed_voltage = (analog_val * multiplier)
# compute current
# V = IR -> I = V/R
computed_current = (computed_voltage / 1000) # 1000 = 1K Ohm
# compute power
# P = IV
computed_power = (computed_voltage * computed_current)
print 'analog val: ' + str(analog_val) + ' voltage: ' + str(computed_voltage) + ' current: ' + str(computed_current) + ' power: ' + computed_power
# optional send to server, this is using this public service called posttestserver but v2
# you create a random string, test if it's available, if it is an endpoint will be created for you
# then you use the endpoint to send this data to
# at the time of writing this file, I got this endpoint: http://ptsv2.com/t/0436952175/post
requests.post('http://ptsv2.com/t/0436952175/post', data = {
'analog_value': analog_val,
'computed_voltage': computed_voltage,
'computed_current': computed_current,
'computed_power': computed_power
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment