我正在使用esp8266作为wifi网络上nodejs服务器的客户端。 这是我的服务器代码:
var express = require('express');
var socket = require('socket.io');
var fs = require('fs');
var app = express();
var io = socket(server);
var server = app.listen(3000,'0.0.0.0',function() {
console.log('Server connected at port 3000');
});
io.on('connection',function(socket){
console.log('socket connected');
socket.on('data',function (data) {
console.log('Data transfer is ready');
});
});
这是我的esp8266客户端代码:
#include <SocketIOClient.h>
#include <ESP8266WiFi.h>
char* ssid = "GD Coffee"; // SSID
char* password = "23456789@"; // Password
String host = "192.168.1.79"; // IP serveur - Server IP
const int port = 3000; // Port serveur - Server Port
const int watchdog = 5000; // Fréquence du watchdog - Watchdog frequency
unsigned long previousMillis = millis();
WiFiClient client;
SocketIOClient socket;
void setup() {
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (!client.connect(host, port)) {
Serial.println("connection failed");
}
else
{
Serial.println("CONNECT TO SERVER SUCCESSFULLY");
}
}
void loop() {
// Read all the lines of the reply from server and print them to Serial
if(!socket.connect(host,port))
{
Serial.println("socket failed");
}
else
{
Serial.println("Socket ON");
}
if(!socket.connected())
{
socket.reconnect(host,port);
}
}
但是从串口报道回复给我: Socket failure
由于我是ESP8266的新手,所以任何人都可以帮我这个或者你能提供一些示例代码吗? 如果你能提供帮助,我真的很感激。