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

Pi Pico W Expansion Board: Basic Emon CMS Client API Call #14

Open
VincentSaelzler opened this issue Jul 18, 2023 · 0 comments
Open

Pi Pico W Expansion Board: Basic Emon CMS Client API Call #14

VincentSaelzler opened this issue Jul 18, 2023 · 0 comments

Comments

@VincentSaelzler
Copy link

Is this line just a sample/placeholder, or is it actually supposed to send valid requests to Emon CMS?

req = "https://emoncms.org/input/post?apikey=apikey&node=emontx4pico&data="+data

Link to File

I am asking because it seems to send back a 200 response with the content being ok regardless of whether I send the correct or incorrect API key.

I am working on a more robust micropython client, and have gotten to the point of actually making the API call to Emon CMS.

from machine import UART, Pin
from time import sleep

# UART configuration ---------------------------------------------------------
# default timeout for uart.any() to decide if a new line is coming is 0 (ms).
# increase to 1 (ms) to give emon time to send the next line.
# sending output to uart1 is optional - for debugging
# required to debug because if the micro-usb cable is plugged to pico
# the power cannot simultaneously be connected to emon (3.3V vs 5V)

uart0 = UART(0, timeout=1) # pins GP0/GP1
uart1 = UART(1)  # pins GP4/GP5


def blink_led(times_to_blink):
    # assumptions
    led = Pin('LED', Pin.OUT)
    a_brief_moment = .1
    # start with the led off
    led.value(0)
    for _ in range(times_to_blink):
        # turn on and wait
        led.toggle()
        sleep(a_brief_moment)
        # turn back off and wait
        led.toggle()
        sleep(a_brief_moment)


def process_line():
    line = read_line()
    trimmed_line = line.strip()

    # for now just write output to console and outgoing uart
    uart1.write(line)
    print(trimmed_line)

    # trimmed line looks something like:
    # MSG:540,Vrms:240.00,P1:2,P2:6,P3:6,P4:5,P5:5,P6:4,E1:3,E2:10,E3:10,E4:8,E5:8,E6:7,pulse:0
    # TODO: Make a correct API call
    
def read_line():
    try:
        # get input until a newline character is reached (or the uart's configured timeout_char expires)
        incoming_line = uart0.readline()

        if incoming_line is None:
            # blink thrice to signal an error
            blink_led(3)
            return('Readline Error: No Input')
        
        # return the line in string format
        return incoming_line.decode('utf-8')
    
    except UnicodeError:
        # blink thrice to signal an error
        blink_led(3)
        return('Unicode Error: '+ str(incoming_line))


def main():
    while True:
        
        # if one or more lines of incoming data are available
        if uart0.any():
            # while there is still data available
            while uart0.any():
                # process the next line of data
                process_line()
            
            # blink once to signal that line(s) came in
            blink_led(1)
        else:
            # blink twice to signal that nothing came in
            blink_led(2)

        # wait a few seconds
        sleep(2)
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

No branches or pull requests

1 participant