c++ - 连接被拒绝 ESP32
问题描述
我有使用带有 ESP32 的 GSR 传感器的项目。来自 ESP32 的数据保存在数据库中。
但是,在我的代码中我有一个错误。使用静态 IP 时,HTTPClient.begin()
总是返回连接被拒绝。
我已经看到这个问题被问了几次,但从来没有得到一个好的答案。
#include <ssl_client.h>
#include <WiFiClientSecure.h>
#include <WiFi.h>
#include <SPI.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
#define USE_SERIAL Serial
/* Konfigurasi pada SSID dan Password Wifi */
const char* ssid = "AndroidAP";
const char* password = "stayC00L";
/* Konfigurasi koneksi ke server */
char server[] = "192.168.43.111"; // Ganti dengan IP Address komputer aplikasi
int port = 81;
/*konfigurasi*/
const int GSR = 32;
float sensorValue = 0;
float gsr_average = 0;
IPAddress local_IP(192, 168, 43, 113);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8); // Google DNS
WiFiClient client;
void setup() {
USE_SERIAL.begin(9600); // Baudrate/kec. komunikasi pengiriman data ke serial terminal
delay(10);
Serial.println('\n');
WiFi.begin(ssid, password);
USE_SERIAL.print("Terhubung ke ");
USE_SERIAL.print(ssid);
WiFi.config(local_IP, gateway, subnet, dns);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
USE_SERIAL.print('.');
}
USE_SERIAL.print('\n');
USE_SERIAL.println("Identitas ESP-32");
USE_SERIAL.print("IP Address:\t");
USE_SERIAL.println(WiFi.localIP()); // IP Address ESP-32
USE_SERIAL.print('\n');
USE_SERIAL.println("Identitas Web Server");
USE_SERIAL.println("IP Address:");
USE_SERIAL.print(server);
USE_SERIAL.print("\tport:\t");
USE_SERIAL.print(port);
}
void loop() {
long sum=0;
for(int i=0;i<10;i++) //Average the 10 measurements to remove the glitch
{
sensorValue=analogRead(GSR);
sum += sensorValue;
delay(100);
}
gsr_average = sum/10;
USE_SERIAL.println(gsr_average);
if(WiFi.status() == WL_CONNECTED){
//URL target untuk menyimpan data sensor GSR ke database
String url = "http://192.168.43.111:81/";
url += "TA/Monitoring/simpan/";
url += String(gsr_average);
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
http.begin(url);
USE_SERIAL.print("[HTTP] GET...\n");
int httpCode = http.GET();
if(httpCode > 0) {
USE_SERIAL.printf("[HTTP] GET... code: %d\n",
httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n",
http.errorToString(httpCode).c_str());
}
http.end();
}
}
错误是
拒绝连接
请帮我。我应该怎么办??
解决方案
推荐阅读
- c - “使用未声明的标识符‘高度’”的问题 mario.c
- javascript - Reactjs重定向网址更改但组件未加载
- c++ - 为已经转换的 QGraphicsItem 设置转换原点
- python-3.x - 使用 kafka 运行 Faust 会导致 ConsumerStoppedError 崩溃
- python - Python 评论:为什么代码 1 产生的结果与代码 2 不同?
- azure-devops - Azure Pipeline - 为什么需要参数值
- datetime - 可以在没有日期信息的情况下解析时间字符串吗?
- spring - spring boot 2.1.3 以后抛出循环引用
- flutter - 我有一个关于在 initstate 中使用异步函数的问题
- redis - 如何将 aof 文件重新加载到 Redis