Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

msg.payload output is zero #293

Closed
shuynh84 opened this issue Mar 30, 2017 · 5 comments · May be fixed by #912
Closed

msg.payload output is zero #293

shuynh84 opened this issue Mar 30, 2017 · 5 comments · May be fixed by #912

Comments

@shuynh84
Copy link

I have a MCP3008 connected to a ACS712 current sensor. I have everything connected correctly. It would output around 500 like its supposed to when nothing is connected to the current sensor. But when I run a python script on the raspberry pi to log data for the current sensor, msg.payload would display zero for output. Would you happen to know why it is doing this? Thanks!

@dceejay
Copy link
Member

dceejay commented Apr 3, 2017

Hi,
can you provide a bit more info ? Are you saying it works when using python - Ie 0 when no current and a value when there is. And then when using Node-RED (not at the same time) - it reads 500 for no input ?

@shuynh84
Copy link
Author

shuynh84 commented Apr 3, 2017

I've used this guide for connections:

https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008

this is the code I'm using for my current sensor in python:

#!/usr/bin/env python

# Written by Limor "Ladyada" Fried for Adafruit Industries, (c) 2015
# This code is released into the public domain

import time
import os
import RPi.GPIO as GPIO
import sys
import datetime
import time
import csv

from csv import reader

# Setup the RPI pin with BCM labeling
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)

# Power management registers
power_mgmt_1 = 0x6b

# Turn on relay (pin 18), HIGH means "ON"
GPIO.output(18,GPIO.HIGH)
DEBUG = 1

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
        if ((adcnum > 7) or (adcnum < 0)):
                return -1
        GPIO.output(cspin, True)

        GPIO.output(clockpin, False)  # start clock low
        GPIO.output(cspin, False)     # bring CS low

        commandout = adcnum
        commandout |= 0x18  # start bit + single-ended bit
        commandout <<= 3    # we only need to send 5 bits here
        for i in range(5):
                if (commandout & 0x80):
                        GPIO.output(mosipin, True)
                else:
                        GPIO.output(mosipin, False)
                commandout <<= 1
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)

        adcout = 0
        # read in one empty bit, one null bit and 10 ADC bits
        for i in range(12):
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
                adcout <<= 1
                if (GPIO.input(misopin)):
                        adcout |= 0x1

        GPIO.output(cspin, True)
        
        adcout >>= 1       # first bit is 'null' so drop it
        return adcout

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8

# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)

# 10k trim pot connected to adc #0
potentiometer_adc = 0;

last_read = 0       # this keeps track of the last potentiometer value
tolerance = 5       # to keep from being jittery we'll only change
                    # volume when the pot has moved more than 5 'counts'
print('% Voltage')
while True:
        # read the analog pin
        trim_pot = readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)

        print (trim_pot)
        #Write Temp data to csv file
        mydate = datetime.datetime.now()
        with open("/home/pi/Desktop/Currentdata.txt", "a+") as tempFile:
            tempFileWriter = csv.writer(tempFile)
            tempFileWriter.writerow([mydate,trim_pot])

        if  (abs(trim_pot) > 600):
                GPIO.output(18,GPIO.LOW)
                print("Threshold Exceeded!")
                sys.exit()
        #set_volume = abs((trim_pot) / 10.24)  # for potentiometer             
        #set_volume = abs((trim_pot-512) / 10.24)*800 #for mA current sensor # convert 10bit adc0 (0-1024) trim pot read into 0-100 volume level
        #set_volume = round(set_volume)          # round out decimal value
        #set_volume = int(set_volume)            # cast volume as integer

        #print (set_volume)

        #print ('Voltage = {volume}%'.format(volume = set_volume))

        # hang out and do nothing for a half second
        state = GPIO.input(18)
        if  (state is 0):
                sys.exit()
        time.sleep(2)

GPIO.cleanup()

In node-red I've selected channel 0 and CE0 but getting zero for output. Before, it was outputting a range from 490-520 which is what the code was giving me. I've now connected it to a potentiometer to test but getting same result of zero no matter which way to turn the potentiometer.

@shuynh84
Copy link
Author

shuynh84 commented Apr 4, 2017

It's just outputting zero all the time instead of a number around 500 even without running another script. Could this be affected by other nodes I'm trying to run? like raspi-io, wiring-pi, or johnny five?

@shuynh84
Copy link
Author

shuynh84 commented Apr 5, 2017

think i found my answer:

I think I found my answers:

"Here's a guess: During the initialization phase of a Johnny-Five/Raspi-IO application all available GPIOs on the Raspberey Pi are automatically configured as GPIO outputs, irrespective of whether the Johnny-Five/Raspi-IO application actually use those GPIOs or not. On the other hand, node-red-node-pi-mcp3008 requires that the SPI specific GPIOs be configured as SPI GPIOs, not as GPIOs outputs. They were actually configured as SPI GPIOs at boot time but the fact that Johnny-Five/Raspi-IO reconfigures everything to be a GPIO output then breaks node-red-node-pi-mcp3008."

Here's the link to the thread:

nebrius/raspi-io#76

@dceejay
Copy link
Member

dceejay commented Apr 7, 2017

closing as it's not a problem with the node, just config and conflict with other device errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants