-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_to_helix_dht.ino
156 lines (119 loc) · 4.23 KB
/
node_to_helix_dht.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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "DHT.h"
#include <math.h>
#include <Adafruit_Sensor.h>
#define DHTTYPE DHT22
#define dht_dpin D1
#define LED_BUILTIN 2
const char* orionAddressPath = "IP_HELIX:1026/v2";
const char* deviceID = "ID_DEVICE";
const char* ssid = "SSID";
const char* password = "PASSWORD";
//variáveis globais
int medianTemperature;
float totalTemperature;
int medianHumidity;
float totalHumidity;
WiFiClient espClient;
HTTPClient http;
DHT dht(dht_dpin, DHTTYPE);
void setup() {
//setup
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
//iniciar sensor DHT
dht.begin();
//iniciar acesso a rede Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected WiFi network!");
delay(2000);
Serial.println("Creating " + String(deviceID) + " entitie...");
delay(2000);
//criar entidade no Helix (plug&play)
orionCreateEntitie(deviceID);
}
void loop(){
//zerar variávies
medianTemperature=0;
totalTemperature=0;
medianHumidity=0;
totalHumidity=0;
//looping para leitura do valor médio de temperatura e umidade
for(int i = 0; i < 5; i ++){
totalHumidity += dht.readHumidity();
totalTemperature += dht.readTemperature();
Serial.println("COUNT[" + String(i+1) + "] - Total Humidity: " + String(totalHumidity) + " Total Temperature: " + String(totalTemperature));
delay(5000); //delay 5 seg
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(250);
};
//cálculo dos valores médios
medianHumidity = totalHumidity/5;
medianTemperature = totalTemperature/5;
Serial.println("Median after 5 reads is Humidity: " + String(medianHumidity) + " Temperature: " + String(medianTemperature));
char msgHumidity[10];
char msgTemperature[10];
sprintf(msgHumidity,"%d",medianHumidity);
sprintf(msgTemperature,"%d",medianTemperature);
//update dos dados no Helix
Serial.println("Updating data in orion...");
orionUpdate(deviceID, msgTemperature, msgHumidity);
//indicação luminosa do envio
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
Serial.println("Finished updating data in orion...");
}
//função para a criação da entidade (plug&play)
void orionCreateEntitie(String entitieName) {
String bodyRequest = "{\"id\": \"" + entitieName + "\", \"type\": \"sensor\", \"temperature\": { \"value\": \"0\", \"type\": \"integer\"},\"humidity\": { \"value\": \"0\", \"type\": \"integer\"}}";
httpRequest("/entities", bodyRequest);
}
//função de update
void orionUpdate(String entitieID, String temperature, String humidity){
String bodyRequest = "{\"temperature\": { \"value\": \""+ temperature + "\", \"type\": \"integer\"}, \"humidity\": { \"value\": \""+ humidity +"\", \"type\": \"integer\"}}";
String pathRequest = "/entities/" + entitieID + "/attrs?options=forcedUpdate";
httpRequest(pathRequest, bodyRequest);
}
//função request
void httpRequest(String path, String data)
{
String payload = makeRequest(path, data);
if (!payload) {
return;
}
Serial.println("##[RESULT]## ==> " + payload);
}
//request
String makeRequest(String path, String bodyRequest)
{
String fullAddress = "http://" + String(orionAddressPath) + path;
http.begin(fullAddress);
Serial.println("Orion URI request: " + fullAddress);
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
http.addHeader("fiware-service", "helixiot");
http.addHeader("fiware-servicepath", "/");
Serial.println(bodyRequest);
int httpCode = http.POST(bodyRequest);
String response = http.getString();
Serial.println("HTTP CODE");
Serial.println(httpCode);
if (httpCode < 0) {
Serial.println("request error - " + httpCode);
return "";
}
if (httpCode != HTTP_CODE_OK) {
return "";
}
http.end();
return response;
}