-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRS485_MQTT_nano33_EM-4.ino
93 lines (79 loc) · 1.96 KB
/
RS485_MQTT_nano33_EM-4.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
// 09/11/23 13:00
// version 4.0.1
// working for modbus
// includes read time
// use ArduinoMQTT
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoMqttClient.h>
#include <NTPClient.h>
#include <ArduinoModbus.h>
#include <ArduinoRS485.h>
#include <ArduinoOTA.h>
#include "arduino_secrets.h"
char wifi_ssid[] = SECRET_SSID;
char wifi_pass[] = SECRET_PASS;
char mqtt_server[] = SECRET_MQTT_SERVER;
char mqtt_user[] = SECRET_MQTT_USER;
char mqtt_pass[] = SECRET_MQTT_PASS;
//For mqtt
String topicStr;
String contentStr;
//For EM
double activePower;
double totalEnergy;
double thismonthEnergy;
double lastmonthEnergy;
float todayEnergy;
float lastdayEnergy;
double thisyearEnergy;
double lastyearEnergy;
float dcVolts;
float dcAmps;
float inverterTemp;
float acFrequency;
//For Time
float inverterYear;
float inverterMonth;
float inverterDay;
float inverterHours;
float inverterMinutes;
float inverterSeconds;
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
//MqttClient mqttClient(256);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
void setup() {
// Serial.begin(9600);
// set up wifi
while (WiFi.begin(wifi_ssid, wifi_pass)!= WL_CONNECTED) {
Serial.println (".");
delay(1000);
}
// set up MQTT
mqttClient.setId("nano33_1");
mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);
while (!mqttClient.connect(mqtt_server, 1883)) {delay(1000);}
// set up OTA
ArduinoOTA.begin(WiFi.localIP(),"nano33-1","nano1pass", InternalStorage);
// set up Modbus
ModbusRTUClient.begin(9600);
timeClient.begin();
}
void loop() {
// check connection to MQTT server
while (!mqttClient.connect(mqtt_server, 1883))
{
WiFi.begin(wifi_ssid, wifi_pass);
delay(1000);
}
mqttClient.poll();
ArduinoOTA.poll();
mqttAutoDiscovery();
mqttHeartbeat();
mqttStatus();
mqttData();
mqttTime();
delay(8800);
}