Page 3 of 3

Re: Write data just when the output changes

Posted: Thu Jul 23, 2020 4:34 pm
by pcmanbob
Having looked at the datasheet for the sensor you are using I can see 2 possible problems.

1 . It appears to use an npn open collector output so have you configured the connection to the pi to take this in to consideration as you will need a 3.3V pull up on the output.

2. If you have the correct wiring configuration the your sensor will output a 1 when no object is detected and a 0 when an object is detected. So your could will need to be adjusted to work with these inputs.

Re: Write data just when the output changes

Posted: Fri Jul 24, 2020 1:21 am
by Anugerah Nuri Satria
when i started to excute this code in terminal, the output always like this


Proses Tapper Roll ke 1
08:18:27
('time between detections', 0.0, 'seconds')

it is first sense but i do nothing for that

Re: Write data just when the output changes

Posted: Fri Jul 24, 2020 8:18 am
by pcmanbob
That is probably because of the 2 problems I mentioned in my last post.

As we don't know how your sensor is connected to your pi and if you have the required pull-up to 3.3V , we cant really help you .

I can only offer suggestions on the information supplied, I suggest you read the data sheet for your sensor to understand how to use / connect it correctly with the pi.

either that or you need to post pictures / diagrams showing how its connected so we can understand and make suggestions on how you should do it.

Re: Write data just when the output changes

Posted: Mon Jul 27, 2020 7:27 am
by Anugerah Nuri Satria
thanks in advanced sir, you help me a lot!

Re: Write data just when the output changes

Posted: Mon Jul 27, 2020 7:34 am
by Anugerah Nuri Satria
I have another problem Sir.

When I try to run MAX6675 program that can work in the past, now i always get this in terminal

Traceback (most recent call last):
File "simple_test.py", line 50, in <module>
sensor = MAX6675.MAX6675(CLK, CS, DO)
File "build/bdist.linux-armv7l/egg/MAX6675/MAX6675.py", line 45, in __init__
File "/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.4-py2.7.egg/Adafruit_GPIO/GPIO.py", line 420, in get_platform_gpio
ImportError: No module named Adafruit_BBIO.GPIO

This is my code

Code: Select all

#!/usr/bin/python
# coding: utf8

# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Can enable debug output by uncommenting:
#import logging
#logging.basicConfig(level=logging.DEBUG)
import requests
import time
import csv
import sys
import Adafruit_GPIO.SPI as SPI
import MAX6675.MAX6675 as MAX6675
import json
csvfile = "temperature.csv"

API_ENDPOINT = "http://192.168.0.179:58103/saveheatingtemp"



# Define a function to convert celsius to fahrenheit.
def c_to_f(c):
        return c * 9.0 / 5.0 + 32.0


# Raspberry Pi software SPI configuration.
CLK = 25
CS  = 24
DO  = 18
sensor = MAX6675.MAX6675(CLK, CS, DO)

