Back to List

Developing on a Raspberry Pi using Gulpjs

Eric Ditter Eric Ditter  |  
Jan 15, 2019
 

Developing on one machine and running on another is a tedious process, but sometimes you need to when a library has different features for ARM vs x64 (and then there are always the Windows vs Linux issues).

This was the issue I had when I was working on a Raspberry Pi project using Python. I got everything working on Windows and then copied it over to the Pi, thinking the libraries would work the same between the two - but they didn't.

After copying and pasting to a share a few times, I realized it was kind of annoying having to move my files to the share, kill the terminal, and re-run the start command. So, I turned to everyone's favorite task runner, Gulp, and it was like greeting an old friend.

In order for this to work with a remote Linux machine, you need to install Putty - which is a common SSH and telnet client for Windows. Once you install that, you get a few programs that will allow you to easily copy files and run remote commands on your connected machine through a command line. There may be more than one way to do this same thing, but this is just how I ended up doing it

Note: If plink and pscp aren't in your PATH, then they are located inside C:\Program Files\PuTTY\

The folder structure of the project was pretty simple:

⊢ gulpfile.js
⊢ src
  ⊢ main.py
  ⊢ lib.py

Then this is where the magic happened.

gulpfile.js

const
  gulp = require("gulp"),
  process = require("child_process");

const
  piName = '192.168.1.5',
  piUsr = 'myUser',
  piPwd = 'securePassword';

const spawnTask = (command, doneCallback) => {
  // run the task through the command line
  const proc = process.spawn("cmd.exe", ['/c', command])
  // log the output and errors to get some insights into what's going on
  proc.stdout.on('data', (output) => console.log(output.toString()));
  proc.stderr.on('data', (output) => console.log(output.toString()));
  // when the command line is done let the calling gulp taks know
  proc.once('close', doneCallback);
}

gulp.task("start", (done) => {
  // run the python command on the Pi
  // https://the.earth.li/~sgtatham/putty/0.70/htmldoc/Chapter7.html#plink
  spawnTask(`plink -ssh -l ${piUsr} -pw ${piPwd} "sudo python /home/pi/share/my_app/main.py"`, done);
});

gulp.task("move", (done) => {
  // copy the files from src to the working directory of the pi
  // https://the.earth.li/~sgtatham/putty/0.70/htmldoc/Chapter5.html#pscp
  spawnTask(`pscp -pw ${piPwd} -r ./src/*.* ${piUsr}@${piName}:/home/pi/share/my_app`, done);
});

// gulp 4 syntax - npm install gulp@next
// if you are using gulp 3 check out the npm package run-sequence
gulp.task('default', () => {
  return gulp.watch(['./src/*.py'], gulp.series('move', 'start'))
});

As you can see, it doesn't take much to accomplish this. Child_process was something I had never used before this, but it has come in handy a few times since then. Now when I develop on the Pi, I can just run the default gulp task and everything copies over and runs pretty seamlessly.

JavaScript

 

Love our Blogs?

Sign up to get notified of new Skyline posts.

 


Related Content


Blog Article
CQS, SimpleInjector, and the Power of Decorators
Dan LorenzDan Lorenz  |  
Jan 08, 2019
Over the years of developing with n-tier style, I was wondering if there was anything else out there. The biggest problem with n-tier for me is that the interfaces and classes tend to get super large. When that happens, you start breaking SOLID principals and unit testing becomes much more...
Blog Article
Async, Await, and ConfigureAwait – Oh My!
Dan LorenzDan Lorenz  |  
Dec 11, 2018
In .NET Framework 4.5, async/await keywords were added to the language to make async programming easier to work with. In order to maximize device resources and not block UI, you should really try to use asynchronous programming wherever you can. While async/await has made this much easier than...
Blog Article
How to Add Electronic and Digital Signatures to a Universal Application (UWP) with iText
Paul MadaryPaul Madary  |  
Aug 14, 2018
When paying for gas at the pump, checking out at Walmart, or electronically signing a contract to purchase real estate, have you ever thought about what technically goes into that electronic signature?  If not, then this is your lucky day! I recently had to implement this functionality in a...
Blog Article
Updated Mile of Music App Provides an Even Better User Experience
John PtacekJohn Ptacek  |  
Jul 26, 2018
As we get to the end of July at Skyline Technologies, our organization starts to get excited. We know that the Mile of Music festival is just around the corner. With over 70,000 people coming to Appleton, Wisconsin, for four days of original music, it is quite an adventure. Given one of the main...
Blog Article
WinForms Fluid Design - A Cautionary Tale
Jeff LucasJeff Lucas  |  
May 01, 2018
As a developer, you can find yourself digging into all sorts of code, even the occasional WinForms application.  WinForms come with a lot of handy features that can make fluid design quite simple, allowing your forms to be resized by the user but still hold the basic shape you desire without...