Development Guide with Intel Galileo 2: Scuba Diving into the Code • HWzone
ComputersGuidesFeatured articles

Development Guide with Intel Galileo 2: Scuba diving into code

Make an Idea a Reality: A Guide to Writing Programs in Javascript for Galley's Calendar , In the vicinity XDK

Get updates from everyone in TelgramGet updates from us all at TelgramJoin the channel now

We recently met Galileo 2 Board של And the many development possibilities that the board allows us.
In this tutorial we will go step by step from installing the board to developing a number With the tools available to us.

For the guide we received the sensor kit from Intel Grove starter kit plus Which contains a wide range of sensors including light sensor, temperature, As well as a number of additional components such as potentiometer (variable resistor), relay, momentary button and more.

12 3 4


You must have a card to install Galileo Has a volume of at least 4GB.
The operating system that runs on the card is Linux and can be downloaded from the Intel site Link below.
The file weighs as 210MB and should be retired on the card with the intended software. We will use the win32diskimager software that You can download from here.


The image file containing the operating system should be extracted with the help of 7Zip.
Select the file extracted by pressing the browse button (highlighted in red) and then select the drive letter representing the card You have.
Pressing the write button will begin with the process of retirement, which begins with deleting all the information on the card (do not forget to collect information on the card before the process).

Because the clipboard shows how to output a video, the clipboard displays information using the serial port located near the network input. To see this information, use the serial cable that is attached with the kit by connecting it to the board as described in the picture:


After connecting the cable to the computer, the operating system will identify a new USB Serial Port device and give it a unique name from the COMxx form. To connect to the serial connection, the new name must be found, in our case COM3. This connection can be connected using various terminal software, one of which is PuTTY
Note: You must connect at 115200bps speed

putty boot

For the development of this guide we will use the Intel XDK IoT Edition environment that enables With Javascript and contains a wide variety of ready-made samples.
The software can be downloaded from the following link.
The software requires registration among other things will allow you to save the projects in the cloud so that they will be available both on the computer physically and from any other computer where you install the software and enter the account.

Preparing the sensors for the project

To make it easy to connect and work with the sensors we have, we have printed a frame in 3D that fits the sensors and allows you to place the sensor on it regularly.
Our desktop will be neat and comfortable to work.


You can download the STL file for printing here.

The first project

Let's start with the hello world project of the non-blink hardware world. For those who do not know, the goal of the program is to make the LED flash and change its condition every second from the inlet to the lighter and vice versa.

To start a new project, select Templates from the menu on the left. On the right side are several ready-made templates that can serve as the basis for the program we want to write. Although one of the templates is already the Blink LED program, we are selected in Digital Write because we will write it in a slightly different way.

blink-1 blink-2

Before we get to write a code there is a connection between Galileo and XDK so that the software will know how to load our code into the clipboard, compile and run it.
Make sure the board is on and connected to your network. When the board has finished loading the operating system XDK will see it in the device list as shown in the following picture.

blink-connectIn the list we see the name of the board (galileo) and the IP address it received from the router + port number with which the software is calling.

After the panel is selected from the list, the login window will appear showing the connection details in front of the board, as well as a user name and password. If a password is not set to the clipboard (by default the password is empty), the password field must be left blank and the username must be left root. Clicking Connect will connect to the board if all is normal.
You may see a window asking to perform after the connection To synchronize the tablet clock with the software clock, there is no need to press a button .

connect2 connect3

Now let's look at the code provided to us by the template, understand it and make changes to reach our non-Blink target program.

var mraa = require ('mraa'); // require mraa console.log ('MRAA Version:' + mraa.getVersion ()); // write the mraa version to the console var myDigitalPin5 = new mraa.Gpio (5); // setup digital read on Digital pin #5 (D5) myDigitalPin5.dir (mraa.DIR_OUT); // set the gpio direction to output myDigitalPin5.write (1); // set the digital pin to high (1)

As can be seen at a glance at the code, Make sure to provide detailed notes and explanations throughout the program which makes it very easy to learn the platform.
In the first line we see that you have been called to the library called mraa. This is a library designed to communicate with the board's hardware components.
In the next line, we emit the version number by using the object we created and calling the getVersion function.
At this point we already have a plan that does something very basic. We will compile and see what it does.
In order to compile and run, we will use the toolbar next to the name of the clipboard we previously connected to.


First click on the second install / build button, the first time we run the program, install it on the board and compile all the components that we use in the program, in our case the mraa library.
The entire process can be seen in the black console window. If this is correct, a message will appear as shown in the following picture.


Now if we run the program by pressing the Run button from the toolbar we will see the following output:

