Saturday, August 21, 2021

BBC Microbit: Hourglass

 



Code en JavaScript


Code Microbit 1:

input.onButtonPressed(Button.Afunction () {
    radio.sendNumber(cmp)
    basic.showLeds(`
        . . # . .
        . . # . .
        # . # . #
        . # # # .
        . . # . .
        `)
    basic.showLeds(`
        . . . . .
        . . # . .
        . . # . .
        # . # . #
        . # # # .
        `)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . # . .
        . . # . .
        # . # . #
        `)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . # . .
        . . # . .
        `)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . # . .
        `)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        `)
})
input.onGesture(Gesture.LogoUpfunction () {
    basic.clearScreen()
    pause2 = cmp * 1000 / 10
    radio.sendValue("up", pause2)
    basic.showLeds(`
        # # # # #
        # # # # #
        # # # # #
        # # # # #
        # # # # #
        `)
    basic.showLeds(`
        # # # # #
        # # # # #
        # # # # #
        # # # # #
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        # # # # #
        # # # # #
        # # # # #
        # # # # #
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        # # # # #
        # # # # #
        # # # # #
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        # # # # #
        # # # # #
        # # # # #
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        . . . . .
        # # # # #
        # # # # #
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        . . . . .
        # # # # #
        # # # # #
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        # # # # #
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . # # # .
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . # . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        `)
})
input.onButtonPressed(Button.ABfunction () {
    radio.sendString("reset")
    basic.showIcon(IconNames.SmallSquare)
    basic.pause(100)
    basic.showIcon(IconNames.Square)
    basic.clearScreen()
    control.reset()
})
input.onButtonPressed(Button.Bfunction () {
    cmp += 1
    basic.showNumber(cmp)
})
radio.onReceivedValue(function (name, value) {
    if (name == "down") {
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            `)
        basic.showLeds(`
            . . # . .
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            `)
        basic.pause(value)
        basic.showLeds(`
            . # # # .
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            . . # . .
            . . . . .
            . . . . .
            . . . . .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            . # # # .
            . . . . .
            . . . . .
            . . . . .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            # # # # #
            . . # . .
            . . . . .
            . . . . .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            # # # # #
            . # # # .
            . . . . .
            . . . . .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            . . # . .
            . . . . .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            . # # # .
            . . . . .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            . . # . .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            . # # # .
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    }
})
let pause2 = 0
let cmp = 0
radio.setGroup(1)
cmp = 0


Code Microbit2:


radio.onReceivedNumber(function (receivedNumber) {
    pause2 = receivedNumber * 1000 / 10
    basic.showLeds(`
        . . # . .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        `)
    basic.showLeds(`
        . # # # .
        . . # . .
        . . . . .
        . . . . .
        . . . . .
        `)
    basic.showLeds(`
        # . # . #
        . # # # .
        . . # . .
        . . . . .
        . . . . .
        `)
    basic.showLeds(`
        . . # . .
        # . # . #
        . # # # .
        . . # . .
        . . . . .
        `)
    basic.showLeds(`
        . . # . .
        . . # . .
        # . # . #
        . # # # .
        . . # . .
        `)
    basic.clearScreen()
})
radio.onReceivedString(function (receivedString) {
    basic.showIcon(IconNames.SmallSquare)
    basic.pause(100)
    basic.showIcon(IconNames.Square)
    basic.clearScreen()
    control.reset()
})
radio.onReceivedValue(function (name, value) {
    if (name == "up") {
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            `)
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            . . # . .
            `)
        basic.pause(value)
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            . # # # .
            `)
        basic.pause(value)
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . . # . .
            # # # # #
            `)
        basic.pause(value)
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . # # # .
            # # # # #
            `)
        basic.pause(value)
        basic.showLeds(`
            . . . . .
            . . . . .
            . . # . .
            # # # # #
            # # # # #
            `)
        basic.pause(value)
        basic.showLeds(`
            . . . . .
            . . . . .
            . # # # .
            # # # # #
            # # # # #
            `)
        basic.pause(value)
        basic.showLeds(`
            . . . . .
            . . # . .
            # # # # #
            # # # # #
            # # # # #
            `)
        basic.pause(value)
        basic.showLeds(`
            . . . . .
            . # # # .
            # # # # #
            # # # # #
            # # # # #
            `)
        basic.pause(value)
        basic.showLeds(`
            . . # . .
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
        basic.pause(value)
        basic.showLeds(`
            . # # # .
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
        basic.pause(value)
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    }
})
input.onGesture(Gesture.LogoDownfunction () {
    basic.clearScreen()
    radio.sendValue("down", pause2)
    basic.showLeds(`
        # # # # #
        # # # # #
        # # # # #
        # # # # #
        # # # # #
        `)
    basic.showLeds(`
        . . # . .
        # # # # #
        # # # # #
        # # # # #
        # # # # #
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . # . .
        # # # # #
        # # # # #
        # # # # #
        # # # # #
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . # . .
        # # # # #
        # # # # #
        # # # # #
        . . . . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . # . .
        # # # # #
        # # # # #
        # # # # #
        . . . . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . # . .
        # # # # #
        # # # # #
        . . . . .
        . . . . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . # . .
        # # # # #
        # # # # #
        . . . . .
        . . . . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . # . .
        # # # # #
        . . . . .
        . . . . .
        . . . . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . # . .
        # # # # #
        . . . . .
        . . . . .
        . . . . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . # # # .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . # . .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        `)
    basic.pause(pause2)
    basic.showLeds(`
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        . . . . .
        `)
})
let pause2 = 0
radio.setGroup(1)

