Tuesday, May 24, 2022

EXP 3. Create simple security alarm system using Arduino

 

Program

#define trigPin 7 

#define echoPin 6 

#define LEDlampRed 10

#define LEDlampYellow 11

 

#define LEDlampGreen 12

 #define soundbuzzer 4

 

void setup() 

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

 pinMode(echoPin, INPUT); 

pinMode(LEDlampRed, OUTPUT); 

pinMode(LEDlampYellow, OUTPUT);

 pinMode(LEDlampGreen, OUTPUT); 

pinMode(soundbuzzer, OUTPUT);

}



void loop() {

digitalWrite(trigPin, LOW);

 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH);

 delayMicroseconds(10); 

digitalWrite(trigPin, LOW);

 durationindigit=pulseIn(echoPin, HIGH);

 distanceincm= (durationindigit/5) /29.1;



digitalWrite(LEDlampGreen, HIGH); 

digitalWrite(LEDlampRed, LOW);

 digitalWrite(LEDlampYellow, LOW); 

noTone(soundbuzzer);

}

else {

digitalWrite(LEDlampGreen, LOW);

 

}



digitalWrite(LEDlampYellow, HIGH); 

digitalWrite(LEDlampGreen, LOW); 

digitalWrite(LEDlampRed, LOW);

}

else { digitalWrite(LEDlampYellow,LOW);



}

digitalWrite(LEDlampRed, HIGH); 

digitalWrite(LEDlampGreen, LOW); 

digitalWrite(LEDlampYellow, LOW); 

sound=1000; tone(soundbuzzer,sound);

}

}


Exp 4. Interface and control an LED with NODEMCU in online

 

Install ESP8266 Add-on in Arduino IDE

To install the ESP8266 board in your Arduino IDE, follow these next instructions:

 

1.  




In your Arduino IDE, go to File> Preferences

2.   Enter http://arduino.esp8266.com/stable/package_esp8266com_ind ex.json into the “Additional Boards Manager URLs” field as shown in the figure below. Then, click the “OK” button:


 

 



 

Note: if you already have the ESP32 boards URL, you can separate the URLs with a comma as follows:

https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json

 

3.   Open the Boards Manager. Go to Tools > Board > Boards Manager…


 

 



 

4.   Search for ESP8266 and press install button for the ESP8266 by ESP8266 Community“:



5.   That’s it. It should be installed after a few seconds.


 


 


 

1.   ESP8266 NodeMCU

2.   Breadboard

3.   LED

4.   Jumper Wires

5.   Arduino IDE

 

Step 3: Pin Connections


 


 

int pin =2;

 

voidsetup(){

// initialize GPIO 2 as an output. pinMode(pin, OUTPUT);

}

 

// the loop function runs over and over again forever voidloop(){

digitalWrite(pin, HIGH);// turn the LED on (HIGH is the voltage level) delay(1000);// wait for a second

digitalWrite(pin, LOW);// turn the LED off by making the voltage LOW delay(1000);// wait for a second

}

 

 

 

Having the right board name and COM port selected, follow these steps:

 

§  Hold-down the BOOT/FLASH button in your ESP8266 development board

§  Press the Upload button in the Arduino IDE to upload your sketch:


§  When you see the Connecting…. message in your Arduino IDE, release the finger from the “BOOT/FLASH button

§  After that, you should see the Done uploading message

Your ESP8266 should have the new sketch running. Press the ENABLE/RESET button to restart the ESP8266 and run the new uploaded sketch.

EXP 6 CREATE A SMARTLIGHT USING RASPBERRY PI AND ARDUINO UNO

 



const int ledpin = 13; // ledpin and lightpin are not changed throughout the process const int lightpin = A2; const int LIGHT = 10; // sets LIGHT value for light sensor void setup() { Serial.begin(9600); pinMode(ledpin, OUTPUT); pinMode(lightpin, INPUT); } void loop() { int lightsens = analogRead(lightpin); // reads analog data from light sensor if (lightsens < LIGHT) { digitalWrite(ledpin, HIGH); //turns led on delay(1500); } else { digitalWrite(ledpin, LOW); } }


EXP 1 simple LED Bar Graph using Arduino.





Coding:

void setup() { 

pinMode(13, OUTPUT);

 pinMode(12, OUTPUT); 

pinMode(11, OUTPUT);

 pinMode(10, OUTPUT); 

pinMode(9, OUTPUT); 

pinMode(8, OUTPUT);

}


void loop() 

digitalWrite(8, HIGH); 

delay(1000); 

digitalWrite(9, HIGH); 

delay(1000); 

digitalWrite(10, HIGH); 

delay(1000); 

digitalWrite(11, HIGH); 

delay(1000);

 digitalWrite(12, HIGH);

 delay(1000); 

digitalWrite(13, HIGH); 

delay(1000); 

digitalWrite(8, LOW); 

delay(1000); 

digitalWrite(9, LOW); 

delay(1000); 

digitalWrite(10, LOW); 

delay(1000); 

digitalWrite(11, LOW);

 delay(1000);

 

