-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP32LR42_firmware.ino
executable file
·147 lines (127 loc) · 3.98 KB
/
ESP32LR42_firmware.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
/******************************************************************************
* V1.7
* Added escape char \ so that double quote (") can be included in passwords
*
******************************************************************************/
#include <Preferences.h>
#include <WiFi.h>
const char ver[] = {"1.7"};
const char moduleID = 40; // ESP32LR20 = 39, LR42=40, LR88=41
Preferences nvm;
WiFiServer server(80);
WiFiServer tcpServer(0);
IPAddress local_IP(192, 168, 0, 121);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
#define Rly1 33
#define Rly2 25
#define Rly3 26
#define Rly4 27
#define Inp1 4
#define Inp2 16
#define Led 23
#define BUFSIZE 51
char buffer[BUFSIZE];
char ssid[BUFSIZE];
char WifiPassword[BUFSIZE];
char password2[BUFSIZE];
uint AsciiPort;
char MqttServer[BUFSIZE];
char MqttID[BUFSIZE];
uint MqttPort;
char MqttUser[BUFSIZE];
char MqttPasswd[BUFSIZE];
char mqttpasswd2[BUFSIZE];
char R1Topic[BUFSIZE]; // Relay topics
char R2Topic[BUFSIZE];
char R3Topic[BUFSIZE];
char R4Topic[BUFSIZE];
char N1Topic[BUFSIZE]; // Input topics
char N2Topic[BUFSIZE];
char AsciiPassword[BUFSIZE];
char Inputs[8] = {2,2,2,2,2,2,2,2};
void setup()
{
pinMode(Rly1, OUTPUT); // set the pin modes
pinMode(Rly2, OUTPUT);
pinMode(Rly3, OUTPUT);
pinMode(Rly4, OUTPUT);
pinMode(Led, OUTPUT);
digitalWrite(Rly1, LOW);
digitalWrite(Rly2, LOW);
digitalWrite(Rly3, LOW);
digitalWrite(Rly4, LOW);
digitalWrite(Led, HIGH);
pinMode(Inp1, INPUT);
pinMode(Inp2, INPUT);
Serial.begin(115200);
delay(10);
wifi_connect();
}
void loop(){
modeHttp();
modeAscii();
modeMQTT();
serialMonitor();
if(WiFi.status() != WL_CONNECTED)wifi_connect();
}
void wifi_connect(void)
{
unsigned int x;
digitalWrite(Led, HIGH);
Serial.println("");
nvm.begin("devantech", false); // Note: Namespace name is limited to 15 chars
local_IP = nvm.getUInt("IPAddress", 0);
gateway = nvm.getUInt("GateWay", 0);
subnet = nvm.getUInt("SubNet", 0);
primaryDNS = nvm.getUInt("primaryDNS", 0);
secondaryDNS = nvm.getUInt("secondaryDNS", 0);
nvm.getString("ssid", ssid, sizeof(ssid)-1);
nvm.getString("WifiPassword", WifiPassword, sizeof(WifiPassword)-1);
strcpy(password2, "********");
AsciiPort = nvm.getUInt("AsciiPort", 17123);
nvm.getString("MqttServer", MqttServer, BUFSIZE-1);
nvm.getString("MqttID", MqttID, BUFSIZE-1);
nvm.getString("MqttUser", MqttUser, BUFSIZE-1);
nvm.getString("MqttPasswd", MqttPasswd, BUFSIZE-1);
strcpy(mqttpasswd2, "********");
MqttPort = nvm.getUInt("MqttPort", 0); // 0 means do not connect, normally should be 1883
nvm.getString("R1Topic", R1Topic, BUFSIZE-1);
nvm.getString("R2Topic", R2Topic, BUFSIZE-1);
nvm.getString("R3Topic", R3Topic, BUFSIZE-1);
nvm.getString("R4Topic", R4Topic, BUFSIZE-1);
nvm.getString("N1Topic", N1Topic, BUFSIZE-1);
nvm.getString("N2Topic", N2Topic, BUFSIZE-1);
nvm.getString("AsciiPassword", AsciiPassword, BUFSIZE-1);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
if(local_IP != 0) {
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
}
WiFi.mode(WIFI_STA);
while(WiFi.status() != WL_CONNECTED){
WiFi.disconnect();
delay(100);
WiFi.begin(ssid, WifiPassword);
for(x = 0; x < 300; x++){
delay(10);
serialMonitor();
}
}
digitalWrite(Led, LOW);
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
tcpServer.close();
tcpServer = WiFiServer(AsciiPort);
tcpServer.begin();
setupMQTT();
}