-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathmain.cpp
More file actions
117 lines (95 loc) · 3.05 KB
/
main.cpp
File metadata and controls
117 lines (95 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
#elif defined(ESP32)
#include <WiFi.h>
#else
#error only ESP32 or ESP8266 supported at the moment
#endif
#include <MicroOcpp.h>
#define STASSID "YOUR_WIFI_SSID"
#define STAPSK "YOUR_WIFI_PW"
#define OCPP_BACKEND_URL "ws://echo.websocket.events"
#define OCPP_CHARGE_BOX_ID ""
//
// Settings which worked for my SteVe instance:
//
//#define OCPP_BACKEND_URL "ws://192.168.178.100:8180/steve/websocket/CentralSystemService"
//#define OCPP_CHARGE_BOX_ID "esp-charger"
void setup() {
/*
* Initialize Serial and WiFi
*/
Serial.begin(115200);
Serial.print(F("[main] Wait for WiFi: "));
#if defined(ESP8266)
WiFiMulti.addAP(STASSID, STAPSK);
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
#elif defined(ESP32)
WiFi.begin(STASSID, STAPSK);
while (!WiFi.isConnected()) {
Serial.print('.');
delay(1000);
}
#else
#error only ESP32 or ESP8266 supported at the moment
#endif
Serial.println(F(" connected!"));
/*
* Initialize the OCPP library
*/
mocpp_initialize(OCPP_BACKEND_URL, OCPP_CHARGE_BOX_ID, "My Charging Station", "My company name");
/*
* Integrate OCPP functionality. You can leave out the following part if your EVSE doesn't need it.
*/
setEnergyMeterInput([]() {
//take the energy register of the main electricity meter and return the value in watt-hours
return 0.f;
});
setSmartChargingCurrentOutput([](float limit) {
//set the SAE J1772 Control Pilot value here
Serial.printf("[main] Smart Charging allows maximum charge rate: %.0f\n", limit);
});
setConnectorPluggedInput([]() {
//return true if an EV is plugged to this EVSE
return false;
});
//... see MicroOcpp.h for more settings
}
void loop() {
/*
* Do all OCPP stuff (process WebSocket input, send recorded meter values to Central System, etc.)
*/
mocpp_loop();
/*
* Energize EV plug if OCPP transaction is up and running
*/
if (ocppPermitsCharge()) {
//OCPP set up and transaction running. Energize the EV plug here
} else {
//No transaction running at the moment. De-energize EV plug
}
/*
* Use NFC reader to start and stop transactions
*/
if (/* RFID chip detected? */ false) {
String idTag = "0123456789ABCD"; //e.g. idTag = RFID.readIdTag();
/*
* Begin Transaction. The OCPP lib will prepare transaction by checking the Authorization
* and listen to the ConnectorPlugged Input. When the Authorization succeeds and an EV
* is plugged, the OCPP lib will send the StartTransaction
*
* If transaction already exists, stop by RFID card (if idTag or parentIdTag match)
*/
authorize(idTag.c_str());
}
//... see MicroOcpp.h for more possibilities
}