-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChecker.ino
174 lines (149 loc) · 4.59 KB
/
Checker.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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <string.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define MAX_STRING_LEN 60
String sensorrequest = "http://domain.com/sensorlist.php?r=sensor&s=";
String senddata = "http://domain.com/sensorlist.php?r=data&s=";
const char *ssid = ""; // SSID
const char *password = ""; // password
const char *record;
char *ignore = "/ ,";
char *p, *i;
int address[8] = {};
char *add0;
int addr;
String req;
int pin, senno;
int pin1 = 5;
int pin2 = 11;
int przerwa = 1800000; // time between readings
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
// Serial.begin(9600);
// sensors.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, LOW);
delay(200);
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
// Serial.println("Connecting..");
}
digitalWrite(LED_BUILTIN, LOW);
}
void loop()
{
int nct = numbercheck();
// Serial.print("Ilość czujników: ");
// Serial.println(nct);
// int ct = 2;
for (int ct = 1; ct <= nct; ct++)
{
req = sensorrequest + ct;
// Serial.println(req);
// Serial.print("Odczyt numer: ");
// Serial.println(ct);
connection(req);
delay(500);
}
// Serial.print("Przerwa w działaniu");
delay(przerwa); // Send a request every 30 seconds
}
int numbercheck()
{
if (WiFi.status() == WL_CONNECTED)
{ // Check WiFi connection status
HTTPClient http; // Declare an object of class HTTPClient
http.begin("http://temp.ilovegeckos.com/sensorlist.php?r=number"); // Specify request destination
int httpCode = http.GET(); // Send the request
if (httpCode > 0)
{ // Check the returning code
String payload = http.getString(); // Get the request response payload
// Serial.println(payload); //Print the response payload
return payload.toInt();
}
http.end(); // Close connection
}
}
void connection(String reqaddress)
{
if (WiFi.status() == WL_CONNECTED)
{ // Check WiFi connection status
HTTPClient http; // Declare an object of class HTTPClient
http.begin(reqaddress); // Specify request destination
int httpCode = http.GET(); // Send the request
if (httpCode > 0)
{ // Check the returning code
String payload = http.getString(); // Get the request response payload
// Serial.println(payload); //Print the response payload
// record = payload;
record = const_cast<char *>(payload.c_str());
}
http.end(); // Close connection
}
add0 = subStr(record, ignore, 1);
addr = strtol(add0, NULL, 10);
senno = addr;
// Serial.print("Numer czujnika: ");
// Serial.print(senno);
add0 = subStr(record, ignore, 2);
addr = strtol(add0, NULL, 10);
pin = addr;
// Serial.print(", numer pinu: ");
// Serial.print(pin);
for (int x = 3; x <= 10; x++)
{
add0 = subStr(record, ignore, x);
addr = strtol(add0, NULL, 16);
address[x - 2] = addr;
}
// Serial.print(", adres czujnika: {");
// for(int y = 1; y <= 8; y++ ) {
// Serial.print(address[y], HEX);
// Serial.print(", ");
// }
// Serial.println(" }");
OneWire ds(pin);
// ds.reset();
DallasTemperature sensors(&ds);
sensors.begin();
// sensors.requestTemperatures();
uint8_t hexaddress[8] = {address[1], address[2], address[3], address[4], address[5], address[6], address[7], address[8]};
sensors.setResolution(11);
sensors.requestTemperaturesByAddress(hexaddress);
float tempread = sensors.getTempC(hexaddress);
// Serial.println(tempread);
String sendddress = senddata + senno + "&v=" + tempread;
HTTPClient http;
http.begin(sendddress); // Specify request destination
int httpCode = http.GET();
}
// Function to return a substring defined by a delimiter at an index
char *subStr(const char *str, char *delim, int index)
{
char *act, *sub, *ptr;
static char copy[MAX_STRING_LEN];
int i;
// Since strtok consumes the first arg, make a copy
strcpy(copy, str);
for (i = 1, act = copy; i <= index; i++, act = NULL)
{
// Serial.print(".");
sub = strtok_r(act, delim, &ptr);
if (sub == NULL)
break;
}
return sub;
}