urequests micropython问题(对Google表单的多个POST请求)

时间:2019-09-14 22:36:04

标签: http python-requests esp8266 esp32 micropython

我正在尝试使用带有micropython的esp8266将数据直接发送到Google Forms(不使用IFTTT等外部服务)。我已经使用过IFTTT,但目前对我没有用,我需要大于或等于100 Hz的采样率,并且您知道这超出了IFTTT的使用限制。我曾尝试制作一个RAM缓冲区,但出现一个错误,说该缓冲区超出了RAM大小(4 MB),所以这就是为什么我试图直接这样做。

尝试了一段时间后,我得到了一部分。我之所以说“部分”,是因为我必须在后请求之后随机进行一次get请求;我不知道它为什么起作用,但是它起作用(通过这种方式,我可以大约每秒或更短的时间将数据发送到Google表单)。我想问题是esp8266无法关闭与Google Forms的连接,并且在尝试执行新的后请求时会卡住,如果这是问题,我不知道如何通过其他方式解决, 有什么建议么?完整的代码在这里:

ssid = 'my_network'
password = 'my_password'

import urequests

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect(ssid, password)
        while not sta_if.isconnected():
            pass
    print('network config:', sta_if.ifconfig())


def main():
    do_connect()
    print ("CONNECTED")
    url = 'url_of_my_google_form'
    form_data = 'entry.61639300=example'   #have to change the entry
    user_agent = {'Content-Type': 'application/x-www-form-urlencoded'}
    while True:
        response = urequests.post(url, data=form_data, headers=user_agent)
        print ("DATA HAVE BEEN SENT")
        response.close
        print("TRYING TO SEND ANOTHER ONE...")
    response = urequests.get("http://micropython.org/ks/test.html")  #<------ RANDOM URL, I DON'T KNOW WHY THIS CODE WORKS CORRECTLY IN THIS WAY
    print("RANDOM GET:")
        print(response.text)
        response.close


if __name__ == '__main__':
    main()

谢谢你们的时间。我也曾尝试过使用此代码,但它不起作用。如果没有随机的get-request,它将在发布一两次后卡住:

while True:
        response = urequests.post(url, data=form_data, headers=user_agent)
        print ("DATA HAVE BEEN SENT")
        response.close
        print("TRYING TO SEND ANOTHER ONE...")

1 个答案:

答案 0 :(得分:0)

不是response.close()(带括号)吗?..?,

不带括号,您可以访问对象close的(不存在)属性 response,而不用调用方法 close() ,并且不要真正关闭连接。这可能导致内存溢出。