diff --git a/app/components/MarkdownNotes.tsx b/app/components/MarkdownNotes.tsx new file mode 100644 index 0000000..b6dddff --- /dev/null +++ b/app/components/MarkdownNotes.tsx @@ -0,0 +1,90 @@ +import ReactMarkdown from "react-markdown"; +import remarkGfm from "remark-gfm"; +import type { Components } from "react-markdown"; + +const markdownComponents: Components = { + h1: ({ children }) => ( +
{children}
, + ul: ({ children }) =>+ {children} ++ ), + pre: ({ children }) => ( +
+ {children}
+
+ ),
+ code: ({ className, children }) => {
+ const isBlock = Boolean(className);
+ if (isBlock) {
+ return {children};
+ }
+ return (
+
+ {children}
+
+ );
+ },
+ img: ({ src, alt }) => (
+ // eslint-disable-next-line @next/next/no-img-element
+ Content coming soon.
; + } + + const navLinkClass = + "px-4 py-1 text-2xl flex items-center justify-center bg-[#e2d1c1] text-[#1b0d00] rounded hover:bg-[#ac9e91] transition"; + + return ( ++ {chapter.title} +
+ ++ Welcome to the Internet of Things (IoT){" "} + masterclass on openCSE. This 10-module engineering-first curriculum takes you + from basic sensor reads to industrial cloud deployments, smart automation, and + capstone system design. +
+ ++ Use the sidebar to open each module. Content is written for hands-on labs with + Arduino IDE or PlatformIO and common hobbyist hardware. +
+Select a module from the sidebar to get started.
+Status: UNKNOWN
+ TOGGLE POWER + + +)rawliteral"; + +void setup() { + Serial.begin(115200); // ESP32 uses faster serial by default + pinMode(relayPin, OUTPUT); + digitalWrite(relayPin, LOW); + + // 1. Connect to Wi-Fi + Serial.print("Connecting to Wi-Fi"); + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + // Print the IP Address assigned by the router + Serial.println("\nConnected!"); + Serial.print("Visit this IP in your browser: http://"); + Serial.println(WiFi.localIP()); + + // 2. Define Web Server Routes + // What to do when user visits the main page "/" + server.on("/", []() { + server.send(200, "text/html", htmlPage); // Send the HTML string! + }); + + // What to do when user clicks the toggle button and goes to "/toggle" + server.on("/toggle", []() { + relayState = !relayState; // Flip the boolean state + digitalWrite(relayPin, relayState ? HIGH : LOW); // Update the physical pin + + // Redirect the user back to the main page immediately + server.sendHeader("Location", "/"); + server.send(303); + }); + + // 3. Start the server + server.begin(); + Serial.println("HTTP Server Started."); +} + +void loop() { + // 4. Listen for incoming client connections constantly + server.handleClient(); +} +``` + +### Code Explanation & Data Flow +1. **`WiFi.begin()`**: The ESP32's radio turns on and negotiates an encrypted connection with your home router. +2. **`server.on("/", ...)`**: We set up "Routes". When your phone asks for the root directory (`/`), the ESP32 replies with an HTTP `200 OK` code and transmits the `htmlPage` string. Your phone's browser renders this HTML into a visual button. +3. **`server.on("/toggle", ...)`**: When you tap the button on your phone, your browser makes a request to `http://[IP_ADDRESS]/toggle`. The ESP32 receives this, flips the voltage on Pin 2 (turning the Relay on), and tells your phone to redirect back to the home page. + +### Testing & Troubleshooting +- **Cannot connect to Wi-Fi**: The ESP32 *only* supports **2.4 GHz** Wi-Fi networks. It cannot see or connect to 5 GHz networks. Ensure your router is broadcasting a 2.4 GHz band. +- **Serial Monitor shows garbage**: Make sure the baud rate in the bottom right corner of the Serial Monitor is set to `115200` to match the code. + +### Future Improvements +This system works beautifully, but it has a fatal flaw: The web interface is trapped inside the ESP32. If you want 50 devices, you have to type 50 different IP addresses into your browser. In the coming modules, we will decouple the UI from the device, using MQTT to send data to a centralized Cloud Dashboard. + +--- +**[End of Module 4]** diff --git a/notes/iot/module-05-protocols.md b/notes/iot/module-05-protocols.md new file mode 100644 index 0000000..ef48427 --- /dev/null +++ b/notes/iot/module-05-protocols.md @@ -0,0 +1,209 @@ +# Module 5: Communication & Networking Protocols + +## 1. Introduction +In Module 4, we connected an ESP32 to a local Wi-Fi router. While Wi-Fi is great for homes, what if you are building a smart collar to track a cow across a 500-acre farm? There is no Wi-Fi router in the middle of a field. + +To solve different engineering problems, IoT uses many different "languages" and communication methods. In this module, we will explore how devices talk to each other across different ranges, and dive deep into **MQTT**, the undisputed industry standard protocol for IoT data transfer. + +--- + +## 2. Learning Objectives +By the end of this module, you will: +- Understand the trade-offs between Range, Bandwidth, and Power in IoT Networking. +- Compare Short-Range (RFID, Bluetooth, Zigbee) and Long-Range (LoRaWAN, Cellular) protocols. +- Understand why HTTP is terrible for IoT, and why MQTT is better. +- Build an RFID Security System that publishes data via MQTT. + +--- + +## 3. The Iron Triangle of IoT Communication + +When choosing a communication protocol for an IoT project, you must balance three competing factors. You can generally only pick two: +1. **Long Range** (Miles/Kilometers) +2. **High Bandwidth** (Sending lots of data quickly, like video) +3. **Low Power** (Battery lasts for years) + +- **Wi-Fi**: High Bandwidth, Low Range, High Power. (Great for Smart TVs). +- **Cellular (4G/5G)**: High Bandwidth, Long Range, HIGH Power. (Your phone battery dies in a day). +- **LoRaWAN**: Low Bandwidth, Long Range, Low Power. (Perfect for agricultural sensors sending 10 bytes of data per hour). +- **Bluetooth Low Energy (BLE)**: Low Bandwidth, Low Range, Low Power. (Perfect for a smartwatch). + +--- + +## 4. Hardware Protocols: Short vs. Long Range + +### Short-Range Protocols + +#### Hardware Spotlight: RC522 RFID Module + +*The RC522 Module reads the unique serial number hidden inside blank plastic cards or blue keyfobs using near-field radio waves.* + +- **RFID / NFC**: Near-Field Communication. Range is literally a few centimeters. Extremely secure. The "Tag" has no battery; it steals power wirelessly from the "Reader". *Use Case: Keycard door locks, Apple Pay.* +- **Zigbee / Thread**: Mesh networking. Range is ~10-100 meters. Instead of all devices talking to one central router (like Wi-Fi), every smart bulb talks to its neighbor, passing messages along the chain. *Use Case: Philips Hue Smart Bulbs.* + +### Long-Range Protocols (LPWAN) +- **LoRaWAN**: Long Range Wide Area Network. Can transmit data up to 15 kilometers in rural areas. It uses sub-GHz radio frequencies. It is incredibly slow (often taking seconds to send a single sentence of text), but a battery can last 10 years. *Use Case: Forest fire detection sensors.* +- **NB-IoT (Narrowband IoT)**: A cellular standard. It uses existing 4G/5G cell towers but requires very little power. Requires a SIM card and a monthly subscription. *Use Case: Smart city parking meters.* + +--- + +## 5. Application Protocols: HTTP vs. MQTT + +Once the physical radio waves reach the internet, how is the data formatted? + +### Why HTTP fails in IoT +In Module 4, we used HTTP (Hypertext Transfer Protocol). HTTP is a "Request/Response" protocol. Your phone *asks* for the webpage, and the server *responds*. +- **The Problem**: It is extremely "heavy". Sending a single number like `25` (the temperature) using HTTP requires sending hundreds of bytes of "Header" text (telling the server what browser you use, what language you speak, etc.). For a battery-powered sensor on a slow LoRa connection, this is a massive waste of energy. + +### The Solution: MQTT (Message Queuing Telemetry Transport) +MQTT was invented in 1999 for monitoring oil pipelines over satellite links. It is a "Publish/Subscribe" protocol. It is incredibly lightweight; the header is only 2 bytes! + +#### How MQTT Works (Pub/Sub) +Instead of devices talking directly to each other, everything talks to a central server called a **Broker**. + +1. **Topics**: Data is organized into folders called topics, like `home/livingroom/temperature`. +2. **Publishing**: A temperature sensor *Publishes* the number `25` to the topic `home/livingroom/temperature`. +3. **Subscribing**: Your phone app *Subscribes* to that topic. +4. **The Magic**: The moment the sensor publishes the data, the Broker instantly pushes that data to *any* device subscribed to the topic. The phone didn't have to ask for it! + +### Visual Flow: MQTT Architecture + +*Visual Connection Flow: Notice how the Publisher (ESP32) and the Subscriber (Phone) NEVER talk directly to each other. They only talk to the central Broker.* + +--- + +## 6. Interview-Style Conceptual Questions + +1. **You need to stream a live video feed from a drone 5 miles away. Should you use LoRaWAN?** + *Answer: No. LoRaWAN has extremely low bandwidth (kilobits per second). Video requires megabits per second. You would need Cellular (4G/5G) or a dedicated high-frequency radio link.* +2. **In MQTT, what happens if a sensor publishes data to a topic, but the phone app is turned off?** + *Answer: The Broker receives the data, realizes no one is currently subscribed to listen, and discards the data. (Unless you configure "Retained Messages", which tells the Broker to save the very last message sent for the next person who connects).* + +--- + +## 7. Practical Project: RFID Security System & MQTT Publisher + +We will build an industrial-style security system. Employees will scan an RFID keycard. The microcontroller will check if the card is authorized. If it is, it will trigger a relay (to unlock a door) AND publish a message via Wi-Fi/MQTT to a global cloud broker so the security team can monitor access live. + +### Required Components +- 1x ESP32 Development Board +- 1x RC522 RFID Reader Module & Blank Cards/Tags +- 1x LED (Simulating a Door Lock Relay) + +### Circuit Connection (SPI Protocol) & Breadboard Visual + +*Visual Connection Flow: SPI is a high-speed synchronous protocol requiring 4 data lines (MISO, MOSI, SCK, CS). The RC522 operates strictly at 3.3V. Connecting its VCC to 5V will instantly destroy it.* + +The RC522 uses the **SPI** (Serial Peripheral Interface) protocol, which requires 4 specific pins to communicate very fast. +- **SDA (SS)** to ESP32 Pin `5` +- **SCK (Clock)** to ESP32 Pin `18` +- **MOSI** to ESP32 Pin `23` +- **MISO** to ESP32 Pin `19` +- **GND** to `GND`, **3.3V** to `3.3V` (DO NOT connect to 5V, the RC522 will fry). + +### Step-by-Step Code Implementation + +> [!TIP] +> You will need to install two libraries in the Arduino IDE: `MFRC522` (for the RFID reader) and `PubSubClient` (for MQTT). + +```cpp +#include