0% found this document useful (0 votes)
2 views

Nodemcu Workshop Material

This document provides a configuration tutorial for using the ESP8266 with various sensors and components, including moisture sensors, LDRs, DHT11, and accelerometers, along with example code for each. It also details how to control an LED via a web server and send sensor data to ThingSpeak for IoT applications. The document includes hardware requirements and code snippets for setting up and reading data from the sensors.

Uploaded by

seenuhassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Nodemcu Workshop Material

This document provides a configuration tutorial for using the ESP8266 with various sensors and components, including moisture sensors, LDRs, DHT11, and accelerometers, along with example code for each. It also details how to control an LED via a web server and send sensor data to ThingSpeak for IoT applications. The document includes hardware requirements and code snippets for setting up and reading data from the sensors.

Uploaded by

seenuhassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

www.resprolabs.

in 1
CONFIGURATION TUTORIAL

▪ Firstly open the Arduino IDE

▪ Go to files and click on the preference in the Arduino IDE

▪ Copy the below code in the Additional boards Manager

http://arduino.esp8266.com/stable/package_esp8266com_index.json

▪ Click OK to close the preference tab

www.resprolabs.in 2
▪ After completing the above steps, go to Tools , select board and

select board Manager

www.resprolabs.in 3
▪ Navigate to esp8266 by esp8266 community and install the
software for Arduino

www.resprolabs.in 4
NODE MCU PINOUT DIAGRAM

www.resprolabs.in 5
MOISTURE SENSOR:
The Soil Moisture Sensor uses capacitance to measure the water
content of soil (by measuring the dielectric permittivity of the soil, which
is a function of the water content).Simply insert this rugged sensor into
the soil to be tested, and the volumetric water content of the soil is
reported in percent.

HARDWARE REQUIRED:
▪ ESP8266
▪ MOISTURE SENSOR

CODE:

void setup() {
pinMode(A0,INPUT);
pinMode(16,OUTPUT);
Serial.begin(9600);
}
void loop() {
int moisture;
moisture=analogRead(A0);//Reads analog value
if(moisture<1000){
digitalWrite(16,HIGH); //enables Motor
Serial.println("WATER NEEDED");
}
else
{
digitalWrite(16,LOW);
Serial.println("WATER NEEDED");
}
delay(100);
}

www.resprolabs.in 6
LDR:
A photoresistor (or light-dependent resistor, LDR, or photo-
conductive cell) is a light-controlled variable resistor. The resistance of a
photoresistor decreases with increasing incident light intensity; in other
words, it exhibits photoconductivity.
HARDWARE REQUIRED:
▪ ESP8266
▪ ldr

CODE:

const int LDR = A0; // Defining LDR PIN


int input_val = 0; // Varible to store Input values

void setup() {
Serial.begin(9600);
}

void loop() {
input_val = analogRead(LDR); // Reading Input
Serial.print("LDR value is : " );
Serial.println(input_val); // Writing input on serial monitor.
delay(1000);
}

www.resprolabs.in 7
DHT11:
The DHT11 is a basic, ultra low-cost digital temperature and
humidity sensor. It uses a capacitive humidity sensor and a thermistor
to measure the surrounding air, and spits out a digital signal on the data
pin (no analog input pins needed). Its fairly simple to use, but requires
careful timing to grab data.
HARDWARE REQUIRED:
▪ ESP8266
▪ DHT11
CODE:
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 2
#define DHTTYPE DHT11 // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println("DHTxx Unified Sensor Example");
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Temperature");

www.resprolabs.in 8
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value);
Serial.println(" *C");
Serial.print ("Min Value: "); Serial.print(sensor.min_value);
Serial.println(" *C");
Serial.print ("Resolution: "); Serial.print(sensor.resolution);
Serial.println(" *C");
Serial.println("------------------------------------");
dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value);
Serial.println("%");
Serial.print ("Min Value: "); Serial.print(sensor.min_value);
Serial.println("%");
Serial.print ("Resolution: "); Serial.print(sensor.resolution);
Serial.println("%");
Serial.println("------------------------------------");
delayMS = sensor.min_delay / 1000;
}

www.resprolabs.in 9
void loop() {
delay(delayMS);
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
}
else {
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println(" *C");
}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
}
else {
Serial.print("Humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
}
}

