-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoodwe_MT_Gateway.ino
78 lines (73 loc) · 1.79 KB
/
Goodwe_MT_Gateway.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
#include "ESPAsyncWebServer.h"
#include <WiFiUdp.h>
#include <ModbusRTU.h>
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
uint16_t Mread[20];
uint16_t Mread1[1];
int Call_Stp = 1;
int Slave_ID = 1;
ModbusRTU mb;
WiFiClient Goodwe_MT;
PubSubClient client(Goodwe_MT);
#define MQTT_MAX_PACKET_SIZE 512
#include "Settings.h"
#include "IntToFloat.h"
#include "Data_Structure.h"
#include "Webpage.h"
#include "WebserverSetup.h"
#include "MoveData.h"
#include "Modbus_callback.h"
#include "JsonPack.h"
#include "Modbus_Slave_Query.h"
#include "MQTT.h"
#include "WIFISet.h"
#include "OTA.h"
SoftwareSerial S(Rx, Tx);
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
S.begin(9600, SWSERIAL_8N1);
Slave_ID = Start_ID;
mb.begin(&S, DE_RE);
mb.master();
if (MQTT_Enable) {
setup_wifi();
asynkweb_setup();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
String MQTT_Topic_s = MQTT_Topic;
MQTT_Topic_s = MQTT_Topic_s + "-%ld";
MQTT_Topic_s.toCharArray(MQTT_Topic, (MQTT_Topic_s.length() + 1));
Serial.println((MQTT_Topic_s.length() + 1));
}
otasetup();
}
void loop() {
ArduinoOTA.handle();
if (millis() - LastScan > Interval) {
MBslaveQuery(Slave_ID);
ArduinoOTA.handle();
}
if (MQTT_Enable) {
if (!client.connected()) {
long now = millis();
if (now - lastReconnectAttempt > MQTT_Re_Connect_Interval) {
lastReconnectAttempt = now;
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
}
else client.loop();
}
mb.task();
yield();
}