forked from jhughes1010/weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot_data.ino
97 lines (87 loc) · 3.18 KB
/
iot_data.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
extern const char* server;
extern const char* api_key;
//=======================================================================
// sendData: all sensor data in structure is sent to Blynk or Thingspeak
//=======================================================================
void sendData(struct sensorData *environment)
{
int hourPtr = timeinfo.tm_hour;
// code block for uploading data to BLYNK App
if (App == "BLYNK") { // choose application
//Data assigned to Blynk virtual pins
Blynk.virtualWrite(V1, environment->humidity );
Blynk.virtualWrite(V2, environment->barometricPressure );
Blynk.virtualWrite(V3, environment->UVIndex);
Blynk.virtualWrite(V4, environment->windSpeed );
Blynk.virtualWrite(V5, environment->windDirection);
#ifndef METRIC
Blynk.virtualWrite(V0, environment->temperatureF );
Blynk.virtualWrite(V6, rainfall.hourlyRainfall[hourPtr] * 0.011);
Blynk.virtualWrite(V7, last24() * 0.011);
#else
Blynk.virtualWrite(V0, environment->temperatureC );
Blynk.virtualWrite(V6, rainfall.hourlyRainfall[hourPtr] * 0.011 * 25.4);
Blynk.virtualWrite(V7, last24() * 0.011 * 25.4);
#endif
Blynk.virtualWrite(V8, environment->BMEtemperature);
Blynk.virtualWrite(V9, environment->batteryVoltage);
Blynk.virtualWrite(V10, environment->lux);
Blynk.virtualWrite(V11, bootCount);
float battPerc = ((environment->batteryVoltage - batteryLowVoltage) / (4.2 - batteryLowVoltage)) * 100;
Blynk.virtualWrite(V12, battPerc);
delay(1000);
}
// code block for uploading data to Thingspeak website
else if (App == "Thingspeak")
{
Serial.println("Attempting to connect to Thingspeak");
// Send data to ThingSpeak
WiFiClient client;
if (client.connect(thingspeak_server, 80))
{
Serial.println("Connect to ThingSpeak - OK");
Serial.println("");
Serial.println("********************************************");
int hourPtr = timeinfo.tm_hour;
String postStr = "";
postStr += "GET /update?api_key=";
postStr += api_key;
postStr += "&field1=";
postStr += String(environment->batteryVoltage);
postStr += "&field2=";
postStr += String(environment->humidity);
postStr += "&field3=";
postStr += String(environment->barometricPressure);
postStr += "&field4=";
#ifdef METRIC
postStr += String(environment->temperatureC);
#else
postStr += String(environment->temperatureF);
#endif
postStr += "&field5=";
postStr += String(environment->windSpeed );
postStr += "&field6=";
postStr += String(environment->windDirection);
postStr += "&field7=";
#ifdef METRIC
postStr += String(rainfall.hourlyRainfall[hourPtr] * 0.011 * 25.4);
#else
postStr += String(rainfall.hourlyRainfall[hourPtr] * 0.011);
#endif
postStr += "&field8=";
#ifdef METRIC
postStr += String(last24() * 0.011 * 25.4);
#else
postStr += String(last24() * 0.011);
#endif
postStr += " HTTP/1.1\r\nHost: a.c.d\r\nConnection: close\r\n\r\n";
postStr += "";
client.print(postStr);
MonPrintf("Thingspeak output: %s\n", postStr);
delay(5000);
}
}
else if (App == "MQTT") {
SendDataMQTT(environment);
}
}