www.resprolabs.in 10
3 AXIS ACCELEROMETER:
An accelerometer is an electromechanical device that will measure
acceleration forces. These forces may be static, like the constant force of
gravity pulling at your feet, or they could be dynamic - caused by moving
or vibrating the accelerometer.
HARDWARE REQUIRED:
▪ ESP8266
▪ ACCELEROMETER
CODE:
#include <Wire.h>

// MPU6050 Slave Device Address

const uint8_t MPU6050SlaveAddress = 0x68;

// Select SDA and SCL pins for I2C communication

const uint8_t scl = D6;

const uint8_t sda = D7;

// sensitivity scale factor respective to full scale setting provided in datasheet

const uint16_t AccelScaleFactor = 16384;

const uint16_t GyroScaleFactor = 131;

// MPU6050 few configuration register addresses

const uint8_t MPU6050_REGISTER_SMPLRT_DIV = 0x19;

const uint8_t MPU6050_REGISTER_USER_CTRL = 0x6A;

const uint8_t MPU6050_REGISTER_PWR_MGMT_1 = 0x6B;

const uint8_t MPU6050_REGISTER_PWR_MGMT_2 = 0x6C;

const uint8_t MPU6050_REGISTER_CONFIG = 0x1A;

const uint8_t MPU6050_REGISTER_GYRO_CONFIG = 0x1B;

const uint8_t MPU6050_REGISTER_ACCEL_CONFIG = 0x1C;

www.resprolabs.in 11
const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23;

const uint8_t MPU6050_REGISTER_INT_ENABLE = 0x38;

const uint8_t MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B;

const uint8_t MPU6050_REGISTER_SIGNAL_PATH_RESET = 0x68;

int16_t AccelX, AccelY, AccelZ, Temperature, GyroX, GyroY, GyroZ;

void setup() {

Serial.begin(9600);

Wire.begin(sda, scl);

MPU6050_Init();

void loop() {

double Ax, Ay, Az, T, Gx, Gy, Gz;

Read_RawValue(MPU6050SlaveAddress,
MPU6050_REGISTER_ACCEL_XOUT_H);

//divide each with their sensitivity scale factor

Ax = (double)AccelX/AccelScaleFactor;

Ay = (double)AccelY/AccelScaleFactor;

Az = (double)AccelZ/AccelScaleFactor;

T = (double)Temperature/340+36.53; //temperature formula

Gx = (double)GyroX/GyroScaleFactor;

Gy = (double)GyroY/GyroScaleFactor;

Gz = (double)GyroZ/GyroScaleFactor;

Serial.print("Ax: "); Serial.print(Ax);

Serial.print(" Ay: "); Serial.print(Ay);

Serial.print(" Az: "); Serial.print(Az);


www.resprolabs.in 12
Serial.print(" T: "); Serial.print(T);

Serial.print(" Gx: "); Serial.print(Gx);

Serial.print(" Gy: "); Serial.print(Gy);

Serial.print(" Gz: "); Serial.println(Gz);

delay(100);

void I2C_Write(uint8_t deviceAddress, uint8_t regAddress, uint8_t data){

Wire.beginTransmission(deviceAddress);

Wire.write(regAddress);

Wire.write(data);

Wire.endTransmission();

// read all 14 register

void Read_RawValue(uint8_t deviceAddress, uint8_t regAddress){

Wire.beginTransmission(deviceAddress);

Wire.write(regAddress);

Wire.endTransmission();

Wire.requestFrom(deviceAddress, (uint8_t)14);

AccelX = (((int16_t)Wire.read()<<8) | Wire.read());

AccelY = (((int16_t)Wire.read()<<8) | Wire.read());

AccelZ = (((int16_t)Wire.read()<<8) | Wire.read());

Temperature = (((int16_t)Wire.read()<<8) | Wire.read());

GyroX = (((int16_t)Wire.read()<<8) | Wire.read());

GyroY = (((int16_t)Wire.read()<<8) | Wire.read());

GyroZ = (((int16_t)Wire.read()<<8) | Wire.read());

www.resprolabs.in 13
}

//configure MPU6050

void MPU6050_Init(){

delay(150);

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_SMPLRT_DIV, 0x07);

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_1, 0x01);

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_PWR_MGMT_2, 0x00);

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_CONFIG, 0x00);

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_GYRO_CONFIG,
0x00);//set +/-250 degree/second full scale

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_ACCEL_CONFIG,
0x00);// set +/- 2g full scale

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_FIFO_EN, 0x00);

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_INT_ENABLE, 0x01);

