Monday, December 18, 2017

AIY Google Assistant Kit Start on Boot

I picked up the AIY Google Assistant kit from MicroCenter the other day. Man it is a cool project. All the instructions you need to get up and rolling are in the box. The MicroCenter deal came with the Raspberry Pi 3 included and only cost 35$, essentially you get the kit for free.

After the build I wanted a way to start the Google Voice Python script on startup. The AIY project site has great instructions that I will summarize here.


  1. Create a new file in /home/pi called voice_assistant.service using the command: sudo nano /home/pi/voice_assistant.service
  2. Enter the following text into the file

    Description=My awesome assistant app
    [Service]
    ExecStart=/bin/bash -c '/home/pi/AIY-voice-kit-python/env/bin/python3 -u src/assistant_library_demo.py'
    WorkingDirectory=/home/pi/AIY-voice-kit-python
    Restart=always
    User=pi

    [Install]
    WantedBy=multi-user.target

  3. Move the file to the correct folder using this command: sudo mv voice_assistant.service /lib/systemd/system
  4. Enable your service: sudo systemctl enable voice_assistant.service
  5. Start your service: sudo service voice_assistant start
  6. Reboot the pi: reboot
  7. After reboot check the status of the service: sudo service voice_assistant status
  8. The status should be started and you should be able to say "Okay Google"



Friday, December 1, 2017

ESP8266 Servo Control from Website

I wanted a way to host a web site on the ESP8266 and control a Servo from that website. I had to search around for a bit and piece together a few bits of code, but it was pretty simple once I found the right combo.

The code is pretty straight forward. Enter your SSIS and your password. Set the int servoPin = to whatever pin you connected the servo data line to. Update the webPage line to style the web page like you want. I am going to mess around with some mobile frameworks to see if I can get it to build a good mobile looking site for me.

You can see where we are moving the servo if you find the Servo1.write() lines. You can tweak the angles to meet your needs. If you want to add a button just copy and paste the section that starts with server.on

Paste your new section above the line that says server.begin(). You will also need to add the button in your webpage+= line to add the new button: <a href=\"socket1On\"><button>UP</button> Make sure the ahref matches whatever you call the new button in the server on section.

Upload the script to the ESP and you should be rockin and rollin.

If you open the Serial Monitor you will see the IP address assigned to the ESP.


The main reason I wanted this servo functionality is because I saw the Paper Signals Google project. The voice thing is cool, but I just wanted a simple way to do the Arrow up and down. So I am going to print out the paper molds and try to assemble it this weekend.

The goal being I can have a web enabled Up Down arrow. When someone walks by my cube and gives me some lame idea I will click the down arrow. If they say something funny I will click the up arrow. The cool part is if this works well I can attach whatever symbol I want to it and move it around with the web enabled servo.

UPDATE:

Remote Boss Thumbs UP/DOWN

While planning the Paper Signals build I decided to make a remote Thumbs UP/DOWN for my boss. Now from his desk he can give me a Thumbs up or Thumbs down by hitting a web site from anywhere in the office.







I also baked in the Bootstrap Mobile framework into the web site hosted on the ESP, however Blogger was having a hard time displaying the code since it had several <script> tags. I have included the .INO file below that you can load up in Arduino IDE.


Tuesday, November 28, 2017

Raspberry Pi Relay Control from Android and Internet

This turned out to be drop dead simple. Once you have your outlets wired up to your relays and everything working good this is so easy. See my other post on how to setup the relays and such here.

I wanted to create my own, low dollar, solution for Home Automation and such. Jason Findlay does all of the software heavy lifting for you. Assuming you have a Relay hooked up to your Raspberry Pi, follow along.

  1. Install Wiring PI
    1. git clone git://git.drogon.net/wiringPi
    2. cd into the directory: cd wiringPi
    3. Run this: sudo git pull original
    4. then run: ./build
  2. Install Apache and PHP
    1. Run this command: sudo apt-get install apache2 -y
    2. Run this command: sudo apt-get install php7.0 libapache2-mod-php7.0 -y
  3. Create the PHP script
    1. Run this command: sudo nano /var/www/html/gpio.php
    2. Paste this code
    3. Save the page. You should now have a gpio.php file. This will drive the calls from the Android app.
  4. Download and Configure Android APP
    1. Search the Play Store for PiRelay and download the app: https://play.google.com/store/apps/details?id=com.jasonfindlay.pirelaypro
    2. Fire up the app and click on the Hamburger Icon in the top left to show the config sections.
    3. Click on the Pi URL Addresses option
    4. Enter the address to your Pi Example: http://192.168.1.200 (or whatever the IP on your Pi is. Run an ifconfig to see the IP)
    5. Click the Hamburger Icon in the top right corner again and Click Number of Relays
    6. Enter the number of Relays you will configure
    7. Click Hamburger Icon in the top right corner again and Click Setup Relays
    8. Click Relay 1
      1. Enter a name for the relay
      2. Choose the WiringPI Pin #. Use this as a guide
      3. Select a Relay Icon
      4. Choose the appropriate Relay type. The Relays I used were Active Low. Just play around with it until it works
      5. Then click the back arrow.
      6. You should now be able to toggle the relay and turn whatever you have plugged in on and off.