Intel XDK - Message Received: run => Stopping App <= MRAA Version: v0.6.0

The program began to run, released the version number and finished its work.
We will proceed to the next three lines of the program.
We create a new object called myDigitalPin5 that represents pin number 5 on the board by calling the Gpio function which is part of the mraa object.
Then define the direction of the penis whether it is output or input. Since in our program the LED is powered by the receiver of the board and does not feed it and the pin information is set to Output.
In the last row, writing to the pin is done with the 1 value where 1 represents a high logical level (and there is pin voltage) and 0 represents a low logical level (no pin voltage).

Now we attach to the physical board the LED supplied in the kit for the D5 port on the expansion panel.


Now save the changes (Ctrl + S) and then click the first button on the Upload toolbar. This button will send the updated file to the clipboard. Then press the Run button and if everything is connected properly we will see the LED on permanently.
* Use of a note that we have not yet re-configured, because we have not added additional libraries to the project which must be recompiled for the program.

If you notice, the program ran once, turned the LED on and closed. This is because there is nothing in our code that prevents the program from ending its life. The LED remained on after the program came out because no one else had changed the output of Pin 5 from Logical 1 to 0.
Those of you who have worked with Arduino in the past know that the program itself is listed in a function called a loop. This function behind the scenes is called again and again without interruption, that's exactly what is done in our program.

Now we'll reorganize the code and try to write it a little more.

first version:

var mraa = require ('mraa'); console.log ("Application Version 1"); var myDigitalPin5 = new mraa.Gpio (5); var LEDState = 1; function setup () {myDigitalPin5.dir (mraa.DIR_OUT); } function loop () {myDigitalPin5.write (LEDState); LEDState = (LEDState == 1)? 0: 1; } setup (); setInterval (loop, 1000);

We've arranged the code to look a bit more like the standard structure of the Arduino program.
Tip: At the beginning of the code we added the line console.log with the output we want to see when the program starts running. More than once you forget to save the program and when we upload the program it does not do what we expected to happen. It is recommended that you change the message so that each message shows a different message and we will know that the program you wanted is really the program you intended to run.

In the above program you can see two new functions that we have added to the code and setup and loop when we call the setup function once and it will execute reboots or actions that we only want to do once in our program.
We then call the loop function by using a setInterval timer with a 1000 millisecond turnover time.
The loop function writes the value to the pin in the LEDState variable. We then change the value of LEDState depending on its content. If it contains 1 it will be changed to 0 and vice versa.
Now we'll save, upload, and click run.
The LED should begin flashing at intervals of a second between each situation.
Note that the run button remains in run mode, because now our program does not come out, it keeps running all the time since there is no one to stop the timer.
If we want to stop the program proactively, press the run button again, which will stop the program.

Second version:

In Javascript we do not have the delay function that delays the program for a certain time.
In this example we will write this function alone and use it so that the final result will be very similar in structure to the program written in Arduino.

var mraa = require ('mraa'); console.log ("Application Version 2"); function delay (ms) {var now = new Date (). getTime (); while (new Date (). getTime () <now + ms); } var myDigitalPin5 = new mraa.Gpio (5); function setup () {myDigitalPin5.dir (mraa.DIR_OUT); } function loop () {myDigitalPin5.write (1); delay (1000); myDigitalPin5.write (0); delay (1000); } setup (); while (1) {loop (); }

Let's look at the program and see some differences.
First delay function. All it does is to model the time at its entrance (the output of getTime is in milliseconds) and then within the while loop we will perform another sampling of time and remain if the current time is less than the initial sampling time + the number of milliseconds we would like to wait.
As long as the condition is maintained the loop will continue to run and essentially suspends the rest of the program. When the loop stops running, the current time is greater than the sampling time + the amount of milliseconds to wait, the program will continue to run and perform the remaining commands later on.
We have changed the loop function to use the delay function and we call it all the time by using infinite loop while (1).

We have successfully completed the first stage and are ready to move on.
Now we will write a program that contains an analog component that is called and in response to the data it is called a change in the light intensity of the LED.
We take the potentiometer from the kit and connect it to the A0 input on the expansion panel and the LED will be transferred to the D3 port.


In order to change the light intensity of the LED, a PWM (Pulse Width Modulation) capability is required with a different standard digital penis that allows only a state of no voltage or no voltage, PWM also allows different levels of voltage to be simulated between the two modes by changing the Duty Cycle whose meaning is what percentage of the signal cycle will be in High mode and how long it will be in Low mode.
If Duty Cycle of 50% is determined, the light intensity of the LED will be half that what it would be if the pin was digital in High mode.

