Assembly – File Management ”; Previous Next The system considers any input or output data as stream of bytes. There are three standard file streams − Standard input (stdin), Standard output (stdout), and Standard error (stderr). File Descriptor A file descriptor is a 16-bit integer assigned to a file as a file id. When a new file is created or an existing file is opened, the file descriptor is used for accessing the file. File descriptor of the standard file streams – stdin, stdout and stderr are 0, 1 and 2, respectively. File Pointer A file pointer specifies the location for a subsequent read/write operation in the file in terms of bytes. Each file is considered as a sequence of bytes. Each open file is associated with a file pointer that specifies an offset in bytes, relative to the beginning of the file. When a file is opened, the file pointer is set to zero. File Handling System Calls The following table briefly describes the system calls related to file handling − %eax Name %ebx %ecx %edx 2 sys_fork struct pt_regs – – 3 sys_read unsigned int char * size_t 4 sys_write unsigned int const char * size_t 5 sys_open const char * int int 6 sys_close unsigned int – – 8 sys_creat const char * int – 19 sys_lseek unsigned int off_t unsigned int The steps required for using the system calls are same, as we discussed earlier − Put the system call number in the EAX register. Store the arguments to the system call in the registers EBX, ECX, etc. Call the relevant interrupt (80h). The result is usually returned in the EAX register. Creating and Opening a File For creating and opening a file, perform the following tasks − Put the system call sys_creat() number 8, in the EAX register. Put the filename in the EBX register. Put the file permissions in the ECX register. The system call returns the file descriptor of the created file in the EAX register, in case of error, the error code is in the EAX register. Opening an Existing File For opening an existing file, perform the following tasks − Put the system call sys_open() number 5, in the EAX register. Put the filename in the EBX register. Put the file access mode in the ECX register. Put the file permissions in the EDX register. The system call returns the file descriptor of the created file in the EAX register, in case of error, the error code is in the EAX register. Among the file access modes, most commonly used are: read-only (0), write-only (1), and read-write (2). Reading from a File For reading from a file, perform the following tasks − Put the system call sys_read() number 3, in the EAX register. Put the file descriptor in the EBX register. Put the pointer to the input buffer in the ECX register. Put the buffer size, i.e., the number of bytes to read, in the EDX register. The system call returns the number of bytes read in the EAX register, in case of error, the error code is in the EAX register. Writing to a File For writing to a file, perform the following tasks − Put the system call sys_write() number 4, in the EAX register. Put the file descriptor in the EBX register. Put the pointer to the output buffer in the ECX register. Put the buffer size, i.e., the number of bytes to write, in the EDX register. The system call returns the actual number of bytes written in the EAX register, in case of error, the error code is in the EAX register. Closing a File For closing a file, perform the following tasks − Put the system call sys_close() number 6, in the EAX register. Put the file descriptor in the EBX register. The system call returns, in case of error, the error code in the EAX register. Updating a File For updating a file, perform the following tasks − Put the system call sys_lseek () number 19, in the EAX register. Put the file descriptor in the EBX register. Put the offset value in the ECX register. Put the reference position for the offset in the EDX register. The reference position could be: Beginning of file – value 0 Current position – value 1 End of file – value 2 The system call returns, in case of error, the error code in the EAX register. Example The following program creates and opens a file named myfile.txt, and writes a text ”Welcome to Tutorials Point” in this file. Next, the program reads from the file and stores the data into a buffer named info. Lastly, it displays the text as stored in info. section .text global _start ;must be declared for using gcc _start: ;tell linker entry point ;create the file mov eax, 8 mov ebx, file_name mov ecx, 0777 ;read, write and execute by all int 0x80 ;call kernel mov [fd_out], eax ; write into the file mov edx,len ;number of bytes mov ecx, msg ;message to write mov ebx, [fd_out] ;file descriptor mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel ; close the file mov eax, 6 mov ebx, [fd_out] ; write the message indicating end of file write mov eax, 4 mov ebx, 1 mov ecx, msg_done mov edx, len_done int 0x80 ;open the file for reading mov eax, 5 mov ebx, file_name mov ecx, 0 ;for read only access mov edx, 0777 ;read, write and execute by all int 0x80 mov [fd_in], eax ;read from file mov eax, 3 mov ebx, [fd_in] mov ecx, info mov edx, 26 int 0x80 ; close the file mov eax, 6 mov ebx, [fd_in] int 0x80 ; print the info mov eax, 4 mov ebx, 1 mov ecx, info mov edx, 26 int 0x80 mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data file_name db ”myfile.txt” msg db ”Welcome to Tutorials Point” len equ $-msg msg_done db
Category: Computer Programming
Assembly – System Calls
Assembly – System Calls ”; Previous Next System calls are APIs for the interface between the user space and the kernel space. We have already used the system calls. sys_write and sys_exit, for writing into the screen and exiting from the program, respectively. Linux System Calls You can make use of Linux system calls in your assembly programs. You need to take the following steps for using Linux system calls in your program − Put the system call number in the EAX register. Store the arguments to the system call in the registers EBX, ECX, etc. Call the relevant interrupt (80h). The result is usually returned in the EAX register. There are six registers that store the arguments of the system call used. These are the EBX, ECX, EDX, ESI, EDI, and EBP. These registers take the consecutive arguments, starting with the EBX register. If there are more than six arguments, then the memory location of the first argument is stored in the EBX register. The following code snippet shows the use of the system call sys_exit − mov eax,1 ; system call number (sys_exit) int 0x80 ; call kernel The following code snippet shows the use of the system call sys_write − mov edx,4 ; message length mov ecx,msg ; message to write mov ebx,1 ; file descriptor (stdout) mov eax,4 ; system call number (sys_write) int 0x80 ; call kernel All the syscalls are listed in /usr/include/asm/unistd.h, together with their numbers (the value to put in EAX before you call int 80h). The following table shows some of the system calls used in this tutorial − %eax Name %ebx %ecx %edx %esx %edi 1 sys_exit int – – – – 2 sys_fork struct pt_regs – – – – 3 sys_read unsigned int char * size_t – – 4 sys_write unsigned int const char * size_t – – 5 sys_open const char * int int – – 6 sys_close unsigned int – – – – Example The following example reads a number from the keyboard and displays it on the screen − Live Demo section .data ;Data segment userMsg db ”Please enter a number: ” ;Ask the user to enter a number lenUserMsg equ $-userMsg ;The length of the message dispMsg db ”You have entered: ” lenDispMsg equ $-dispMsg section .bss ;Uninitialized data num resb 5 section .text ;Code Segment global _start _start: ;User prompt mov eax, 4 mov ebx, 1 mov ecx, userMsg mov edx, lenUserMsg int 80h ;Read and store the user input mov eax, 3 mov ebx, 2 mov ecx, num mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information int 80h ;Output the message ”The entered number is: ” mov eax, 4 mov ebx, 1 mov ecx, dispMsg mov edx, lenDispMsg int 80h ;Output the number entered mov eax, 4 mov ebx, 1 mov ecx, num mov edx, 5 int 80h ; Exit code mov eax, 1 mov ebx, 0 int 80h When the above code is compiled and executed, it produces the following result − Please enter a number: 1234 You have entered:1234 Print Page Previous Next Advertisements ”;
Assembly – Environment Setup
Assembly – Environment Setup ”; Previous Next Local Environment Setup Assembly language is dependent upon the instruction set and the architecture of the processor. In this tutorial, we focus on Intel-32 processors like Pentium. To follow this tutorial, you will need − An IBM PC or any equivalent compatible computer A copy of Linux operating system A copy of NASM assembler program There are many good assembler programs, such as − Microsoft Assembler (MASM) Borland Turbo Assembler (TASM) The GNU assembler (GAS) We will use the NASM assembler, as it is − Free. You can download it from various web sources. Well documented and you will get lots of information on net. Could be used on both Linux and Windows. Installing NASM If you select “Development Tools” while installing Linux, you may get NASM installed along with the Linux operating system and you do not need to download and install it separately. For checking whether you already have NASM installed, take the following steps − Open a Linux terminal. Type whereis nasm and press ENTER. If it is already installed, then a line like, nasm: /usr/bin/nasm appears. Otherwise, you will see just nasm:, then you need to install NASM. To install NASM, take the following steps − Check The netwide assembler (NASM) website for the latest version. Download the Linux source archive nasm-X.XX.ta.gz, where X.XX is the NASM version number in the archive. Unpack the archive into a directory which creates a subdirectory nasm-X. XX. cd to nasm-X.XX and type ./configure. This shell script will find the best C compiler to use and set up Makefiles accordingly. Type make to build the nasm and ndisasm binaries. Type make install to install nasm and ndisasm in /usr/local/bin and to install the man pages. This should install NASM on your system. Alternatively, you can use an RPM distribution for the Fedora Linux. This version is simpler to install, just double-click the RPM file. Print Page Previous Next Advertisements ”;
Assembly – Memory Segments
Assembly – Memory Segments ”; Previous Next We have already discussed the three sections of an assembly program. These sections represent various memory segments as well. Interestingly, if you replace the section keyword with segment, you will get the same result. Try the following code − Live Demo segment .text ;code segment global _start ;must be declared for linker _start: ;tell linker entry point mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel segment .data ;data segment msg db ”Hello, world!”,0xa ;our dear string len equ $ – msg ;length of our dear string When the above code is compiled and executed, it produces the following result − Hello, world! Memory Segments A segmented memory model divides the system memory into groups of independent segments referenced by pointers located in the segment registers. Each segment is used to contain a specific type of data. One segment is used to contain instruction codes, another segment stores the data elements, and a third segment keeps the program stack. In the light of the above discussion, we can specify various memory segments as − Data segment − It is represented by .data section and the .bss. The .data section is used to declare the memory region, where data elements are stored for the program. This section cannot be expanded after the data elements are declared, and it remains static throughout the program. The .bss section is also a static memory section that contains buffers for data to be declared later in the program. This buffer memory is zero-filled. Code segment − It is represented by .text section. This defines an area in memory that stores the instruction codes. This is also a fixed area. Stack − This segment contains data values passed to functions and procedures within the program. Print Page Previous Next Advertisements ”;
Assembly – Logical Instructions ”; Previous Next The processor instruction set provides the instructions AND, OR, XOR, TEST, and NOT Boolean logic, which tests, sets, and clears the bits according to the need of the program. The format for these instructions − Sr.No. Instruction Format 1 AND AND operand1, operand2 2 OR OR operand1, operand2 3 XOR XOR operand1, operand2 4 TEST TEST operand1, operand2 5 NOT NOT operand1 The first operand in all the cases could be either in register or in memory. The second operand could be either in register/memory or an immediate (constant) value. However, memory-to-memory operations are not possible. These instructions compare or match bits of the operands and set the CF, OF, PF, SF and ZF flags. The AND Instruction The AND instruction is used for supporting logical expressions by performing bitwise AND operation. The bitwise AND operation returns 1, if the matching bits from both the operands are 1, otherwise it returns 0. For example − Operand1: 0101 Operand2: 0011 —————————- After AND -> Operand1: 0001 The AND operation can be used for clearing one or more bits. For example, say the BL register contains 0011 1010. If you need to clear the high-order bits to zero, you AND it with 0FH. AND BL, 0FH ; This sets BL to 0000 1010 Let”s take up another example. If you want to check whether a given number is odd or even, a simple test would be to check the least significant bit of the number. If this is 1, the number is odd, else the number is even. Assuming the number is in AL register, we can write − AND AL, 01H ; ANDing with 0000 0001 JZ EVEN_NUMBER The following program illustrates this − Example Live Demo section .text global _start ;must be declared for using gcc _start: ;tell linker entry point mov ax, 8h ;getting 8 in the ax and ax, 1 ;and ax with 1 jz evnn mov eax, 4 ;system call number (sys_write) mov ebx, 1 ;file descriptor (stdout) mov ecx, odd_msg ;message to write mov edx, len2 ;length of message int 0x80 ;call kernel jmp outprog evnn: mov ah, 09h mov eax, 4 ;system call number (sys_write) mov ebx, 1 ;file descriptor (stdout) mov ecx, even_msg ;message to write mov edx, len1 ;length of message int 0x80 ;call kernel outprog: mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data even_msg db ”Even Number!” ;message showing even number len1 equ $ – even_msg odd_msg db ”Odd Number!” ;message showing odd number len2 equ $ – odd_msg When the above code is compiled and executed, it produces the following result − Even Number! Change the value in the ax register with an odd digit, like − mov ax, 9h ; getting 9 in the ax The program would display: Odd Number! Similarly to clear the entire register you can AND it with 00H. The OR Instruction The OR instruction is used for supporting logical expression by performing bitwise OR operation. The bitwise OR operator returns 1, if the matching bits from either or both operands are one. It returns 0, if both the bits are zero. For example, Operand1: 0101 Operand2: 0011 —————————- After OR -> Operand1: 0111 The OR operation can be used for setting one or more bits. For example, let us assume the AL register contains 0011 1010, you need to set the four low-order bits, you can OR it with a value 0000 1111, i.e., FH. OR BL, 0FH ; This sets BL to 0011 1111 Example The following example demonstrates the OR instruction. Let us store the value 5 and 3 in the AL and the BL registers, respectively, then the instruction, OR AL, BL should store 7 in the AL register − Live Demo section .text global _start ;must be declared for using gcc _start: ;tell linker entry point mov al, 5 ;getting 5 in the al mov bl, 3 ;getting 3 in the bl or al, bl ;or al and bl registers, result should be 7 add al, byte ”0” ;converting decimal to ascii mov [result], al mov eax, 4 mov ebx, 1 mov ecx, result mov edx, 1 int 0x80 outprog: mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .bss result resb 1 When the above code is compiled and executed, it produces the following result − 7 The XOR Instruction The XOR instruction implements the bitwise XOR operation. The XOR operation sets the resultant bit to 1, if and only if the bits from the operands are different. If the bits from the operands are same (both 0 or both 1), the resultant bit is cleared to 0. For example, Operand1: 0101 Operand2: 0011 —————————- After XOR -> Operand1: 0110 XORing an operand with itself changes the operand to 0. This is used to clear a register. XOR EAX, EAX The TEST Instruction The TEST instruction works same as the AND operation, but unlike AND instruction, it does not change the first operand. So, if we need to check whether a number in a register is even or odd, we can also do this using the TEST instruction without changing the original number. TEST AL, 01H JZ EVEN_NUMBER The NOT Instruction The NOT instruction implements the bitwise NOT operation. NOT operation reverses the bits in an operand. The operand could be either in a register or in the memory. For example, Operand1: 0101 0011 After NOT -> Operand1: 1010 1100 Print Page Previous Next Advertisements ”;
Assembly – Numbers
Assembly – Numbers ”; Previous Next Numerical data is generally represented in binary system. Arithmetic instructions operate on binary data. When numbers are displayed on screen or entered from keyboard, they are in ASCII form. So far, we have converted this input data in ASCII form to binary for arithmetic calculations and converted the result back to binary. The following code shows this − Live Demo section .text global _start ;must be declared for using gcc _start: ;tell linker entry point mov eax,”3” sub eax, ”0” mov ebx, ”4” sub ebx, ”0” add eax, ebx add eax, ”0” mov [sum], eax mov ecx,msg mov edx, len mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov ecx,sum mov edx, 1 mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data msg db “The sum is:”, 0xA,0xD len equ $ – msg segment .bss sum resb 1 When the above code is compiled and executed, it produces the following result − The sum is: 7 Such conversions, however, have an overhead, and assembly language programming allows processing numbers in a more efficient way, in the binary form. Decimal numbers can be represented in two forms − ASCII form BCD or Binary Coded Decimal form ASCII Representation In ASCII representation, decimal numbers are stored as string of ASCII characters. For example, the decimal value 1234 is stored as − 31 32 33 34H Where, 31H is ASCII value for 1, 32H is ASCII value for 2, and so on. There are four instructions for processing numbers in ASCII representation − AAA − ASCII Adjust After Addition AAS − ASCII Adjust After Subtraction AAM − ASCII Adjust After Multiplication AAD − ASCII Adjust Before Division These instructions do not take any operands and assume the required operand to be in the AL register. The following example uses the AAS instruction to demonstrate the concept − Live Demo section .text global _start ;must be declared for using gcc _start: ;tell linker entry point sub ah, ah mov al, ”9” sub al, ”3” aas or al, 30h mov [res], ax mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov edx,1 ;message length mov ecx,res ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data msg db ”The Result is:”,0xa len equ $ – msg section .bss res resb 1 When the above code is compiled and executed, it produces the following result − The Result is: 6 BCD Representation There are two types of BCD representation − Unpacked BCD representation Packed BCD representation In unpacked BCD representation, each byte stores the binary equivalent of a decimal digit. For example, the number 1234 is stored as − 01 02 03 04H There are two instructions for processing these numbers − AAM − ASCII Adjust After Multiplication AAD − ASCII Adjust Before Division The four ASCII adjust instructions, AAA, AAS, AAM, and AAD, can also be used with unpacked BCD representation. In packed BCD representation, each digit is stored using four bits. Two decimal digits are packed into a byte. For example, the number 1234 is stored as − 12 34H There are two instructions for processing these numbers − DAA − Decimal Adjust After Addition DAS − decimal Adjust After Subtraction There is no support for multiplication and division in packed BCD representation. Example The following program adds up two 5-digit decimal numbers and displays the sum. It uses the above concepts − Live Demo section .text global _start ;must be declared for using gcc _start: ;tell linker entry point mov esi, 4 ;pointing to the rightmost digit mov ecx, 5 ;num of digits clc add_loop: mov al, [num1 + esi] adc al, [num2 + esi] aaa pushf or al, 30h popf mov [sum + esi], al dec esi loop add_loop mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov edx,5 ;message length mov ecx,sum ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .data msg db ”The Sum is:”,0xa len equ $ – msg num1 db ”12345” num2 db ”23456” sum db ” ” When the above code is compiled and executed, it produces the following result − The Sum is: 35801 Print Page Previous Next Advertisements ”;
Assembly – Loops
Assembly – Loops ”; Previous Next The JMP instruction can be used for implementing loops. For example, the following code snippet can be used for executing the loop-body 10 times. MOV CL, 10 L1: <LOOP-BODY> DEC CL JNZ L1 The processor instruction set, however, includes a group of loop instructions for implementing iteration. The basic LOOP instruction has the following syntax − LOOP label Where, label is the target label that identifies the target instruction as in the jump instructions. The LOOP instruction assumes that the ECX register contains the loop count. When the loop instruction is executed, the ECX register is decremented and the control jumps to the target label, until the ECX register value, i.e., the counter reaches the value zero. The above code snippet could be written as − mov ECX,10 l1: <loop body> loop l1 Example The following program prints the number 1 to 9 on the screen − Live Demo section .text global _start ;must be declared for using gcc _start: ;tell linker entry point mov ecx,10 mov eax, ”1” l1: mov [num], eax mov eax, 4 mov ebx, 1 push ecx mov ecx, num mov edx, 1 int 0x80 mov eax, [num] sub eax, ”0” inc eax add eax, ”0” pop ecx loop l1 mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel section .bss num resb 1 When the above code is compiled and executed, it produces the following result − 123456789: Print Page Previous Next Advertisements ”;
Arduino – Network Communication ”; Previous Next The CC3000 WiFi module from Texas Instruments is a small silver package, which finally brings easy-to-use, affordable WiFi functionality to your Arduino projects. It uses SPI for communication (not UART!) so you can push data as fast as you want or as slow as you want. It has a proper interrupt system with IRQ pin so you can have asynchronous connections. It supports 802.11b/g, open/WEP/WPA/WPA2 security, TKIP & AES. A built-in TCP/IP stack with a “BSD socket” interface supports TCP and UDP in both the client and the server mode. Components Required You will need the following components − 1 × Arduino Uno 1 × Adafruit CC3000 breakout board 1 × 5V relay 1 × Rectifier diode 1 × LED 1 × 220 Ohm resistor 1 × Breadboard and some jumper wires For this project, you just need the usual Arduino IDE, the Adafruit’s CC3000 library, and the CC3000 MDNS library. We are also going to use the aREST library to send commands to the relay via WiFi. Procedure Follow the circuit diagram and make the connections as shown in the image given below. The hardware configuration for this project is very easy. Connect the IRQ pin of the CC3000 board to pin number 3 of the Arduino board. VBAT to pin 5, and CS to pin 10. Connect the SPI pins to Arduino board: MOSI, MISO, and CLK to pins 11, 12, and 13, respectively. Vin is connected to Arduino 5V, and GND to GND. Let us now connect the relay. After placing the relay on the breadboard, you can start identifying the two important parts on your relay: the coil part which commands the relay, and the switch part where we will attach the LED. First, connect pin number 8 of Arduino board to one pin of the coil. Connect the other pin to the ground of Arduino board. You also have to place the rectifier diode (anode connected to the ground pin) over the pins of the coil to protect your circuit when the relay is switching. Connect the +5V of Arduino board to the common pin of the relay’s switch. Finally, connect one of the other pin of the switch (usually, the one which is not connected when the relay is off) to the LED in series with the 220 Ohm resistor, and connect the other side of the LED to the ground of Arduino board. Testing Individual Components You can test the relay with the following sketch − const int relay_pin = 8; // Relay pin void setup() { Serial.begin(9600); pinMode(relay_pin,OUTPUT); } void loop() { // Activate relay digitalWrite(relay_pin, HIGH); // Wait for 1 second delay(1000); // Deactivate relay digitalWrite(relay_pin, LOW); // Wait for 1 second delay(1000); } Code to Note The code is self-explanatory. You can just upload it to the board and the relay will switch states every second, and the LED will switch ON and OFF accordingly. Adding WiFi Connectivity Let us now control the relay wirelessly using the CC3000 WiFi chip. The software for this project is based on the TCP protocol. However, for this project, Arduino board will be running a small web server, so we can “listen” for commands coming from the computer. We will first take care of Arduino sketch, and then we will see how to write the server-side code and create a nice interface. First, the Arduino sketch. The goal here is to connect to your WiFi network, create a web server, check if there are incoming TCP connections, and then change the state of the relay accordingly. Important Parts of the Code #include <Adafruit_CC3000.h> #include <SPI.h> #include <CC3000_MDNS.h> #include <Ethernet.h> #include <aREST.h> You need to define inside the code what is specific to your configuration, i.e. Wi-Fi name and password, and the port for TCP communications (we have used 80 here). // WiFi network (change with your settings!) #define WLAN_SSID “yourNetwork” // cannot be longer than 32 characters! #define WLAN_PASS “yourPassword” #define WLAN_SECURITY WLAN_SEC_WPA2 // This can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, // WLAN_SEC_WPA or WLAN_SEC_WPA2 // The port to listen for incoming TCP connections #define LISTEN_PORT 80 We can then create the CC3000 instance, server and aREST instance − // Server instance Adafruit_CC3000_Server restServer(LISTEN_PORT); // DNS responder instance MDNSResponder mdns; // Create aREST instance aREST rest = aREST(); In the setup() part of the sketch, we can now connect the CC3000 chip to the network − cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); How will the computer know where to send the data? One way would be to run the sketch once, then get the IP address of the CC3000 board, and modify the server code again. However, we can do better, and that is where the CC3000 MDNS library comes into play. We will assign a fixed name to our CC3000 board with this library, so we can write down this name directly into the server code. This is done with the following piece of code − if (!mdns.begin(“arduino”, cc3000)) { while(1); } We also need to listen for incoming connections. restServer.begin(); Next, we will code the loop() function of the sketch that will be continuously executed. We first have to update the mDNS server. mdns.update(); The server running on Arduino board will wait for the incoming connections and handle the requests. Adafruit_CC3000_ClientRef client = restServer.available(); rest.handle(client); It is now quite easy to test the projects via WiFi. Make sure you updated the sketch with your own WiFi name and password, and upload the sketch to your Arduino board. Open your Arduino IDE serial monitor, and look for the IP address of your board. Let us assume for the rest here that it is something like 192.168.1.103. Then, simply go to your favorite web browser, and type − 192.168.1.103/digital/8/1 You should see that your relay automatically turns ON. Building the Relay Interface We will now code the interface of the project. There will be two parts here: an HTML file containing the interface, and a client-side Javascript file
Arduino – Discussion
Discuss Arduino ”; Previous Next Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It consists of a circuit board, which can be programed (referred to as a microcontroller) and a ready-made software called Arduino IDE (Integrated Development Environment), which is used to write and upload the computer code to the physical board. Arduino provides a standard form factor that breaks the functions of the micro-controller into a more accessible package. Print Page Previous Next Advertisements ”;
Arduino – PIR Sensor
Arduino – PIR Sensor ”; Previous Next PIR sensors allow you to sense motion. They are used to detect whether a human has moved in or out of the sensor’s range. They are commonly found in appliances and gadgets used at home or for businesses. They are often referred to as PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors. Following are the advantages of PIR Sensors − Small in size Wide lens range Easy to interface Inexpensive Low-power Easy to use Do not wear out PIRs are made of pyroelectric sensors, a round metal can with a rectangular crystal in the center, which can detect levels of infrared radiation. Everything emits low-level radiation, and the hotter something is, the more radiation is emitted. The sensor in a motion detector is split in two halves. This is to detect motion (change) and not average IR levels. The two halves are connected so that they cancel out each other. If one-half sees more or less IR radiation than the other, the output will swing high or low. PIRs have adjustable settings and have a header installed in the 3-pin ground/out/power pads. For many basic projects or products that need to detect when a person has left or entered the area, PIR sensors are great. Note that PIRs do not tell you the number of people around or their closeness to the sensor. The lens is often fixed to a certain sweep at a distance and they are sometimes set off by the pets in the house. Components Required You will need the following components − 1 × Breadboard 1 × Arduino Uno R3 1 × PIR Sensor (MQ3) Procedure Follow the circuit diagram and make the connections as shown in the image below. Sketch Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open a new sketch File by clicking New. Arduino Code #define pirPin 2 int calibrationTime = 30; long unsigned int lowIn; long unsigned int pause = 5000; boolean lockLow = true; boolean takeLowTime; int PIRValue = 0; void setup() { Serial.begin(9600); pinMode(pirPin, INPUT); } void loop() { PIRSensor(); } void PIRSensor() { if(digitalRead(pirPin) == HIGH) { if(lockLow) { PIRValue = 1; lockLow = false; Serial.println(“Motion detected.”); delay(50); } takeLowTime = true; } if(digitalRead(pirPin) == LOW) { if(takeLowTime){ lowIn = millis();takeLowTime = false; } if(!lockLow && millis() – lowIn > pause) { PIRValue = 0; lockLow = true; Serial.println(“Motion ended.”); delay(50); } } } Code to Note PIR sensor has three terminals – Vcc, OUT and GND. Connect the sensor as follows − Connect the +Vcc to +5v on Arduino board. Connect OUT to digital pin 2 on Arduino board. Connect GND with GND on Arduino. You can adjust the sensor sensitivity and delay time via two variable resistors located at the bottom of the sensor board. Once the sensor detects any motion, Arduino will send a message via the serial port to say that a motion is detected. The PIR sense motion will delay for certain time to check if there is a new motion. If there is no motion detected, Arduino will send a new message saying that the motion has ended. Result You will see a message on your serial port if a motion is detected and another message when the motion stops. Print Page Previous Next Advertisements ”;