javascript - ESP8266 拒绝连接到 Raspberry Pi Zero W
问题描述
我正在建立一个气象站并尝试使用我的 NodeMCU ESP8266 写入我的数据库。因此,我连接到 Node.js 服务器,我的 API 处理数据库。当我在我的 Windows 机器上运行服务器代码时,它工作得很好。我在 Raspberry Pi Zero W 上安装了所有东西,当我用浏览器或 Postman 对其进行测试时,它仍然可以工作……但不幸的是,我的 ESP 现在拒绝建立连接。有人知道这是什么原因吗?
Arduino代码:
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_BMP085.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
//declare
Adafruit_BMP085 bmp;
HTTPClient http;
StaticJsonBuffer<256> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
char ssid[] = //SSID;
char key[] = //PASSWORD;
//declare variables
float temperature = 0.0;
float pressure = 0.0;
float pressureSea = 0.0;
float rssi = 0.0;
String mac = "00:00:00:00:00:00";
char payload[256];
void setup() {
// --------------- WIFI Configuration --------------
Serial.begin(115200);
while (!Serial);
Serial.println();
WiFi.begin(ssid, key);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
// ---------------- Sensor Config --------------------
Wire.pins(5, 4);
Wire.begin(5, 4);
if (!bmp.begin()) {
Serial.println("No BMP180 / BMP085");// we dont wait for this
while (1) {}
}
}
void loop() {
measure();
sendData();
Serial.print("Temperatur: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Druck: ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("RSSI: ");
Serial.println(rssi);
Serial.print("MAC: ");
Serial.println(mac);
Serial.println();
delay(10000);
}
void measure () {
// Environment Measure
temperature = bmp.readTemperature();
pressure = bmp.readPressure() / 100;
// Signal Measure
rssi = WiFi.RSSI();
mac = WiFi.macAddress();
}
void sendData () {
root["temp"] = temperature;
root["press"] = pressure;
root["rssi"] = rssi;
root.printTo(payload, sizeof(payload));
Serial.println(payload);
http.begin("http://192.168.87.76:3000/api/weather");
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(payload);
//http.writeToStream(&Serial);
http.end();
Serial.println(httpCode);
if (httpCode < 0) {
Serial.printf("Request failed: %s\n", http.errorToString(httpCode).c_str());}
Serial.println();
}
Node.js-服务器:
// SERVER SETUP
// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
const cors = require('cors')
// Get our API routes
const api = require('./server/routes/api');
const app = express();
//Enable CORS
app.use(cors());
// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist')));
// Set our api routes
app.use('/api', api);
// Catch all other routes and return the index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
/**
* Get port from environment and store in Express.
*/
const port = process.env.PORT || '3000';
app.set('port', port);
/**
* Listen on provided port, on all network interfaces.
*/
app.listen(port,'0.0.0.0');
HTTP客户端调试器的输出:
[HTTP-Client][begin] url: http://192.168.87.51:3000/api/weather
[HTTP-Client][begin] host: 192.168.87.51 port: 3000 url: /api/weather
Status: 0
[HTTP-Client] failed connect to 192.168.87.51:3000
[HTTP-Client][returnError] error(-1): connection refused
[HTTP-Client][end] tcp is closed
-1
Request failed: connection refused
Pi上的Netstat:
sudo netstat -tulpen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 11766 466/sshd
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 1000 12846 590/node
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 109 10991 354/postgres
tcp6 0 0 :::22 :::* LISTEN 0 11773 466/sshd
tcp6 0 0 :::5432 :::* LISTEN 109 10992 354/postgres
udp 0 0 0.0.0.0:5353 0.0.0.0:* 108 10184 238/avahi-daemon: r
udp 0 0 0.0.0.0:68 0.0.0.0:* 0 10888 450/dhcpcd
udp 0 0 0.0.0.0:48824 0.0.0.0:* 108 10186 238/avahi-daemon: r
udp6 0 0 :::5353 :::* 108 10185 238/avahi-daemon: r
udp6 0 0 :::34435 :::* 108 10187 238/avahi-daemon: r
如果您有任何线索或任何提示,我非常感谢!谢谢!
解决方案
推荐阅读
- r - 将列表循环到 N 元素
- shell - 为什么我不能从 M1 Mac zsh 会话打开密码?
- installation - “mscorelib .. 在系统 IO 中找不到文件....
- r - 在 R 中压缩 CSV 文件
- java - GKE GCE-Internal Ingress 不适用于 Elasticsearch REST 客户端
- html - 为什么我的页面没有根据我的 CSS 显示
- node.js - 关于 Node + Ngynx、可扩展性、超时的问题
- javascript - CORS 错误:响应中的标头不能是通配符 javascript
- r - RStudio:在一个笔记本中绘制一个函数;在另一个笔记本中,我得到“plot.new 尚未被调用”错误
- javascript - 有没有办法让套接字中间件用于发送的数据包?