-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266_sensor_module.ino
128 lines (98 loc) · 3.49 KB
/
esp8266_sensor_module.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
// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
// DHT 11 sensor
#define DHTPIN D5
#define DHTTYPE DHT11
// WiFi parameters
const char* ssid = "youseewifi";
const char* password = "youseewifi1";
//#define WLAN_SSID "Filip"
//#define WLAN_PASS "PasswordIsFilip"
//#define WLAN_SSID "SpaceExtremeDownstairs"
//#define WLAN_PASS "iskagehuset"
// Adafruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "filipgermanek"
#define AIO_KEY "18437194c3b845eb814a1831b140e62e" // Obtained from account info on io.adafruit.com
// DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// Setup feeds for temperature & humidity
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature");
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity");
/*************************** Sketch Code ************************************/
void setup() {
// Init sensor
dht.begin();
Serial.begin(115200);
delay(5000);
Serial.println('\n');
// Connect to WiFi access point.
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
//WiFi.mode(WIFI_STA); // SETS TO STATION MODE!
WiFi.begin(ssid, password);
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
// connect to adafruit io
connect();
}
void loop() {
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
// Grab the current state of the sensor
//int humidity_data = (int)dht.readHumidity();
int temperature_data = (int)dht.readTemperature();
// By default, the temperature report is in Celsius, for Fahrenheit uncomment
// following line.
// temperature_data = temperature_data*(9.0/5.0) + 32.0;
// Publish data
if (! temperature.publish(temperature_data))
Serial.println(F("Failed to publish temperature"));
else
Serial.println(F("Temperature published!"));
//if (! humidity.publish(humidity_data))
// Serial.println(F("Failed to publish humidity"));
//else
// Serial.println(F("Humidity published!"));
// Repeat every 10 seconds
delay(10000);
}
// connect to adafruit io via MQTT
void connect() {
Serial.print(F("Connecting to Adafruit IO... "));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (ret) {
case 1: Serial.println(F("Wrong protocol")); break;
case 2: Serial.println(F("ID rejected")); break;
case 3: Serial.println(F("Server unavail")); break;
case 4: Serial.println(F("Bad user/pass")); break;
case 5: Serial.println(F("Not authed")); break;
case 6: Serial.println(F("Failed to subscribe")); break;
default: Serial.println(F("Connection failed")); break;
}
if(ret >= 0)
mqtt.disconnect();
Serial.println(F("Retrying connection..."));
delay(5000);
}
Serial.println(F("Adafruit IO Connected!"));
}