-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartevse.ino
157 lines (130 loc) · 3.38 KB
/
smartevse.ino
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <ModbusMaster.h>
#include <WiFi.h>
#include <PubSubClient.h>
#define MAX485_RE_NEG 4
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqtt_server = "YOUR_MQTT_SERVER_IPADDRESS";
WiFiClient espClient;
PubSubClient client(espClient);
ModbusMaster node;
boolean startCharging = false;
boolean stopCharging = false;
int chargeCurrent = 6;
unsigned long lastMillis = 0;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
digitalWrite(MAX485_RE_NEG, 0);
Serial2.begin(9600);
node.begin(1, Serial2);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
if (String(topic) == "smartevse/current") {
current_to_smartevse(messageTemp.toInt());
}
if (String(topic) == "smartevse/start") {
startCharging = true;
}
if (String(topic) == "smartevse/stop") {
stopCharging = true;
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("smartevseclient")) {
Serial.println("connected");
// Subscribe
client.subscribe("smartevse/current");
client.subscribe("smartevse/start");
client.subscribe("smartevse/stop");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void current_to_smartevse(int current) {
Serial.print("Changing current to ");
Serial.println(current);
if (client.connected()) {
client.publish("smartevse/debug", "Changing current");
}
chargeCurrent = current;
}
void loop()
{
if (!client.connected()) {
reconnect();
}
if (millis() - lastMillis > 1000)
{
node.writeSingleRegister(0x00A6, chargeCurrent * 10);
Serial.print("Writing current to smartEVSE, current: ");
Serial.println(chargeCurrent);
lastMillis = millis();
}
if (startCharging == true)
{
Serial.println("Start charging");
if (client.connected()) {
client.publish("smartevse/debug", "Start charging");
}
node.writeSingleRegister(0x00A7, 1);
startCharging = false;
}
if (stopCharging == true) {
Serial.println("Stop charging");
if (client.connected()) {
client.publish("smartevse/debug", "Stop charging");
}
node.writeSingleRegister(0x00A7, 0);
stopCharging = false;
}
client.loop();
}