Saturday, June 15, 2019

It’s all about perspective


Stephenopolous interviews trump. Trump comes off exactly as he is.  See if you can guess which headline came from which source:

Stephonopolous filets trump
Trump calls stephonopolous a little wise guy

Isn’t it amusing how his base just doesn’t care what he says or does.  He is some kind of messiah. 

Thursday, June 6, 2019

Raspberry Pi garage door, part 2

In case I forgot to put it in the last post, here is how to setup the camera to take pictures at regular intervals.

Now on to setting up the webserver.  This was more painful than I thought it would be.  But, it finally worked.  I went to /var/www/html and did this:
npm init to use the node package manager (NPM) to setup a new node project. You can press enter to accept the default settings, but when you get to entry point type in webserver.js
Now, do this:
sudo apt-get update

sudo apt-get dist-upgrade

npm install socket.io --save

npm install express --save


npm install onoff
A quick note here - some of these were added as I tested various methods for making this work.  I am not 100% positive that all of them are needed, but it can't hurt to have some extra code installed that's unused.

There are two files you will need:


sudo nano /public/index.html
<!DOCTYPE html>
<html>
<body>
<!--<p><input type="checkbox" id="light"></p> -->
<input type="image" id="light" src="http://yoursever/opener.jpg">
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script> 
<script>
var socket = io(); //load socket.io-client and connect to the host that serves $
window.addEventListener("load", function(){ //when page loads
  var lightbox = document.getElementById("light");
  lightbox.addEventListener("click", function() {
    socket.emit("light", Number(this.checked)); //send button status to server
  });
});
socket.on('light', function (data) { //get button status from client
  document.getElementById("light").checked = data; //change checkbox accordin$
  socket.emit("light", data); //send push button status to back to server
});

</script>

<br><br>

<img src='yourserver/doorpic.jpg'>

</html>
</body>
</html>




And sudo nano webserver.js

var http = require('http').createServer(handler); //require http server
var fs = require('fs'); //require filesystem module
var io = require('socket.io')(http) //require socket.io module and pass the http server
var Gpio = require('onoff/').Gpio; //include onoff 
var sensor = new Gpio(17, 'out'); //use GPIO pin 17, to check reed sensor 
var pushButton = new Gpio(4, 'high'); //use GPIO pin 4 as input, set to 1/high (off)
var mycounter=0;

http.listen(8080); //listen to port 8080


function handler (req, res) { //create server
  fs.readFile(__dirname + '/public/index.html', function(err, data) { 
    if (err) {

      res.writeHead(404, {'Content-Type': 'text/html'}); //display 404 if needed
   return res.end("404 Not Found");
    } 
    res.writeHead(200, {'Content-Type': 'text/html'}); //write HTML
    res.write(data); //write data from index.html
       if (sensor.readSync()==0) { res.write ("<h2>Garage sensor sez: Open!</h2>")}
       if (sensor.readSync()==1) { res.write ("<h2>Garage sensor sez: Closed.</h2>")}

    return res.end();
  });
}
}

io.sockets.on('connection', function (socket) {// WebSocket Connection
socket.on('light', function(data) { //get light switch status from client
console.log("Engage! " + mycounter);

pushButton.writeSync(1);
pushButton.writeSync(0);
// Turn off after 3 seconds
setTimeout(_ => {
pushButton.writeSync(1);
}, 3000);
mycounter++;


});
});

process.on('SIGINT', function () { //on ctrl+c
  sensor.writeSync(0); // Turn LED off
  sensor.unexport(); // Unexport LED GPIO to free resources
  pushButton.unexport(); // Unexport Button GPIO to free resources
  process.exit(); //exit completely
});

Doorpic is the picture the camera takes
Opener is a picture of the opener that I want to display in the app.
--put both in the folder noted above

Run node webserver.js and you are ready to go!