# Raspberry Pi hardware SPI configuration.
SPI_PORT   = 0
SPI_DEVICE = 0
sensor = MAX6675.MAX6675(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
mesinno = '2-04-1-QUE'
mydatadescription = 'data temperature'

# Loop printing measurements every second.
#print 'Press Ctrl-C to quit.'
while True:
    try:
        temp = sensor.readTempC()
        timeC = time.strftime("%I")+':' +time.strftime("%M")+':'+time.strftime("%S")
        data = [temp, timeC]
        datajson = {'data':temp,'unit':'Celcius'}

    #contohjson = {'dataqty' :10,'dataqtyunit':'pcs','datatemperatur':50,'datatemperaturunit':'celcius'}

        print 'Thermocouple Temperature: {0:0.1F}°C / {1:0.1F}°F'.format(temp, c_to_f(temp))

    #sending post request and saving response as response object
        r = requests.post(url = API_ENDPOINT, params=[('mesinno',mesinno),('datadescription',mydatadescription),('data',json.dumps(datajson))])
        print r.text

    except Exception as exc:
        print('[!!!] {err}'.format(err=exc))
        continue





    #time.sleep(0.5) # update script every 60 seconds


    with open(csvfile, "a") as output:
        writer = csv.writer(output, delimiter=",", lineterminator = '\n')
        writer.writerow(data)
        time.sleep(30)


#extracting response text
 #pastebin_url = r.text
 #print("The pastebin URL is:%s"%pastebin_url)


What should I do to fix it?

Re: Write data just when the output changes

Posted: Mon Jul 27, 2020 8:49 am
by pcmanbob
The error message tells you what's wrong ImportError: No module named Adafruit_BBIO.GPIO

so either you have not installed the module or you have installed it for the wrong version of python.

Note this library is written for the Beaglebone not the raspberry pi.

Re: Write data just when the output changes

Posted: Mon Jul 27, 2020 10:52 am
by B.Goode
Anugerah Nuri Satria wrote:
Mon Jul 27, 2020 7:34 am
I have another problem Sir.

When I try to run MAX6675 program that can work in the past, now i always get this in terminal

Traceback (most recent call last):
File "simple_test.py", line 50, in <module>
sensor = MAX6675.MAX6675(CLK, CS, DO)
File "build/bdist.linux-armv7l/egg/MAX6675/MAX6675.py", line 45, in __init__
File "/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.4-py2.7.egg/Adafruit_GPIO/GPIO.py", line 420, in get_platform_gpio
ImportError: No module named Adafruit_BBIO.GPIO

This is my code

Code: Select all

#!/usr/bin/python
# coding: utf8

# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Can enable debug output by uncommenting:
#import logging
#logging.basicConfig(level=logging.DEBUG)
import requests
import time
import csv
import sys
import Adafruit_GPIO.SPI as SPI
import MAX6675.MAX6675 as MAX6675
import json
csvfile = "temperature.csv"

API_ENDPOINT = "http://192.168.0.179:58103/saveheatingtemp"



# Define a function to convert celsius to fahrenheit.
def c_to_f(c):
        return c * 9.0 / 5.0 + 32.0


# Raspberry Pi software SPI configuration.
CLK = 25
CS  = 24
DO  = 18
sensor = MAX6675.MAX6675(CLK, CS, DO)

# Raspberry Pi hardware SPI configuration.
SPI_PORT   = 0
SPI_DEVICE = 0
sensor = MAX6675.MAX6675(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
mesinno = '2-04-1-QUE'
mydatadescription = 'data temperature'

# Loop printing measurements every second.
#print 'Press Ctrl-C to quit.'
while True:
    try:
        temp = sensor.readTempC()
        timeC = time.strftime("%I")+':' +time.strftime("%M")+':'+time.strftime("%S")
        data = [temp, timeC]
        datajson = {'data':temp,'unit':'Celcius'}

    #contohjson = {'dataqty' :10,'dataqtyunit':'pcs','datatemperatur':50,'datatemperaturunit':'celcius'}

        print 'Thermocouple Temperature: {0:0.1F}°C / {1:0.1F}°F'.format(temp, c_to_f(temp))

    #sending post request and saving response as response object
        r = requests.post(url = API_ENDPOINT, params=[('mesinno',mesinno),('datadescription',mydatadescription),('data',json.dumps(datajson))])
        print r.text

    except Exception as exc:
        print('[!!!] {err}'.format(err=exc))
        continue





    #time.sleep(0.5) # update script every 60 seconds


    with open(csvfile, "a") as output:
        writer = csv.writer(output, delimiter=",", lineterminator = '\n')
        writer.writerow(data)
        time.sleep(30)


#extracting response text
 #pastebin_url = r.text
 #print("The pastebin URL is:%s"%pastebin_url)


What should I do to fix it?


You could try asking the commercial organisation (Adafruit) whose software you are using to support their product.

My hunch is that you have an outdated version of the library which is misidentifying your RPi board as not in fact being an RPi and then going off to treat it as some other hardware for which - quite reasonably - you have not installed the required modules.

The error message tells you exactly which part of the code to check.

Re: Write data just when the output changes

Posted: Tue Jul 28, 2020 12:50 am
by Anugerah Nuri Satria
I dont know exactly what line makes that code wrong sir. I use 2.7.16 version of phyton

Re: Write data just when the output changes

Posted: Tue Jul 28, 2020 7:23 am
by B.Goode
Anugerah Nuri Satria wrote:
Tue Jul 28, 2020 12:50 am
I dont know exactly what line makes that code wrong sir. I use 2.7.16 version of phyton


This message from the developer/maintainer of the Adafruit Python GPIO Library seems to be relevant -
!!!Deprecation Warning!!!
This library has been deprecated in favor of our python3 Blinka library. We have replaced all of the libraries that use this repo with CircuitPython libraries that are Python3 compatible, and support a wide variety of single board/linux computers!

Visit https://circuitpython.org/blinka for more information

CircuitPython has support for almost 200 different drivers, and a as well as FT232H support for Mac/Win/Linux!

!!!Deprecation Warning!!!
Ref: https://github.com/adafruit/Adafruit_Python_GPIO



Only early Raspberry Pi boards are correctly detected by the library - anything later will incorrectly be assumed to be some other hardware such as a BeagleBoneBlack, hence the error you receive.



The error you report refers to the function get_platform_gpio()

Code: Select all

def get_platform_gpio(**keywords):
    """Attempt to return a GPIO instance for the platform which the code is being
    executed on.  Currently supports only the Raspberry Pi using the RPi.GPIO
    library and Beaglebone Black using the Adafruit_BBIO library.  Will throw an
    exception if a GPIO instance can't be created for the current platform.  The
    returned GPIO object is an instance of BaseGPIO.
    """
    plat = Platform.platform_detect()
    if plat == Platform.RASPBERRY_PI:
        import RPi.GPIO
        return RPiGPIOAdapter(RPi.GPIO, **keywords)
    elif plat == Platform.BEAGLEBONE_BLACK:
        import Adafruit_BBIO.GPIO
        return AdafruitBBIOAdapter(Adafruit_BBIO.GPIO, **keywords)
    elif plat == Platform.MINNOWBOARD:
        import mraa
        return AdafruitMinnowAdapter(mraa, **keywords)
    elif plat == Platform.JETSON_NANO:
        import Jetson.GPIO
        return RPiGPIOAdapter(Jetson.GPIO, **keywords)
    elif plat == Platform.UNKNOWN:
        raise RuntimeError('Could not determine platform.')
And the failing detection in Platform.platform_detect() is

Code: Select all

def pi_version():
    """Detect the version of the Raspberry Pi.  Returns either 1, 2 or
    None depending on if it's a Raspberry Pi 1 (model A, B, A+, B+),
    Raspberry Pi 2 (model B+), or not a Raspberry Pi.
    """
    # Check /proc/cpuinfo for the Hardware field value.
    # 2708 is pi 1
    # 2709 is pi 2
    # 2835 is pi 3 on 4.9.x kernel
    # Anything else is not a pi.
    with open('/proc/cpuinfo', 'r') as infile:
        cpuinfo = infile.read()
    # Match a line like 'Hardware   : BCM2709'
    match = re.search('^Hardware\s+:\s+(\w+)$', cpuinfo,
                      flags=re.MULTILINE | re.IGNORECASE)
    if not match:
        # Couldn't find the hardware, assume it isn't a pi.
        return None
    if match.group(1) == 'BCM2708':
        # Pi 1
        return 1
    elif match.group(1) == 'BCM2709':
        # Pi 2
        return 2
    elif match.group(1) == 'BCM2835':
        # Pi 3 / Pi on 4.9.x kernel
        return 3
    else:
        # Something else, not a pi.
        return None


If you wanted to persevere with this unsupported library you could possibly 'patch' the list of valid responses from /proc/cpuinfo to include the hardware you are running this code on?



I have no interest in trying to fix this deficiency for you and will not be contributing further.

Re: Write data just when the output changes

Posted: Tue Jul 28, 2020 8:04 am
by Anugerah Nuri Satria
It means I should change my sensor and follow the list of sensor that can run https://circuitpython.org/blinka ???

Re: Write data just when the output changes

Posted: Tue Jul 28, 2020 1:39 pm
by Anugerah Nuri Satria
I need your help. sir