Kanna
Posts: 3
Joined: Wed Feb 12, 2020 2:51 pm

help me i am getting output always zero only

Wed Mar 25, 2020 8:18 am

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()

Image
Attachments
Webp.net-compress-image (2).jpg
Webp.net-compress-image (2).jpg (139.93 KiB) Viewed 175 times

User avatar
B.Goode
Posts: 9560
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: help me i am getting output always zero only

Wed Mar 25, 2020 8:26 am

Seriously?



If you hope to get help from the volunteer helpers in these forums some facts would be useful!


What model of RPi board?

What Operating System is it running?

What specific sensor?

How is it wired to your RPi? (A clear, focused, detailed photo of the cabling would be useful.)

pcmanbob
Posts: 8295
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: help me i am getting output always zero only

Wed Mar 25, 2020 9:23 am

The first this is you are missing the spi.max_speed line in your program.

Code: Select all

# Open up SPI bus
spi = spidev.SpiDev()
spi.open(0,1)
spi.max_speed_hz=1000000

you need to add it like this.

If you still get a zero result then you need to give us more information as @B.Goode suggested above.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Kanna
Posts: 3
Joined: Wed Feb 12, 2020 2:51 pm

Re: help me i am getting output always zero only

Thu Mar 26, 2020 6:24 am

i added the new line but still there is no change in output

i am using raspberry pi 3 model
soilmoisture sensor

vcc=pin 2
gnd=pin 39
d0=pin 29(gpio 5)

i used following commands in terminal
enabled spidev

sudo apt-get install git python-dev


git clone git://github.com/doceme/py-spidev


cd py-spidev/


sudo python setup.py install


sudo apt-get install python-numpy

User avatar
bensimmo
Posts: 4302
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: help me i am getting output always zero only

Thu Mar 26, 2020 7:59 am

You have nothing wired up to SPI?
Just 5V and Ground and a random Gpio pin.

A link to the product you have bought, there are probably many soil moisture sensors.

User avatar
B.Goode
Posts: 9560
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: help me i am getting output always zero only

Thu Mar 26, 2020 10:40 am

Kanna wrote:
Thu Mar 26, 2020 6:24 am
i added the new line but still there is no change in output

i am using raspberry pi 3 model
soilmoisture sensor

vcc=pin 2
gnd=pin 39
d0=pin 29(gpio 5)

i used following commands in terminal
enabled spidev

sudo apt-get install git python-dev


git clone git://github.com/doceme/py-spidev


cd py-spidev/


sudo python setup.py install


sudo apt-get install python-numpy



I tried to help by suggesting a list of facts that might be relevant to volunteer helpers able to assist. Time is too precious at the moment to be playing guessing games. I am withdrawing from the discussion.

pcmanbob
Posts: 8295
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: help me i am getting output always zero only

Thu Mar 26, 2020 11:02 am

Kanna wrote:
Thu Mar 26, 2020 6:24 am

vcc=pin 2
gnd=pin 39
d0=pin 29(gpio 5)
Well you should not be powering your sensor with 5V unless it specifically says that the output will be 3V when powered by 5V,

and you don't have a sensor that supports SPI by the look of it as you would have 4 data connections as well as VCC/ground.

Please post a link to the sensor you have, if you want people to be able to help you.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Troubleshooting”