IOT Lab Manual (1) - Compressed
IOT Lab Manual (1) - Compressed
IOT Lab Manual (1) - Compressed
ON
Internet Of Things
Prepared by:
void setup()
{
statements-1;
.
.
.
statement-n;
}
void loop ( )
{
statement-1;
.
.
.
statement-n;
}
Here, setup ( ) is the preparation block and loop ( ) is an execution block.
The setup function is the first to execute when the program is executed, and
this function is called only once. The setup function is used to initialize the pin
modes and start serial communication. This function has to be included even if
there are no statements to execute.
void setup ( )
{
pinMode (pin-number, OUTPUT); // set the ‘pin-number’ as output
pinMode (pin-number, INPUT); // set the ‘pin-number’ as output
}
After the setup ( ) function is executed, the execution block runs next. The
execution block hosts statements like reading inputs, triggering outputs,
checking conditions etc..
In the above example loop ( ) function is a part of execution block. As the name
suggests, the loop( ) function executes the set of statements (enclosed in curly
braces) repeatedly.
Void loop ( )
{
digitalWrite (pin-number,HIGH); // turns ON the component connected to ‘pin-
number’
delay (1000); // wait for 1 sec
digitalWrite (pin-number, LOW); // turns OFF the component connected to
‘pin-number’
delay (1000); //wait for 1sec
}
Note: Arduino always measures the time duration in millisecond. Therefore,
whenever you mention the delay, keep it in milli seconds.
void loop()
{
// put your main code here, to run repeatedly:
}
int digital_pin = 1;
instead of
int p = 1;
You will thank yourself later.
Variables
Programs are useful when they process data. Processing data is what programs
do, all the time. Programs will either get some data to process from a user
(perhaps via a keypad). From a sensor (like a thermistor that measures
temperature), the network (like a remote database), a local file system (like an
SD Card), a local memory (like an EEPROM), and so many other places.
Regardless of the place where your program gets its data from, it must store
them in memory to work with it. To do this, we use variables. A variable is a
programming construct that associates a memory location with a name (an
identifier). Instead of using the address of the memory location in our
program, we use an easy to remember a name. You have already met a
variable. In the earlier section on custom functions, we defined a bunch of
variables, a, b and c, that each holds an integer.
Variables can hold different kinds of data other than integers. The Arduino
language (which, remember, is C++) has built-in support for a few of them
(only the most frequently used and useful are listed here):
C++
SIZE DESCRIPTION
KEYWORD
1 Holds only two possible values, true or false, even though it occupies a
boolean
byte byte in memory.
Holds a number from -127 to 127. Because it is marked as a “char,” the
1
char compiler will try to match it to a character from the ASCII table of
byte
characters.
1
byte Can hold numbers from 0 to 255.
byte
2
int Can hold numbers from -32768 to 32767.
byte
unsigned 2
Can hold numbers from 0-65535
int byte
C++
SIZE DESCRIPTION
KEYWORD
2 Same as the “unsigned int.” People often use “word” for simplicity and
word
byte clarity.
4
long Can hold numbers from -2,147,483,648 to 2,147,483,647.
byte
unsigned 4
Can hold numbers from 0-4,294,967,295
long byte
Can hold numbers from -3.4028235E38 to 3.4028235E38. Notice that
this number contains a decimal point. Only use float if you have no
4
float other choice. The ATMEGA CPU does not have the hardware to deal
byte
with floats, so the compiler has to add a lot of code to make it possible
for your sketch to use them, making your sketch larger and slower.
A way to store multiple characters as an array of chars. C++ also offers a
string - char
- String object that you can use instead that provides more flexibility
array
when working with strings in exchange for higher memory use.
array - A structure that can hold multiple data of the same type.
To create a variable, you need a valid name and a type. Just like with functions,
a valid name is one that contains numbers, letters, and an underscore starts
with a letter and is not reserved. Here is an example:
byte sensor_A_value;
This line defines a variable named sensor_A_value, which will hold a single
byte in memory. You can store a value in it like this:
sensor_A_value = 196;
You can print out this value to the serial monitor like this:
Serial.print(sensor_A_value);
The serial monitor is a feature of the Arduino IDE that allows you to get a text
from the Arduino displayed on your screen. More about this later, here I want
to show you how to retrieve the value stored in a variable. Just call its name.
Also, remember the earlier discussion about scope: the variable has to be
within scope when it is called.
Another beautiful thing about a variable is that you can change the value
stored in it. You can take a new reading from the sensor and update the
variable like this:
sensor_A_value = 201;
No problem, the old value is gone, and the new value is stored.
Constants
If there is a value that will not be changing in your sketch, you can mark it as a
constant.
Constants have benefits regarding memory and processing speed, and it is a
good habit to use these.
You can declare a constant like this:
const int sensor_pin = 1;
Here, you define the name of the variable sensor_pin, mark it as constant, and
set it to 1. If you try to change the value later, you will get a compiler error
message, and your program will not even get uploaded to the Arduino.
Operators
Operators are special functions that operate one or more pieces of data.
Most people are familiar with the basic arithmetic functions, = (assignment), +,
-, * and /, But there are a lot more.
For example, here are the most commonly used operators:
Operator Function Example
Modulo operator. It returns the remainder
% 5%2=1
of a division.
int a = 5;
Compound operator. It performs an a+= 2;
+=, -=, *=,
operation on the current value of a This will result in a containing 7
/=
variable. (the original 5 plus a 2 from the
addition operation).
int a = 5;
++, -- Increment and decrement by 1. a++;
This will result in a becoming 6.
boolean a = true;
boolean b = true;
Logical operators. The “!” operator will
boolean c = false;
invert a boolean value.
!, &&, || ! → NOT (invert) of a boolean value
boolean x = !a; // x → false
&& → AND of two booleans
boolean y = b && c; // y → false
|| → OR of two booleans
boolean z = b || c; // z → true
Arduino Programming in C
Arduino is an open-source hardware and software company, project and user
community that designs and manufactures single-board microcontrollers and
microcontroller kits for building digital devices.
Board Components
These are the components that make up an Arduino board and what each of
their functionalities are.
Reset Button ~ This will restart any code that is loaded to the Arduino
board
AREF ~ Stands for “Analog Reference” and is used to set an external
reference voltage
Ground Pin ~ There are a few ground pins on the Arduino and they all
work the same
Analog Pins ~ These pins read the signal from an analog sensor and
convert it to digital
Digital Input/Output ~ Pins 0-13 can be used for digital input or output
PWM ~ The pins marked with the (~) symbol can simulate analog
output
USB Connection ~ Used for powering up your Arduino and uploading
sketches
TX/RX ~ Transmit and receive data indication LEDs
ATmega Microcontroller ~ Popular microcontroller chip, this is where
the programs are stored
Power LED Indicator ~ This LED lights up anytime the board is plugged
in a power source
Voltage Regulator ~ Controls the amount of voltage going into the
Arduino board
DC Power Barrel Jack ~ This is used for powering your Arduino with a
power supply
3.3V Pin ~ This pin supplies 3.3 volts of power
5V Pin ~ This pin supplies 5 volts of power
Install Arduino Code Editor
The open-source Arduino Software (IDE) makes it easy to write code and
upload it to the board. It runs on Windows, Mac OS X, and Linux. The
environment is written in Java and based on Processing and other open-
source software.
Download Arduino Code Editor
Navigate to arduino.cc and download The open-source Arduino
Software (IDE) for Windows, MacOSX& Linux.
Use the Online Code Editor, or scroll down on the arduino download
page to download the version for Windows, MacOSX& Linux.
Blink a LED light
Example Arduino Schema
A step by step example showing how to blink a LED light with an Arduino, this
example provides the board schematics, code and a list of components that
are required.
Components Required
1 × Arduino Uno R3
1 × LED
1 × 330Ω Resistor
Example Code (Blink a LED light)
// The pin the LED is connected to
intledPin = 13;
Components Required
1 x Breadboard
1 × Arduino Uno R3
1 × LED
1 x LRD Light Sensor
1 × 330Ω Resistor
1 x 100k Ohm Resistor
7 x Jumper Cables
Example Code (Using a LRD Light Sensor, to turn on a LED light)
constintledPin = 13; // The pin the LED is connected to
constintldrPin = A0; // The pin the LDR sensor is connected to
intldrStatus = 0; // Variable for reading the pin status
// Executes once when the arduino power button is pressed on
voidsetup()
{
pinMode(ledPin, OUTPUT); // Declare the LED as an output
pinMode(ldrPin, INPUT); // Declare LDR Sensor as an input
}
if (ldrStatus<= 400) {
digitalWrite(ledPin, HIGH); //It's dark, Turn on LED
} else {
digitalWrite(ledPin, LOW); // It's bright, Turn off LED
}
}
Run Project with Arduino IDE
To run the code, on the menu bar, choose Sketch > Upload. The code
will be uploaded to the ATmega Microcontroller
Press the reset button to restart any code that is loaded to the Arduino
board
LDR – Automatic Night Lamp
1. Introduction:
A LDR (Light Dependent Resistor) or a photo resistor is a photo conductive
sensor. It is a variable resistor and changes its resistance in a proportion to the
light exposed to it. It resistance decreases with the intensity of light.
It senses the light intensity from surroundings and find whether its day or night
then it automatically turns ON when the surrounding is dark and it turns OFF
when it receives light from surroundings.
2. Required Hardware
Following Hardware will be required to perform this LDR circuit.
S.No. Item Quantity
1 Arduino UNO 1
2 Breadboard 1
3 LDR 1
4 LED 1
5 Resistor 1k 1
6 Resistor 10k 1
3. Building Circuit
Make following circuit with the help of above mentioned components. Some
key points to understand about the circuit-
LDR is connected to a 10 Resistance in series. +5 Voltage is applied to this
arrangement. As the light intensity changes LDR value changes thus the voltage
drop on LDR will change and we are going to measure that voltage change.
4. Programming:
Once we are done with circuit part, here is our programme to this circuit.
/Robo India Tutorial on Night Lamp using LDR
//https://www.roboindia.com/tutorials
void setup()
{
pinMode (LED, OUTPUT);
Serial.begin(9600);
}
void loop()
{
state= analogRead(LDRSensor); //sensor reading value stored in state variable
if (state < threshold)
{
digitalWrite(LED, HIGH); //if the light is below the threshold value, the LED
will turns on
Serial.println(state);
delay(2000);
}
else
{
digitalWrite(LED, LOW); //otherwise, the LED is off
Serial.println(state);
delay(1000);
}
}
/Robo India Tutorial on Night Lamp using LDR
//https://www.roboindia.com/tutorials
void loop()
{
state= analogRead(LDRSensor); //sensor reading value stored in state variable
if (state < threshold)
{
digitalWrite(LED, HIGH); //if the light is below the threshold value, the LED
will turns on
Serial.println(state);
delay(2000);
}
else
{
digitalWrite(LED, LOW); //otherwise, the LED is off
Serial.println(state);
delay(1000);
}
}
Control the light exposing on LDR by covering it. If it gets dark, the LED
connected will get turn ON automatically.
Applications
DHT11 Relative Humidity and Temperature Sensor can be used in many
applications like:
HVAC (Heating, Ventilation and Air Conditioning) Systems
Weather Stations
Medical Equipment for measuring humidity
Home Automation Systems
Automotive and other weather control applications
Pin diagram and description of each pin have explained in the following table.
Pin No Pin Name Pin Description
This pin is a ground pin and the LCD is connected to the
Pin 1 GND Ground
Pin 2 VCC The VCC pin is used to supply the power to the LCD
This pin is used for adjusting the contrast of the LCD by
Pin 3 VEE connecting the variable resistor in between the VCC &
Ground.
The digital input lines (DB4-DB7) are interfaced with the Arduino pins from 5-2.
To adjust the contrast of the display here we are using a 10K potentiometer.
The current through the back LED light is from the 560-ohm resistor. The
external power jack is provided by the board to the Arduino. Using the PC
through the USB port the Arduino can power. Some parts of the circuit can
require the +5V power supply it is taken from the 5V source on the Arduino
board.
The following schematic diagram shows the LCD module interfacing with the
Arduino.
void setup()
{
lcd.begin(16, 2); // initializes the 16x2 LCD
}
void loop()
{
lcd.setCursor(0,0); //sets the cursor at row 0 column 0
lcd.print("16x2 LCD MODULE"); // prints 16x2 LCD MODULE
lcd.setCursor(2,1); //sets the cursor at row 1 column 2
lcd.print("HELLO WORLD"); // prints HELLO WORLD
}
Interfacing Air Quality Sensor-pollution (e.g. MQ135) – display data on LCD ,
switch on LED when data sensed is higher than specified value.
In this project we are going to make an IoT Based Air Pollution Monitoring
System in which we will monitor the Air Quality over a webserver using
internet and will trigger a alarm when the air quality goes down beyond a
certain level, means when there are sufficient amount of harmful gases are
present in the air like CO2, smoke, alcohol, benzene and NH3. It will show the
air quality in PPM on the LCD and as well as on webpage so that we can
monitor it very easily.
Previously we have built the LPG detector using MQ6 sensor and Smoke
detector using MQ2 sensor but this time we have used MQ135 sensor as the
air quality sensor which is the best choice for monitoring Air Quality as it can
detects most harmful gases and can measure their amount accurately. In
this IOT project, you can monitor the pollution level from anywhere using your
computer or mobile. We can install this system anywhere and can also trigger
some device when pollution goes beyond some level, like we can switch on the
Exhaust fan or can send alert SMS/mail to the user.
Required Components:
MQ135 Gas sensor
Arduino Uno
Wi-Fi module ESP8266
16X2 LCD
Breadboard
10K potentiometer
1K ohm resistors
220 ohm resistor
Buzzer
Circuit Diagram and Explanation:
First of all we will connect the ESP8266 with the Arduino. ESP8266 runs on
3.3V and if you will give it 5V from the Arduino then it won’t work properly and
it may get damage. Connect the VCC and the CH_PD to the 3.3V pin of Arduino.
The RX pin of ESP8266 works on 3.3V and it will not communicate with the
Arduino when we will connect it directly to the Arduino. So, we will have to
make a voltage divider for it which will convert the 5V into 3.3V. This can be
done by connecting three resistors in series like we did in the circuit. Connect
the TX pin of the ESP8266 to the pin 10 of the Arduino and the RX pin of the
esp8266 to the pin 9 of Arduino through the resistors.
ESP8266 Wi-Fi module gives your projects access to Wi-Fi or internet. It is a
very cheap device and make your projects very powerful. It can communicate
with any microcontroller and it is the most leading devices in the IOT platform.
Learn more about using ESP8266 with Arduino here.
Then we will connect the MQ135 sensor with the Arduino. Connect the VCC
and the ground pin of the sensor to the 5V and ground of the Arduino and the
Analog pin of sensor to the A0 of the Arduino.
Connect a buzzer to the pin 8 of the Arduino which will start to beep when the
condition becomes true.
In last, we will connect LCD with the Arduino. The connections of the LCD are
as follows
Connect pin 1 (VEE) to the ground.
Connect pin 2 (VDD or VCC) to the 5V.
Connect pin 3 (V0) to the middle pin of the 10K potentiometer and
connect the other two ends of the potentiometer to the VCC and the
GND. The potentiometer is used to control the screen contrast of the
LCD. Potentiometer of values other than 10K will work too.
Connect pin 4 (RS) to the pin 12 of the Arduino.
Connect pin 5 (Read/Write) to the ground of Arduino. This pin is not
often used so we will connect it to the ground.
Connect pin 6 (E) to the pin 11 of the Arduino. The RS and E pin are
the control pins which are used to send data and characters.
The following four pins are data pins which are used to communicate
with the Arduino.
Connect pin 11 (D4) to pin 5 of Arduino.
Connect pin 12 (D5) to pin 4 of Arduino.
Connect pin 13 (D6) to pin 3 of Arduino.
Connect pin 14 (D7) to pin 2 of Arduino.
Connect pin 15 to the VCC through the 220 ohm resistor. The resistor
will be used to set the back light brightness. Larger values will make
the back light much more darker.
Connect pin 16 to the Ground.
Working Explanation:
The MQ135 sensor can sense NH3, NOx, alcohol, Benzene, smoke, CO2 and
some other gases, so it is perfect gas sensor for our Air Quality Monitoring
Project. When we will connect it to Arduino then it will sense the gases, and
we will get the Pollution level in PPM (parts per million). MQ135 gas sensor
gives the output in form of voltage levels and we need to convert it into PPM.
So for converting the output in PPM, here we have used a library for MQ135
sensor, it is explained in detail in “Code Explanation” section below.
Sensor was giving us value of 90 when there was no gas near it and the safe
level of air quality is 350 PPM and it should not exceed 1000 PPM. When it
exceeds the limit of 1000 PPM, then it starts cause Headaches, sleepiness and
stagnant, stale, stuffy air and if exceeds beyond 2000 PPM then it can cause
increased heart rate and many other diseases.
When the value will be less than 1000 PPM, then the LCD and webpage will
display “Fresh Air”. Whenever the value will increase 1000 PPM, then the
buzzer will start beeping and the LCD and webpage will display “Poor Air, Open
Windows”. If it will increase 2000 then the buzzer will keep beeping and the
LCD and webpage will display “Danger! Move to fresh Air”.
Code Explanation:
Using library of MQ135 you can directly get the PPM values, by just using the
below two lines:
MQ135 gasSensor = MQ135(A0);
float air_quality = gasSensor.getPPM();
But before that we need to calibrate the MQ135 sensor, for calibrating the
sensor upload the below given code and let it run for 12 to 24 hours and then
get the RZERO value.
#include "MQ135.h"
void setup (){
Serial.begin (9600);
}
void loop() {
MQ135 gasSensor = MQ135(A0); // Attach sensor to pin A0
float rzero = gasSensor.getRZero();
Serial.println (rzero);
delay(1000);
}
After getting the RZERO value. Put the RZERO value in the library file you
downloaded "MQ135.h": #define RZERO 494.63
Now we can begin the actual code for our Air quality monitoring project.
In the code, first of all we have defined the libraries and the variables for the
Gas sensor and the LCD. By using the Software Serial Library, we can make any
digital pin as TX and RX pin. In this code, we have made Pin 9 as the RX pin and
the pin 10 as the TX pin for the ESP8266. Then we have included the library for
the LCD and have defined the pins for the same. We have also defined two
more variables: one for the sensor analog pin and other for
storing air_quality value.
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(9,10);
#include <LiquidCrystal.h>
LiquidCrystallcd(12,11, 5, 4, 3, 2);
const int sensorPin= 0;
int air_quality;
Then we will declare the pin 8 as the output pin where we have connected the
buzzer. lcd.begin(16,2) command will start the LCD to receive data and then
we will set the cursor to first line and will print the ‘circuitdigest’. Then we will
set the cursor on the second line and will print ‘Sensor Warming’.
pinMode(8, OUTPUT);
lcd.begin(16,2);
lcd.setCursor (0,0);
lcd.print ("circuitdigest ");
lcd.setCursor (0,1);
lcd.print ("Sensor Warming ");
delay(1000);
Then we will set the baud rate for the serial communication. Different ESP’s
have different baud rates so write it according to your ESP’s baud rate. Then
we will send the commands to set the ESP to communicate with the Arduino
and show the IP address on the serial monitor.
Serial.begin(115200);
esp8266.begin(115200);
sendData("AT+RST\r\n",2000,DEBUG);
sendData("AT+CWMODE=2\r\n",1000,DEBUG);
sendData("AT+CIFSR\r\n",1000,DEBUG);
sendData("AT+CIPMUair_quality=1\r\n",1000,DEBUG);
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG);
pinMode(sensorPin, INPUT);
lcd.clear();
For printing the output on the webpage in web browser, we will have to
use HTML programming. So, we have created a string named webpage and
stored the output in it. We are subtracting 48 from the output because
the read() function returns the ASCII decimal value and the first decimal
number which is 0 starts at 48.
if(esp8266.available())
{
if(esp8266.find("+IPD,"))
{
delay(1000);
int connectionId = esp8266.read()-48;
String webpage = "<h1>IOT Air Pollution Monitoring System</h1>";
webpage += "<p><h2>";
webpage+= " Air Quality is ";
webpage+= air_quality;
webpage+=" PPM";
webpage += "<p>";
The following code will call a function named sendData and will send the data
& message strings to the webpage to show.
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
The following code will print the data on the LCD. We have applied various
conditions for checking air quality, and LCD will print the messages according
to conditions and buzzer will also beep if the pollution goes beyond 1000 PPM.
lcd.setCursor (0, 0);
lcd.print ("Air Quality is ");
lcd.print (air_quality);
lcd.print (" PPM ");
lcd.setCursor (0,1);
if (air_quality<=1000)
{
lcd.print("Fresh Air");
digitalWrite(8, LOW);
Finally the below function will send and show the data on the webpage. The
data we stored in string named ‘webpage’ will be saved in string
named ‘command’. The ESP will then read the character one by one from
the ‘command’ and will print it on the webpage.
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) >millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
Testing and Output of the Project:
Before uploading the code, make sure that you are connected to the Wi-Fi of
your ESP8266 device. After uploading, open the serial monitor and it will show
the IP address like shown below.
Type this IP address in your browser, it will show you the output as shown
below. You will have to refresh the page again if you want to see the current
Air Quality Value in PPM.
We have setup a local server to demonstrate its working, you can check
the Video below. But to monitor the air quality from anywhere in the world,
you need to forward the port 80 (used for HTTP or internet) to your local or
private IP address (192.168*) of you device. After port forwarding all the
incoming connections will be forwarded to this local address and you can open
above shown webpage by just entering the public IP address of your internet
from anywhere. You can forward the port by logging into your router
(192.168.1.1) and find the option to setup the port forwarding.
Code (Merged)
#include "MQ135.h"
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(9,10); // This makes pin 9 of Arduino as RX pin and pin 10 of Arduino as the
TX pin
const int sensorPin= 0;
int air_quality;
#include <LiquidCrystal.h>
LiquidCrystallcd(12,11, 5, 4, 3, 2);
void setup() {
pinMode(8, OUTPUT);
lcd.begin(16,2);
lcd.setCursor (0,0);
lcd.print ("circuitdigest ");
lcd.setCursor (0,1);
lcd.print ("Sensor Warming ");
delay(1000);
Serial.begin(115200);
esp8266.begin(115200); // your esp's baud rate might be different
sendData("AT+RST\r\n",2000,DEBUG); // reset module
sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUair_quality=1\r\n",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
pinMode(sensorPin, INPUT); //Gas sensor will be an input to the arduino
lcd.clear();
}
void loop() {
MQ135 gasSensor = MQ135(A0);
float air_quality = gasSensor.getPPM();
if(esp8266.available()) // check if the esp is sending a message
{
if(esp8266.find("+IPD,"))
{
delay(1000);
int connectionId = esp8266.read()-48; /* We are subtracting 48 from the output because the
read() function returns the ASCII decimal value and the first decimal number which is 0 starts at
48*/
String webpage = "<h1>IOT Air Pollution Monitoring System</h1>";
webpage += "<p><h2>";
webpage+= " Air Quality is ";
webpage+= air_quality;
webpage+=" PPM";
webpage += "<p>";
if (air_quality<=1000)
{
webpage+= "Fresh Air";
}
else if(air_quality<=2000 &&air_quality>=1000)
{
webpage+= "Poor Air";
}
else if (air_quality>=2000 )
{
webpage+= "Danger! Move to Fresh Air";
}
webpage += "</h2></p></body>";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(closeCommand,3000,DEBUG);
}
}
lcd.setCursor (0, 0);
lcd.print ("Air Quality is ");
lcd.print (air_quality);
lcd.print (" PPM ");
lcd.setCursor (0,1);
if (air_quality<=1000)
{
lcd.print("Fresh Air");
digitalWrite(8, LOW);
}
else if( air_quality>=1000 &&air_quality<=2000 )
{
lcd.print("Poor Air, Open Windows");
digitalWrite(8, HIGH );
}
else if (air_quality>=2000 )
{
lcd.print("Danger! Move to Fresh Air");
digitalWrite(8, HIGH); // turn the LED on
}
lcd.scrollDisplayLeft();
delay(1000);
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) >millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
Interfacing Bluetooth module (e.g. HC05)- receiving data from mobile phone
on Arduino and display on LCD
CODE
void loop(){
while(Serial.available()>0){
inputByte=Serial.read();
Serial.println(inputByte);
if(inputByte=='Z'){
digitalWrite(13,HIGH);
}
elseif(inputByte=='z'){
digitalWrite(13,LOW);
}
}
}
SCHEMATICS
Commands:
Command sent by mobile Message receives by mobile
all on All ON
white on White ON
blue on Blue ON
green on Green ON
#include <LiquidCrystal.h>
LiquidCrystallcd(8, 9, 10, 11, 12, 13);
#define white 3
#define blue 4
#define green 5
int tx=1;
int rx=0;
char inSerial[15];
void setup(){
Serial.begin(9600);
pinMode(white, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(tx, OUTPUT);
pinMode(rx, INPUT);
digitalWrite(white, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(green, HIGH);
lcd.begin(16, 2);
lcd.clear();
lcd.print("MICROCONTROLLERS ");
lcd.setCursor(0,1);
lcd.print(" LAB ");
delay(2000);
lcd.clear();
lcd.print("HOME AUTOMATION ");
lcd.setCursor(0,1);
lcd.print("USING BLUETOOTH");
delay(2000);
lcd.clear();
lcd.print("1. Bulb 1 WHITE");
lcd.setCursor(0,1);
lcd.print("2. Bulb 2 BLUE");
delay(2000);
lcd.clear();
lcd.print("3. Bulb 3 GREEN");
delay(2000);
lcd.clear();
lcd.print("Bulb 1 2 3 ");
lcd.setCursor(0,1);
lcd.print(" OFF OFF OFF");
void loop(){
int i=0;
int m=0;
delay(500);
if (Serial.available() > 0) {
while (Serial.available() > 0) {
inSerial[i]=Serial.read();
i++;
}
inSerial[i]='\0';
Check_Protocol(inSerial);
}}
void Check_Protocol(char inStr[]){
int i=0;
int m=0;
Serial.println(inStr);
if(!strcmp(inStr,"all on")){
digitalWrite(white, LOW);
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
Serial.println("ALL ON");
lcd.setCursor(4,1);
lcd.print("ON ");
lcd.setCursor(8,1);
lcd.print("ON ");
lcd.setCursor(12,1);
lcd.print("ON ");
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}
if(!strcmp(inStr,"all off")){
digitalWrite(white, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(green, HIGH);
Serial.println("ALL OFF");
lcd.setCursor(4,1);
lcd.print("OFF ");
lcd.setCursor(8,1);
lcd.print("OFF ");
lcd.setCursor(12,1);
lcd.print("OFF ");
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}
if(!strcmp(inStr,"white on")){
digitalWrite(white, LOW);
Serial.println("White ON");
lcd.setCursor(4,1);
lcd.print("ON ");
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}
if(!strcmp(inStr,"white off")){
digitalWrite(white, HIGH);
Serial.println("White OFF");
lcd.setCursor(4,1);
lcd.print("OFF ");
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}
if(!strcmp(inStr,"blue on")){
digitalWrite(blue, LOW);
Serial.println("Blue ON");
lcd.setCursor(8,1);
lcd.print("ON ");
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}
if(!strcmp(inStr,"blue off")){
digitalWrite(blue, HIGH);
Serial.println("Blue OFF");
lcd.setCursor(8,1);
lcd.print("OFF ");
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}
if(!strcmp(inStr,"green on")){
digitalWrite(green, LOW);
Serial.println("Green ON");
lcd.setCursor(12,1);
lcd.print("ON ");
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}
if(!strcmp(inStr,"green off")){
digitalWrite(green, HIGH);
Serial.println("Green OFF");
lcd.setCursor(12,1);
lcd.print("OFF ");
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}
else{
for(m=0;m<11;m++){
inStr[m]=0;
}
i=0;
}}