|
| 1 | +/* |
| 2 | + Wifi secure connection example for ESP32 using a pre-shared key (PSK) |
| 3 | + This is useful with MQTT servers instead of using a self-signed cert, tested with mosquitto. |
| 4 | + Running on TLS 1.2 using mbedTLS |
| 5 | +
|
| 6 | + To test run a test server using: openssl s_server -accept 8443 -psk 1a2b3c4d -nocert |
| 7 | + It will show the http request made, but there's no easy way to send a reply back... |
| 8 | +
|
| 9 | + 2017 - Evandro Copercini - Apache 2.0 License. |
| 10 | + 2018 - Adapted for PSK by Thorsten von Eicken |
| 11 | +*/ |
| 12 | + |
| 13 | +#include <WiFiClientSecure.h> |
| 14 | + |
| 15 | +#if 0 |
| 16 | +const char* ssid = "your-ssid"; // your network SSID (name of wifi network) |
| 17 | +const char* password = "your-password"; // your network password |
| 18 | +#else |
| 19 | +const char* ssid = "test"; // your network SSID (name of wifi network) |
| 20 | +const char* password = "securetest"; // your network password |
| 21 | +#endif |
| 22 | + |
| 23 | +//const char* server = "server.local"; // Server hostname |
| 24 | +const IPAddress server = IPAddress(192, 168, 0, 14); // Server IP address |
| 25 | +const int port = 8443; // server's port (8883 for MQTT) |
| 26 | + |
| 27 | +const char* pskIdent = "Client_identity"; // PSK identity (sometimes called key hint) |
| 28 | +const char* psKey = "1a2b3c4d"; // PSK Key (must be hex string without 0x) |
| 29 | + |
| 30 | +WiFiClientSecure client; |
| 31 | + |
| 32 | +void setup() { |
| 33 | + //Initialize serial and wait for port to open: |
| 34 | + Serial.begin(115200); |
| 35 | + delay(100); |
| 36 | + |
| 37 | + Serial.print("Attempting to connect to SSID: "); |
| 38 | + Serial.println(ssid); |
| 39 | + WiFi.begin(ssid, password); |
| 40 | + |
| 41 | + // attempt to connect to Wifi network: |
| 42 | + while (WiFi.status() != WL_CONNECTED) { |
| 43 | + Serial.print("."); |
| 44 | + // wait 1 second for re-trying |
| 45 | + delay(1000); |
| 46 | + } |
| 47 | + |
| 48 | + Serial.print("Connected to "); |
| 49 | + Serial.println(ssid); |
| 50 | + |
| 51 | + client.setPreSharedKey(pskIdent, psKey); |
| 52 | + |
| 53 | + Serial.println("\nStarting connection to server..."); |
| 54 | + if (!client.connect(server, port)) |
| 55 | + Serial.println("Connection failed!"); |
| 56 | + else { |
| 57 | + Serial.println("Connected to server!"); |
| 58 | + // Make a HTTP request: |
| 59 | + client.println("GET /a/check HTTP/1.0"); |
| 60 | + client.print("Host: "); |
| 61 | + client.println(server); |
| 62 | + client.println("Connection: close"); |
| 63 | + client.println(); |
| 64 | + |
| 65 | + while (client.connected()) { |
| 66 | + String line = client.readStringUntil('\n'); |
| 67 | + if (line == "\r") { |
| 68 | + Serial.println("headers received"); |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + // if there are incoming bytes available |
| 73 | + // from the server, read them and print them: |
| 74 | + while (client.available()) { |
| 75 | + char c = client.read(); |
| 76 | + Serial.write(c); |
| 77 | + } |
| 78 | + |
| 79 | + client.stop(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void loop() { |
| 84 | + // do nothing |
| 85 | +} |
0 commit comments