-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme-regue.ino
130 lines (112 loc) · 3.05 KB
/
me-regue.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
#include <ESP8266WiFi.h>
//defines
#define SSID_NETWORK ""
#define PASSWORD_NETWORK ""
#define INTERVAL_SEND_THINGSPEAK 30000
//constantes e variáveis globais
char UrlAPI[] = "api.thingspeak.com";
String ApiWriteKeyThingSpeak = "713U5W87STXFDUGO";
long lastConnectionTime;
WiFiClient client;
/*
* Prototypes
*/
bool checkIfCanSend(void);
void connectWifi(void);
float getHumidity(void);
void sendInformationsAPI(String data);
/**
* Send information to API.
*/
void sendInformationsAPI(String data)
{
unsigned long timeout;
if (client.connect(UrlAPI, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+ ApiWriteKeyThingSpeak +"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(data.length());
client.print("\n\n");
client.print(data);
lastConnectionTime = millis();
Serial.println("- Informações enviadas ao ThingSpeak!");
}
}
/**
* To do connection with Wifi.
*/
void connectWifi(void)
{
client.stop();
Serial.println("Connecting with WiFi network...");
Serial.println();
delay(1000);
WiFi.begin(SSID_NETWORK, PASSWORD_NETWORK);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected!");
Serial.println("IP obtido: ");
Serial.println(WiFi.localIP());
delay(1000);
}
//Função: faz a leitura do nível de umidade
//Parâmetros: nenhum
//Retorno: umidade percentual (0-100)
//Observação: o ADC do NodeMCU permite até, no máximo, 3.3V. Dessa forma,
// para 3.3V, obtem-se (empiricamente) 978 como leitura de ADC
float getHumidity(void)
{
int adc;
float percentHumidity;
adc = analogRead(0); //978 -> 3,3V
Serial.print("[Leitura ADC] ");
Serial.println(adc);
percentHumidity = 100 * ((978-(float)adc) / 978);
Serial.print("[Umidade Percentual] ");
Serial.print(percentHumidity);
Serial.println("%");
return percentHumidity;
}
/*
* Check if you're connected to WiFi and if it's time to send data to ThingSpeak.
*/
bool checkIfCanSend(void)
{
return !client.connected() && (millis() - lastConnectionTime > INTERVAL_SEND_THINGSPEAK);
}
void setup()
{
Serial.begin(9600);
lastConnectionTime = 0;
connectWifi();
Serial.println("Me Regue IoT with ESP8266.");
}
void loop()
{
float percentHumidity;
int percentHumidityTruncate;
char FieldHumidity[11];
//Força desconexão ao ThingSpeak (se ainda estiver desconectado)
if (client.connected())
{
client.stop();
Serial.println("- Desconectado do ThingSpeak");
Serial.println();
}
percentHumidity = getHumidity();
percentHumidityTruncate = (int)percentHumidity;
if(checkIfCanSend())
{
sprintf(FieldHumidity,"field1=%d", percentHumidityTruncate);
sendInformationsAPI(FieldHumidity);
}
delay(1000);
}