Skip to content

Commit 63a5e56

Browse files
committed
initial commit
1 parent 0b9445f commit 63a5e56

File tree

8 files changed

+350
-0
lines changed

8 files changed

+350
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

chapter1/.DS_Store

6 KB
Binary file not shown.
6 KB
Binary file not shown.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Import required libraries
2+
#include <ESP8266WiFi.h>
3+
#include "DHT.h"
4+
5+
// WiFi parameters
6+
const char* ssid = "your_wifi_network_name";
7+
const char* password = "your_wifi_network_password";
8+
9+
// Pin
10+
#define DHTPIN 5
11+
12+
// Use DHT11 sensor
13+
#define DHTTYPE DHT11
14+
15+
// Initialize DHT sensor
16+
DHT dht(DHTPIN, DHTTYPE, 15);
17+
18+
// Host
19+
const char* host = "54.84.241.136";
20+
21+
void setup() {
22+
23+
// Start Serial
24+
Serial.begin(115200);
25+
delay(10);
26+
27+
// Init DHT
28+
dht.begin();
29+
30+
// We start by connecting to a WiFi network
31+
Serial.println();
32+
Serial.println();
33+
Serial.print("Connecting to ");
34+
Serial.println(ssid);
35+
WiFi.begin(ssid, password);
36+
while (WiFi.status() != WL_CONNECTED) {
37+
delay(500);
38+
Serial.print(".");
39+
}
40+
41+
Serial.println("");
42+
Serial.println("WiFi connected");
43+
Serial.println("IP address: ");
44+
Serial.println(WiFi.localIP());
45+
}
46+
47+
void loop() {
48+
49+
Serial.print("Connecting to ");
50+
Serial.println(host);
51+
52+
// Use WiFiClient class to create TCP connections
53+
WiFiClient client;
54+
const int httpPort = 80;
55+
if (!client.connect(host, httpPort)) {
56+
Serial.println("connection failed");
57+
return;
58+
}
59+
60+
// Reading temperature and humidity
61+
int h = dht.readHumidity();
62+
// Read temperature as Celsius
63+
int t = dht.readTemperature();
64+
65+
// This will send the request to the server
66+
client.print(String("GET /dweet/for/myesp8266?temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
67+
"Host: " + host + "\r\n" +
68+
"Connection: close\r\n\r\n");
69+
delay(10);
70+
71+
// Read all the lines of the reply from server and print them to Serial
72+
while(client.available()){
73+
String line = client.readStringUntil('\r');
74+
Serial.print(line);
75+
}
76+
77+
Serial.println();
78+
Serial.println("closing connection");
79+
80+
// Repeat every 10 seconds
81+
delay(10000);
82+
83+
}
84+

chapter1/dht_test/.DS_Store

6 KB
Binary file not shown.

chapter1/dht_test/dht_test.ino

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Libraries
2+
#include "DHT.h"
3+
4+
// Pin
5+
#define DHTPIN 5
6+
7+
// Use DHT11 sensor
8+
#define DHTTYPE DHT11
9+
10+
// Initialize DHT sensor
11+
DHT dht(DHTPIN, DHTTYPE, 15);
12+
13+
void setup() {
14+
15+
// Start Serial
16+
Serial.begin(115200);
17+
18+
// Init DHT
19+
dht.begin();
20+
}
21+
22+
void loop() {
23+
24+
// Reading temperature and humidity
25+
float h = dht.readHumidity();
26+
// Read temperature as Celsius
27+
float t = dht.readTemperature();
28+
29+
// Display data
30+
Serial.print("Humidity: ");
31+
Serial.print(h);
32+
Serial.print(" %\t");
33+
Serial.print("Temperature: ");
34+
Serial.print(t);
35+
Serial.println(" *C ");
36+
37+
// Wait a few seconds between measurements.
38+
delay(2000);
39+
40+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Libraries
2+
#include <ESP8266WiFi.h>
3+
#include <PubSubClient.h>
4+
5+
// Credentials
6+
char* deviceId = "device_id"; // * set your device id (will be the MQTT client username)
7+
char* deviceSecret = "device_secret"; // * set your device secret (will be the MQTT client password)
8+
char* outTopic = "devices/device_id/set"; // * MQTT channel where physical updates are published
9+
char* inTopic = "devices/device_id/get"; // * MQTT channel where lelylan updates are received
10+
char* clientId = "7dslk2dfksd"; // * set a random string (max 23 chars, will be the MQTT client id)
11+
12+
// WiFi name & password
13+
const char* ssid = "your_WiFi_name";
14+
const char* password = "your_WiFi_password";
15+
16+
// Sketch logic
17+
char* payloadOn = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"on\"}]}";
18+
char* payloadOff = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"off\"}]}";
19+
20+
byte server[] = { 178, 62, 108, 47 }; // MQTT server address
21+
22+
// WiFi Client
23+
WiFiClient wifiClient;
24+
25+
// MQTT
26+
void callback(char* topic, byte* payload, unsigned int length); // subscription callback
27+
PubSubClient client(server, 1883, callback, wifiClient); // mqtt client
28+
29+
// Pins
30+
int outPin = 5; // led
31+
32+
// Logic
33+
int state = HIGH; // current state of the output pin
34+
int reading; // current reading from the input pin
35+
int previous = LOW; // previous reading from the input pin
36+
long time = 0; // the last time the output pin was toggled
37+
long debounce = 200; // the debounce time, increase if the output flickers
38+
39+
void setup() {
40+
Serial.begin(115200);
41+
delay(500);
42+
43+
// Connect to WiFi
44+
Serial.println();
45+
Serial.println();
46+
Serial.print("Connecting to ");
47+
Serial.println(ssid);
48+
49+
WiFi.begin(ssid, password);
50+
51+
while (WiFi.status() != WL_CONNECTED) {
52+
delay(500);
53+
Serial.print(".");
54+
}
55+
Serial.println("");
56+
Serial.println("WiFi connected");
57+
Serial.println("IP address: ");
58+
Serial.println(WiFi.localIP());
59+
60+
// MQTT server connection
61+
lelylanConnection();
62+
pinMode(outPin, OUTPUT); // led pin setup
63+
}
64+
65+
void loop() {
66+
67+
// Keep connection open
68+
lelylanConnection();
69+
70+
}
71+
72+
/* MQTT server connection */
73+
void lelylanConnection() {
74+
// add reconnection logics
75+
if (!client.connected()) {
76+
// connection to MQTT server
77+
if (client.connect(clientId, deviceId, deviceSecret)) {
78+
Serial.println("[PHYSICAL] Successfully connected with MQTT");
79+
lelylanSubscribe(); // topic subscription
80+
}
81+
}
82+
client.loop();
83+
}
84+
85+
/* MQTT publish */
86+
void lelylanPublish(char* value) {
87+
if (value == "on")
88+
client.publish(outTopic, payloadOn); // light on
89+
else
90+
client.publish(outTopic, payloadOff); // light off
91+
}
92+
93+
/* MQTT subscribe */
94+
void lelylanSubscribe() {
95+
client.subscribe(inTopic);
96+
}
97+
98+
/* Receive Lelylan message and confirm the physical change */
99+
void callback(char* topic, byte* payload, unsigned int length) {
100+
// copu the payload content into a char*
101+
char* json;
102+
json = (char*) malloc(length + 1);
103+
memcpy(json, payload, length);
104+
json[length] = '\0';
105+
106+
// update the physical status and confirm the executed update
107+
if (String(payloadOn) == String(json)) {
108+
Serial.println("[LELYLAN] Led turned on");
109+
lelylanPublish("on");
110+
state = HIGH;
111+
} else {
112+
Serial.println("[LELYLAN] Led turned off");
113+
lelylanPublish("off");
114+
state = LOW;
115+
}
116+
117+
digitalWrite(outPin, state);
118+
free(json);
119+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Libraries
2+
#include <ESP8266WiFi.h>
3+
#include <PubSubClient.h>
4+
#include "DHT.h"
5+
6+
// Credentials
7+
char* deviceId = "device_id"; // * set your device id (will be the MQTT client username)
8+
char* deviceSecret = "device_secret"; // * set your device secret (will be the MQTT client password)
9+
char* outTopic = "devices/device_id/set"; // * MQTT channel where physical updates are published
10+
char* inTopic = "devices/device_id/get"; // * MQTT channel where lelylan updates are received
11+
char* clientId = "7dslk2dfksd"; // * set a random string (max 23 chars, will be the MQTT client id)
12+
13+
// WiFi name & password
14+
const char* ssid = "your_WiFi_name";
15+
const char* password = "your_WiFi_password";
16+
17+
// MQTT server address
18+
byte server[] = { 178, 62, 108, 47 };
19+
20+
// WiFi client
21+
WiFiClient wifiClient;
22+
23+
// Pin & type
24+
#define DHTPIN 5
25+
#define DHTTYPE DHT11
26+
27+
// Initialize DHT sensor
28+
DHT dht(DHTPIN, DHTTYPE, 15);
29+
30+
// MQTT communication
31+
void callback(char* topic, byte* payload, unsigned int length); // subscription callback
32+
PubSubClient client(server, 1883, callback, wifiClient); // mqtt client
33+
34+
void setup() {
35+
Serial.begin(115200);
36+
delay(500);
37+
38+
// Init DHT
39+
dht.begin();
40+
41+
// Connect to WiFi
42+
Serial.println();
43+
Serial.println();
44+
Serial.print("Connecting to ");
45+
Serial.println(ssid);
46+
47+
WiFi.begin(ssid, password);
48+
49+
while (WiFi.status() != WL_CONNECTED) {
50+
delay(500);
51+
Serial.print(".");
52+
}
53+
Serial.println("");
54+
Serial.println("WiFi connected");
55+
Serial.println("IP address: ");
56+
Serial.println(WiFi.localIP());
57+
58+
lelylanConnection(); // MQTT server connection
59+
}
60+
61+
void loop() {
62+
63+
// Keep connection open
64+
lelylanConnection();
65+
66+
// Reading temperature and humidity
67+
int h = dht.readHumidity();
68+
69+
// Read temperature as Celsius
70+
int t = dht.readTemperature();
71+
72+
// Messages for MQTT
73+
String temperature = "{\"properties\":[{\"id\":\"552b72f1c70d1fd785000003\",\"value\":\"" + String(t) + "\"}]}";
74+
String humidity = "{\"properties\":[{\"id\":\"552b7315c70d1f4ee0000003\",\"value\":\"" + String(h) + "\"}]}";
75+
76+
// Publish temperature
77+
client.publish(outTopic, (char *) temperature.c_str());
78+
delay(100);
79+
80+
// Publish humidity
81+
client.publish(outTopic, (char *) humidity.c_str());
82+
delay(10000);
83+
84+
}
85+
86+
/* MQTT server connection */
87+
void lelylanConnection() {
88+
// add reconnection logics
89+
if (!client.connected()) {
90+
// connection to MQTT server
91+
if (client.connect(clientId, deviceId, deviceSecret)) {
92+
Serial.println("[PHYSICAL] Successfully connected with MQTT");
93+
lelylanSubscribe(); // topic subscription
94+
}
95+
}
96+
client.loop();
97+
}
98+
99+
/* MQTT subscribe */
100+
void lelylanSubscribe() {
101+
client.subscribe(inTopic);
102+
}
103+
104+
/* Receive Lelylan message and confirm the physical change */
105+
void callback(char* topic, byte* payload, unsigned int length) {
106+
107+
}

0 commit comments

Comments
 (0)