Let's look at the following program:

var mraa = require ('mraa'); console.log ("PWM"); function delay (ms) {var now = new Date (). getTime (); while (new Date (). getTime () <now + ms); } var analogPin0 = new mraa.Aio (0); var LED = new mraa.Pwm (3); function setup () {LED.enable (true); } function loop () {var analogValue = (); var value = analogValue / 1023.0; LED.write (value); console.log (value); delay (10); } setup (); while (1) {loop (); }

We defined Pin A0 by using the mraa.Aio object as well as defining our lead as PWM with the help of mraa.Pwm (3).
In the setup function we called the "enable" option which allows the use of this pin in pwm mode.
In the loop function we first call the same value as the potentiometer from A0.
Unlike Arduino, the values ​​to write to the pwm port are between 0 and 1 and not between 0 and 255, for this reason we divide the value obtained by the maximum value that can be read from the entry.
The resulting value we write back to Led and output it to the screen for convenience.
Between each run of the function can be easy dilay of 10 milliseconds.
Now run the program and play with the potentiometer, the intensity of the LED should vary depending on the game.

Let's take the program one step further and write the illumination power on the LCD monitor provided with the kit.
The display is connected to one of the I2C connections on the expansion board.
The I2C connection represents the Protocol I2C Which Philips developed. It allows you to connect multiple devices (up to 127) on a shared bass when each device has its own unique address. This allows you to connect a large number of devices using only two pins, one for a common clock, and the other to transfer the information to the desired device.


Let's look at the following program:

var mraa = require ('mraa'); var LCD = require ('jsupm_i2clcd'); console.log ("LCD"); function delay (ms) {var now = new Date (). getTime (); while (new Date (). getTime () <now + ms); } var myLCD = new LCD.Jhd1313m1 (0); var analogPin0 = new mraa.Aio (0); var LED = new mraa.Pwm (3); var lcdMessage = ""; function setup () {LED.enable (true); myLCD.setColor (255, 128, 0); } function loop () {var analogValue = (); var value = analogValue / 1023.0; LED.write (value); var lastLcdMessage = lcdMessage; lcdMessage = "Brightness:" + (value.toFixed (2) * 100) + "%"; if (lcdMessage! = lastLcdMessage) {myLCD.clear (); } myLCD.setCursor (0,0); myLCD.write (lcdMessage); delay (100); } setup (); while (1) {loop (); }

The second line of the program seems to be using a new library called jsupm_i2clcd.
This library will manage all the communications in front of the LCD screen and spread it to a number of simple functions and easy to use.
This library is a small part of the UPM repository that manages the interface with external sensors and components. Unlike the mraa that deals with the board itself UPM reaches a level above and allows for simple conduction against other components.
You can see the repository code and a wide variety of examples on the project's github page:

Below we create the myLCD object by calling the LCD function.Jhd1313m1.
Jhd1313m1 This is the specific model of the board that we have and which can be seen on the back of the monitor as shown in the picture.


In the setup function we added a call to the setColor of the monitor. This function changes the display backlight according to the values Provided to her. Any value could be between 0 and 255. For example, 0, 0, 255 will produce only blue light because the values ​​of R, G are 0 and B at its highest intensity, 255.
In the loop function we will add an lcdMessage variable that will contain the text that we want to display on the display.
A call to myLCD.clear will erase anything displayed on the monitor. Because this function is relatively slow, if we keep calling it even when the text is unchanged, we can see a slight jitter on the monitor. To resolve the problem we save another variable that will contain the previous text If the text is changed only then we will clear the display.
myLCD.setCursor determines where we start writing when the first parameter is the position in X and the second in Y (row number). Our show is 16 × 2, which means two lines in each line with 16 characters.
myLCD.write Displays the text on the display.

Now run the new program.
* Please note that we have added a new directory to the project, so you must first (after saving) rebuild the project by using the second button in the toolbar, the install / build button.

We'll take another step and this time we want the screen's color to change depending on the ambient light intensity. To do this we will add a new sensor to the collection, Light Sensor.
The new sensor is connected to the A1 port and we make the appropriate changes so that the program will look like this:

