File "SaveData6.py", line 48, in <module>
conn = httplib.HTTPconnection("api.thingspeak.com:80")
AttributeError: 'module' object has no attribute 'HTTPconnection'
how to solve this error?
Code: Select all
import spidev
import datetime
import RPi.GPIO as GPIO
from time import sleep
import httplib, urllib, time
GPIO.setmode(GPIO.BOARD)
# Open up SPI bus
spi = spidev.SpiDev()
spi.open(0,1)
# Initialize Sensor
SoilSensor = 5
Pump = 22
sleepTime = 1
# Setup the pin that pump connected to
GPIO.setup(Pump, GPIO.OUT)
#Declare API Key
apikey = 'xxx'
def getReading(channel):
# Get Raw Data from chip
rawData = spi.xfer([1, (8 + channel) << 4, 0])
# Process Data to Understandable
processedData = ((rawData[1]&3) <<8) + rawData[2]
return processedData
def convertVoltage(bitValue, decimalPlaces=2):
voltage = (bitValue * 3.3) / float(1023)
voltage = round(voltage, decimalPlaces)
return voltage
try:
while True:
delta = datetime.timedelta(minutes=1)
next_time = datetime.datetime.now() + delta
SoilData = getReading(SoilSensor)
SoilVoltage = convertVoltage(SoilData)
params = urllib.urlencode({'field': SoilData, 'key': apikey})
headers = {"Contect-type": "application/x-www-form-urlencode","Accept": "text/plain"}
conn = httplib.HTTPconnection("api.thingspeak.com:80")
while True:
#Print Values
print("Soil Moisture bitValue = {} ; Voltage = {} V".format(SoilData, SoilVoltage))
sleep(sleepTime)
dt = datetime.datetime.now()
#Save Result Every One Hour
if dt > next_time:
print("--------------- write file -----------------------")
file = open("/home/pi/Desktop/result/result.txt", "a")
file.write("{} ; Soil Moisture bitValue = {} ; Voltage = {} V\n".format(dt.strftime("%d-%m-%Y %H:%M:%S"),SoilData, SoilVoltage))
file.close()
next_time = dt + delta
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()
if SoilData < 40:
GPIO.output (Pump, True)
else:
GPIO.output (Pump, False)
finally:
# Reset the GPIO Pins to a safe state
GPIO.output (Pump, False)
GPIO.cleanup()