Now all you have to do is point to the IP address of your Pi and with the port 8080 (ie, http://192.168.0.123:8080) and you should be good to go!


Thursday, May 30, 2019

Raspberry Pi Garage Door opener

I wanted to repurpose my raspberry pi to be a garage door opener.  I tried several different methods to make this work, but wasn’t happy with how it was coming out, so I came up with more-or-less my own easy custom solution that I wanted to share.

Of course, you have to start by setting up Raspian on the Pi.

Then, enable VNC, SSH, and the camera in the settings.

VNC makes it a heck of a lot easier to log in once you've put the pi in the garage.

There were three things I needed to add to my Pi:
A camera
A relay switch
and a reed switch


Next, there are 4 pieces of software to install:

1. Apache (will allow you to create a web page to control the pi from a phone)
https://www.raspberrypi.org/documentation/remote-access/web-server/apache.md

2. Webcam (so you can look and see what state the garage door is in, as a visual check)
https://www.raspberrypi.org/documentation/usage/webcams/

3.Node.js (allows for the algorithms to be written in javascript, my language of choice)
https://www.instructables.com/id/Install-Nodejs-and-Npm-on-Raspberry-Pi/
This one is a little tricky!  You have to follow the directions to get the exact right copy of the file for your Pi.  The full listing of available zip files is here https://nodejs.org/dist/v10.16.0/

Note: You may also have to install sudo apt-get install npm and sudo npm install

4. OnOff (this is the javascript tool that allows you to check the GPIO pin conditions)
https://www.npmjs.com/package/onoff


And then comes the wiring.  Your Pi has 40 GPIO pins to control external devices.


In short, you are going to use Java to look at the pins and either turn them on and off, or to evaluate whether the circuit is complete.

First the relay.  I pinned it to GPIO4, Ground, and a 5V

Pi Pin .  Relay input
2            VCC
 6            GND
7             IN2

(Note: in a previous version of this post, I noted the ground as pin 4; the picture was correct. I made a typo)






Then, I created a javascript that looks like this:
'use strict';

const Gpio = require('../onoff').Gpio; // Gpio class
const led = new Gpio(4, 'out');       // Export GPIO4 as an output

led.writeSync(1);
led.writeSync(0);

// Turn off after 3 seconds
setTimeout(_ => {
led.writeSync(1);
  led.unexport();    // Unexport GPIO and free resources
}, 3000);

Save it, and call it whatever you want - by running node yourfilename.js you will cause the relay to turn on and then off again 3 seconds later.

On the relay end, you want to attach two wires, which will open a circuit when the relay comes on.  The other ends of the wire attach to the opener itself, in my case to the left most screws.  When the circuit opens, the connection is shorted and the door opens or closes.  




Cool right?


For the reed switch, I pinned it to GPIO17 and a 3.3v



Note: I connected the wires to longer wires so I could run it across my garage.  Doesn't matter which is which on this end.


Then, I created a javascript that looks like this:
'use strict';

const Gpio = require('../onoff').Gpio; // Gpio class
const led = new Gpio(17, 'out');       // Export GPIO17 as an output

console.log (led.readSync());
if (led.readSync()==0) { console.log ("open")};
if (led.readSync()==1) { console.log ("closed")};

Save it, and call it whatever you want - by running node yourfilename2.js you will to see if the reed switch is closed or not - closed would indicate the door is closed.

And that is 99% of the work.

Next up is the html so that it will look like you want it to, and allow for "remote control"


Sunday, March 31, 2019

Classic kid reaction at the science center

Last year, we visited the franklin institute in Philadelphia. Fun and interactive place.

We watch a demo by a museum worker about liquid nitrogen. The kids were enthused and the persons patter was good.

One young boy (maybe about 10) was more thrilled than the rest. He was excited by it and kept shouting things back when she'd talk.

Toward the end she told everyone she had something we could all take home. The kid yells out "she's giving us liquid nitrogen!" And I can hear him telling his friends that he was planning to have fun at home.

The show ends and she tells us that we are going to take home nitrogen - and before she can finish he gives her a "see I told you!"

...and then she tells us to take a deep breath. We now have nitrogen in our lungs that we take home.

The kid stands up, points, and, at the top of his lungs, yells "she lied!"

Silence for a moment and then laughter among the adults.

I talked with the experimenter and had a good laugh. She has a good story to tell!