Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Networking is a crucial aspect of modern electronics, enabling devices to communicate and share data seamlessly. In the context of Arduino, networking allows us to create projects that can interact with the internet or other devices, opening up a vast array of possibilities for automation, monitoring, and control. This article will guide you through the process of setting up a simple IoT (Internet of Things) network using Arduino, focusing on the practical aspects and providing a detailed example to help you get started.
Project: In this project, we will create a simple IoT network where an Arduino board collects temperature and humidity data using a DHT11 sensor and sends this data to a web server. The web server will then display the data in real-time. The objectives of this project are to:
Components List:
Examples:
// Include the necessary libraries
#include "DHT.h"
// Define the pin where the DHT11 is connected
#define DHTPIN 2
// Define the type of sensor
#define DHTTYPE DHT11
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start the serial communication
Serial.begin(9600);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Read temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the results to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}
#include <ESP8266WiFi.h>
#include <DHT.h>
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Web server URL
const char* server = "http://yourserver.com/submit_data";
// DHT sensor settings
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Prepare the data to send
String data = "temperature=" + String(temperature) + "&humidity=" + String(humidity);
// Send data to the server
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
if (client.connect(server, 80)) {
client.println("POST /submit_data HTTP/1.1");
client.println("Host: yourserver.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
client.stop();
}
}
// Print the results to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}