# 2025-04-08-NCL Introduction to the Internet of Things
## This document:
## https://hackmd.io/@RSETeam/2025-04-08-NCL
## Links
- [Carpentries](https://carpentries.org)
- [RSE Team](https://rse.ncldata.dev)
- [Code of Conduct](https://docs.carpentries.org/policies/coc/)
- 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://forms.office.com/e/AK2JaeajYf)
- [Post-workshop survey](https://forms.office.com/e/xCxsFn41CE)
- [Code Community at Newcastle](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/
- Espressif ESP32: https://espressif.github.io/arduino-esp32/package_esp32_index.json
- (WINDOWS ONLY) 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
- IDE: https://www.arduino.cc/en/software
- language reference: https://docs.arduino.cc/language-reference/
Apple issue accept license: `sudo xcodebuild -license`
pubsubclient by Nick O'Leary
# Wifi Credentials:
```
SSID: RaspAP
Pass: CARPENTRIES
IP: 192.168.100.58
```
# - [Windows] Arduino IDE downloads
- packages: %appdata%\..\Local\Arduino15\staging\packages
- libraries: %appdata%\..\Local\Arduino15\staging\libraries
# - Survey:
[Pre-workshop survey](https://forms.office.com/e/AK2JaeajYf)
[Post-workshop suervey](https://forms.office.com/e/xCxsFn41CE)
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
* Carol Booth, Newcastle University, carol.booth2@ncl.ac.uk
* Janetta Steyn
* Imre Draskovits
1. Joel Thornton, Durham University, joel.g.thornton@durham.ac.uk
1. Aliya Edwards, Durham University aliya.edwards@durham.ac.uk
1. Huijun Tang, Durham University huijun.tang@durham.ac.uk
1. Jielun Liao, Durham University, gtqw61@durham.ac.uk
1. Huanyan He, Durham University, xbjh62@durham.ac.uk
1. Quinne Lin, Newcastle University, y.lin64@newcastle.ac.uk
1. Basil Daniel Devote, Newcastle University, d.d.basil2@newcastle.ac.uk
1. Marc Milgrom, Newcastle University, nmm396@ncl.ac.uk
1. Roy Sanderson, Newcastle University, roy.sanderson@newcastle.ac.uk
1. Nana Aboagye, Newcastle University, n.y.aboagye2@newcastle.ac.uk
1. Leon Ashley, Newcastle University, c0007550@newcastle.ac.uk
2. Ibrahim Elsayed, Durham University, cfhv42@durham.ac.uk
3. Jingtong Li, Durham University, kjct63@durham.ac.uk
4. Jianlu Feng, mxnx23@durham.ac.uk
## Connecting to Eduroam
* Navigate to your wifi settings and select 'Eduroam' as the wifi network
* Input your credentials when prompted.
* Your username will either be your name-based email or univeristy code-based email: [name]@[university].ac.uk // [code]@[university].ac.uk
* The password will be your usual password to log into the univeristy system
* Once submitted, you will be prompted to accept a certificate, which may require admin approval. This is essential to be able to access the network
## Two sensors
```
#include <DHT.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;
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
void setup() {
Serial.begin(9600); // initialize serial
Serial.println(F("Starting ..."));
dht_sensor.begin(); // initialize the DHT sensor
}
void 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];
// Print the temperature to the monitor
Serial.print("Temperature: ");
Serial.println(temperature);
// Print the light level to the monitor
Serial.print("Light: ");
Serial.println(lightlevel);
// wait a 3 seconds between readings
delay(readingdelay);
}
}
```
## 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;
}
```
error message
�����������%�(k���� �Starting ...
Connecting to RaspAP
E (102) phy_comm: gpio[0] number: 2 is reserved
This is solved by using the Board type "DOIT ESP32 DEVKIT V1" target in Arduino IDE. ESP32 WROOM-DA will not work.