ESP8266无法连接到客户端

时间:2018-05-15 22:13:30

标签: c esp8266

我有一个非常简单的mysql数据库,它可以通过一个简单的命令接收新值:

 http://7751b6b1.ngrok.io/ethernet/data.php?movement=test

如果我在浏览器中输入#34; test"写入数据库。现在我想通过我的ESP8266写入该数据库:

 #include "ESP8266WiFi.h"
//General Definition

const char server[] = "https://7751b6b1.ngrok.io";
 //localhost into server via ngrok
const char* MY_SSID = "Gastzugang";
const char* MY_PWD =  "12345678";
String mov; //create Text string
WiFiClient client; //Create Wifi client

//Setup
void setup()
{
 Serial.begin(115200);
 Serial.println("Connecting to "+*MY_SSID);
 WiFi.begin(MY_SSID, MY_PWD);
Serial.println("going into wl connect");
 int PIR_output=16; // output of pir sensor 
 while (WiFi.status() != WL_CONNECTED) //not connected,  ...waiting to 
 connect
 {
  delay(1000);
  Serial.print(".");
 }
 Serial.println("wl connected");
 Serial.println("");
 Serial.println("Credentials accepted! Connected to wifi\n ");
 Serial.println("");
 }

 void loop() {
 if(digitalRead(16) == HIGH) // reading the data from the pir sensor
 {
 mov = "1";
 Serial.println("1");
 }
 else {
 mov = "0" ;
 Serial.println("0");
 }

 if (client.connect(server, 8095)) { //Connecting at the IP address and port

 Serial.println("connected to server");
 WiFi.printDiag(Serial);
 client.println("Get /ethernet/data.php?");
 //Connecting and Sending values to database
 client.print("movement=");
 client.print(mov);
 client.stop();
 }
 else {
 // if you didn't get a connection to the server:
 Serial.println("connection failed");
 }
 delay(50);
 }

传感器读数和Wifi连接正常工作,但我最终导致连接失败"。

有人能发现错误吗?

编辑:更新代码