digitalWrite(12, LOW);

 delay(1000); 

digitalWrite(13,LOW); 

delay(1000);

}

Saturday, May 21, 2022

EXP NO 8 ESP 8266 Electric Appliances CONTROL USING WEB PAGE

 







#include <ESP8266WiFi.h>

 

const char* ssid = "rkk";

const char* password = "rkk@1234";

 

int relay_pin = 14;

WiFiServer server(80);

 

void setup() 

{

  Serial.begin(115200);

  pinMode(relay_pin, OUTPUT);

  digitalWrite(relay_pin, HIGH);

  

  Serial.print("Connecting to the WIFI Network\n");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 

  {

    delay(500);

    Serial.print(" * ");

  }

  Serial.println("\nSuccessfully connected to "+String(ssid));

  server.begin();

  Serial.println("\nServer is Active");

  Serial.println("\nType below mentioned IP address in your browser\n"); 

  Serial.println(WiFi.localIP());

}

 

void loop() 

{

  WiFiClient client = server.available();

  if (!client)

  {

    return;

  }

  while(!client.available())

  {

    delay(1);

  }

  String request = client.readStringUntil('\r');

  client.flush();

 

  int value = HIGH;

  if (request.indexOf("/LAMP=ON") != -1)  

  {

    digitalWrite(relay_pin, LOW);

    value = LOW;

  }

  if (request.indexOf("/LAMP=OFF") != -1) 

  {

    digitalWrite(relay_pin, HIGH);

    value = HIGH;

  }

  client.println("HTTP/1.1 200 OK");

  client.println("Content-Type: text/html");

  client.println("");

  client.println("<!DOCTYPE HTML>");

  client.println("<html>");

  client.println("<style> body {background-color:#B2D1D5;}</style>");

  client.print("<center><h1>Table Lamp is now<h1></center>");

  if(value == LOW)

  {

    client.print("<center><h1><b>ON</b></h1></center>");

  } 

  else 

  {

    client.print("<center><h1><b>OFF</b></h1></center>");

  }

  client.println("<br><a href=\"/LAMP=ON\"\"><center><button>Switch ON </button></center></a><br><br>");

  client.println("<a href=\"/LAMP=OFF\"\"><center><button>Switch OFF </button></center></a><br/>");  

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

}

EXP NO 7 PUSH THE TEMPERATURE DATA TO BYLNK IoT


 


// Fill-in information from your Blynk Template here

#define BLYNK_TEMPLATE_ID           "TMPLD1CKpt9_"

#define BLYNK_DEVICE_NAME           "temp"


#define BLYNK_FIRMWARE_VERSION        "0.1.0"


#define BLYNK_PRINT Serial

//#define BLYNK_DEBUG


#define APP_DEBUG


// Uncomment your board, or configure a custom board in Settings.h

//#define USE_SPARKFUN_BLYNK_BOARD

#define USE_NODE_MCU_BOARD

//#define USE_WITTY_CLOUD_BOARD

//#define USE_WEMOS_D1_MINI


#include "BlynkEdgent.h"

#include "DHT.h"

#define DHTPIN D2

#define DHTTYPE DHT22


DHT dht(DHTPIN, DHT22);

float t, h;


void setup()

{

  Serial.begin(115200);

  delay(100);


  BlynkEdgent.begin();

  h = dht.readHumidity();

t = dht.readTemperature();

Blynk.virtualWrite(V0, h);

Blynk.virtualWrite(V1, t);

Serial.begin(9600);

dht.begin();

BlynkEdgent.begin();

delay(2000);


}


void loop() {

  BlynkEdgent.run();

}

EXP 9. PUSHING THE ESP 8266 DATA INTO GOOGLE SPREAD SHEET

 

Program

#include <ESP8266WiFi.h>

#include <WiFiClientSecure.h>

//----------------------------------------

//----------------------------------------Include the DHT Library

#include "DHT.h"

//----------------------------------------


#define DHTTYPE DHT11 //--> Defines the type of DHT sensor used (DHT11, DHT21, and DHT22), in this project the sensor used is DHT11.


const int DHTPin = 5; //--> The pin used for the DHT11 sensor is Pin D1 = GPIO5

DHT dht(DHTPin, DHTTYPE); //--> Initialize DHT sensor, DHT dht(Pin_used, Type_of_DHT_Sensor);


#define ON_Board_LED 2  //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router


//----------------------------------------SSID and Password of your WiFi router.

const char* ssid = "rkk"; //--> Your wifi name or SSID.

const char* password = "rkk@1234"; //--> Your wifi password.

//----------------------------------------


//----------------------------------------Host & httpsPort

const char* host = "script.google.com";

const int httpsPort = 443;

//----------------------------------------


WiFiClientSecure client; //--> Create a WiFiClientSecure object.


String GAS_ID = "AKfycbwHyCamO34TCYGdx8SNIU7x4qf3wAODbrxcXCp75PnHA4kivHoFxvcbGgr1rxFZyPiX"; //--> spreadsheet script ID


//============================================================================== void setup

