Chapter 4: Implementation of IoT with Raspberry Pi and Data Handling Analytics
4.1 Implementation of IoT with Raspberry Pi
What is Raspberry Pi? Raspberry Pi is a small, affordable, and powerful computer that can be used for various electronics projects. It is popular for IoT applications because of its flexibility, connectivity options, and ability to run a full operating system like Linux.
Why Use Raspberry Pi for IoT?
- Computational Power: More powerful than microcontrollers like Arduino, making it suitable for complex IoT tasks.
- Connectivity: Built-in Wi-Fi and Bluetooth for easy connection to the internet and other devices.
- Versatile: Can handle multiple sensors, actuators, and complex software applications.
- Operating System: Runs on a complete OS like Raspbian (now Raspberry Pi OS), allowing multitasking and software installations.
Basic Components of Raspberry Pi:
- Processor and Memory: The CPU and RAM are the brain of the Pi, managing tasks and running applications.
- GPIO Pins: General Purpose Input/Output pins used to connect sensors, actuators, and other devices.
- USB Ports: For connecting peripherals like a mouse, keyboard, or USB storage.
- HDMI Port: For connecting a monitor or TV.
- MicroSD Card Slot: For storage and running the operating system.
- Power Supply Port: Typically uses a micro-USB or USB-C port for power.
Setting Up Raspberry Pi for IoT:
- Install Raspberry Pi OS:
- Download the OS image from the official website.
- Use software like Balena Etcher to flash the OS onto a microSD card.
- Boot the Raspberry Pi:
- Insert the microSD card and power on the Pi. Connect a monitor, keyboard, and mouse for initial setup.
- Connect to the Internet:
- Use the built-in Wi-Fi or Ethernet to connect to the internet.
- Enable GPIO:
- Open the terminal and type
sudo raspi-config
to enable GPIO and other interfaces like I2C or SPI.
- Open the terminal and type
Programming Raspberry Pi:
- Raspberry Pi can be programmed using various languages, but Python is most commonly used due to its simplicity and rich libraries.
Basic Python Program to Blink an LED:
Circuit Setup:
- Connect the LED’s positive leg (anode) to GPIO pin 17 and the negative leg (cathode) to a resistor, then to ground (GND).
Python Code:
"PYTHON"import RPi.GPIO as GPIO import time LED_PIN = 17 GPIO.setmode(GPIO.BCM) # Use Broadcom pin numbering GPIO.setup(LED_PIN, GPIO.OUT) # Set the LED pin as output try: while True: GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED on time.sleep(1) # Wait for 1 second GPIO.output(LED_PIN, GPIO.LOW) # Turn LED off time.sleep(1) # Wait for 1 second except KeyboardInterrupt: GPIO.cleanup() # Clean up GPIO settings on exit
- This code will make an LED connected to GPIO pin 17 blink on and off every second.
Integrating Sensors and Actuators with Raspberry Pi:
- Sensors and actuators are connected to the GPIO pins.
- For example, to read data from a temperature sensor like the DHT11, you can use a Python library to access the sensor data and display it on the console or a web server.
Example: Reading Temperature and Humidity from DHT11:
Circuit Setup:
- Connect the DHT11 sensor’s VCC pin to 5V, GND to ground, and data pin to GPIO 4.
Python Code:
"PYTHON"import Adafruit_DHT DHT_SENSOR = Adafruit_DHT.DHT11 DHT_PIN = 4 while True: humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN) if humidity is not None and temperature is not None: print(f"Temp={temperature}C Humidity={humidity}%") else: print("Failed to retrieve data from sensor") time.sleep(2)
- This code will print the temperature and humidity readings from the DHT11 sensor every 2 seconds.
Running a Simple IoT Project:
- Set Up a Web Server:
- Use Flask or Django in Python to create a simple web server that displays sensor data in real-time.
- Display Sensor Data:
- Use a web page to show the temperature and humidity data, making it accessible over the internet.
- Remote Control:
- Add buttons on the web page to control actuators like turning on/off LEDs or motors.
4.2 Data Handling and Analytics
Why is Data Handling Important in IoT? IoT devices generate a large amount of data. Efficient data handling and analysis are crucial for making sense of this data and taking informed actions. This involves data collection, storage, processing, and analysis.
Key Steps in Data Handling:
Data Collection:
- Sensors collect data from the physical world, such as temperature, humidity, motion, etc.
- Data can be collected at regular intervals and sent to a central server for further processing.
Data Storage:
- Data needs to be stored for future analysis. This can be done locally on the Raspberry Pi or sent to cloud services like AWS, Google Cloud, or Azure.
- Use databases like SQLite, MySQL, or NoSQL databases like MongoDB for storing IoT data.
Data Processing:
- Data processing involves cleaning, filtering, and transforming raw data into a usable format.
- Python libraries like Pandas and NumPy are often used for data processing.
Data Analytics:
- Data analytics involves examining data sets to uncover patterns, trends, and insights.
- This can be done using statistical analysis, machine learning, or data visualization techniques.
- Tools like Python's Matplotlib or Seaborn can be used to create graphs and charts to visualize the data.
Example: Analyzing Temperature Data with Python:
Storing Data in CSV:
- Record temperature data from a sensor and store it in a CSV file for analysis.
"PYTHON"import csv import Adafruit_DHT DHT_SENSOR = Adafruit_DHT.DHT11 DHT_PIN = 4 with open('temperature_data.csv', mode='w') as file: writer = csv.writer(file) writer.writerow(["Time", "Temperature", "Humidity"]) while True: humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN) if humidity is not None and temperature is not None: writer.writerow([time.ctime(), temperature, humidity]) print(f"Data logged: {temperature}C, {humidity}%") else: print("Failed to retrieve data") time.sleep(2)
Analyzing Data:
- Use Pandas to read the CSV file and analyze data, like finding average temperature.
"PYTHON"import pandas as pd data = pd.read_csv('temperature_data.csv') print(data.describe()) # Print summary statistics
Visualizing Data:
- Plot the data using Matplotlib to find trends.
"PYTHON"import matplotlib.pyplot as plt plt.plot(data['Time'], data['Temperature']) plt.xlabel('Time') plt.ylabel('Temperature') plt.title('Temperature over Time') plt.show()
Cloud Analytics for IoT:
- Cloud Platforms: Use cloud services like AWS IoT, Google Cloud IoT, or Azure IoT for large-scale data storage and analytics.
- Real-Time Analytics: Monitor and analyze data in real-time to make instant decisions, like triggering alerts or controlling devices.
Challenges in Data Handling and Analytics:
- Scalability: As the number of devices increases, handling large amounts of data becomes challenging.
- Data Security: Ensuring that data is secure and not accessible by unauthorized users.
- Data Integrity: Ensuring the accuracy and consistency of data.
By understanding the implementation of IoT with Raspberry Pi and data handling, students can create robust IoT systems that not only collect data but also analyze it for meaningful insights. For more detailed notes and updates, keep visiting the 'Rajasthan Polytechnic BTER' blog.
These notes are according to Syllabus provided by BTER (Board of Technical Education Rajasthan, Jodhpur) for Polytechnic 5th Semester Students.
Hope this post will help you. Write down Your query and Suggestion in Comment Section.
Thankyou
Regards
Garima Kanwar
0 Comments