#include "ESP8266WiFi.h"
//General Definition
const char server[] = "http://b90e9280.ngrok.io"; //localhost?
const char* MY_SSID = "Gastzugang";
const char* MY_PWD =  "12345678";
String mov; //create Text string
WiFiClient client; //Create Wifi client
//Setup
void setup()
{
Serial.begin(115200);
Serial.println("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
Serial.println("going into wl connect");
int PIR_output=16; // output of pir sensor 
while (WiFi.status() != WL_CONNECTED) //not connected,  ...waiting to 
connect
{
   delay(1000);
   Serial.print(".");
 }
Serial.println("wl connected");
Serial.println("");
Serial.println("Credentials accepted! Connected to wifi\n ");
Serial.println("");
}

void loop() {
//here sensor reading einfügen

if (client.connect(server, 80)) { //Connecting at the IP address and port
Serial.println("connected to server");
WiFi.printDiag(Serial);
client.println("Get /ethernet/data.php?"); //making a http request
Serial.println("Get /ethernet/data.php?");
//Connecting and Sending values to database
if(digitalRead(16) == HIGH) // reading the data from the pir sensor
{
 mov = "1";
client.println("movement=1");
Serial.println("movement=1");
 }
 else {
  mov = "0" ;
  client.println("movement=0");
  Serial.println("movement=0");
  }
  delay(5000);
  client.stop();
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
delay(50);
}

1 个答案:

答案 0 :(得分:0)

文档不清楚,但WiFiClient仅支持http,而非https

你使用https真是太棒了!但您需要使用WiFiClientSecure代替WifiClient

一旦连接,您很可能想要验证服务器的身份。您将使用verify对象上的client方法执行此操作。为此,您需要知道Web服务器使用的证书的指纹。

所以你的代码看起来像这样:

#include "ESP8266WiFi.h"
//General Definition

#define SERVER_FINGERPRINT "C0 5D 08 5E E1 3E E0 66 F3 79 27 1A CA 1F FC 09 24 11 61 62"

const char server[] = "https://7751b6b1.ngrok.io";
 //localhost into server via ngrok
const char* MY_SSID = "Gastzugang";
const char* MY_PWD =  "12345678";
String mov; //create Text string
WiFiClientSecure client; //Create Wifi client

//Setup
void setup()
{
 Serial.begin(115200);
 Serial.println("Connecting to "+*MY_SSID);
 WiFi.begin(MY_SSID, MY_PWD);
Serial.println("going into wl connect");
 int PIR_output=16; // output of pir sensor 
 while (WiFi.status() != WL_CONNECTED) //not connected,  ...waiting to 
 connect
 {
  delay(1000);
  Serial.print(".");
 }
 Serial.println("wl connected");
 Serial.println("");
 Serial.println("Credentials accepted! Connected to wifi\n ");
 Serial.println("");
 }

 void loop() {
 if(digitalRead(16) == HIGH) // reading the data from the pir sensor
 {
 mov = "1";
 Serial.println("1");
 }
 else {
 mov = "0" ;
 Serial.println("0");
 }

 if (client.connect(server, 8095)) { //Connecting at the IP address and port

 Serial.println("connected to server");

 if (client.verify(SERVER_FINGERPRINT, host)) {
   Serial.println("certificate matches");
  } else {
   Serial.println("certificate doesn't match");
 }

 WiFi.printDiag(Serial);
 client.println("Get /ethernet/data.php?");
 //Connecting and Sending values to database
 client.print("movement=");
 client.print(mov);
 client.stop();
 }
 else {
 // if you didn't get a connection to the server:
 Serial.println("connection failed");
 }
 delay(50);
 }

三个重要的变化是: - 将client定义为WiFiClientSecure而不是WiFiClient - 在连接成功后添加对client.verify的调用 - 提供持续的服务器指纹与

进行比较

我提供的指纹不适用于您的服务器。您需要自己找到它的指纹。您可以使用Chrome等网络浏览器点击地址栏中的“安全”字段(在Chrome的情况下)。单击“证书”,然后在证书视图中展开“详细信息”。滚动到最后,你会看到两个指纹,SHA-256和SHA-1。复制SHA-1指纹并将其作为字符串提供,如我所包含的SERVER_FINGERPRINT

如果需要,您可以跳过验证证书,但是您无法确定是否实际连接到真实服务器而不是冒充它的服务器。

WiFiClientSecure Client Secure上有更多信息,包括如何在Windows下查找服务器指纹的另一个示例。

如果您遇到问题,我建议您将程序简化为基础知识 - WiFi connect,然后WiFiClientSecure;不要担心PIR传感器或其他任何东西。一旦你使用你的服务器的精简版本,将你所做的更改转换回你正在努力工作的真实程序。

更新

对于您修改后的代码,它现在写成:

client.println("Get /ethernet/data.php?"); //making a http request
Serial.println("Get /ethernet/data.php?");
//Connecting and Sending values to database
if(digitalRead(16) == HIGH) // reading the data from the pir sensor
{
 mov = "1";
client.println("movement=1");
Serial.println("movement=1");
 }
 else {
  mov = "0" ;
  client.println("movement=0");
  Serial.println("movement=0");
  }
  delay(5000);
  client.stop();
}

尝试:

client.print("Get /ethernet/data.php?"); //making a http request
Serial.println("Get /ethernet/data.php?");
//Connecting and Sending values to database
if(digitalRead(16) == HIGH) // reading the data from the pir sensor
{
 mov = "1";
client.print("movement=1");
Serial.println("movement=1");
 }
 else {
  mov = "0" ;
  client.print("movement=0");
  Serial.println("movement=0");
  }

  client.println(" HTTP/1.1");
  client.println("Host: 7751b6b1.ngrok.io");

  delay(5000);
  client.stop();
}

现在它的编写方式是使用换行符切断URL中的参数,省略该行末尾的“HTTP / 1.1”标记,并且不指定您尝试联系的虚拟主机(可能或者可能不是问题,具体取决于您的服务器)。它需要向Web服务器发送两行,如下所示:

GET /ethernet/data.php?movement=1 HTTP/1.1
HOST: 7751b6b1.ngrok.io