void setup() {

  // put your setup code here, to run once:

  Serial.begin(115200);

  delay(500);


  dht.begin();  //--> Start reading DHT11 sensors

  delay(500);

  

  WiFi.begin(ssid, password); //--> Connect to your WiFi router

  Serial.println("");

    

  pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output

  digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board


  //----------------------------------------Wait for connection

  Serial.print("Connecting");

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

    Serial.print(".");

    //----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.

    digitalWrite(ON_Board_LED, LOW);

    delay(250);

    digitalWrite(ON_Board_LED, HIGH);

    delay(250);

    //----------------------------------------

  }

  //----------------------------------------

  digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.

  //----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor

  Serial.println("");

  Serial.print("Successfully connected to : ");

  Serial.println(ssid);

  Serial.print("IP address: ");

  Serial.println(WiFi.localIP());

  Serial.println();

  //----------------------------------------


  client.setInsecure();

}

//==============================================================================

//============================================================================== void loop

void loop() {

  // Reading temperature or humidity takes about 250 milliseconds!

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

  int h = dht.readHumidity();

  // Read temperature as Celsius (the default)

  float t = dht.readTemperature();

  

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

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

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

    delay(500);

    return;

  }

  String Temp = "Temperature : " + String(t) + " °C";

  String Humi = "Humidity : " + String(h) + " %";

  Serial.println(Temp);

  Serial.println(Humi);

  

  sendData(t, h); //--> Calls the sendData Subroutine

}

//==============================================================================

//============================================================================== void sendData

// Subroutine for sending data to Google Sheets

void sendData(float tem, int hum) {

  Serial.println("==========");

  Serial.print("connecting to ");

  Serial.println(host);

  

  //----------------------------------------Connect to Google host

  if (!client.connect(host, httpsPort)) {

    Serial.println("connection failed");

    return;

  }

  //----------------------------------------


  //----------------------------------------Processing data and sending data

  String string_temperature =  String(tem);

  // String string_temperature =  String(tem, DEC); 

  String string_humidity =  String(hum, DEC); 

  String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity;

  Serial.print("requesting URL: ");

  Serial.println(url);


  client.print(String("GET ") + url + " HTTP/1.1\r\n" +

         "Host: " + host + "\r\n" +

         "User-Agent: BuildFailureDetectorESP8266\r\n" +

         "Connection: close\r\n\r\n");


  Serial.println("request sent");

  //----------------------------------------


  //----------------------------------------Checking whether the data was sent successfully or not

  while (client.connected()) {

    String line = client.readStringUntil('\n');

    if (line == "\r") {

      Serial.println("headers received");

      break;

    }

  }

  String line = client.readStringUntil('\n');

  if (line.startsWith("{\"state\":\"success\"")) {

    Serial.println("esp8266/Arduino CI successfull!");

  } else {

    Serial.println("esp8266/Arduino CI has failed");

  }

  Serial.print("reply was : ");

  Serial.println(line);

  Serial.println("closing connection");

  Serial.println("==========");

  Serial.println();

  //----------------------------------------

//==============================================================================


Script

function doGet(e) { 
  Logger.logJSON.stringify(e) );
  var result = 'Ok';
  if (e.parameter == 'undefined') {
    result = 'No Parameters';
  }
  else {
    var sheet_id = '1fN1cT0CYQhljYGHLL7UmM4uHhBW6stWhn2WxKR8BUp8';  // Spreadsheet ID
    var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet();
    var newRow = sheet.getLastRow() + 1;            
    var rowData = [];
    var Curr_Date = new Date();
    rowData[0] = Curr_Date// Date in column A
    var Curr_Time = Utilities.formatDate(Curr_Date"Asia/Jakarta"'HH:mm:ss');
    rowData[1] = Curr_Time// Time in column B
    for (var param in e.parameter) {
      Logger.log('In for loop, param=' + param);
      var value = stripQuotes(e.parameter[param]);
      Logger.log(param + ':' + e.parameter[param]);
      switch (param) {
        case 'temperature':
          rowData[2] = value// Temperature in column C
          result = 'Temperature Written on column C'
          break;
        case 'humidity':
          rowData[3] = value// Humidity in column D
          result += ' ,Humidity Written on column D'
          break;  
        default:
          result = "unsupported parameter";
      }
    }
    Logger.log(JSON.stringify(rowData));
    var newRange = sheet.getRange(newRow11rowData.length);
    newRange.setValues([rowData]);
  }
  return ContentService.createTextOutput(result);
}
function stripQuotesvalue ) {
  return value.replace(/^["']|['"]$/g"");
}

OUTPUT AT MONITOR

Temperature : 25.30 °C Humidity : 48 % ========== connecting to script.google.com requesting URL: /macros/s/1fN1cT0CYQhljYGHLL7UmM4uHhBW6stWhn2WxKR8BUp8/exec?temperature=25.30&humidity=48 request sent headers received esp8266/Arduino CI has failed reply was : bee closing connection





Control led light using remote

 // Include IR Remote Library by Ken Shirriff #include <IRremote.h> // Define sensor pin const int RECV_PIN = 4; // Define LED pin #de...