DelayMonkey
Posts: 1
Joined: Sun Feb 25, 2018 1:28 pm

How do you control your raspberry pi from a custom domain (url)

Sun Feb 25, 2018 1:38 pm

Hi, after some coding I got my raspberry pi to work. So I can control a servo with 2 buttons on html open and close. But this is on a local server (raspberrpiIP:3000) and I want to control the raspberry pi on a live website. So everybody in the world should be able to control it. After much research I couldn't find the right answers to control my raspberry pi with the script to a live website. This is the script for controlling the servo and connecting to the internet.

Code: Select all

  var http = require('http');
  var express = require('express');

  var lockedState = 2200;
  var unlockedState = 500;

  var motorPin = 14;
  var buttonPin = 17;

  var app = express();

  // *** Start code *** //

  var locked = true;

  //Setup servo
  var Gpio = require(
          motor = new Gpio(motorPin, {mode: Gpio.OUTPUT});
  //setup button
  button = new Gpio(buttonPin, {mode: Gpio.INPUT,
          pullUpDown: Gpio.PUD_DOWN,
          edge: Gpio.EITHER_EDGE
    }),
    servo = new Gpio(motorPin, {mode: Gpio.OUTPUT});

  button.on('interrupt', function () {
    motor.servoWrite(lockedState);
   setTimeout(function(){motor.servoWrite(0)}, 2000);
  });



  // ------------------------------------------------------------------------
  // configure Express to serve index.html and any other static pages stored
  // in the home directory
  app.use(express.static(__dirname));


  //lock rest get call
  app.get('/uit', function (req, res) {
          motor.servoWrite(lockedState);
          locked = true;
          res.send('uit')
          console.log('uit gezet')
          //After 1.5 seconds, the door lock servo turns off to avoid stall current
   setTimeout(function(){motor.servoWrite(0)}, 2000);
   });

  //unlock rest get call
  app.get('/aan', function (req, res){
          motor.servoWrite(unlockedState);
          locked = false;
          res.send('aan')
          console.log('aan gezet')
          //After 1.5 seconds, the door lock servo turns off to avoid stall current
          setTimeout(function(){motor.servoWrite(0)}, 2000)


   });
  // ------------------------------------------------------------------------
  // Start Express App Server
  //
  app.listen(3000);
  console.log('App Server is listening on port 3000');

with this simple html code

Code: Select all

<!DOCTYPE html>
<html lang="en">
 <head>
        <meta charset="utf-8" />
</script>
   <title>Aan en uit zetten van de servo</title>
   <script src="http://code.jquery.com/jquery-latest.js"></script>
 <script type="text/javascript" src="script.js"></script>
 </head>
 <body>
   <H1>Aan en uit zetten van de servo</H1>
<button onclick="window.location.href='/aan'">Aan</button><br>
<button onclick="window.location.href='/uit'">Uit</button><br>
<img src="https://media1.tenor.com/images/bbf211a950b564ab385e880ab436eff9/teno$
  <div id="input"></div>
<style>
body{
color: white;
background-color: #00cc00;}
 </body>
</html>
Thanks for your help

Heater
Posts: 13389
Joined: Tue Jul 17, 2012 3:02 pm

Re: How do you control your raspberry pi from a custom domain (url)

Sun Feb 25, 2018 2:38 pm

First of all you will need a domain name. You can buy yourself a domain name very cheaply from people like https://godaddy.com/

What you do next depends on if your ISP gives you a public IP address. Some do.

If so, if you don't already have one get a router. Connect to you ISP with that router. Have your Pi connect to the router. Then configure your router to do do port forwarding. If you forward port 80 to your Pi that will now allow public access to your Pi on that public IP address.

Configure your godaddy domain name to point to your public IP.

That public IP address is likely to change so you need to be ready to change it at godaddy.

Or, if you don't need a domain name of your choice use the servers of http://freedns.afraid.org/ They will supply a domain name you can use and it is very easy to have a script update the IP address if it ever changes. There are other such dynamic domain name servers (DNS) out there.

If you have no possibility of a public IP address then you need help from a server in the cloud.

Dataplicity (dataplicity.com) allow your Pi to connect to its service via an encrypted tunnel that gets you a command line shell in your browser. Dataplicity also allow you to set up a tunnel from your PI for HTTP so that your web server is now visible on a dataplicity URL. It's all very easy to use.

There are other services out there that can do this kind of thing. resin.io for example.

Or, you could get yourself a server instance on Google Cloud, Amazon, Digital Ocean etc and run your web server there. Of course that means you now need to arrange for your Pi at home to connect to that web server in the cloud so as to get commands. I do this using node.js.

Basically it would mean splitting the code you have there into two node.js programs. The HTML server part would run in the cloud server instance. Where it has a public IP address. The GPIO control part would run on your Pi at home. I use websockets to do this kind of thing. Use use the socket.io node module to make that even easier.
Memory in C++ is a leaky abstraction .

User avatar
rados-dataplicity
Posts: 40
Joined: Wed Aug 03, 2016 11:56 am
Location: UK
Contact: Website Facebook Twitter

Re: How do you control your raspberry pi from a custom domain (url)

Mon Feb 26, 2018 10:50 am

Thank you @Heater for recommending our service. It's very much appreciated.

The only thing I would like to add on our side of the above reply is our getting started guide for controlling GPIOs:

https://blog.dataplicity.com/control-ra ... f46daf2971

We're using websockets in the example above (which is a bit of an overkill :D ) but there are also other ways:

https://docs.dataplicity.com/docs/contr ... g-rest-api

That is ofcourse if using Node JS is not a strict requirement for you.
I tinker around with Raspberry Pis daily and make sure that people can easily access theirs remotely.

Community outreach @ dataplicity.com

Return to “General discussion”