This may seem like a lot of instructions, but honestly it was very easy to setup and get rolling. If you have troubles head over to https://pirelay.jasonfindlay.com for help. You can also use the gpio.php page you built above to control the relays without using the Android app.

http://{your pi IP address}.gpio.php/?pin=1&status=0
http://{your pi IP address}.gpio.php/?pin=1&status=1

Use the Pin look up image I pasted above to specify the correct Pin number, this is the WiringPi pin, not the GPIO or Raspberry PI pin numbers.










Monday, October 16, 2017

Post to Web Service using ESP8266

I want to park this here before I lose the code. Simple HTTP POST to a web service using ESP8266.


This just logs some data to a SQL Server table for me. I am cooking up some push button stuff to act as a data logger using WIFI.

Simple Insert Procedure

Then I created a table to store the data.
And Finally the web service code is as follows:

Monday, September 18, 2017

Arduino Presence using PIR Motion

I cooked up a low tech presence project using an Arduino and PIR motion sensor in my cube at work. That way my colleagues can see if I am at my cube before walking down the hall. There are other more sophisticated ways to detect human presence with different sensors but this cheap PIR method works pretty well.

I  used a delay in the code to make it more usable. The out of the box PIR code will just constantly check for motion on the PIR. I inserted a delay so if it sees motion, wait 1 minute and then start checking again. Every once in a while I am real still and the sensor does not pick up but again we are talking low-tech easy solution here.

Materials

  • Arduino UNO
  • 2 - LEDs
  • PIR Motion Sensor
  • Jumper Cables


Code



Pics





Buddy said I should add a mood feature to display the mood. I have an RGB LED laying around so I may implement that next.

Thursday, August 10, 2017

Python Windows Toast Notifications

Started working with Microsoft Life Cycle Services recently deploying some cloud hosted environments. Wanted a way to get notified when an environment was finished deploying. I am sure there are better ways, but I wanted something quick and easy.

Double bonus is that you can use this for other Python projects where you want a Windows Toaster notification. Windows Toaster notifications are those little pop ups that appear in the bottom right corner of your screen.



I fired up Fiddler hoping to find that LCS would give me some sweet JSON action. At first I was a little bummed because the return application type showed javascript instead of JSON



When I clicked JSON view the response looked like JSON, but when I tried to load it into Python as JSON I kept receiving the "is not JSON serializable error".



After some searching I stumbled on some stack overflow articles mentioning this was JSONP not normal JSON. I finally landed at this article that gave me a really simple solution.  Here is my final script to generate the Toast Messages for the LCS deployment status.


You can see where I convert the JSONP to JSON by just removing the first and last character (Line 16) of the response. It is really that simple, just remove the ( and the ) and we now have valid JSON we can feed in.

We loop every 5 minutes checking status and pop up a toast notification giving the status. Here is a screen shot showing the result (I stopped all my environments for some maintenance thus all are Stopped status).

One thing to keep in mind is you are limited on height of the toast message. So your message will be cut off if your message cannot fit in the toast notification window size.




Friday, August 4, 2017

Raspberry Pi Sound Board

I have wanted to create a sound board for some time. I finally got around to getting it done. This project took way longer than I wanted mostly because I did not make time to work on it. I created a really simple wood structure to mount the arcade buttons to.

I wanted a solderless solution so I ordered some female spade connectors so I could just crimp the wires instead of soldering. Turned out pretty good.

