IOT AND ITS APPLICATIONS ESP8266
IOT AND ITS APPLICATIONS ESP8266
ESP8266 :-
The ESP8266 module enables microcontrollers to connect to 2.4 GHz Wi-Fi, using IEEE
802.11 bgn. It can be used with ESP-AT firmware to provide Wi-Fi connectivity to
external host MCUs, or it can be used as a self-sufficient MCU by running an RTOS-
based SDK. The module has a full TCP/IP stack and provides the ability for data
processing, reads and controls of GPIOs.
LED TO ESP8266 :-
CODE
#define LED_PIN 01 // Use GPIO4 (D2 on NodeMCU)
void setup() {
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
void loop() {
digitalWrite(LED_PIN, HIGH); // Turn LED ON
delay(500); // Wait 1 second
digitalWrite(LED_PIN, LOW); // Turn LED OFF
delay(500);
// Wait 1 second
}
void setup() {
pinMode(IR_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
int sensorValue = digitalRead(IR_SENSOR_PIN);
CONNECTIONS :
ESP8266
DHT11 Pin
(NodeMCU)
VCC (5V or
3.3V
3.3V)
GND GND
DATA D4 (GPIO2)
Between DATA and
10kΩ Resistor
VCC
CODE
#include <DHT.h>
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C | Humidity: ");
Serial.print(humidity);
Serial.println("%");
CONNECTIONS :
CODE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000); // Display uptime in seconds
delay(1000);
}
5.POTENTIOMETER TO ESP8266 :
CONNECTIONS :
CODE
#define POTENTIOMETER_PIN A0
long duration;
float distanceCm;
float distanceInch;
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in
microseconds
duration = pulseIn(echoPin, HIGH);
// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;
CODE
#include <Servo.h>
Servo servo;
void setup() {
servo.attach(2); //D4
servo.write(0);
delay(2000);
}
void loop() {
servo.write(0);
delay(1000);
servo.write(180);
delay(1000);
}
CODE
#define RAIN_SENSOR_PIN 4 // D2 (GPIO4)
void setup() {
Serial.begin(115200);
pinMode(RAIN_SENSOR_PIN, INPUT);
}
void loop() {
int rainState = digitalRead(RAIN_SENSOR_PIN);
delay(1000);
}