Skip to content

Commit c645295

Browse files
committed
Add Let's Build on AWS IoT episode sample code
1 parent b83d6f4 commit c645295

19 files changed

+415
-5
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
secrets.h

README.md

+37-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
1-
## My Project
1+
# Let's Build on AWS – IoT
22

3-
TODO: Fill this README out!
3+
## Setup
44

5-
Be sure to:
5+
### Arduino IDE
66

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
7+
Download install Arduino IDE: https://www.arduino.cc/en/software
8+
9+
### ESP32 Support for Arduino
10+
11+
1. Launch Arduino IDE
12+
2. Open Preferences
13+
3. Enter `https://dl.espressif.com/dl/package_esp32_index.json` into *Additional Boards Manager URLs* field
14+
4. Click the *OK* button ![Additional Boards Manager URLs on Arduino IDE Preferences](images/additional-boards-manager.png)
15+
5. Go to Tools > Board > Boards Manager Manager...
16+
6. Search for *esp32*
17+
7. Install *esp32* by **Espressif Systems** ![Install esp32 on Arduino IDE Boards Manager](images/esp32.png)
18+
8. Go to Tools > Board > ESP32 Arduino and select *ESP32 Dev Module* ![Select ESP32 Dev Module on Tools > Board menu](images/esp32-dev-module.png)
19+
20+
### Additional Libraries
21+
22+
Some of the sample code will require additional libraries. You can install them through the Arduino IDE Library Manager:
23+
24+
- Launch Arduino IDE
25+
- Go to Tools > Manage Libraries... to open the Library Manager ![Select Manage Libries on Tools menu](images/library-manager.png)
26+
27+
#### MQTT
28+
29+
- Search for *lwmqtt*
30+
- Install [*MQTT*](https://github.com/256dpi/arduino-mqtt) by **Joel Gahwiler** ![Install MQTT library on Arduino IDE Library Manager](images/libraries/mqtt.png)
31+
32+
#### ArduinoJson
33+
34+
- Search for *ArduinoJson*
35+
- Install [*ArduinoJson*](https://github.com/bblanchon/ArduinoJson) by **Benoit Blanchon** ![Install ArduinoJson library on Arduino IDE Library Manager](images/libraries/arduinojson.png)
36+
37+
#### ClosedCube HDC1080
38+
39+
- Search for *1080*
40+
- Install [*ClosedCube HDC1080*](https://github.com/closedcube/ClosedCube_HDC1080_Arduino) by **ClosedCube** ![Install ClosedCube HDC1080 library on Arduino IDE Library Manager](images/libraries/closedcube-hdc1080.png)
941

1042
## Security
1143

images/additional-boards-manager.png

138 KB
Loading

images/esp32-dev-module.png

705 KB
Loading

images/esp32.png

83.5 KB
Loading

images/libraries/arduinojson.png

407 KB
Loading
340 KB
Loading

images/libraries/mqtt.png

324 KB
Loading

images/library-manager.png

873 KB
Loading

module-1/hello-world/hello-world.ino

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
ESP32 Blink
3+
4+
Blink the built-in LED on ESP32 Dev Kit V1
5+
*/
6+
7+
// Built-in LED on ESP32 is connected to pin number 2
8+
const int LED_BUILTIN = 2;
9+
10+
// the setup function runs once when you press reset or power the board
11+
void setup() {
12+
// initialize digital pin LED_BUILTIN as an output.
13+
pinMode(LED_BUILTIN, OUTPUT);
14+
}
15+
16+
// the loop function runs over and over again forever
17+
void loop() {
18+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
19+
delay(1000); // wait for a second
20+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
21+
delay(1000); // wait for a second
22+
}

module-1/pub-sub-mqtt/AWS.ino

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
void connectAWS() {
2+
// Configure WiFiClientSecure to use the AWS IoT device credentials
3+
net.setCACert(AWS_CERT_CA);
4+
net.setCertificate(AWS_CERT_CRT);
5+
net.setPrivateKey(AWS_CERT_PRIVATE);
6+
7+
// Connect to the MQTT broker on the AWS endpoint
8+
client.begin(AWS_IOT_ENDPOINT, 8883, net);
9+
10+
Serial.println("Connecting to AWS IOT");
11+
12+
while (!client.connect(AWS_THING_NAME)) {
13+
Serial.print(".");
14+
delay(100);
15+
}
16+
17+
Serial.println("AWS IoT Connected!");
18+
}

module-1/pub-sub-mqtt/WiFi.ino

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const char* ssid = "Let's Build on AWS";
2+
const char* password = "lets-build";
3+
4+
void connectWiFi() {
5+
Serial.println("Connecting");
6+
WiFi.begin(ssid, password);
7+
while (WiFi.status() != WL_CONNECTED) {
8+
delay(500);
9+
Serial.print(".");
10+
}
11+
Serial.println("Ready");
12+
Serial.print("IP address: ");
13+
Serial.println(WiFi.localIP());
14+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <WiFiClientSecure.h>
2+
#include <MQTTClient.h>
3+
4+
#include "secrets.h"
5+
6+
#define AWS_THING_NAME "esp32"
7+
#define AWS_IOT_PUBLISH_TOPIC "foodstuff/publish"
8+
#define AWS_IOT_SUBSCRIBE_TOPIC "foodstuff/subscribe"
9+
10+
WiFiClientSecure net = WiFiClientSecure();
11+
MQTTClient client = MQTTClient(512);
12+
13+
// the setup function runs once when you power the board or press reset
14+
void setup() {
15+
Serial.begin(115200);
16+
connectWiFi();
17+
connectAWS();
18+
19+
// Subscribe to a topic
20+
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
21+
22+
// Create a message handler
23+
client.onMessage(messageHandler);
24+
}
25+
26+
void messageHandler(String &topic, String &payload) {
27+
Serial.println("Receiving MQTT message:");
28+
Serial.println(topic + " " + payload);
29+
}
30+
31+
// the loop function runs over and over again forever
32+
void loop() {
33+
// Sends and receives packets
34+
client.loop();
35+
36+
// Publish to a topic
37+
client.publish(AWS_IOT_PUBLISH_TOPIC, "{\"message\": \"Hello from ESP32!\"}");
38+
}

module-2/call-waiter-button/AWS.ino

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
void connectAWS() {
2+
// Configure WiFiClientSecure to use the AWS IoT device credentials
3+
net.setCACert(AWS_CERT_CA);
4+
net.setCertificate(AWS_CERT_CRT);
5+
net.setPrivateKey(AWS_CERT_PRIVATE);
6+
7+
// Connect to the MQTT broker on the AWS endpoint
8+
client.begin(AWS_IOT_ENDPOINT, 8883, net);
9+
10+
Serial.println("Connecting to AWS IOT");
11+
12+
while (!client.connect(AWS_THING_NAME)) {
13+
Serial.print(".");
14+
delay(100);
15+
}
16+
17+
Serial.println("AWS IoT Connected!");
18+
}

module-2/call-waiter-button/WiFi.ino

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const char* ssid = "Let's Build on AWS";
2+
const char* password = "lets-build";
3+
4+
void connectWiFi() {
5+
Serial.println("Connecting");
6+
WiFi.begin(ssid, password);
7+
while (WiFi.status() != WL_CONNECTED) {
8+
delay(500);
9+
Serial.print(".");
10+
}
11+
Serial.println("Ready");
12+
Serial.print("IP address: ");
13+
Serial.println(WiFi.localIP());
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <WiFiClientSecure.h>
2+
#include <MQTTClient.h>
3+
#include <ArduinoJson.h>
4+
5+
#include "secrets.h"
6+
7+
#define AWS_THING_NAME "esp32"
8+
#define AWS_IOT_PUBLISH_BUTTON "callwaiter/button"
9+
10+
#define TABLE_NUMBER 13 // table number
11+
#define BUTTON_PIN 23 // push-buton pin
12+
13+
WiFiClientSecure net = WiFiClientSecure();
14+
MQTTClient client = MQTTClient(512);
15+
volatile unsigned long lastCallWaiterTime = 0; // Records last call waiter time
16+
volatile bool callWaiter = false; // Flag to call waiter
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
21+
// Initialize the push-button pin as an input
22+
// with an internal pull-up resistor enabled:
23+
pinMode(BUTTON_PIN, INPUT_PULLUP);
24+
25+
// When push-button pin goes from high to low (FALLING),
26+
// call the buttonPressed() function.
27+
attachInterrupt(BUTTON_PIN, buttonPressed, FALLING);
28+
29+
connectWiFi();
30+
connectAWS();
31+
}
32+
33+
void buttonPressed() {
34+
unsigned long currentTime = millis();
35+
36+
// We want to avoid calling the waiter multiple times at once
37+
if (currentTime - lastCallWaiterTime > 1000) {
38+
callWaiter = true;
39+
lastCallWaiterTime = currentTime;
40+
}
41+
}
42+
43+
void loop() {
44+
// Sends and receives packets
45+
client.loop();
46+
47+
if (callWaiter) {
48+
Serial.println("Calling waiter...");
49+
50+
// Create JSON document
51+
StaticJsonDocument<200> json;
52+
json["tableNumber"] = TABLE_NUMBER;
53+
json["deviceTime"] = millis();
54+
json["deviceName"] = AWS_THING_NAME;
55+
56+
// Serialize JSON
57+
char jsonBuffer[128];
58+
serializeJson(json, jsonBuffer);
59+
60+
// Publish message to callwaiter/button topic
61+
client.publish(AWS_IOT_PUBLISH_BUTTON, jsonBuffer);
62+
63+
// Reset the callWaiter flag
64+
callWaiter = false;
65+
}
66+
}

module-2/push-button/push-button.ino

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#define BUTTON_PIN 23 // push-button pin
2+
3+
int buttonState = 0; // variable for reading the push-button status
4+
5+
// the setup routine runs once when you press reset:
6+
void setup() {
7+
// initialize serial communication at 115200 bits per second:
8+
Serial.begin(115200);
9+
10+
// initialize the push-button pin as an input
11+
// and enable the internal pull-up resistor
12+
pinMode(BUTTON_PIN, INPUT_PULLUP);
13+
}
14+
15+
// the loop routine runs over and over again forever:
16+
void loop() {
17+
// read the state of the push-button value:
18+
buttonState = digitalRead(BUTTON_PIN);
19+
20+
// check if the push-button is pressed
21+
// if it is, the buttonState is LOW:
22+
if (buttonState == LOW) {
23+
Serial.println("Button Down");
24+
} else {
25+
Serial.println("Button Up");
26+
}
27+
}

module-3/AWS.ino

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
void connectAWS()
2+
{
3+
// Configure WiFiClientSecure to use the AWS IoT device credentials
4+
net.setCACert(AWS_CERT_CA);
5+
net.setCertificate(AWS_CERT_CRT);
6+
net.setPrivateKey(AWS_CERT_PRIVATE);
7+
8+
// Connect to the MQTT broker on the AWS endpoint we defined earlier
9+
client.begin(AWS_IOT_ENDPOINT, 8883, net);
10+
client.onMessage(messageHandler);
11+
Serial.print("Connecting to AWS IOT");
12+
13+
while (!client.connect(THINGNAME)) {
14+
Serial.print(".");
15+
delay(100);
16+
}
17+
18+
if (!client.connected()) {
19+
Serial.println("AWS IoT Timeout!");
20+
return;
21+
}
22+
// Subscribe to a topic
23+
client.subscribe("control/lamp");
24+
// Create a message handler
25+
26+
Serial.println("AWS IoT Connected!");
27+
}
28+
29+
void messageHandler(String &topic, String &payload) {
30+
Serial.println("receiving MQTT message");
31+
Serial.println("incoming: " + topic + " - " + payload);
32+
int i = payload.toInt();
33+
digitalWrite(IO_RELAY,i);
34+
}

0 commit comments

Comments
 (0)