Raspberry Pi GPIO control in electron App

Viewed 223

I have created a simple app that can control the GPIO pins on the Raspberry Pi with a simple button press. However, when I press the button, there is no response from the GPIO pins (here connected to GPIO 24). Also, I have already installed the onoff module from npm install onoff. When I create a simple javascript file with only GPIO control and run node <file_name>.js, it runs successfully. But when I try to integrate into electron, it stops working. Please check the following code. Help would be appreciated.

index.html

<!DOCTYPE html>
<html>
    <body>
        <h1>GPIO Control</h1>
        <button id="Btn" class="button is-primary">LED ON</button>
        <script src="./render.js"></script>
    </body>
</html>

main.js

const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow () {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
        nodeIntegration: true
      }
  })

  mainWindow.loadFile('index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

render.js

var Gpio = require('onoff').Gpio; 
var LED = new Gpio(24, 'out');

const Btn = document.getElementById("Btn");
Btn.onclick = function() {
  myFunction()
};

function myFunction() {
  LED.writeSync(1);
}
0 Answers
Related