-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushing_data.ino
95 lines (62 loc) · 2.83 KB
/
pushing_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
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTTYPE DHT11 // DHT 11
// Pressure Sensor
int fsrPin = 0;
// DHT Sensor
const int DHTPin = D1;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
void setup() {
Serial.begin(115200); //Serial connection
delay(10);
dht.begin();
WiFi.begin("Michaels iPhone", "hello123"); //WiFi connection
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
delay(500);
Serial.println("Waiting for connection");
}
}
void loop() {
// DHT + Pressure Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// pressure
int w = analogRead(fsrPin);
String hstring = String(h);
String tstring = String(t);
String wstring = String(w);
HTTPClient http; //Declare object of class HTTPClient
while(true){
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read Pressure
int w = analogRead(fsrPin);
hstring = hstring + "," + String(h);
tstring = tstring + "," + String(t);
wstring = wstring + "," + String(w);
//temp to rest server
String bodytemp;
bodytemp = "{\"$class\":\"org.example.mynetwork.TempHumidData\",\"temperature\":[" + tstring + "],\"humidity\":[" + hstring + "],\"dataId\":\"2_tempHumid_data\",\"sensor\": \"resource:org.example.mynetwork.Sensor#2_tempHumid\"}";
http.begin("http://172.20.10.10:3000/api/TempHumidData/2_tempHumid_data"); //Specify request destination
http.addHeader("Content-Type", "application/json"); //Specify content-type header
int httpCode = http.PUT(bodytemp); //Send the request
String payload = http.getString(); //Get the response payload
http.end(); //Close connection
//pressure to rest server
String bodyweight;
bodyweight = "{\"$class\":\"org.example.mynetwork.WeightData\",\"weight\":[" + wstring + "],\"dataId\":\"2_weight_data\",\"sensor\": \"resource:org.example.mynetwork.Sensor#2_weight\"}";
http.begin("http://172.20.10.10:3000/api/WeightData/2_weight_data"); //Specify request destination
http.addHeader("Content-Type", "application/json"); //Specify content-type header
int httpCode2 = http.PUT(bodyweight); //Send the request
String payload2 = http.getString(); //Get the response payload
Serial.println(httpCode2); //Print HTTP return code
Serial.println(payload2); //Print request response payload
http.end(); //Close connection
delay(5000); //Send a request every 30 seconds
}
}