# 2025-04-08-NCL Introduction to the Internet of Things
## Links
- [Carpentries](https://carpentries.org)
- [RSE Team](https://rse.ncldata.dev)
- [Code of Conduct](https://docs.carpentries.org/topic_folders/policies/code-of-conduct.html)
- This document: https://hackmd.io/@rseteam-ncl/2025-04-08-NCL
- Workshop website: https://nclrse-training.github.io/2025-04-08-NCL
- Lesson: https://carpentries-incubator.github.io/iot-novice/index.html
- [Pre-workshop survey](https://carpentries.typeform.com/to/wi32rS?slug=2025-04-08-NCL)
- [Post-workshop survey](https://carpentries.typeform.com/to/UgVdRQ?slug=2025-04-08-NCL)
- [Code Community] https://teams.microsoft.com/l/team/19%3aG79Rz7Mhk6rC0mhia04YCD-nj7WabLMxhnyb1YLp04A1%40thread.tacv2/conversations?groupId=7059214c-2200-4ad6-a739-9d350c74c7a9&tenantId=9c5012c9-b616-44c2-a917-66814fbe3e87)
- Arduino website: https://www.arduino.cc/
- Code of Conduct: https://docs.carpentries.org/topic_folders/policies/code-of-conduct.html
- Arduino Unofficial ESP32 Board Manager URL:
- Espressif ESP32: https://espressif.github.io/arduino-esp32/package_esp32_index.json
- UART Bridge: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads
select: CP210x Windows Drivers v6.7.6 9/3/2020
Apple issue accept license: `sudo xcodebuild -license`
# Wifi Credentials:
```
SSID:
Pass:
```
# - [Windows] Arduino IDE downloads
- packages: %appdata%\..\Local\Arduino15\staging\packages
- libraries: %appdata%\..\Local\Arduino15\staging\libraries
# - Survey:
[Pre-workshop survey](https://forms.office.com/Pages/DesignPageV2.aspx?prevorigin=shell&origin=NeoPortalPage&subpage=design&collectionid=2wr5tjot1sxkczrzvefc3a&id=yRJQnBa2wkSpF2aBT74-h17JJljOu2tIgSzn-7bdWh1UNE1QNThEWkRLVFhQSzhXSkVYUDNONzc4RiQlQCN0PWcu)
[Post-workshop suervey](https://forms.office.com/Pages/DesignPageV2.aspx?prevorigin=shell&origin=NeoPortalPage&subpage=design&id=yRJQnBa2wkSpF2aBT74-h17JJljOu2tIgSzn-7bdWh1UODBNRDMzNEI4RU1RRk5YMUhRSTM2SUtHSyQlQCN0PWcu)
Workshop Identifier: 2025-04-08-NCL
# - To add:
1. diagram breadboard
## Attendance:
## Please sign in using your <span style="color:red">university email</span> and your <span style="color:red">name</span>:
## Attendance
* Samantha Finnigan, Durham University, samantha.finnigan@durham.ac.uk
## MQTT Connection Code
```
#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h>
#define DHT_SENSOR_TYPE DHT11
#define DHT_SENSOR_PIN 32 // ESP32 pin connected to DHT sensor
#define LIGHT_SENSOR_PIN 36 // ESP32 pin GIOP36 (ADC0)
const int readingdelay = 3000;
const char* ssid = "Lagertha";
const char* password = "gXL5WGxz";
const char* mqtt_server = "10.42.0.1";
const String your_name = "/Samantha";
const char* mqtt_user = "carpentries";
const char* mqtt_pass = "carpentries";
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
WiFiClient espClient;
PubSubClient client(espClient);
String topic_temperature = your_name + "/dht22/temperature";
String topic_light = your_name + "/ldr/light/";
void setup() {
Serial.begin(9600); // initialize serial
Serial.println(F("Starting ..."));
dht_sensor.begin(); // initialize the DHT sensor
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
float temperature = dht_sensor.readTemperature(); // read temperature in Celsius
int lightlevel = analogRead(LIGHT_SENSOR_PIN); // read light level
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
/* When reading the temperature and light levels, we get the values
as numbers, but when we send it to the MQTT broker the values
have to be converted to strings. We therefore have to declare
variables of type char into which the number values can be transferred
*/
char tempString[8];
char lightString[8];
/**
* Transfer the number values into strings
*/
dtostrf(temperature, 1, 2, tempString);
dtostrf(lightlevel, 1, 2, lightString);
// Print the temperature to the monitor
Serial.print("Temperature: ");
Serial.println(temperature);
// Publish the temperature to the MQTT broker
publishMQTT(topic_temperature, tempString);
// Print the light level to the monitor
Serial.print("Light: ");
Serial.println(lightlevel);
// Publish the light level to the MQTT broker
publishMQTT(topic_light, lightString);
// wait a 3 seconds between readings
delay(readingdelay);
}
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte * message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
Serial.print(clientId + " attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId.c_str(), mqtt_user, mqtt_pass)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(500);
}
}
}
void publishMQTT(String topicString, char payload[8]) {
unsigned int length = topicString.length() + 1;
char topic[length];
topicString.toCharArray(topic, length);
Serial.print(topic);
Serial.print(": ");
Serial.println(payload);
client.publish(topic, payload, true);
}
String byteArrayToString(byte * byteArray, unsigned int length) {
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)byteArray[i];
}
return messageTemp;
}
```