How to Embed Google Apps Script Web Apps in Websites

Tuesday, August 17, 2021

How to create a form with Google Apps Script and submit the data to Goog...


 Code.gs
function doGet(request) {
  return HtmlService.createTemplateFromFile('Index').evaluate();
  
}

function include(filename){
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

function processForm(formObject){
  var url="";
  var ssSpreadsheetApp.openByUrl(url);
  var ws=ss.getSheetByName("Data");

  ws.appendRow([
    formObject.first_name,
    formObject.last_name,
    formObject.gender,
    formObject.dateOfBirth,
    formObject.email,
    formObject.phone

  ]);
}


Index.html
<!DOCTYPE html>
<html>
  <head>
  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
 integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" 
crossorigin="anonymous">
  <?!= include('JavaScript'); ?>
  </head>
  
  <body>
    <div class="container">
            <div class="row">
                <div class="col-6">
                    <form id="myForm" onsubmit="handleFormSubmit(this)">
                        <p class="h4 mb-4 text-center">Contact Details</p>

                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="first_name">First Name</label>
                                <input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name">
                            </div>
                            <div class="form-group col-md-6">
                                <label for="last_name">Last Name</label>
                                <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name">
                            </div>
                        </div>

                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <p>Gender</p>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" type="radio" name="gender" id="male" value="male">
                                    <label class="form-check-label" for="male">Male</label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input class="form-check-input" type="radio" name="gender" id="female" value="female">
                                    <label class="form-check-label" for="female">Female</label>
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="dateOfBirth">Date of Birth</label>
                                <input type="date" class="form-control" id="dateOfBirth" name="dateOfBirth">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="email">Email</label>
                            <input type="email" class="form-control" id="email" name="email" placeholder="Email">
                        </div>

                        <div class="form-group">
                            <label for="phone">Phone Number</label>
                            <input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone Number">
                        </div>

                        <button type="submit" class="btn btn-primary btn-block">Submit</button>
                    </form>
                    <div id="output"></div>
                </div>
            </div>      
        </div>
  </body>
</html>



JavaScript

<script>

  function preventFormSubmit(){
    var forms=document.querySelectorAll('form');
    for (var i=0;i<forms.length;i++){
      forms[i].addEventListener('submit',function(event){
        event.preventDefault();
      });
    }
  }
window.addEventListener('load',preventFormSubmit);

function handleFormSubmit(formObject){
  google.script.run.processForm(formObject);
  document.getElementById("myForm").reset();
}


</script>

Thursday, August 5, 2021

How to connect Arduino UNO to a WIFI Network: ESP8266 - AT Commands



ESP8266 is an integrated circuit with a microcontroller allowing connection in WiFi.
In this video, we will study this WIFI module and learn how to use it with the Arduino UNO board.
You would then be able to:
-Connect the ESP-01 to Arduino.
-Send AT commands using Arduino UNO
-Use the Arduino to program the ESP-01 (the SoftwareSerial library)
Finally, You will have your first web server.
Connecting arduino to the internet allows remote control of objects. We're talking about Internet of Things (IOT)



#include <SoftwareSerial.h>                        
SoftwareSerial esp8266(10,11);                   
#define serialCommunicationSpeed 9600               
#define DEBUG true                                 

void setup()

{
  Serial.begin(serialCommunicationSpeed);           
  esp8266.begin(serialCommunicationSpeed);     
  InitWifiModule();                              
}

void loop()                                                         
{

  if(esp8266.available())                                           
 {    
    if(esp8266.find("+IPD,"))
    {
     delay(1000);
 
     int connectionId = esp8266.read()-48;                                                
     String webpage = "<h1>Hello World!</h1>";
     String cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     sendData(cipSend,1000,DEBUG);
     sendData(webpage,1000,DEBUG);
 
     String closeCommand = "AT+CIPCLOSE="; 
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";    
     sendData(closeCommand,3000,DEBUG);
    }
  }
}

String sendData(String command, const int timeout, boolean debug)
{
    String response = "";                                             
    esp8266.print(command);                                          
    long int time = millis();                                      
    while( (time+timeout) > millis())                                 
    {      
      while(esp8266.available())                                      
      {
        char c = esp8266.read();                                     
        response+=c;                                                  
      }  
    }    
    if(debug)                                                        
    {
      Serial.print(response);
    }    
    return response;                                                  
}

void InitWifiModule()
{
  sendData("AT+RST\r\n", 2000, DEBUG);                                                  
  sendData("AT+CWJAP=\"WIFI_name\",\"pwd\"\r\n", 2000, DEBUG);        
  delay (3000);
  sendData("AT+CWMODE=1\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIFSR\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIPMUX=1\r\n", 1500, DEBUG);                                             
  delay (1500);
  sendData("AT+CIPSERVER=1,80\r\n", 1500, DEBUG);                                     

}


Monday, August 2, 2021

Arduino and PIR Sensor: Home security system


Code Arduino:

int sensor=2;
int led=13;
int buzzer=9;
int state=LOW;
int val=0;

void setup() {
 pinMode(sensor,INPUT);
 pinMode(led,OUTPUT);
 pinMode(buzzer,OUTPUT);
 Serial.begin(9600); 

}

void loop() {
val=digitalRead(sensor);
if(val==HIGH){
  digitalWrite(led,HIGH);
  digitalWrite(buzzer,HIGH);
  delay(400);
}
else{
  digitalWrite(led,LOW);
  digitalWrite(buzzer,LOW);  
}
}