-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme_regue-d0-iot.ino
146 lines (126 loc) · 3.03 KB
/
me_regue-d0-iot.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
#include <ESP8266WiFi.h>
#define SSID_NETWORK "iPhone de Julia"
#define PASSWORD_NETWORK "s3nh4jul14"
#define INTERVAL_SEND_THINGSPEAK 30000
#define INPUT_D0 13
#define LED_HUMIDITY 12
#define LED_NO_HUMIDITY 14
#define LED_NETWORK 15
char UrlAPI[] = "api.thingspeak.com";
String ApiWriteKeyThingSpeak = "713U5W87STXFDUGO";
long lastConnectionTime;
WiFiClient client;
/*
* Prototypes
*/
bool checkIfCanSend(void);
void connectWifi(void);
int hasHumidity(void);
void sendInformationsAPI(String data);
void showHasHumidity(void);
/**
* 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("Sent to 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: ");
Serial.println(WiFi.localIP());
delay(1000);
}
/**
* Verify if has humidity.
*/
int hasHumidity(void)
{
int isHumidity;
isHumidity = digitalRead(INPUT_D0);
Serial.println(isHumidity);
return isHumidity;
}
void showHasHumidity(void)
{
if(!hasHumidity()){
Serial.println("WITH HUMIDITY");
digitalWrite(LED_HUMIDITY, HIGH);
digitalWrite(LED_NO_HUMIDITY, LOW);
} else {
Serial.println("WITHOUT HUMIDITY");
digitalWrite(LED_HUMIDITY, LOW);
digitalWrite(LED_NO_HUMIDITY, HIGH);
}
}
/*
* 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()
{
pinMode(INPUT_D0, INPUT);
pinMode(LED_HUMIDITY, OUTPUT);
pinMode(LED_NO_HUMIDITY, OUTPUT);
pinMode(LED_NETWORK, OUTPUT);
Serial.begin(9600);
lastConnectionTime = 0;
connectWifi();
Serial.println("Me Regue IoT with ESP8266.");
}
void loop()
{
char FieldHumidity[11];
if (client.connected())
{
client.stop();
Serial.println("- Desconnect of the ThingSpeak");
Serial.println();
}
if(checkIfCanSend())
{
sprintf(FieldHumidity,"field1=%d", hasHumidity());
sendInformationsAPI(FieldHumidity);
while(client.connected())
{
digitalWrite(LED_NETWORK, HIGH);
delay(100);
digitalWrite(LED_NETWORK, LOW);
delay(100);
}
}
showHasHumidity();
delay(1000);
}