i am trying read moisture value but
i am always getting output zero
help me out
Code: Select all
import os
import sys
import spidev
import RPi.GPIO as GPIO
import httplib
import urllib2
import time
GPIO.setmode(GPIO.BCM)
# Open up SPI bus
spi = spidev.SpiDev()
spi.open(0,1)
SoilSensor=5
#Declare API Key
apikey = '92L7QM03PXTZU0B2'
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
def main():
print("sustem ready")
URL="https://api.thingspeak.com/update?api_key=%s"%apikey
print("wait")
while True:
SoilData = getReading(SoilSensor)
SoilVoltage = convertVoltage(SoilData)
finalurl=URL+'&field3=%s&field4=%s'%(SoilData,SoilVoltage)
print(finalurl)
s=urllib2.urlopen(finalurl);
#Print Values
print("Soil Moisture bitValue = {} ; Voltage = {} V".format(SoilData, SoilVoltage))
s.close()
time.sleep(10)
if __name__=='__main__':
main()
