从 arduino 和 esp8266 发送 json 到 node.js 套接字服务器

Send json from arduino and esp8266 to node.js socket server

我一直在尝试通过使用 ESP8266 作为 wifi 屏蔽从 arduino mega 发送 JSON 数据,并且我使用 node.js 作为套接字服务器。问题是服务器似乎没有收到任何数据。这是我的代码

   #include <ArduinoJson.h>
#include "SoftwareSerial.h"


String ssid ="ssid";
String password="pwd";
//SoftwareSerial esp(22,23);// RX, TX
String data;
String server = "server ip"; 
byte objlength;
String url = "";
String temp,hum,weight;
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
//String uri = "yourURI";

void reset() {

Serial2.println("AT+RST");
delay(1000);
if(Serial2.find("OK") ) Serial.println("Module Reset");


}
void connectWifi() {

String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
Serial2.println(cmd);
delay(4000);

if(Serial2.find("OK")) {
Serial.println("Connected!");
Serial2.println("AT+CIPSTATUS");
delay(300);
while(Serial2.available()){Serial.print(Serial2.read());}

}
else {
connectWifi();
Serial.println("Cannot connect to wifi"); }
}

void setup() {
delay(5000);
  // put your setup code here, to run once:
Serial2.begin(115200);
Serial.begin(115200);
reset();
connectWifi();

}


void loop() {
temp = 25.60;
hum = 65.3;
weight = 65.3;

root["weight"] = weight;
root["light"] = temp;
root["humid"] = hum;
objlength = root.measureLength();
senddata();
delay(10000);
}
void senddata() 
{
int objlength = root.measureLength();

Serial2.println("AT+CIPSTART=\"TCP\",\"" + server + "\",1336");//start a TCP connection.

delay(500);
if( Serial2.find("OK")) {
Serial.println("TCP connection ready");
} 
else
{
Serial.println("can't establish TCP connection");
}
String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

Serial2.println(sendCmd);
delay(200);
Serial2.println(objlength);

delay(500);

if(Serial2.find(">")) 
{ 
Serial.println("Sending..");
root.printTo(Serial2);
root.printTo(Serial);
//Serial.println(postRequest);
  delay(2000);
  if( Serial2.find("SEND OK")) 
  { 
    Serial.println("Packet sent");
    delay(200);

    while (Serial2.available()) {
    String tmpResp = Serial2.readString();
    Serial.println(tmpResp);
  }
// close the connection

  }
 //delay(1000);
Serial2.print("+++");
delay(1200);
Serial2.println("AT+CIPCLOSE");
delay(50);
Serial.println("Closed");
}

}

这是我的 node.js

var net = require('net');

var server = net.createServer(function(socket){
    socket.write('SEND OK');
//
    socket.pipe(socket);

socket.on('data',function(data){
    //if(typeof data != 'string'){
    var jsontest = JSON.parse(data);
    console.log('Received: ' + data);
    console.log(jsontest.weight);
    console.log(jsontest.light);
    console.log(jsontest.humid);
    //}
    });
socket.on('listening',function(){
    console.log(listen);
});
});
/*server.getConnections(function(err,count){
console.log(count);
});*/
server.listen(1336, '10.42.0.1');

我觉得esp8266可以和服务器建立连接,但是不知道为什么数据显示不出来。也许是关于 esp8266 响应时间? screenshot 从这个截图可以看出,我 运行 node.js 服务器和 arduino,但数据不会显示在服务器端。因此,我不确定导致此问题的问题在哪里。

我不是专家。我只是在学习如何去做,但我会说你没有通过连接给予足够的时间。在等待真正的响应时,您依赖于延迟而不是超时。有区别。

使用 serial.available() 增加更多时间或更改策略,并在一个持续审慎时间的循环中读取,并在获得数据时退出。

抱歉没有粘贴一些代码,但我希望你明白了。

我也遇到了电源问题。 arduino uno 中的 3,3v 引脚在克隆板上可能很弱。但我不这么认为。