Networking and Communications
Assignment in Progress
To set up your system, connect the DHT11 sensor to an Arduino Uno for temperature sensing and a servo motor to an Adafruit nRF52840 board for control.
Assign unique I2C addresses (e.g., Uno as address 8) and wire the nodes accordingly.
Monitor temperature data using a serial interface on the nRF52840 board and control the servo motor based on temperature thresholds received over I2C.
Ensure proper power supply and establish the I2C bus for communication between the nodes.
#include
#include
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Wire.begin(); // Initialize I2C communication
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
Wire.beginTransmission(8); // Address of the nRF52840 board
Wire.write((byte*)&temperature, sizeof(float));
Wire.endTransmission();
delay(1000); // Adjust delay according to your requirements
}
This Arduino code snippet utilizes a DHT11 sensor to read temperature data and then transmits this data over I2C communication to another device, potentially an Adafruit nRF52840 board. Here is a breakdown of the code:
- Library Inclusion:
- The code includes the necessary libraries: Wire.h for I2C communication and DHT.h for interfacing with the DHT11 sensor.
- Pin Definitions:
- DHTPIN is defined as 2, indicating the pin to which the DHT11 sensor is connected.
- DHTTYPE is set to DHT11, specifying the type of the connected sensor.
- Setup Function:
- In the setup() function, the code initializes the I2C communication using Wire.begin() and sets up communication with the DHT11 sensor via dht.begin().
- Loop Function:
- The loop() function continuously reads the temperature from the DHT11 sensor using dht.readTemperature() and stores the value in the temperature variable.
- The temperature value is then transmitted over I2C to the device with the address 8 (assumed to be the nRF52840 board) using Wire.beginTransmission(), Wire.write(), and Wire.endTransmission().
- A delay of 1000 milliseconds (1 second) is included to control the frequency of temperature readings. You can adjust this delay based on your specific requirements.
- Functionality:
- This code snippet essentially acts as a temperature sensor node that reads temperature data from the DHT11 sensor and sends it over I2C to another device for further processing or display.
By running this code on an Arduino board with a DHT11 sensor connected to pin 2, you can continuously monitor and transmit temperature data over I2C to another device, enabling temperature monitoring and control applications.
#include
#include
Servo servoMotor;
int servoPin = 9;
float temperature = 0;
void setup() {
Wire.begin(8); // Initialize I2C communication with address 8
Wire.onReceive(receiveEvent);
servoMotor.attach(servoPin);
servoMotor.write(0); // Initially set the servo to stop
}
void loop() {
if (temperature < 24.5) {
servoMotor.write(90); // Move the servo to a specific angle (e.g., 90 degrees)
} else {
servoMotor.write(0); // Stop the servo
}
}
void receiveEvent(int numBytes) {
while (Wire.available() < sizeof(float));
Wire.readBytes((uint8_t*)&temperature, sizeof(float));
Serial.print("Received Temperature: ");
Serial.println(temperature);
}
This Arduino code snippet depicts a scenario where an Adafruit nRF52840 board is receiving temperature data via I2C from another device and controlling a servo motor connected to pin 9 based on a temperature threshold (24.5°C in this case). Here's a breakdown of the code:
- Library Inclusion:
- The code includes the necessary libraries: Wire.h for I2C communication with another device and Servo.h for controlling a servo motor.
- Global Variables:
- servoMotor is an instance of the Servo class to control the servo motor.
- servoPin is set to 9, indicating the pin to which the servo motor is connected.
- temperature is initialized to 0, representing the temperature value received over I2C.
- Setup Function:
- In the setup() function, the code initializes I2C communication with the nRF52840 board using Wire.begin(8) with address 8 for communication.
- The receiveEvent function is set as the event handler for incoming I2C data.
- The servo motor is attached to pin 9, and its initial position is set to stop (0 degrees).
- Loop Function:
- The loop() function continuously checks the temperature value received over I2C.
- If the temperature is less than 24.5°C, the servo motor is rotated to a specified angle (90 degrees in this case) using servoMotor.write(90).
- If the temperature is 24.5°C or higher, the servo motor is stopped by setting it to 0 degrees with servoMotor.write(0).
- Receive Event Function:
- The receiveEvent() function is called when data is received over I2C.
- It reads the temperature data from the I2C communication buffer and stores it in the temperature variable.
- The received temperature value is then printed to the serial monitor for monitoring purposes.
This code demonstrates a simple application where the servo motor's movement is controlled based on the temperature received over I2C, allowing for a basic temperature-dependent servo motor control system.