Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
512 views

NodeMCU Tutorial

The document provides information about the ASE CSE IoT Board including: 1) The NodeMCU is the controlling unit and pin mappings are provided between the NodeMCU and ESP8266 chip. 2) On-board components including LEDs and a switch are described along with example code to interface with them. 3) Instructions are given on installing the NodeMCU in the Arduino IDE and example exercises are listed including blinking LEDs, reading a sensor, and interfacing with an LCD display.

Uploaded by

v_ananthu
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
512 views

NodeMCU Tutorial

The document provides information about the ASE CSE IoT Board including: 1) The NodeMCU is the controlling unit and pin mappings are provided between the NodeMCU and ESP8266 chip. 2) On-board components including LEDs and a switch are described along with example code to interface with them. 3) Instructions are given on installing the NodeMCU in the Arduino IDE and example exercises are listed including blinking LEDs, reading a sensor, and interfacing with an LCD display.

Uploaded by

v_ananthu
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 49

ASE CSE IoT Board

NodeMCU is the controlling unit of ASE CSE IoT Board


Board level pin details of ASE CSE IoT Board

Note: All IO devices, sensor modules of Ardunio All-In One board can be used in this IoT
board without any changes.

S- Signal

V – 5V

G - Ground
NodeMCU Board from Third Party

ESP8266 GPIO s are mapped as detailed below in NodeMCU

NodeMCU ESP8266
D0 GPIO 16
D1 GPIO 5
D2 GPIO 4
D3 GPIO 0
D4 GPIO 2
D5 GPIO 14
D6 GPIO 12
D7 GPIO 13
D8 GPIO 15
D9 GPIO 3
D10 GPIO 1
LED_BUILTIN = D0 (GPIO 16)
Details about On-Board LEDs and Switch:

Flash Switch is connected to D3 and Flash LED is connected to D4

LED_BUILTIN is connected to D0.

So users can make use of theses On-Board Switch and LEDs for exercises.

Please, note that, To Switch ON LED, give Logic LOW and to Switch OFF LED, give logic
HIGH. (Negative Logic is used)
When Flash Button is not Pressed (IDLE state), D3 will be HIGH and when Flash Button is
Pressed (Active State), D3 will be LOW.
Program to Blink LED_BuiltIN

void setup() {

pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output

// the loop function runs over and over again forever

void loop() {

digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level

// but actually the LED is on; this is because

// it is acive low on the ESP-01)

delay(1000); // Wait for a second

digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH

delay(2000); // Wait for two seconds (to demonstrate the active low LED)

You can Replace LED_BUILTIN by D0 also…

Program to interface LED_BUILTIN and On-Board Button

// constants won't change. They're used here to

// set pin numbers:

constintbuttonPin = D3; // the number of the pushbutton pin


constintledPin = D0; // the number of the LED pin

// variables will change:

intbuttonState = 0; // variable for reading the pushbutton status

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED on:

digitalWrite(ledPin, HIGH);

} else {
// turn LED off:

digitalWrite(ledPin, LOW);

}
Instructions to install NodeMCU in Arduino:

Keep your PC /lapop connected to the internet while installing


NodeMCU.

1. Download Arduino IDE.


2. Open you IDE and click on "File -> Preferences".
3. In "Aditional Boards Manager URLs" add this line and click on "OK":
4. "http://arduino.esp8266.com/stable/package_esp8266com_index.
json"
5. Go to "Tools -> Board -> Boards Manager", type "ESP8266" and
install it.
6. Go again to "Tools -> Board" and select "NodeMCU 1.0 (ESP12E
module)"
7. Go again to “Tools->Port” and select the com port to which NodeMCU
is connected.

https://www.youtube.com/watch?v=cNqNhR_2mp0

Exercises:

Exercise 1: WiFi Scan

File -> Examples -> ESP8266WiFi -> WiFiScan

/* This sketch demonstrates how to scan WiFi networks. */

