Community
cancel
Showing results for 
Search instead for 
Did you mean: 
MJack3
Novice
1,377 Views

C++ Creating Release Version

Jump to solution

I am very new to developing using Eclipse and C++. I have noticed that every time I close my laptop or leave Eclipse that the Edison stops running code. Is there a way of creating a release version so that it can run independently from my computer?

1 Solution
Pedro_M_Intel
Employee
100 Views

Hello ]mattjackson114,

Sure there is a way to permanently store your scripts on your Edison's storage, nevertheless it has the disadvantage that you cannot set it up using Eclipse but instead you have to compile your code directly from the Edison's internal Linux.

I'm talking of course of creating a system service. I will give you a brief example on how to do it:

1. First, you need your code. Create the file where it'll be store with "vi blink.c". Now, this is what I'm going to use:

# include

# include

# include

# include "mraa/gpio.h"

int main(int argc, char** argv)

{

mraa_platform_t platform = mraa_get_platform_type();

mraa_gpio_context gpio, gpio_in = NULL;

char* board_name = mraa_get_platform_name();

int ledstate = 0;

gpio = mraa_gpio_init(13);

mraa_gpio_dir(gpio, MRAA_GPIO_OUT);

for (;;)

{

if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0)

{

return 0;

}

ledstate = !ledstate;

mraa_gpio_write(gpio, !ledstate);

sleep(1);

}

return 0;

}

I will leave you the man page of vi so you can learn how to use it http://linux.die.net/man/1/vi vi(1): Vi IMproved, programmers text editor - Linux man page.

2. Now we have to compile it and create an executable file that your Edison can run:

gcc blink.c -o blink -lmraa

3. Once you've done it, you can test it with "./blink". Once you are sure the code is working as expected, then we have to create our service's file.

4. The service's file must be stored in /lib/systemd/system/ and it'll look like this:

# !/bin/sh

[Unit]

Description=Blinking LED

[Service]

ExecStart=/home/root/blink

Type=idle

[Install]

WantedBy=basic.target

The service file name must include the ".service" extension. Also, note that on "ExecStart=" I used "/home/root", note that this is the location where I stored my blink script, you should change it to wherever you stored yours. I called my service "blink.service".

5. Now we will enable the service with the command "systemctl enable blink.service". Now your board should start the script on every reboot. If you see that this is not happening, you can check the status of the service with "systemctl status blink.service" and if it fails it's probably some configuration that was incorrectly set.

Let us know how it goes.

Peter.

View solution in original post

3 Replies
Pedro_M_Intel
Employee
101 Views

Hello ]mattjackson114,

Sure there is a way to permanently store your scripts on your Edison's storage, nevertheless it has the disadvantage that you cannot set it up using Eclipse but instead you have to compile your code directly from the Edison's internal Linux.

I'm talking of course of creating a system service. I will give you a brief example on how to do it:

1. First, you need your code. Create the file where it'll be store with "vi blink.c". Now, this is what I'm going to use:

# include

# include

# include

# include "mraa/gpio.h"

int main(int argc, char** argv)

{

mraa_platform_t platform = mraa_get_platform_type();

mraa_gpio_context gpio, gpio_in = NULL;

char* board_name = mraa_get_platform_name();

int ledstate = 0;

gpio = mraa_gpio_init(13);

mraa_gpio_dir(gpio, MRAA_GPIO_OUT);

for (;;)

{

if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0)

{

return 0;

}

ledstate = !ledstate;

mraa_gpio_write(gpio, !ledstate);

sleep(1);

}

return 0;

}

I will leave you the man page of vi so you can learn how to use it http://linux.die.net/man/1/vi vi(1): Vi IMproved, programmers text editor - Linux man page.

2. Now we have to compile it and create an executable file that your Edison can run:

gcc blink.c -o blink -lmraa

3. Once you've done it, you can test it with "./blink". Once you are sure the code is working as expected, then we have to create our service's file.

4. The service's file must be stored in /lib/systemd/system/ and it'll look like this:

# !/bin/sh

[Unit]

Description=Blinking LED

[Service]

ExecStart=/home/root/blink

Type=idle

[Install]

WantedBy=basic.target

The service file name must include the ".service" extension. Also, note that on "ExecStart=" I used "/home/root", note that this is the location where I stored my blink script, you should change it to wherever you stored yours. I called my service "blink.service".

5. Now we will enable the service with the command "systemctl enable blink.service". Now your board should start the script on every reboot. If you see that this is not happening, you can check the status of the service with "systemctl status blink.service" and if it fails it's probably some configuration that was incorrectly set.

Let us know how it goes.

Peter.

View solution in original post

MJack3
Novice
100 Views

Hi Peter,

Thanks for your quick response! I am having an issue with the second step, it tells me that there is no such file or directory. Also, when should I run this command? Right now the file is in /tmp and is running from eclipse, should I have done something different?

This is what I typed in...

root@edison:/tmp# gcc PWM.cpp -o PWM -lmraa

gcc: error: PWM.cpp: No such file or directory

Pedro_M_Intel
Employee
100 Views

First, I wouldn't use /tmp to store my scripts on Edison, since the same issue as in Eclipse would happen, they would be automatically erased on the next boot.

That being said, there are some other details I must point out, the first and the most important is that you need to install mraa on your board. Follow these steps to do so:

1. Set up AlexT's repo:

Type the commands:

echo src/gz all http://repo.opkg.net/edison/repo/all http://repo.opkg.net/edison/repo/all > /etc/opkg/base-feeds.conf

echo src/gz edison http://repo.opkg.net/edison/repo/edison http://repo.opkg.net/edison/repo/edison >> /etc/opkg/base-feeds.conf

echo src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32 http://repo.opkg.net/edison/repo/core2-32 >> /etc/opkg/base-feeds.conf

Update the repo:

opkg update

2. Install mraa:

opkg install mraa

Once you have installed the mraa library, you should be able to compile code on Linux.

Now, I also noticed that you are trying to compile a .cpp file, when you use C++ a different compiler is needed, but fortunately it can also be downloaded from AlexT's repo with the command:

opkg install g++

Once those two packages are installed you should be ready to compile C++ files. And you can do it with a very similar command:

g++ script.cpp -o script -lmraa

Peter.

Reply