2016-10-10 158 views
1

如何将数据从Socket.io(NodeJs服务器)发送到arduino? 我有一个ESP8266 Wifi盾,我可以发送和接收数据吗?如果我可以有任何基本的例子吗?我如何使用Arduino套接字客户端? 我觉得像这个例子Arduino Socket.io沟通

我可以这样使用吗?

#include <SPI.h> 
#include <Ethernet.h> 

#include "SocketIOClient.h" 

SocketIOClient client; 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
char hostname[] = ""; 

// Socket.io "chat_message" event handler 
void chat_message(EthernetClient ethclient, char *data){ 
    Serial.print("Message : "); 
    Serial.println(data); 
} 

void setup() { 
    Serial.begin(9600); 
    Ethernet.begin(mac); 
    Serial.print("Arduino is on "); 
    Serial.println(Ethernet.localIP()); 

    if(client.connect(hostname, 3000, "socket.io", "/chat_room")) { 
    Serial.println("Socket.IO connected !"); 
    } else { 
    Serial.println("Socket.IO not connected."); 
    } 

    //Event hanlders 
    client.setEventHandler("chat_message", chat_message); 

    //Say hello! to the server 
    client.emit("chat_message", "Arduino here, hello!"); 
} 

void loop() { 
    client.monitor(); 
} 

回答

-1

Socket.IO是一个用于WebSockets的API,大多数Websocket库都支持它。

我很满意这个最贡献的Arduino WebSocket library,它也支持Socket.IO。这是你的Socket.IO example

下面是从例如socket.io implementaiton的心跳消息类型:

if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) { 
    heartbeatTimestamp = now; 
    // socket.io heartbeat message 
    webSocket.sendTXT("2"); 
} 
+0

ABI萨纳ulaşabileceğim井KAPI变种mıdıracaba? :( –

+0

解决了你的问题?@Ahmad – cagdas

+0

Yok abiçözümbulamadı[email protected]ım –