ssl - Arduino ESP8266 + Mega 使用 SSL

标签 ssl arduino esp8266

我在将代码上传到我的设备时遇到问题。我使用带有 wifi 屏蔽 (ESP8266) 的 Arduino Mega。我想使用 WifiClientSecure 类在我的 wifi 连接中实现 SSL。这是我要上传到我的设备的代码:

/*
  HTTP over TLS (HTTPS) example sketch

  This example demonstrates how to use
  WiFiClientSecure class to connect to a TLS server.

  This example verifies server certificate using the
  root CA certificate.

  We fetch and display the status of
  esp8266 / Arduino project continuous integration
  build.

  Created by Ivan Grokhotkov, 2017.
  This example is in public domain.
*/

#include <time.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "........";
const char* password = "........";

const char* host = "api.github.com";
const int httpsPort = 443;

// Root certificate used by api.github.com.
// Defined in "CACert" tab.
extern const unsigned char caCert[] PROGMEM;
extern const unsigned int caCertLen;

WiFiClientSecure client;

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Synchronize time useing SNTP. This is necessary to verify that
  // the TLS certificates offered by the server are currently valid.
  Serial.print("Setting time using SNTP");
  configTime(8 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  time_t now = time(nullptr);
  while (now < 8 * 3600 * 2) {
    delay(500);
    Serial.print(".");
    now = time(nullptr);
  }
  Serial.println("");
  struct tm timeinfo;
  gmtime_r(&now, &timeinfo);
  Serial.print("Current time: ");
  Serial.print(asctime(&timeinfo));

  // Load root certificate in DER format into WiFiClientSecure object
  bool res = client.setCACert_P(caCert, caCertLen);
  if (!res) {
    Serial.println("Failed to load root CA certificate!");
    while (true) {
      yield();
    }
  }
}

void loop() {
  // Connect to remote server
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  // Verify validity of server's certificate
  if (client.verifyCertChain(host)) {
    Serial.println("Server certificate verified");
  } else {
    Serial.println("ERROR: certificate verification failed!");
    return;
  }

  String url = "/repos/esp8266/Arduino/commits/master/status";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println(); enter code here

  static int repeat = 0;
  if (++repeat == 3) {
    Serial.println("Done");
    while (true) {
      delay(1000);
    }
  }
  delay(10000);
}

IDE 发送给我的错误是:

warning: espcomm_sync failed

error: espcomm_open failed

error: espcomm_upload_mem failed

error: espcomm_upload_mem failed

最佳答案

解决方案是使用 ESP8266 模块,因为 Arduino Shield R3 不支持 ssl/tls 方法。

关于ssl - Arduino ESP8266 + Mega 使用 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48843033/

相关文章:

android - 使用证书或其他身份验证针对 Web 服务进行验证

WCF 4 - TransportWithMessageCredential 使用 X.509 证书进行传输和消息安全

audio - Arduino播放Midi文件

arduino - 使用attachInterrupt时没有匹配函数错误

ssl - nodeMCU TLS 证书错误

.htaccess - 将所有流量重定向到 SSL,某些域除外

c# - 从 .net 客户端获取有关 WCF ssl 证书的到期日期/详细信息

c++ - Arduino C++ 将对象作为参数传递

azure - 无法读取 Azure IoT 中心 MQTT 订阅消息数据

arduino - arduino 网络服务器太慢 - 可能使用队列?