Error posting data to my server



  • Hi

    I'm using this code to post a json to my server. In Arduino we use HTTP client using this variables:

    char server[] = "********.com";
    char path[] = "/api/v2/gprs";
    int port = 11880;

    In the request library I changed the default port 80 in HTTP to 11880
    urequest.py

    def request(method, url, data=None, json=None, headers={}, stream=None):
        try:
            proto, dummy, host, path = url.split("/", 3)
        except ValueError:
            proto, dummy, host = url.split("/", 2)
            path = ""
        if proto == "http:":
            port = 11880
        elif proto == "https:":
            import ussl
            port = 443
        else:
            raise ValueError("Unsupported protocol: " + proto)
    

    main.py

    import os
    import pycom
    import machine
    import time
    import ujson
    import urequests
    from network import WLAN
    wlan = WLAN(mode=WLAN.STA)
    # Configure first UART bus to see the communication on the pc
    uart = machine.UART(0, 115200)
    
    # Configure second UART bus on pins P3(TX1) and P4(RX1)
    uart1 = UART(1, baudrate=115200) # 9600 baudrate also causes the same problem
    # Init WLAN
    try:
      wlan.connect(ssid="******", auth=(WLAN.WPA2, "*********"))
      while not wlan.isconnected():
        time.sleep_ms(50)
        print(wlan.ifconfig())
    
      print("WLAN connection on ...")
    except:
      print("[WLAN] try() (except). reset board.")
    
    data_set = {"dot":"gsm001","data":{"5":0,"6":999,"7":0,"14":3.0832,"31":0,"32":0,"33":0,"35":22,"38":300,"39":-50,"40":300,"42":300}}
    jsondump= ujson.dumps(data_set)
    urequests.request("POST","http://********.com/api/v2/gprs",data = jsondum, None, {"Content-Type": "application/json"},None).text
    

    but I got the following error:

    WiFi connection established
    MQTT ERROR! function got multiple values for argument 'user'
    ERROR! Could not connect to Pybytes!
    Pybytes configuration read from /flash/pybytes_config.json
    Traceback (most recent call last):
      File "main.py", line 27, in <module>
    SyntaxError: non-keyword arg after keyword arg
    Pycom MicroPython 1.20.2.r2 [v1.11-3a7776d] on 2020-11-23; GPy with ESP32
    

    Any idea what I'm doing wrong? I'm a newbie in pycom code, sorry

    thank's



  • Hi @jcaron
    WOW!!! thanks now is working perfect!!!

    Thanks again!!!



  • @ecabanas If you don't use Pybytes you probably want to disable it with pycom.pybytes_on_boot(False) to avoid confusing messages.

    The issue is that you have a mix of arguments with explicit names (name =) and without in your call to urequests.request.

    Either use names for all args (method="POST", url="...", data=jsondum, json=None, headers={"Content-Type": "application/json"}, stream=None — in which case you can actually skip those where your argument matches the default) or don't use any and just respect the order (i.e. just remove data=).


Log in to reply
 

Pycom on Twitter