I2C_Write(MPU6050SlaveAddress,
MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00);

I2C_Write(MPU6050SlaveAddress, MPU6050_REGISTER_USER_CTRL, 0x00);

www.resprolabs.in 14
CONTROL AN LED FROM WEBSERVER USING ESP8266:
The best thing about ESP8266 is the web server feature. The chip
enable wifi connectivity and can be turned into a small functioning web
server. It can connect to the local wifi router and with some dydns
configuration and port forwarding basically you can control your
ESP8266 from the webserver.
HARDWARE REQUIRED:
▪ ESP8266
▪ LED
CODE:
#include <ESP8266WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
int button_two = 13; //d7
int button_one = 12;//d6
WiFiServer server(80);//browser port 80
void setup() {
Serial.begin(9600);
delay(10);
pinMode(button_two, OUTPUT);
pinMode(button_one, OUTPUT);
digitalWrite(button_two, LOW);
digitalWrite(button_one, LOW);
// Connect to WiFi network
Serial.println();

www.resprolabs.in 15
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
www.resprolabs.in 16
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request button_2
int v1 = LOW;
if (request.indexOf("/button_2=ON") != -1) {
digitalWrite(button_two, HIGH);
v1 = HIGH;
}
if (request.indexOf("/stop") != -1) {
digitalWrite(button_two, LOW);
v1 = LOW;
}
// Match the request button_1
int v2 = LOW;
if (request.indexOf("/button_1=ON") != -1) {
www.resprolabs.in 17
digitalWrite(button_one, HIGH);
v2 = HIGH;
}
if (request.indexOf("/stop") != -1) {
digitalWrite(button_one, LOW);
v2 = LOW;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<center>");
client.println("<h1>RESPROLABS NODEMCU SERVER
DEMO</h1>");
client.println("<br>");
client.println("<h1>STATUS=</h1>");
client.println("</center>");
client.println("</head>");
if (v1 == HIGH) {
client.print("THE DEVICE IS ON");
} else {

www.resprolabs.in 18
client.print("THE DEVICE IS OFF");
}
client.println("<body>");
client.println("<center>");
client.println("<br><br>");
client.println("<a
href=\"/button_2=ON\"\"><button>DEVICE_1</button></a>");
client.println("<a href=\"/stop\"\"><button>STOP</button></a><br
/>");
client.println("<a
href=\"/button_1=ON\"\"><button>DEVICE_2</button></a>");
client.println("</center>");
client.println("</body>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}

www.resprolabs.in 19
CLOUD CODING WITH NODEMCU:
Sending sensor data to ThingSpeak with ESP8266. ThingSpeak is
an open source Internet of Things (IoT) application and API to store
and retrieve data from things using the HTTP protocol over the Internet
or via a Local Area Network. ThingSpeak enables the creation of sensor
logging applications, location tracking applications, and a social network
of things with status updates.
HARDWARE REQUIRED:
▪ ESP8266
▪ ldr
CODE:
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
char ssid[] = "YOUR_SSID"; // your network SSID (name)
char pass[] = "YOUR_PSWD"; // your network password
WiFiClient client;
unsigned long myChannelNumber = 219791;//your channel id
const char * myWriteAPIKey = "CCFN8L3E31L0ESKL";// Read api key void setup()
{
WiFi.begin(ssid, pass);
ThingSpeak.begin(client);

www.resprolabs.in 20
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);//Raw analog data
ThingSpeak.writeField(myChannelNumber,1,sensorValue,myWriteAPIKey);
delay(20000); // ThingSpeak will only accept updates every 15 seconds.
}

NOTE:
• You can add more fields by using
ThingSpeak.writeField(myChannelNumber, field no, Value
myWriteAPIKey);

• Ref:::: www.thingspeak.com
• Ref:::: https://www.arduino.cc/en/Tutorial/BuiltInExamples)

www.resprolabs.in 21

You might also like