NodeMCU ESP-12E模块在使用Web套接字发送数据后重新启动

时间:2017-05-27 00:04:27

标签: websocket arduino esp8266 nodemcu arduino-esp8266

在尝试将数据从网站发送到ESP-12E时,我遇到了NodeMCU ESP-12E模块的问题。到目前为止,我可以在控制台中与ESP-12E建立连接,并根据需要保持打开状态。但是当从网站发送任何数据时,ESP会在大约6-10秒后重新启动。

我使用WebSocketServer库中的示例(WebSocketServer_Demo_ESP8266和WebSocketServer.html)

这是HTML:

<html>
<head>
    <meta charset="utf-8">
    <title>
        WebSocket Test
    </title>
    <style>
        #redbar {
            color: #fff;
            background-color: #f00;
            width: 500px;
            height: 30px;
        }
        #greenbar {
            color: #fff;
            background-color: #0f0;
            width: 500px;
            height: 30px;
        }
        #bluebar {
            color: #fff;
            background-color: #00f;
            width: 500px;
            height: 30px;
        }                       
    </style>

    <script language = "javascript"type = "text/javascript">
        var wsUri = "ws://Ip.from.the.esp.here/";
        var output;

        function init() {
            testWebSocket();
        }

        function testWebSocket() {
            websocket = new WebSocket(wsUri);
            websocket.onopen = function(evt) {
                console.log("CONNECTED");
            };
            websocket.onclose = function(evt) {
                console.log("DISCONNECTED");
            };
            websocket.onmessage = function(evt) {
                var pin = evt.data.charAt(1);
                var element;
                if (pin == 1) {
                    element = document.getElementById("redbar");
                    element.style.width = evt.data.substring(2) + "px";
                } else if (pin == 2) {
                    element = document.getElementById("greenbar");
                    element.style.width = evt.data.substring(2) + "px";                     
                } else if (pin == 3) {
                    element = document.getElementById("bluebar");
                    element.style.width = evt.data.substring(2) + "px";                     
                }
            };
            websocket.onerror = function(evt) {
                console.log("ERROR: " + evt.data);
            };
        }

        function changeRed() {
            var value = document.getElementById("red").checked;
            var message = "d8";

            message += value ? "1" : "0";

            console.log("SENT: " + message);
            websocket.send(message);
        }

        function changeGreen() {
            var value = document.getElementById("green").checked;
            var message = "d9";

            message += value ? "1" : "0";

            console.log("SENT: " + message);
            websocket.send(message);
        }           


        window.addEventListener("load", init, false); 
    </script>
</head>
<body>
    <h2>
        WebSocket Test
    </h2>
    <input id="red" type="checkbox" onchange="changeRed();">Pin 8</input>
    <input id="green" type="checkbox" onchange="changeGreen();">Pin 9</input>

    <div id="redbar">Pin 1</div>
    <div id="greenbar">Pin 2</div>
    <div id="bluebar">Pin 3</div>
</body>

这是Arduino代码:

#include <SPI.h>
#include <ESP8266WiFi.h>

// Enable debug tracing to Serial port.
#define DEBUGGING
// Here we define a maximum framelength to 64 bytes. Default is 256.
#define MAX_FRAME_LENGTH 64
// Define how many callback functions you have. Default is 1.
#define CALLBACK_FUNCTIONS 1

#include <WebSocketServer.h>

const char* ssid     = "My SSID";
const char* password = "PASSWORD";
WiFiServer server(80);
WebSocketServer webSocketServer;

// Called when a new message from the WebSocket is received
// Looks for a message in this form:
//
// DPV
//
// Where:
//        D is either 'd' or 'a' - digital or analog
//        P is a pin #
//        V is the value to apply to the pin
//

void handleClientData(String &dataString) {
  bool isDigital = dataString[0] == 'd';
  int pin = dataString[1] - '0';
  int value;
  value = dataString[2] - '0';
  pinMode(pin, OUTPUT);
  if (isDigital) {
    digitalWrite(pin, value);
  } else {
    analogWrite(pin, value);
  }
  Serial.println(dataString);
}

// send the client the analog value of a pin
void sendClientData(int pin) {
  String data = "a";
  pinMode(pin, INPUT);
  data += String(pin) + String(analogRead(pin));
  webSocketServer.sendData(data);
}

void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  // Print the IP address
  Serial.println(WiFi.localIP());
  // This delay is needed to let the WiFly respond properly
  delay(100);
}

void loop() {
  String data;
  WiFiClient client = server.available();
  if (webSocketServer.handshake(client)) {
    while (client.connected()) {
      data = webSocketServer.getData();
      if (data.length() > 0) {
        handleClientData(data);
      }
      sendClientData(1);
      sendClientData(2);
      sendClientData(3);
      delay(1);
    }
    delay(1);
    Serial.println("connection closed");
  }
  // wait to fully let the client disconnect
  delay(100);
}

这是2次尝试后控制台的输出

WiFi connected
Server started
19.3.9.34
Client connected
Analyzing request headers
Got Line: GET / HTTP/1.1
Got Line: Host: 19.3.9.34
Got Line: Connection: Upgrade
Got Line: Pragma: no-cache
Got Line: Cache-Control: no-cache
Got Line: Upgrade: websocket
Got Line: Origin: file://
Got Line: Sec-WebSocket-Version: 13
Got Line: User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/37.36 (KHTML, like Gecko) Chrome/57.0.3354.98 Safari/347.36
Got Line: Accept-Encoding: gzip, deflate, sdch
Got Line: Accept-Language: en-US,en;q=0.8
Got Line: Sec-WebSocket-Key: kwojjhwhwlwlk/kbK0Tg==
Got Line: Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Got Line: 
ÿ
ÿ+{çùÄ...
WiFi connected
Server started
19.3.9.34
Client connected
Analyzing request headers
Got Line: GET / HTTP/1.1
Got Line: Host: 19.3.9.34
Got Line: Connection: Upgrade
Got Line: Pragma: no-cache
Got Line: Cache-Control: no-cache
Got Line: Upgrade: websocket
Got Line: Origin: file://
Got Line: Sec-WebSocket-Version: 13
Got Line: User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/37.36 (KHTML, like Gecko) Chrome/57.0.3354.98 Safari/347.36
Got Line: Accept-Encoding: gzip, deflate, sdch
Got Line: Accept-Language: en-US,en;q=0.8
Got Line: Sec-WebSocket-Key: kwojjhwhwlwlk/kbK0Tg==
Got Line: Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Got Line: 
ÿ

拜托,我会感激任何帮助。提前谢谢。

0 个答案:

没有答案