Supplies

  • Raspberry Pi 3
  • Female Spade Connectors 2.8mm width. Make sure you get the size that matches your arcade buttons.
    https://www.amazon.com/gp/product/B01962MW2G/ref=oh_aui_detailpage_o03_s00?ie=UTF8&psc=1
  • 10 x 30mm arcade buttons
    https://www.amazon.com/EG-Starts-Buttons-Multicade-Accessories/dp/B01N5DVINY/ref=pd_sbs_21_4?_encoding=UTF8&pd_rd_i=B01N5DVINY&pd_rd_r=4AG8PN4FQKRH00AGR4XN&pd_rd_w=1gW6J&pd_rd_wg=Iv7vh&psc=1&refRID=4AG8PN4FQKRH00AGR4XN
  • Jumper Wires
  • Bore Bit to cut your holes for arcade buttons

My first mistake was buying a bore bit that was too small. My buttons were 30mm and my bore bit was 28mm. Not sure where I got 28mm from but the holes were too small. I was able to use a Dremel to make the holes bigger but it sure wasn't as pretty as clean 30mm holes. 

I used common ground lines for the buttons so I had 4 buttons one one and 5 buttons on the other. I used a wire nut to ground them and attached them to two separate ground ports on the Raspberry Pi. 

Code 

I spent way too long trying to figure out the Python code to detect a button push. Finally found the right combo. Here is my code.


So the jist is to wait for a button to be pushed then call the Command line version of VLC to play the MP3 specified. Not that elegant but works great. You will see here that this version of the sound board is my Dr. Phil quotes soundboard. Good times. You can SCP to copy the files you need to the Pi or use a USB stick as pictured in my pics below. I didn't really plan my project well so I had to run the jumper cables out to the side of box so I could plug stuff into the pi. It works fine and gives the project that hacker feel.

This of course can be used for anything really. You can launch python scripts to do whatever you want. 





Friday, March 10, 2017

Arduino LED Sign

Finally finished version 1 of my Arduino LED sign. Using the awesome tutorial and code from this fella: https://wp.josh.com/2016/05/20/huge-scrolling-arduino-led-sign/. I went with 60 LED 1 meter long strips. Man they are bright!!!

Here are some pics of the build.





Parts List

Everything is driven by the awesome Arduino Software that Josh from the link above built. Once you get everything plugged in and ready to go it just works.




Wednesday, February 15, 2017

Google Traffic API using Python or PowerShell

UPDATE: 2/16/2017 - small tweak needed to the scripts. I changed the JSON element to duration_in_traffic. This will show the duration plus traffic. Duration will just show how long it should take with no traffic.

I have visited this area a few times. I needed a clean solution to get drive times for my commute home. Fortunately Google Maps Distance Matrix has an easy to use API. Of course you will need to enable the API in your Google Dev Console and get your API key to use the traffic feature.

Once your have your key its a cinch. 
  1. Grab your origination and destination LAT, LONG coordinates. Just go to google maps, find your location, right click on the map and select What's Here from the menu. You will see your coordinates.

  2. Plug these into the script 
  3. You can tweak the model if you want. Distance Matrix has 3 traffic models, best-guess, pesimistic, and optimistic.
  4. Run the script

Powershell


Python

In order to get the Python Script to work we have to bypass the SSL verification. Probably not the best idea for production type implementations but it is a workaround.


You can find more information about the Distance Matrix API here: https://developers.google.com/maps/documentation/distance-matrix/

Thursday, January 5, 2017

Simple SQL Server ASP.NET Web Service Part 2: Deploying to IIS

In Part 1 we walked through creating a Simple SQL Server ASP .NET Web Service. Now let's work on deploying it to IIS.

As a another reminder, I am not a .NET developer, I am a database guy. Talk to some of your developer friends if you want some advanced help on the right way to do this stuff. I am just showing a very simple web service to call a Stored Procedure.

