Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Building a Simple IoT Network with Arduino

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:

  • Understand the basics of networking with Arduino.
  • Learn how to interface sensors with Arduino.
  • Send data from Arduino to a web server.
  • Display real-time data on a web page.

Components List:

  • Arduino Uno (1)
  • DHT11 Temperature and Humidity Sensor (1)
  • ESP8266 WiFi Module (1)
  • Breadboard (1)
  • Jumper Wires (assorted)
  • USB Cable for Arduino (1)
  • Resistors (as needed)

Examples:

  1. Connecting the DHT11 Sensor to Arduino:
// 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");
}
  1. Sending Data to a Web Server using ESP8266:
#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");
}

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.