【发布时间】:2016-09-12 05:05:33
【问题描述】:
我一直在尝试通过使用 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,但数据不会显示在服务器端。因此,我不确定导致此问题的问题在哪里。
【问题讨论】:
-
我没有看到任何 POST 请求。
JsonObject会发送吗?你的 Arduino 输出是什么?哪里失败了? -
我没有使用http方式发送数据。在我使用 AT+CIPSEND 命令后,数据通过 arduino 和 Node.js 服务器之间的 TCP 套接字发送,但 Esp8266 没有响应 SEND OK。加上 node.js 服务器不会打印出数据。即使 arduino 可以连接到服务器,因为它在串行监视器中显示“TCP 连接就绪”,但数据不会显示在服务器上。因此我不确定在哪里修复。
-
哦,对不起。我想我忘了更改我的函数名称。我一开始使用http方法,所以我的函数名称是httppost()。
-
如果我们能看到双方的日志,那会有所帮助
-
对于 arduino,我只有在串行监视器上显示的输出。这算不算?