Kanna
Posts: 16
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 525 times

User avatar
B.Goode
Posts: 10356
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: 9464
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: 16
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: 4622
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: 10356
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: 9464
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

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

Re: help me i am getting output always zero only

Wed Apr 01, 2020 8:48 am

i dont have link of the product but
i am uploading the sensor image which i am using
Attachments
Webp.net-compress-image (3).jpg
Webp.net-compress-image (3).jpg (177.39 KiB) Viewed 347 times

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

Re: help me i am getting output always zero only

Wed Apr 01, 2020 8:55 am

There sensor works by sending on/off on the DO wire to you Gpio.
That is it, you get no data from it, it acts like a switch.

You set the setpoint (point that it gives on/off) with the blue square/white circle variable resistor.

For that look at using Gpiozero and a button, as they are similar.


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

Re: help me i am getting output always zero only

Wed Apr 01, 2020 9:40 am

as per the sensor i am using it has both analog and digital outputs
The Analogue output gives a variable voltage reading that allows you to estimate the moisture content of the soil (using some maths!). The digital output gives you a simple "on" or "off" when the soil moisture content is above a certain value. The value can be set or calibrated using an adjustable on board potentiometer. In this case, we just want to know either "Yes, the plant has enough water" or "No, the plant needs watering!", so we'll be using the digital out. We'll set this value later at which you want the sensor to "trigger" a notification.

in order to get values i have to use analog pin but raspberry pi is only having digital pins so i have to use ADC

thank for ur suggestions

User avatar
davidcoton
Posts: 5026
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: help me i am getting output always zero only

Wed Apr 01, 2020 10:00 am

You need to initialise pin 5 with a pull-up.
Signature retired

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

Re: help me i am getting output always zero only

Wed Apr 01, 2020 10:02 am

To read analogue output of your sensor you need to connect it to the MCP3008 like this.

NOTE... you need to use the AO output.

Image

you can then use this code to read the voltage value from the MCP3008

Code: Select all

#!/usr/bin/python
import spidev
import RPi.GPIO as GPIO
import time
import sys

spi = spidev.SpiDev()  # create spi object
spi.open(0, 0)  # open spi port 0, device (CS) 0, for the MCP8008
spi.max_speed_hz=1000000

def readadc(adcnum):  # read out the ADC
    if ((adcnum > 7) or (adcnum < 0)):
        return -1
    r = spi.xfer2([1, (8 + adcnum) << 4, 0])
    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout


while True:
    v = readadc(0) * (3.3 / 1023.0)  # Convert value to voltage

   

    print v
    time.sleep(1)
    
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: help me i am getting output always zero only

Wed Apr 01, 2020 11:55 am

As above, but I would try Gpiozero module again, they have you covered no matter your MCPxxx used

https://gpiozero.readthedocs.io/en/stab ... erters-adc

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

Re: help me i am getting output always zero only

Tue Apr 07, 2020 8:21 am

Anyone suggest me a motor or alternative for smart irrigation purpose
1.product link
2.interfacing code
3.connections

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

Re: help me i am getting output always zero only

Tue Apr 07, 2020 9:15 am

Kanna wrote:
Tue Apr 07, 2020 8:21 am
Anyone suggest me a motor or alternative for smart irrigation purpose
1.product link
2.interfacing code
3.connections
A motor to do what ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Troubleshooting”