var mraa = require ('mraa'); var LCD = require ('jsupm_i2clcd'); console.log ("LCD + Light"); function delay (ms) {var now = new Date (). getTime (); while (new Date (). getTime () <now + ms); } var myLCD = new LCD.Jhd1313m1 (0); var analogPin0 = new mraa.Aio (0); var analogPin1 = new mraa.Aio (1); var LED = new mraa.Pwm (3); var lcdMessage = ""; function setup () {LED.enable (true); myLCD.setColor (255, 128, 0); } function loop () {var analogValue = (); var value = analogValue / 1023.0; LED.write (value); var light = (); var lightValue = (light / 1023.0) * 255; myLCD.setColor (lightValue, 0, 255 - lightValue); var lastLcdMessage = lcdMessage; lcdMessage = "Brightness:" + (value.toFixed (2) * 100) + "%"; if (lcdMessage! = lastLcdMessage) {myLCD.clear (); } myLCD.setCursor (0,0); myLCD.write (lcdMessage); delay (100); } setup (); while (1) {loop (); }

In the loop function we added a sample of the sensor connected to A1, divided by the maximum value and multiplied by 255, which is the maximum value for the values ​​of R, G, B. After calculating the value we performed a call to the setColor function of the monitor with the resulting value. It is possible to play with the values ​​as desired and we chose the red color to be directly proportional to the sampled value, meaning that the more light there is, the higher the value of the red.
We determined the value of the green as 0 and the value of the blue we determined inversely proportional to the intensity, ie the more light there is the lower the value.
Now run the program and change the intensity of the light the sensor feels and notice how the lighting changes.

One of the great advantages of Galileo is its inherent ability to use the media.
All the programs we run on the board with XDK runs on top of the node.js so that it can be set up with ease.
In the following example, we will make a change to the program so that all information sampled by the board is sent to a remote connected customer with the help of TCP that we mount on the board.

Program Code:

var net = require ("net"); var mraa = require ('mraa'); var LCD = require ('jsupm_i2clcd'); console.log ("LCD + Light + TCP"); function delay (ms) {var now = new Date (). getTime (); while (new Date (). getTime () <now + ms); } var myLCD = new LCD.Jhd1313m1 (0); var analogPin0 = new mraa.Aio (0); var analogPin1 = new mraa.Aio (1); var LED = new mraa.Pwm (3); var lcdMessage = ""; var mySocket = null; var overrideLED = false; function setup () {LED.enable (true); myLCD.setColor (255, 128, 0); } function sendData (data) {if (mySocket! = null && mySocket.writable == true) {mySocket.write (data); } {function} (function) {{setInterval (function {{var analogValue = (); var value = analogValue / 1023.0; if (overrideLED) {LED.write (0);} else {LED.write (value) ;} var light = (); var lightValue = (light / 1023.0) * 255; myLCD.setColor (lightValue, 0, 255 - lightValue); var lastLcdMessage = lcdMessage; lcdMessage = "Brightness:" + (value. toFixed (2) * 100) + "%"; if (lcdMessage! = lastLcdMessage) {myLCD.lear ();} myLCD.setCursor (0,0); myLCD.write (lcdMessage); sendData ("B" + (value. toFixed (2) * 100) + "\ r \ n"); sendData ("L" + lightValue + "\ r \ n");}, 100); } var server = net.createServer (function (socket) {console.log ("Connection from" + socket.remoteAddress); socket.on ('end', function () {console.log ('client disconnected');} ); socket.on ('data', function (data) {console.log (data.toString ()); if (data == "LED0") {overrideLED = false;} else if (data == "LED1" {{overrideLED = true;}}); socket.on ('close', function (err) {console.log ("Client Disconnected"); if (err == true) {console.log ("With error" ;})); socket.on ('error', function (err) {console.log ("we had error (" + err + "));}); mySocket = socket;}); server.listen (7000); setup (); main ();

Note that some changes were made to the original code.
For the loop function, we changed the name to main and switched to the 1 method (as in the first blink program) that uses the timer. This change is done so as not to stick the program to this function and allow the rest of the program components
Who treat the media to work properly.
Next we created a server by net.createServer that handles the various communication events that include incoming, given, etc. information.
When a client connects to the board, we update the mySocket variable through which we can communicate with the client.

Our client software is written in C # and can be downloaded here.


Type in the address of the clipboard in the box and the port. After clicking connect, a connection will be made to the board and it will start streaming information to the application.
In the left watch we will see the value read from the potentiometer which affects the intensity of the LED and in the right watch we will see the value called by the light sensor which affects the backlight of the monitor.
Pressing the Turn LED Off button will override the potentiometer value and set its value to 0 so that it will show off. Pressing the button again illuminates the LED according to the intensity of the potentiometer.

In this tutorial we learned how to use XDK and write programs in Javascript and run them on the Galilean board.
We saw the similarity and difference between a standard Arduino program and even used basic communication to read and control the board.

Did you learn? Have you been impressed? We'd love to hear what you think of the comments!


תגובה אחת

Leave a Reply

Back to top button