#include "ESP8266WiFi.h"
void setup() {

Serial.begin(115200);

// Set WiFi to station mode and disconnect from an AP if it was previously connected

WiFi.mode(WIFI_STA);

WiFi.disconnect();

delay(100);

Serial.println("Setup done");

void loop() {

Serial.println("scan start");

// WiFi.scanNetworks will return the number of networks found

int n = WiFi.scanNetworks();

Serial.println("scan done");

if (n == 0) {

Serial.println("no networks found");

} else {

Serial.print(n);

Serial.println(" networks found");

for (int i = 0; i < n; ++i) {


// Print SSID and RSSI for each network found

Serial.print(i + 1);

Serial.print(": ");

Serial.print(WiFi.SSID(i));

Serial.print(" (");

Serial.print(WiFi.RSSI(i));

Serial.print(")");

Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");

delay(10);

Serial.println("");

// Wait a bit before scanning again

delay(5000);

Sample output (Note that COM Port is configured to work at baud rate of 115200 )
Digital IOs
Exercise 2: LED Blinking

Wiring: Connect Single Color Red LED to port D0

Program: blink LED connected to port D0 */

/* to

void setup() {

pinMode ( D0, OUTPUT);

void loop() {

digitalWrite(D0, HIGH);

delay(1000);

digitalWrite(D0, LOW);

delay(1000);
}

Sample Output: Red LED connected to port D0 blinks at an interval of 1 second.

Exercise 3: Blinking of Bi-Color LEDs (Alternate blinking of Red and Green LEDs.)

Wiring: Connect Bicolor LED to D0 and D1 of Node MCUs

Red LED is connected to D0 and Green LED is connected to D1.

Program:

/* Alternate blinking of Red (D0) and Green LEDs (D1) */

void setup() {

pinMode ( D0, OUTPUT);

pinMode (D1, OUTPUT);

void loop() {

digitalWrite(D0, HIGH);

digitalWrite(D1, LOW);

delay(1000);

digitalWrite(D0, LOW);

digitalWrite(D1,HIGH);

delay(1000);

Sample Output: Red LED connected to port D0 is ON, Green LED connected to port D1 will be OFF and
Green LED will be ON for 1 second. This process will be repeated till power is applied.
Exercises 4: Switch and LED (INPUT and OUTPUT Concept)

Wiring: LED is connected to D0 and Switch is connected to D1

When Switch (Push Button) is pressed, LED will be switched ON and when Switch (Push
Button) is released, LED will be switched OFF.

Program:

/* Input – Switch and Output – LED demo */

const int ledPin = D0; // the number of the LED pin

const int buttonPin = D1; // the number of the pushbutton pin

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED on:


digitalWrite(ledPin, HIGH);

} else {

// turn LED off:

digitalWrite(ledPin, LOW);

Sample Output:

When Switch is pressed, LED gets switched ON and the LED will be switched OFF when the switch gets
released.

Exercises 5: Sensor Interface to ADC Channel and sending equivalent digital data through
serial interface.

Wiring: POT is connected to A0 and Serial port is configured to 9600, N81.

When POT is rotated, analog value applied to Analog Channel A0 will be varied from 0V to
5V and its equivalent digital value (in decimal form) will be displayed in the serial monitor..

Program:

// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin 0:

int sensorValue = analogRead(A0);


// print out the value you read:

Serial.println(sensorValue);

delay(100); // delay in between reads for stability

S.No Analog Input in Volts Binary Output Decimal


Output

1 0.0V 0b 00 0000 0000 0

2 1.25 V 0b 00 1111 11111 255

3 2.5V 0b 01 1111 1111 511

4 3.75 V 0b 10 1111 1111 767

5 5.0V 0b 11 1111 1111 1023

Exercise 6: Liquid crystal Display:


4 bit Mode LCD

LCD Pins NodeMCU Pins

RS D0

EN D1

Data 4 D2

Data 5 D5

Data 6 D6

Data 7 D7

/*

LiquidCrystal Library - Hello World Demonstrates the use a 16x2 LCD display. The LiquidCrystal

library works with all LCD displays that are compatible with the Hitachi HD44780 driver. There are many
of them out there, and you can usually tell them by the 16-pin interface. This sketch prints "Hello
World!" to the LCD and shows the time.

The circuit:

* LCD RS pin to digital pin 12

* LCD Enable pin to digital pin 11

* LCD D4 pin to digital pin 5

* LCD D5 pin to digital pin 4

* LCD D6 pin to digital pin 3

* LCD D7 pin to digital pin 2

* LCD R/W pin to ground

* LCD VSS pin to ground

* LCD VCC pin to 5V

* 10K resistor:

* ends to +5V and ground


* wiper to LCD VO pin (pin 3) */

// include the library code:

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin

// with the arduino pin number it is connected to

const int rs = D0, en = D1, d4 = D2, d5 = D5, d6 = D6, d7 = D7;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

lcd.print("hello, world!");

void loop() {

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

lcd.setCursor(0, 1);

// print the number of seconds since reset:

lcd.print(millis() / 1000);

Output:

The following String will be displayed in the LCD.

Hello World
1

Exercise 7:

Digital Voltmeter

Potentiometer POT is connected to Analog Channel A0 and LCD wiring as per the previous exercises.

Program:

// include the library code:

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin

// with the arduino pin number it is connected to

const int rs = D0, en = D1, d4 = D2, d5 = D5, d6 = D6, d7 = D7;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

Serial.begin(9600);

void loop() {

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

int sensorValue = analogRead(A0);

float volt = ( sensorValue * 5.0)/ 1023.0 ;


lcd.setCursor(0,0);

lcd.print("Digital Voltmeter!");

lcd.setCursor(0, 1);

lcd.print(" ");

lcd.setCursor(0, 1);

// print the number of seconds since reset:

lcd.print(volt);

Serial.println(volt);

delay(100);

Output:

Digital Voltmet

1.25
Exercise 8:

Digital Therometer

Temperature Sensor LM35 is connected to Analog Channel A0 and LCD wiring as per the previous
exercises.

Program:

// include the library code:

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin

// with the arduino pin number it is connected to

const int rs = D0, en = D1, d4 = D2, d5 = D5, d6 = D6, d7 = D7;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

Serial.begin(9600);

void loop() {

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

int Temperature = analogRead(A0);

float TempinC = ( Temperature * 3300)/ 10230.0 ;

lcd.setCursor(0,0);

lcd.print("Digital Thermometer!");
lcd.setCursor(0, 1);

lcd.print(" ");

lcd.setCursor(0, 1);

// print the number of seconds since reset:

lcd.print(TempinC);

Serial.println(TempinC);

delay(1000);

Output:

Digital Thermome

38.78
Exercise 9: BlueTooth

Blue Tooth Module HC-05 is interfaced to NodeMCU through Soft Serial.

BlueTooth Rx -> D7 of NodeMCU

BlueTooth Tx -> D8 of NodeMCU

Analog Value at A0 will be sent to mobile phone through Blue Tooth. LED connected to Digital Output DO
will be controlled (ON/OFF) by sending a Character (A to ON and a to OFF) from Mobile phone through
Blue Tooth

Program:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(D7, D8); // RX, TX

char c;

#define LED D4

void setup() {

// Open serial communications and wait for port to open:

Serial.begin(9600);

while (!Serial) {

; // wait for serial port to connect. Needed for native USB port only

pinMode(LED,OUTPUT);

Serial.println("Goodnight moon!");

// set the data rate for the SoftwareSerial port


mySerial.begin(9600);

mySerial.println("Hello, world?");

void loop() { // run over and over

//Serial.println("HI...");

int a =analogRead(A0);

mySerial.println(a);

delay(200);

// if (mySerial.available()) {

// Serial.write(mySerial.read());

// }

if (mySerial.available()) {

c = mySerial.read();

if(c == 'A')

digitalWrite(LED,HIGH);

if(c == 'a')

digitalWrite(LED,LOW);

In your Android Mobile phone, install BlueTooth terminal HC-05 available in Google play store.

Select the paired device RiyassaBT1. (BlueTooth module HC-05 is already renamed as RiyassaBT1 and it is
paired with our mobile phone – default pairing code is 1234)
Run the BlueTooth terminal HC-05 App and select RiyassaBT1 as the device, you will be able to see the
value of Analog POT in The terminal of BT HC-05 App.

By sending character A, LED at port D0 of NodeMCU will be switched ON.

By sending character a, LED at port D0 of NodeMCU will be switched OFF.

Both Transmission and reception through Bluetooth interface of NodeMCU can be understood through
this program.

https://www.youtube.com/watch?v=CW0ZE_f2g30

Exercise 10

DHT Sensor Interface to Node MCU

Connect + pin of 3.3 V of NodeMCU and – pin of DHT 11 to Gnd of NodeMCU and Out pin of DHT11 to
D4 (GPIO 2) of Node MCU.

Program

// Example testing sketch for various DHT humidity/temperature sensors

// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN D2 // what digital pin we're connected to

// Uncomment whatever type you're using!

#define DHTTYPE DHT11 // DHT 11

//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V

// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!

// Connect pin 2 of the sensor to whatever your DHTPIN is

// Connect pin 4 (on the right) of the sensor to GROUND

// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.

// Note that older versions of this library took an optional third parameter to

// tweak the timings for faster processors. This parameter is no longer needed

// as the current DHT reading algorithm adjusts itself to work on faster procs.

DHT dht(DHTPIN, DHTTYPE);

void setup() {

Serial.begin(9600);

Serial.println("DHTxx test!");

dht.begin();

void loop() {

// Wait a few seconds between measurements.

delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!

// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)

float h = dht.readHumidity();
// Read temperature as Celsius (the default)

float t = dht.readTemperature();

// Read temperature as Fahrenheit (isFahrenheit = true)

float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).

if (isnan(h) || isnan(t) || isnan(f)) {

Serial.println("Failed to read from DHT sensor!");

return;

// Compute heat index in Fahrenheit (the default)

float hif = dht.computeHeatIndex(f, h);

// Compute heat index in Celsius (isFahreheit = false)

float hic = dht.computeHeatIndex(t, h, false);

Serial.print("Humidity: ");

Serial.print(h);

Serial.print(" %\t");

Serial.print("Temperature: ");

Serial.print(t);

Serial.print(" *C ");

Serial.print(f);

Serial.print(" *F\t");

Serial.print("Heat index: ");


Serial.print(hic);

Serial.print(" *C ");

Serial.print(hif);

Serial.println(" *F");

In the serial monitor, we get Temperature and humidity values.

ThingSpeak

#include <ESP8266WiFi.h>

String apiWritekey = "GY547B219LGNHOV6";

const char* ssid = "SSID";

const char* password = "PASSWORD" ;

const char* server = "api.thingspeak.com";

float resolution=3.3/1023;

WiFiClient client;

void setup() {

Serial.begin(115200);

WiFi.disconnect();

delay(10);

WiFi.begin(ssid, password);
Serial.println();

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.print("NodeMcu connected to wifi...");

Serial.println(ssid);

Serial.println();

void loop() {

float temp = ((analogRead(A0) * resolution) * 100)+23.89;

if (client.connect(server,80)) {

String tsData = apiWritekey;

tsData +="&field1=";

tsData += String(temp);
tsData += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");

client.print("Host: api.thingspeak.com\n");

client.print("Connection: close\n");

client.print("X-THINGSPEAKAPIKEY: "+apiWritekey+"\n");

client.print("Content-Type: application/x-www-form-urlencoded\n");

client.print("Content-Length: ");

client.print(tsData.length());

client.print("\n\n");

client.print(tsData);

Serial.print("Temperature: ");

Serial.print(temp);

Serial.println("uploaded to Thingspeak server....");

client.stop();

Serial.println("Waiting to upload next reading...");

Serial.println();

delay(15000);

}
https://www.youtube.com/user/master4hereever/search?query=NodeMCU
Riyasaatest

Drowpass1
Write API Key GY547B219LGNHOV6
Read API Key: C92CS35TPKYI2F3R
PROGRAM

#include <ThingSpeak.h>

#include <ESP8266WiFi.h>

char ssid[] = "SSID"; // Change this to your network SSID (name).

char pass[] = "PASSWORD"; // Change this your network password.

long channelID = 785841; // Change this to your channel ID.

char writeAPIKey[] = "GY547B219LGNHOV6"; // Change this to your channel Write API Key.

const unsigned long postingInterval = 20L * 1000L; // Post data every 20 seconds.

WiFiClient client; // Initialize the Wifi client library.

int connectWifi();

void updateTemp();
unsigned long lastConnectionTime = 0;

float Temp = 0.0;

void setup()

Serial.begin(115200);

Serial.println("Start");

connectWifi();

Serial.println("Connected to wifi");

ThingSpeak.begin(client);

void loop()

// If interval time has passed since the last connection, write data to ThingSpeak.

if (millis() - lastConnectionTime > postingInterval)

updateTemp();

ThingSpeak.setField(1, Temp);

ThingSpeak.writeFields(channelID, writeAPIKey);
lastConnectionTime = millis();

// Attempts a connection to WiFi network and repeats until successful.

int connectWifi()

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED)

Serial.println("Connecting to WiFi");

delay(2500);

Serial.println("Connected");

// Get the most recent readings for temperature and humidity.

void updateTemp()

Temp = (analogRead(A0) * 3300 ) / 10230;;

Serial.println(Temp);

}
% Read temperature data from a ThingSpeak channel over the past 24 hours

% to calculate the high and low temperatures and write to another channel.

% Channel 12397 contains data from the MathWorks Weather Station, located
% in Natick, Massachusetts. The data is collected once every minute. Field
% 4 contains temperature data.

% Channel ID to read data from


readChannelID = 785841;
% Temperature Field ID
TemperatureFieldID = 1;

% Channel Read API Key


% If your channel is private, then enter the read API Key between the '' below:
readAPIKey = 'C92CS35TPKYI2F3R';

% Read temperature data for the last 24 hours from the MathWorks weather
% station channel. Learn more about the thingSpeakRead function by going to
% the Documentation tab on the right side pane of this page.

[tempF,timeStamp] = thingSpeakRead(readChannelID,'Fields',TemperatureFieldID, ...

'numDays',1,'ReadKey',readAPIKey);

% Calculate the maximum and minimum temperatures


[maxTempF,maxTempIndex] = max(tempF);
[minTempF,minTempIndex] = min(tempF);

% Select the timestamps at which the maximum and minimum temperatures were
measured
timeMaxTemp = timeStamp(maxTempIndex);
timeMinTemp = timeStamp(minTempIndex);

display(maxTempF,'Maximum Temperature for the past 24 hours is');


display(minTempF,'Minimum Temperature for the past 24 hours is');

fprintf(['Note: To write data to another channel, assign the write channel ID


\n',...
'andAPI Key to ''writeChannelID'' and ''writeAPIKey'' variables. Also
\n',...
'uncomment the line of code containing ''thingSpeakWrite'' \n',...
'(remove ''%%'' sign at the beginning of the line.)']);

% To store the maximum temperature, write it to a channel other than


% the one used for reading data. To write to a channel, assign the write
% channel ID to the 'writeChannelID' variable, and the write API Key to the
% 'writeAPIKey' variable below. Find the write API Key in the right side pane
% of this page.

% Replace the [] with channel ID to write data to:


writeChannelID = [785841];
% Enter the Write API Key between the '' below:
writeAPIKey = 'GY547B219LGNHOV6';

% Learn more about the thingSpeakWrite function by going to the Documentation tab
on
% the right side pane of this page.

%thingSpeakWrite(writeChannelID,maxTempF,'timestamp',timeMaxTemp,'Writekey',write
APIKey);

OUTPUT

Maximum Temperature for the past 24 hours is =

40

Minimum Temperature for the past 24 hours is =

34.0800

Note: To write data to another channel, assign the write channel ID


andAPI Key to 'writeChannelID' and 'writeAPIKey' variables. Also
uncomment the line of code containing 'thingSpeakWrite'
(remove '%' sign at the beginning of the line.)
#include<ESP8266WiFi.h>

/************************* WiFi Access Point


*********************************/

#define WLAN_SSID "WCC_IoT"

#define WLAN_PASS "wcciot321"

#define light "off"

WiFiServer server( 80);

void setup() {

Serial.begin(115200);

delay(10);

pinMode( 5, OUTPUT); // D1 in nodemcu FAN 1

pinMode( 4, OUTPUT); // D2 in nodemcu LIGHT 1

pinMode( 0, OUTPUT); // D3 in nodemcu FAN 2

pinMode( 13, OUTPUT); // D7 in nodemcu LIGHT 2

digitalWrite( 5, LOW);

digitalWrite( 4, LOW);

digitalWrite( 0, LOW);

digitalWrite( 13, LOW); // Connect to WiFi network

Serial.println(); Serial.println();

Serial.print("Connecting to ");

Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

Serial.println();

server.begin();

Serial.println("WiFi connected");

Serial.println("IP address: "); Serial.println(WiFi.localIP());

void loop()

WiFiClient client = server.available();

if (! client) { return; }

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

if (request.indexOf("fan1on") > 0)

{digitalWrite( 5, HIGH);

Serial.println(" FAN 1 on");

if (request.indexOf("fan1off") > 0)

{ digitalWrite( 5, LOW);

Serial.println("FAN 1 off");

if (request.indexOf("light1on") > 0)

{ digitalWrite( 4, HIGH);

Serial.println("LIGHT 1 on");

if (request.indexOf("light1off") > 0)

{ digitalWrite( 4, LOW);

Serial.println("LIGHT 1 off");

if (request.indexOf("fan2on") > 0)

{ digitalWrite( 0, HIGH);

Serial.println("FAN 2 on");

if (request.indexOf("fan2off") > 0)
{ digitalWrite( 0, LOW);

Serial.println("FAN 2 off");

if (request.indexOf("light2on") > 0)

{ digitalWrite( 13, HIGH);

Serial.println("LIGHT 2 on");

if (request.indexOf("light2off") > 0)

{ digitalWrite( 13, LOW);

Serial.println("LIGHT 2 off ");

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(" </head>");

//client.println(" <body bgcolor = \"#f7e6ec\">");

client.println(" <hr/> <hr>");

client.println(" <h4> <center> remote control </center> </h4>");

client.println(" <hr/> <hr>");

client.println(" <br> <br>");

client.println(" <br> <br>");


client.println(" <center>");

client.println(" FAN 1");

//client.println("<button onclick=\"funt1()\"> one</button> ");

//client.println("<script> funtion funt1()");

if (digitalRead( 5))// read digital pin 5

client.println(" <a href =\"/ fan1on\"\"><button style


=\"background-color:green\"> Turn On </button> </a>");

client.println(" <a href =\"/ fan1off\"\"><button> Turn Off


</button> </a> <br/>");

else

client.println(" <a href =\"/ fan1on\"\"><button > Turn On


</button> </a>");

client.println(" <a href =\"/ fan1off\"\"><button style


=\"background-color:red\"> Turn Off </button> </a> <br/>");

client.println(" </center>");

client.println(" <br> <br>");

client.println(" <center>");

client.println(" LIGHT 1");

if (digitalRead( 4))// read digital pin 4

client.println(" <a href =\"/ light1on\"\"><button


style=\"background-color:green\"> Turn On </button> </a>");
client.println(" <a href =\"/ light1off\"\"><button> Turn Off
</button> </a> <br/>");

else

client.println(" <a href =\"/ light1on\"\"><button > Turn On


</button> </a>");

client.println(" <a href =\"/ light1off\"\"><button


style=\"background-color:red\"> Turn Off </button> </a> <br/>");

client.println(" </center>");

client.println(" <br> <br>");

client.println(" <center>");

client.println(" FAN 2 ");

if (digitalRead( 0))// read digital pin 0

client.println(" <a href =\"/ fan2on\"\"><button


style=\"background-color:green\"> Turn On </button> </a>");

client.println(" <a href =\"/ fan2off\"\"><button> Turn Off


</button> </a> <br/>");

else

client.println(" <a href =\"/ fan2on\"\"><button > Turn On


</button> </a>");

client.println(" <a href =\"/ fan2off\"\"><button


style=\"background-color:red\"> Turn Off </button> </a> <br/>");
}

client.println(" </center>");

client.println(" <br> <br>");

client.println(" <center>");

client.println(" LIGHT 2");

if (digitalRead( 13))// read digital pin 13

client.println(" <a href =\"/ light2on\"\"><button


style=\"background-color:green\"> Turn On </button> </a>");

client.println(" <a href =\"/ light2off\"\"><button> Turn Off


</button> </a> <br/>");

else

client.println(" <a href =\"/ light2on\"\"><button > Turn On


</button> </a>");

client.println(" <a href =\"/ light2off\"\"><button


style=\"background-color:red\"> Turn Off </button> </a> <br/>");

client.println(" </center>");

client.println(" <br> <br>");

client.println(" <center>");

client.println(" <table border =\" 5\">");

client.println(" <tr>");

if (digitalRead( 5))// read digital pin 5


{client.print(" <td> FAN 1 is ON </td>"); }

else

{ client.print(" <td> FAN 1 is OFF </td>"); }

if (digitalRead( 4))// read digital pin 4

{ client.print(" <td> LIGHT 1 is ON </td>"); }

else

{ client.print(" <td> LIGHT 1 is OFF </td>"); }

client.println(" </tr>");

client.println(" <tr>");

if (digitalRead( 0))// read digital pin 0

{ client.print(" <td> FAN 2 is ON </td>"); }

else

{ client.print(" <td> FAN 2 is OFF </td>"); }

if (digitalRead( 13))// read digital pin 13

{ client.print(" <td> LIGHT 2 is ON </td>"); }

else

{ client.print(" <td> LIGHT 2 is OFF </td>"); }

client.println(" </tr>");

client.println(" </table>");

client.println(" </center>");

client.println(" </html>");
delay( 1);

Serial.println(" Client disonnected");

Serial.println("");

You might also like