-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
137 lines (110 loc) · 4.1 KB
/
main.cpp
File metadata and controls
137 lines (110 loc) · 4.1 KB
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
#include <WiFi.h>
#include <HttpClient.h>
// Including Wire.h and Adafruit_AHTX0.h libraries to implement in our code
#include <Wire.h>
#include <Adafruit_AHTX0.h>
char ssid[] = "NETGEAR34";
char pass[] = "Cost4Mes4,S4nt44n4,S4nDiego";
const char kHostname[] = "18.218.147.252";
const int kPort = 5000;
const char kPath[] = "/";
const int kNetworkTimeout = 30 * 1000;
const int kNetworkDelay = 1000;
// Creating object for DHT20 Sensor
Adafruit_AHTX0 tempSensor;
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("MAC address: ");
Serial.println(WiFi.macAddress());
// Debugging code to ensure connection to DHT20 is found
Serial.println("Connecting to DHT20 device.");
if (!tempSensor.begin()) {
Serial.println("The DHT20 device is not found.");
while (1); // stop program if the sensor is not found
}
Serial.println("The DHT20 was found. Initializing...");
}
void loop() {
// Debugging to get desired output
// Not necessarily needed, we can probably just comment this out
Serial.println("Start of Loop.");
// Holds sensors event data for humidity and temperature from the DHT20 device
sensors_event_t humidityValue;
sensors_event_t temperatureValue;
// Retrieve temperature and humidity data from DHT20 device
tempSensor.getEvent(&humidityValue, &temperatureValue);
// Assign float variables to hold the temperature and humidity data (using float to be able to hold decimal values)
float temp = temperatureValue.temperature;
float humidity = humidityValue.relative_humidity;
// Send the data to the cloud using new variable dataPath, which holds the path to the data values
String data = "tempVal=" + String(temp) + "&humVal=" + String(humidity);
String dataPath = String(kPath) + "?" + data;
// Print temperature and humidity values to the local terminal
Serial.print("Current temperature: ");
Serial.print(temp);
Serial.println(" °C");
Serial.print("Current humidity: ");
Serial.print(humidity);
Serial.println(" %");
// From here below, this is all the same code from the given code in the instructions
// Commented out some of the debugging print statements for if the connection was found (for the sake of cleanliness)
int err = 0;
WiFiClient c;
HttpClient http(c);
//Serial.println("Sending HTTP request..."); // Debugging output
// Changed the kpath to datapath, to get the values from the temperature and humidity.
// dataPath.c_str() to convert dataPath to const char* since that is what http.get expects to receive
err = http.get(kHostname, kPort, dataPath.c_str());
if (err == 0) {
err = http.responseStatusCode();
if (err >= 0) {
//Serial.print("Got status code: ");
//Serial.println(err);
err = http.skipResponseHeaders();
if (err >= 0) {
int bodyLen = http.contentLength();
//Serial.println("Application> HTTP Response Body : ");
unsigned long timeoutStart = millis();
char character;
while ((http.connected() || http.available()) &&
((millis() - timeoutStart) < kNetworkTimeout)) {
if (http.available()) {
character = http.read();
//Serial.print(character);
bodyLen--;
timeoutStart = millis();
} else {
delay(kNetworkDelay);
}
}
Serial.println();
} else {
Serial.print("Failed to skip response headers: ");
Serial.println(err);
}
} else {
Serial.print("Getting response failed: ");
Serial.println(err);
}
} else {
Serial.print("Connect failed: ");
Serial.println(err);
}
http.stop();
// Added delay of 2.5sec
delay(2500);
}