Skip to content

Commit df0af45

Browse files
committed
add TLS example
1 parent a8bb275 commit df0af45

3 files changed

Lines changed: 154 additions & 3 deletions

File tree

examples/ESP-TLS/main.cpp

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// matth-x/ArduinoOcpp
2+
// Copyright Matthias Akstaller 2019 - 2022
3+
// MIT License
4+
5+
#include <Arduino.h>
6+
#if defined(ESP8266)
7+
#include <ESP8266WiFi.h>
8+
#include <ESP8266WiFiMulti.h>
9+
ESP8266WiFiMulti WiFiMulti;
10+
#elif defined(ESP32)
11+
#include <WiFi.h>
12+
#else
13+
#error only ESP32 or ESP8266 supported at the moment
14+
#endif
15+
16+
#include <ArduinoOcpp.h>
17+
#include <ArduinoOcpp/Core/OcppSocket.h> //need for setting TLS credentials
18+
19+
#define STASSID "YOUR_WIFI_SSID"
20+
#define STAPSK "YOUR_WIFI_PW"
21+
22+
#define OCPP_HOST "echo.websocket.events"
23+
#define OCPP_PORT 443
24+
#define OCPP_URL "wss://echo.websocket.events/"
25+
26+
/*
27+
* OCPP Security Profile 2: TLS with Basic Authentication
28+
*
29+
* Example credentials from the OCPP-JSON document (p. 16)
30+
*/
31+
#define OCPP_AUTH_ID "AL1000"
32+
#define OCPP_AUTH_KEY "0001020304050607FFFFFFFFFFFFFFFFFFFFFFFF"
33+
34+
const char ENDPOINT_CA_CERT[] PROGMEM = R"EOF(
35+
-----BEGIN CERTIFICATE-----
36+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
37+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
38+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
39+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
40+
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6brJFe+GnY+EgPbk6ZGQ
56+
3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK
57+
NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5
58+
ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
59+
TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
60+
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
61+
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
62+
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
63+
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
64+
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
65+
-----END CERTIFICATE-----
66+
)EOF";
67+
68+
WebSocketsClient wsockSecure {};
69+
ArduinoOcpp::EspWiFi::OcppClientSocket osockSecure {&wsockSecure};
70+
71+
void setup() {
72+
73+
/*
74+
* Initialize Serial and WiFi
75+
*/
76+
77+
Serial.begin(115200);
78+
79+
Serial.print(F("[main] Wait for WiFi "));
80+
81+
#if defined(ESP8266)
82+
WiFiMulti.addAP(STASSID, STAPSK);
83+
while (WiFiMulti.run() != WL_CONNECTED) {
84+
Serial.print('.');
85+
delay(1000);
86+
}
87+
#elif defined(ESP32)
88+
WiFi.begin(STASSID, STAPSK);
89+
while (!WiFi.isConnected()) {
90+
Serial.print('.');
91+
delay(1000);
92+
}
93+
#endif
94+
95+
Serial.print(F(" connected\n"));
96+
97+
/*
98+
* Set system time (required for Certificate validation)
99+
*/
100+
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); //alternatively: settimeofday(&de, &tz);
101+
Serial.print(F("[main] Wait for NTP time (used for certificate validation) "));
102+
time_t now = time(nullptr);
103+
while (now < 8 * 3600 * 2) {
104+
delay(1000);
105+
Serial.print('.');
106+
now = time(nullptr);
107+
}
108+
Serial.printf(" finished. Unix timestamp is %lu\n", now);
109+
110+
/*
111+
* Connect to OCPP Central System (using OCPP Security Profile 2: TLS with Basic Authentication )
112+
*/
113+
wsockSecure.beginSslWithCA(OCPP_HOST,
114+
OCPP_PORT,
115+
OCPP_URL,
116+
ENDPOINT_CA_CERT, "ocpp1.6");
117+
wsockSecure.setReconnectInterval(5000);
118+
wsockSecure.enableHeartbeat(15000, 3000, 2);
119+
wsockSecure.setAuthorization(OCPP_AUTH_ID, OCPP_AUTH_KEY); // => Authorization: Basic QUwxMDAwOgABAgMEBQYH////////////////
120+
OCPP_initialize(osockSecure);
121+
122+
/*
123+
* ... see ArduinoOcpp.h for how to integrate the EVSE hardware.
124+
*
125+
* This example only showcases the TLS connection. For examples about the HW integration,
126+
* please see the other examples
127+
*/
128+
129+
/*
130+
* Notify the Central System that this station is ready
131+
*/
132+
bootNotification("My Charging Station", "My company name");
133+
}
134+
135+
void loop() {
136+
137+
/*
138+
* Execute all charge point routines and handle WebSocket
139+
*/
140+
OCPP_loop();
141+
142+
//... see ArduinoOcpp.h and the other examples for how to integrate the EVSE hardware.
143+
}

library.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
"include":
4444
[
4545
"src/*",
46-
"examples/*",
46+
"examples/ESP/*",
47+
"examples/ESP-TLS/*",
48+
"examples/SECC/*",
4749
"platformio.ini",
4850
"library.json",
4951
"README.md",
@@ -53,7 +55,6 @@
5355
[
5456
"src/sdkconfig*",
5557
"examples/SECC/WiFiManager*",
56-
"examples/CompatibilityTest",
5758
"src/main.cpp"
5859
]
5960
},
@@ -66,6 +67,13 @@
6667
"main.cpp"
6768
]
6869
},
70+
{
71+
"name": "OCPP Security Profile 2",
72+
"base": "examples/ESP-TLS",
73+
"files": [
74+
"main.cpp"
75+
]
76+
},
6977
{
7078
"name": "EVSE Communications Controller",
7179
"base": "examples/SECC",

src/ArduinoOcpp/Core/Configuration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ std::shared_ptr<Configuration<T>> declareConfiguration(const char *key, T defaul
100100
std::shared_ptr<ConfigurationContainer> container = getContainer(filename);
101101

102102
if (!container) {
103-
AO_DBG_INFO("init new configurations container on flash filesystem: %s", filename);
103+
AO_DBG_INFO("init new configurations container: %s", filename);
104104

105105
container = createConfigurationContainer(filename);
106106
configurationContainers.push_back(container);

0 commit comments

Comments
 (0)