Python Forum
Values not updating for restful call - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Values not updating for restful call (/thread-32884.html)



Values not updating for restful call - boomramada - Mar-13-2021

Hi, I got following code to get the values from GPOI from raspberry pi then post it on restful.
Code is working but I noticed, values are not updating to the restful.

Look like value from GPOI not getting updated to moist[] on service call.

It would be good if someone can put me on correct direction.

import flask
import spidev
import time
import sys
import RPi.GPIO as GPIO
from flask import request, jsonify

# GPOI settings
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 1000000

def readadc(adcnum):
        r = spi.xfer2([1,8+adcnum<<4,0])
        adcout = ((r[1]&3)<<8)+r[2]
        return adcout

# server settings
app = flask.Flask(__name__)
app.config["DEBUG"] = False

# message settings
try:
        moist = [
        {'id': 0,
        'volt': readadc(0)},
        {'id': 1,
        'volt': readadc(1)},
        {'id': 2,
        'volt': readadc(2)},
        {'id': 3,
        'volt': readadc(3)}
        ]
except KeyboardInterrupt:
        GPIO.cleanup()

@app.route('/', methods=['GET'])
def home():
    return "<h1>Home</h1><p>This site is a prototype API.</p>"

# A route to return all of the available entries in our catalog.
@app.route('/moist', methods=['GET'])
def api_all():
    return jsonify(moist)


#app.run()
app.run(host="0.0.0.0", port=5050) # or host=127.0.0.1, depends on your needs