Nodemcu Workshop Material
Nodemcu Workshop Material
in 1
CONFIGURATION TUTORIAL
http://arduino.esp8266.com/stable/package_esp8266com_index.json
www.resprolabs.in 2
▪ After completing the above steps, go to Tools , select board and
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:
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>
www.resprolabs.in 11
const uint8_t MPU6050_REGISTER_FIFO_EN = 0x23;
void setup() {
Serial.begin(9600);
Wire.begin(sda, scl);
MPU6050_Init();
void loop() {
Read_RawValue(MPU6050SlaveAddress,
MPU6050_REGISTER_ACCEL_XOUT_H);
Ax = (double)AccelX/AccelScaleFactor;
Ay = (double)AccelY/AccelScaleFactor;
Az = (double)AccelZ/AccelScaleFactor;
Gx = (double)GyroX/GyroScaleFactor;
Gy = (double)GyroY/GyroScaleFactor;
Gz = (double)GyroZ/GyroScaleFactor;
delay(100);
Wire.beginTransmission(deviceAddress);
Wire.write(regAddress);
Wire.write(data);
Wire.endTransmission();
Wire.beginTransmission(deviceAddress);
Wire.write(regAddress);
Wire.endTransmission();
Wire.requestFrom(deviceAddress, (uint8_t)14);
www.resprolabs.in 13
}
//configure MPU6050
void MPU6050_Init(){
delay(150);
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_SIGNAL_PATH_RESET, 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