简体   繁体   English

js文件和esp32 sketch之间的数据发送

[英]Data sending between js file and esp32 sketch

I want to use an esp32 as a web server to display information within a browser.我想使用 esp32 作为 web 服务器在浏览器中显示信息。 I have an esp32 sketch that processes some data from sensors and i want to stream them into the js file so that i can display them on the web page as a graph that updates periodically.我有一个处理来自传感器的一些数据的 esp32 草图,我想将它们 stream 放入 js 文件中,以便我可以将它们显示在 web 页面上作为定期更新的图表。 My problem is that no matter what i try, i just can't figure out how to funnel the data between the two files.我的问题是,无论我尝试什么,我都无法弄清楚如何在两个文件之间汇集数据。 Any help, please?有什么帮助吗? Thanks for all responses in advance!提前感谢所有回复!

sorry for posting an answer, i don't have the required level to comment抱歉发布答案,我没有评论所需的级别

first, I am sorry to tell you that even if you update the javascript file in real time, its content will not load on the web.首先,很遗憾地告诉您,即使您实时更新javascript文件,其内容也不会加载到web上。

but you can send the updated sensor data via WebSocket server to the javascript client and then update your interface但您可以通过 WebSocket 服务器将更新的传感器数据发送到 javascript 客户端,然后更新您的界面

this is a little WS library i have done before, its simple but functional这是我以前做过的一个小 WS 库,它简单但实用

export const ws = (url) => {
    let ws = null
    let connectListeners = []
    let receiveListeners = []
    let retryCount = 1
    const api = {
        connect(newUrl, attemps = -1) {
            if (ws != null) {
                ws.onmessage = null
                ws.onerror = null
                ws.onclose = null
                ws.onopen = null
            }
            return new Promise((resolve, reject) => {
                ws = new WebSocket(newUrl || url)
                ws.onopen = (e) => {
                    connectListeners.forEach(cbk => {
                        cbk(e)
                    })
                    resolve(api)
                }
                ws.onmessage = (e) => {
                    let data = e.data
                    try {
                        data = JSON.parse(data)
                    } catch (e) {}

                    receiveListeners.forEach(cbk => {
                        cbk(data)
                    })
                }
                ws.onerror = (e) => {
                    if (attemps > 0 && retryCount < attemps) {
                        retryCount++
                        api.connect(newUrl, attemps)
                    } else if (attemps < 0) {
                        api.connect(newUrl, attemps)
                    } else {
                        reject(e)
                    }
                }
            })
        },
        onconnect(e) {
            connectListeners.push(e)
        },
        onreceive(e) {
            receiveListeners.push(e)
        },
        send(data) {
            if (ws.readyState == WebSocket.OPEN) {
                ws.send(JSON.stringify(data))
            }
            return this;
        },
        close(forced = false) {
            if (forced) {
                this.connect()
            } else {
                ws.close()
            }
            return this;
        }
    }
    return api;
}

and then you can use it as follow然后你可以使用它如下

const wsClient = ws('ws://your-esp32-ip/')
wsClient.onreceive(function(data){
    console.log(data)
})
wsClient.connect() // you can pass a new connection url and number of connection attemps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM