# 2023-02-21-NCL Introduction to the Internet of Things
## Links
#
# - This document:
https://hackmd.io/@rseteam-ncl/2023-02-21-NCL
# - Workshop website:
https://nclrse-training.github.io/2023-02-21-NCL/
# - Lesson:
https://carpentries-incubator.github.io/iot-novice/index.html
# - 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:
- https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
- UART Bridge: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads
elect: CP210x Windows Drivers v6.7.6 9/3/2020
# Pre-workshop Survey:
https://forms.office.com/e/rX6ERJYt2n
# Post-workshop Survey:
https://forms.office.com/e/jpmCzJj7Hs
Workshop Identifier: 2023-02-21-NCL
Admin: michelle.gilbride@newcastle.ac.uk
## Attendance
## Notes
- Access Point: raspi-webgui
- Password: ChangeMe
### Notes for lesson
## Linux
- system python version needs pyserial installed
````
sudo adduser <username> dialout
sudo chmod a+rw /dev/ttyUSB0
Note: At least on Ubuntu Linux - need to do the latter chmod every time device is unplugged and plugged in again!
````
- 10:45 compiled, running hello
- 10:45 start episode 3 understanding the code
- 11:05 Connecting the first sensor
- 14:45 Connecting the second sensor (End of MQTT)
## ESP32-eduroam
https://github.com/martinius96/ESP32-eduroam
This could also be useful for arduino to WPA: https://docs.arduino.cc/library-examples/wifi-library/ConnectWithWPA
## Connect Raspberry Pi to Uni WiFi:
https://gist.github.com/sjmf/0071b750c23dcbeb8ae0a0d448c52343
### Code for MQTT Pub/Sub
```
#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 = "raspi-webgui";
const char* password = "ChangeMe";
const char* mqtt_server = "192.168.0.1";
const String your_name = "/SamanthaFinnigan";
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(), "jannetta", "f0r3v3rl1n8x")) {
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;
}