-
Notifications
You must be signed in to change notification settings - Fork 0
/
ez-instrumentation_0.17.ino
647 lines (548 loc) · 16.5 KB
/
ez-instrumentation_0.17.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ArduinoOTA.h>
//#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <FS.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Ticker.h>
Ticker secondTick;
const char* vers = "0.17";
const char* ssid = "RiverRover";
const char* password = "beroroha";
const char* host = "Default";
const char* MQ_client = "Default"; // your network password
const char* MQ_user = ""; // your network password
const char* MQ_pass = ""; // your network password
char server[]="192.168.43.1";
long ADCfactor = 16;
const unsigned int MAX_INPUT = 50;
extern "C" long DS_loop();
int fmode = 14;
int Watchdogcount = 0;
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 0
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
//DeviceAddress insideThermometer, outsideThermometer;
DeviceAddress deviceAddress;
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
struct SettingsStruct
{
char WifiSSID[32];
char WifiKey[64];
char WifiAPKey[64];
char ControllerUser[26];
char ControllerPassword[64];
char Host[26];
char Broker[4];
char BrokerIP[16];
char Version[16];
} Settings;
boolean ok;
unsigned long start = 0;
unsigned long timer60 = 0;
unsigned int loop_delay = 2000;
volatile int watchdogCount = 0;
void ISRwatchdog() {
watchdogCount++;
if ( watchdogCount == 5 ) {
// Only print to serial when debugging
ESP.reset();
}
}
void process_data (const char * data)
{
// for now just display it
// (but you could compare it to some value, convert to an integer, etc.)
Serial.println (data);
char topic[0];
Controller(topic, data);
} // end of process_data
void processIncomingByte (const byte inByte)
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
//Serial.print(input_line);
break;
} // end of switch
} // end of processIncomingByte
void callback(char* topic, byte* payload, unsigned int length) {
int pinstate = 1;
char buf[80];
// handle message arrived
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
int i = 0;
for ( i=0;i<length;i++) {
buf[i] = payload[i];
}
buf[i] = '\0';
Controller(topic, buf);
}
// strcpy(server,Settings.BrokerIP);
WiFiClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
/********************************************************************************************\
Save data into config file on SPIFFS
\*********************************************************************************************/
void SaveToFile(char* fname, int index, byte* memAddress, int datasize)
{
File f = SPIFFS.open(fname, "r+");
if (f)
{
f.seek(index, SeekSet);
byte *pointerToByteToSave = memAddress;
for (int x = 0; x < datasize ; x++)
{
f.write(*pointerToByteToSave);
pointerToByteToSave++;
}
f.close();
String log = F("FILE : File saved");
// addLog(LOG_LEVEL_INFO, log);
}
}
/********************************************************************************************\
Load data from config file on SPIFFS
\*********************************************************************************************/
void LoadFromFile(char* fname, int index, byte* memAddress, int datasize)
{
File f = SPIFFS.open(fname, "r+");
if (f)
{
f.seek(index, SeekSet);
byte *pointerToByteToRead = memAddress;
for (int x = 0; x < datasize; x++)
{
*pointerToByteToRead = f.read();
pointerToByteToRead++;// next byte
}
f.close();
}
}
/********************************************************************************************\
Save settings to SPIFFS
\*********************************************************************************************/
void SaveSettings(void)
{
SaveToFile((char*)"config.txt", 0, (byte*)&Settings, sizeof(struct SettingsStruct));
//SaveToFile((char*)"security.txt", 0, (byte*)&SecuritySettings, sizeof(struct SecurityStruct));
Serial.print("Setting Saved");
}
/********************************************************************************************\
Load settings from SPIFFS
\*********************************************************************************************/
boolean LoadSettings()
{
LoadFromFile((char*)"config.txt", 0, (byte*)&Settings, sizeof(struct SettingsStruct));
// LoadFromFile((char*)"security.txt", 0, (byte*)&SecuritySettings, sizeof(struct SecurityStruct));
}
void ResetSettings()
{
strcpy_P ( Settings.WifiSSID, ssid);
strcpy_P ( Settings.WifiKey, password);
strcpy_P ( Settings.WifiAPKey, host);
strcpy_P ( Settings.ControllerUser, MQ_user);
strcpy_P ( Settings.ControllerPassword, MQ_pass);
strcpy_P ( Settings.Host, MQ_client);
strcpy_P ( Settings.BrokerIP , server);
strcpy_P ( Settings.Version , vers);
SaveSettings();
SPIFFS.end();
ESP.restart();
}
void runEach60Seconds()
{
start = micros();
timer60 = millis() + 10000;
}
void PrintSettings()
{
Serial.println("Printing...");
char pTopic[30]={0};
strcat (pTopic,Settings.Host);
strcat (pTopic,"/");
strcat (pTopic,"outTopic");
client.publish(pTopic,Settings.WifiSSID);
client.publish(pTopic,Settings.ControllerUser);
client.publish(pTopic,Settings.Host);
char charBuf[50];
client.publish(pTopic,Settings.BrokerIP);
client.publish(pTopic,Settings.Version);
}
void Controller(const char *topic,const char *buf)
{
char * pch;
int i=0;
long pos = 0;
int pos2 = 0;
char gpio[20];
char val[20];
char command[20];
long int lgpio = 0;
Serial.printf ("Topic[%s]\n",topic);
Serial.printf ("Looking for the ':' character in \"%s\"...\n",buf);
pch=strchr(buf,':');
Serial.printf ("found at %d\n",pch-buf+1);
if(pch !=NULL){
pos = pch-buf+1;
pch=strchr(pch+1,':');
if(pch !=NULL){
pos2 = pch-buf+1;
//Serial.printf("pos2 \"%d\"...\n",pos2);
// Serial.printf("pos \"%d\"...\n",pos);
}
}else
{
pos = strlen(buf)+1;
}
for (i=0;i<pos-1;i++) {
command[i] = buf[i];
}
command[i] = '\0';
Serial.printf("Command \"%s\"\n",command);
if((pos2-pos)>0){
for ( i=pos;i<pos2-1;i++) {
gpio[i-pos] = buf[i];
}
gpio[i-pos] = '\0';
Serial.printf("gpio \"%s\"\n",gpio);
lgpio=atol(gpio);
}
if(pos2>0){
Serial.print("val = ");
for ( i=pos2;i<(unsigned)strlen(buf);i++) {
val[i-pos2] = buf[i];
}
val[i-pos2] = '\0';
}
long int lval = atol(val);
Serial.printf("value \"%d\"...\n",lval);
boolean resetflag = false;
if (strcmp (command ,"resetsettings")==0){
Serial.printf("command \"%d\".....\n",command);
ResetSettings();
}
else if(strcmp (command ,"BrokerIP")==0){
//Serial.printf("%d",lval);
strcpy(Settings.BrokerIP,val);
//Serial.printf("%d .../n",Settings.BrokerIP1);
}
else if(strcmp (command ,"WiFi")==0){
strcpy(Settings.WifiSSID,gpio);
strcpy(Settings.WifiKey,val);
resetflag = 1;
}
else if(strcmp (command ,"Broker")==0){
strcpy(Settings.ControllerUser,gpio);
strcpy(Settings.ControllerPassword,val);
resetflag = 1;
}
else if(strcmp (command , "Delay")==0){
loop_delay = lval;
}
else if(strcmp (command , "Host")==0){
strcpy_P (Settings.Host,val);
resetflag = true;
}
else if(strcmp (command , "Reboot")==0){
resetflag = true;
}
else if(strcmp (command , "Reset")==0){
ResetSettings();
}
else if(strcmp (command , "PWM")==0){
pinMode(lgpio, OUTPUT);
analogWriteFreq(500);
analogWrite(lgpio, lval);
}
else if(strcmp (command , "Version")==0){
char pTopic[30]={0};
strcat (pTopic,Settings.Host);
strcat (pTopic,"/");
client.publish(pTopic,vers);
}
else if(strcmp (command , "GPIO")==0){
pinMode(lgpio, OUTPUT);
if (lval==1){
Serial.printf("GPIO \"%d\"...\n",lgpio);
digitalWrite(lgpio, HIGH);
} else {
digitalWrite(lgpio, LOW);
Serial.printf("GPIO-off \"%d\"...\n",lgpio);
}
}
else if(strcmp (command , "PrintSettings")==0){
PrintSettings();
}
else if(strcmp (command , "PrintSensors")==0){
PrintSensors();
}
SaveSettings();
if(resetflag){
ESP.restart();
}
}
void setup() {
secondTick.attach(1, ISRwatchdog);
Serial.begin(115200);
fileSystemCheck();
LoadSettings();
Serial.println(vers);
Serial.println(Settings.Version);
if(strcmp(Settings.Version, vers) != 0)
{
ResetSettings();
}
strcpy(server,Settings.BrokerIP);
Serial.println("Booting");
WiFi.hostname(Settings.Host);
WiFi.mode(WIFI_STA);
Serial.println(Settings.WifiSSID);
Serial.println(Settings.Host);
WiFi.begin(Settings.WifiSSID, Settings.WifiKey);
int count =0;
while ((count<15)&&(WiFi.status() != WL_CONNECTED)){
//WiFi.begin(Settings.WifiSSID, Settings.WifiKey);
Serial.print(WiFi.status());
Serial.println(" Retrying connection...");
delay(1000);
count++;
while (Serial.available () > 0)
processIncomingByte (Serial.read ());
}
MDNS.begin(host);
httpUpdater.setup(&httpServer);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", Settings.Host);
Serial.print(Settings.BrokerIP);
//if (client.connect("", Settings.ControllerUser , Settings.ControllerPassword)) {
if (client.connect(Settings.Host)) {
client.publish(strcat(Settings.Host,"/outTopic"),"hello world");
//client.subscribe(strcat(Settings.Host,"system"));
Serial.print("Connected ");
Serial.println(Settings.BrokerIP);
}
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
for (int i = 0; i < sensors.getDeviceCount(); i++)
{
if (!sensors.getAddress(deviceAddress, i)) Serial.printf("Unable to find address for Device %d",i);
}
// show the addresses we found on the bus
char cSensor[15]={0};
for (int i = 0; i < sensors.getDeviceCount(); i++)
{
bool ok = sensors.getAddress(deviceAddress, i);
Serial.printf("Device %d Address: ",i);
printAddress(deviceAddress);
Serial.println();
Serial.printf("Device %d Resolution: ",i);
Serial.print(sensors.getResolution(deviceAddress), DEC);
Serial.println();
sensors.setResolution(deviceAddress, TEMPERATURE_PRECISION);
convAddress(cSensor,deviceAddress);
Serial.print(cSensor);
Serial.println();
//if (client.connect(Settings.Host, Settings.ControllerUser , Settings.ControllerPassword)) {
if (client.connect(Settings.Host)) {
client.publish("Default/outTopic",cSensor);
}
}
/* switch off led */
ArduinoOTA.setHostname(Settings.Host);
ArduinoOTA.onStart([]() { // switch off all the PWMs during upgrade
SPIFFS.end();
});
ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end
});
ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); });
/* setup the OTA server */
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
int count = 0;
while( (count<15)&&(WiFi.status() != WL_CONNECTED)){
//WiFi.begin(Settings.WifiSSID, Settings.WifiKey);
Serial.print(WiFi.status());
Serial.println(" Retrying connection...");
delay(1000);
count++;
while (Serial.available () > 0)
processIncomingByte (Serial.read ());
}
if(WiFi.status() != WL_CONNECTED);
{
count=0;
while (!client.connected()&&(count < 5)) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
Serial.print(Settings.BrokerIP);
// if (client.connect("", Settings.ControllerUser , Settings.ControllerPassword)){
if (client.connect(Settings.Host)){
Serial.println("connected");
client.publish(strcat(Settings.Host,"/outTopic"),"hello world");
//client.subscribe(strcat(Settings.Host,"system"));
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 1 second");
// Wait a second before retrying
delay(1000);
}
count = count+1;
}
}
}
void MQTT_Topic(char *Host, char *Topic, char *Value)
{
char charBuf[10]={0};
char pTopic[30]={0};
strcat (pTopic,Host);
strcat (pTopic,"/");
strcat (pTopic,Topic);
pTopic[strlen(pTopic)]='\0';
client.publish(pTopic,Value);
Serial.println();
Serial.printf("%s : \"%s\"\n",pTopic, Value);
}
void fileSystemCheck()
{
if (SPIFFS.begin())
{
Serial.println("SPIFFS Mount successful");
//addLog(LOG_LEVEL_INFO, log);
File f = SPIFFS.open("config.txt", "r");
if (!f)
{
Serial.println("formatting...");
//addLog(LOG_LEVEL_INFO, log);
SPIFFS.format();
Serial.println("format done!");
//addLog(LOG_LEVEL_INFO, log);
File f = SPIFFS.open("config.txt", "w");
if (f)
{
for (int x = 0; x < 32768; x++)
f.write(0);
f.close();
ResetSettings();
}
}
}
else
{
Serial.println("SPIFFS Mount failed");
}
}
void convAddress(char *ID, DeviceAddress deviceAddress)
{
char id[4]={""};
char buf[17]={""};
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
//if (deviceAddress[i] < 16) strcat(buf,"0");
int n = snprintf(id,3,"%02X",deviceAddress[i]);
strcat(buf,id);
buf[17]='\0';
}
strncpy (ID,buf,17);
// Serial.printf("%s : %s",id ,ID);
}
void PrintSensors()
{
Serial.println("Printing...");
char pTopic[30]={0};
strcat (pTopic,Settings.Host);
strcat (pTopic,"/");
strcat (pTopic,"outTopic");
for (int i = 0; i < sensors.getDeviceCount(); i++)
{
char cSensor[17]={0};
bool ok = sensors.getAddress(deviceAddress, i);
Serial.printf("Device %d Address: ",i);
convAddress(cSensor,deviceAddress);
client.publish(pTopic,cSensor);
}
}
void loop() {
watchdogCount = 0;
ArduinoOTA.handle();
if (!client.connected()) {
reconnect();
}
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
char cSensor[17]="XXXXXXXXXXXXXXXX";
char temp[8];
char ptemp[8];
float tempC;
char pTopic[60]={0};
// print the device information
for (int i = 0; i < sensors.getDeviceCount(); i++)
{
char pTopic[60]={0};
bool ok = sensors.getAddress(deviceAddress, i);
convAddress(cSensor,deviceAddress);
tempC = sensors.getTempC(deviceAddress);
dtostrf(tempC,3,1,temp);
strcat(pTopic,Settings.Host);
strcat(pTopic,"/");
strcat(pTopic,cSensor);
Serial.print(pTopic);
Serial.print(" : ");
Serial.println(temp);
client.publish(pTopic,temp);
}
delay(loop_delay);
client.loop();
while (Serial.available () > 0)
processIncomingByte (Serial.read ());
httpServer.handleClient();
}