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


Spring 2019 Kentico User Group
Apr 17, 2019
Location: Waukesha County Technical College - Pewaukee Campus - 800 Main Street, Pewaukee, Wisconsin 53072 - Building: Q, Room: Q361
Blog Article
Using Hooks to Reactify a Plain JavaScript Library: A Walkthrough
Andrew PetersenAndrew Petersen  |  
Aug 06, 2019
React Hooks make it really easy to wrap a vanilla JavaScript library with a React component so you can easily reuse it throughout your app and stay in "React Mode".In this walkthrough I'll be focusing on a single library, Shave.js, but the techniques and ideas should be applicable...
Blog Article
How to Create a Readable and Useful Bug Report
Blayne RoselleBlayne Roselle  |  
Jun 25, 2019
Creating a bug that is both readable and provides enough detail is a must-have skill for a Quality Assurance Analyst. Not only will it help when it comes time to retest, but it also provides credibility with your development team. In the content below, I will share the best practices for creating...
Blog Article
Azure Tips & Tricks: Application Insights Snapshot Debugger
Todd TaylorTodd Taylor  |  
May 21, 2019
A painful memory that is burned into my developer-brain is a production support issue for a .NET web API that I wrote years ago. I worked for a large retailer at the time, and the bug was preventing electronic pricing signs from displaying the most up-to-date price for hundreds of products at...
Blog Article
Thinking Outside the Application Development Box with Unity
Jeff WeberJeff Weber  |  
May 14, 2019
Do you or your company have an idea for an application that sits a little outside your comfort zone? Does your idea possibly require game-like graphics, Augmented Reality, Virtual Reality or similar technology? If so, Unity might be worth a look.   I’ve been using Unity in my spare...