Assuming you have your web service project open in Visual studio...
  • Click on the Build Menu and click Build Solution.
  • Assuming you have no errors then click the Build Menu again and this time click Publish Web App
  • You should see something like this (reminder I am using Visual Studio 2015)
  • Click Custom and name it Local
  • Enter your server name and the name of the web site you want to deploy to. You can give your service a name here. Small Side Conversation here: Probably a good idea to name the service whatever you called it at creation time. I of course learned this the hard way when I went to deploy it and gave it a new name in flight. So now I have a project called Website in Visual Studio and a Service named totally different on my IIS server. This is all well and go if I have one service but as you grow it will be difficult to keep track of what is what.
  • Make sure you validate connection and its green and click Next
  • Click Next on the Setting Screen
  • Click Publish on the Preview Screen.
  • If all goes well your app should be deployed...if not google it. Check your output screen for details
  • If the publish was successful, Visual Studio will try to open a browse with your web service. I received a HTTP 500 error during this preview.
  • After some googling I found the work around. Right click on your web site in solution explorer and click Property Pages
  • Click the build option on the left and select .NET 4.5 Framework as Target Framework. Click Yes to the warning message.
  • Build your Web Site again and then Publish the Web App
  • This time I received a 404.13 Forbidden document. 
  • I added Service.asmx to the end of the URL and it worked. 
  • So I then added Service.asmx under Default Document for the web site. Just click on your web site in IIS and then find Default Document. Click Add and then add Service.asmx
  • Reload your web service and it should work now.
  • Now fire up your web service Using the invoke button we did earlier.
  • Ughhh
  • If you get this the fix is simple. You want to setup a local user on your server that has access to EXECUTE a stored proc on your SQL Server. 
  • You then want to create a new Application Pool in IIS. This will be used to call web services that need access to SQL.
  • Click Application Pools and on the Right side of the screen you should see a link for Add Application Pool
  • Give the Pool a name and click OK
  • Now right click on the new pool and click Advanced Settings
  • JUST FOR THIS EXAMPLE I created an Application Pool and then set the identity to my local administrator. This is a bad bad bad idea if you plan to go showtime with this web service. Click the Ellipses and use the account you created that has access to the database.
  • You are doing this because in your C# web service on your database Connection we are using Integrated Security, so the App Pool Identity must have the ability to connect and execute the stored procedure.
  • Now Highlight your service web site and on the Right side you should see a basic settings link. Click that Link
  • Click the Select button by the app pool and choose the app pool you created
  • And hopefully this now works. Whew...crack open a celebratory Mt. Dew.


UPDATE:
In order for this web service to work outside the friendly confides of your internal network you need to add a section to your web.config. In the <system.web> section of your config add the following Web Services section:


Again be careful here when you expose a web service to the world.

Holler if you have questions, remember Google and Stack Exchange are your friends.

Wednesday, January 4, 2017

Simple SQL Server ASP.NET Web Service

I am not a developer. I am a database dude. So don't take this article as your source for the best way to develop a web service. This is just a simple example of how to call a stored procedure from an ASP .NET web service.
  • Start up Visual Studio (I am using 2015)
  • Click New...Web Site
  • Up top you should see a version drop down for .NET Framework. I selected .NET 3.5
  • On the left side under templates choose C# 
  • You should see an option for an ASP .NET Web Service, Select that
  • Give the Web Service a name by the Browse Button and Click OK
  • You should see some sample code and a Hello World Method.
  • Start a new Method by copying this Method.
  • Give the Method a new name. Mine is called ExecuteSweetStoredProc
  • Now we are going to add the SQL Server stuff
  • You will start seeing some sweet red squiggly lines. That means you got problems jack. In my example above we are missing some Reference (add ins to allow  us to connect to SQL and stuff, and also some errors about not all code paths return values. That is just letting you know that yo bro, you run this code and it bombs or something wonky happens its not going to end well.
  • Let's add some references. Up a at the top you should notice some using statements.
  • Add these 3
    • using System.Data;
    • using System.Data.SqlClient;
    • using System.ServiceModel;
  • Now we will add a Try Catch block. This will try a something and then if that something is unsuccessful it will catch the error and display it.
  • So we set the SqlCommand to our stored proc and now we are executing it using the ExecuteNonQuery() function.  We return the success code if its all good
  • If we have a problem then we display the results
  • Click the Build Menu and click Build Solution
  • Check your output Screen
  • If you have errors then start googling, 9 times out of 10 you'll find your answer on stack exchange.
  • Now Click the Play Button in the top menu of Visual Studio
  • Should see something like this
  • Click Hello World Link and then Click Invoke. Should say Hello World in some sweet XML
  • Go Back and Click your Other method (whatever you named it)
  • If all good you should see this. (if not start googling or leave a comment below and I will try to help)
  • So what did it do...well what does your stored procedure do? You will notice on my EXECUTE statement I execute dbo.webServiceBackup. I am running a FULL database backup on one of my databases. Your stored proc can do whatever you want. You can also display results or whatever you want to do.


Full c# Code for Easy Pasting into Visual Studio


I will try to get some more examples of simple web services up on the blog here including reading through results from a stored proc, returning JSON formatted data for easy consumption, etc.