Monday, July 30, 2018

ESP8266 Post to Web Service

Purchased a DHT22 recently for a Work Desk Temperature project. Wrote a web service and created some SQL stuff to capture the temperature and humidity at my desk. Here we go.

Web Service

Just a real simple web service to call the stored procedure I wrote:

See my other blog post for more details on creating a web service: http://tech.thejoestory.com/2017/01/simple-sql-server-aspnet-web-service.html

SQL stuff

Created a Table and a couple stored procedures to support this. Here is the table.
Here are the stored procs.


ESP8266 Post

From there I just needed to figure out the ESP8266 Post. I had to search around a bit but finally landed on the script below. The script will establish a WiFi connection. Then it will loop every one minute taking a temperature/humidity reading from the DHT22. Finally it will call the web service I built to store the data in SQL server.

You will need to grab the DHT and AdaFruit Libraries using your Arduino IDE. From there I created a web site using Chart.js and a couple other stored procedures I wrote to get MAX/MIN/AVG Temp and Humidity.


Thursday, July 5, 2018

Find Text String in Text Files Powershell

Quick script to search a set of text files for a specific text string.



Neat

Wednesday, June 6, 2018

Raspberry Pi Speed Camera Python

I setup a pedestrian speed camera in the office using a great project by pageauc on github: https://github.com/pageauc/speed-camera. This thing was so dead simple to setup and run, and after some tweaks on the config I was clocking fast walkers in my office like a champion.


  1. I started with a fresh Raspberry Pi 3 Raspbian build
  2. Installed OpenCV3 using this link: https://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/. This takes a while
  3. Download the speed camera project from github: https://github.com/pageauc/speed-camera
  4. Extract it and copy it over to your Raspberry Pi. I just parked it under /home/pi/speed-camera
  5. Crack open the config and start messing with stuff

I am using a web cam so I had to change this value in the config right away:



I had to tweak the capture area for my project and did so by playing around with these values

Turn on the gui window so you can see what you are doing


Here is a shot of me walking by the camera. The software will automatically save the image and add the speed calculated to the image which is gangsta.

My camera is pretty potato quality. Another setting I had to tweak often was the track_len_trig. This determines how many pixel changes are required to save an image. Since my target is down the hall I had to reduce this down to 5. If you are tracking a larger area you will want to play with this since the default is 60. I get some false readings but it captures what I need.

Here is a picture of the system in action on the Pi. 
If you have any troubles review the guide on github. It is pretty handy.

Monday, April 2, 2018

AnswerBot 3000 Version 2

Way back I made an AnswerBot using Teensy. I mean not a real one. Basically its a button that you push, it will pick a random saying and send the keystrokes using Teensy Keyboard library.

AnswerBot: keep it gucci fam

Always answerbot. Anyway, I wanted to make a better physical representation of AnswerBot so here is Version 2.0.


He of course has a short in one of the LEDs which is perfect. I need to work on my can cutting skills. Anywho AnswerBot is now online and adding fun quotes to my various instant message platforms.

AnswerBot: i failed you

No way AnswerBot you cool.



Sunday, February 25, 2018

Stream Google Play Music using Python and gmusicapi

Parking this example of how to stream a random song from Google Play music using the gmusicapi python library. You will have to find your device id to plug into the script which can be done using the gmusicapi like so.

Then plugin your Google username and password and you are all set. This code example has a zbar example because I was using  a webcam to scan an QR code. The QR code had an artist name like Drake or The Chainsmokers or something. It would then pass the name into the search call and would select the first song from the songs returned from the search.



You can remove the zbar stuff and take in an argument for a search like so.

Thursday, February 22, 2018

Microsoft Face API using Python

After finding the Google Vision API I stumbled on the Microsoft Face API. it is pretty slick and returns more data than the Google Vision API.



Stuff like Approximate age, hair color, gender, make up presence, etc. Interesting. If I can figure out the Quota limits on the API I may use it instead of Google.


Google Vision API using Python

Stumbled on this guy's cool project: http://labonnesoupe.org/2018/02/14/introducing-qrocodile/ and wanted to play around with that idea a bit. Using the Raspberry Pi and the gmusicapi module I was able to develop a proof of concept that would use the Web Cam to read a QR code and stream a song using google play.

Fast Forward a few hours after that proof of concept and an idea hit me. What if I could use a web cam to detect the emotion of someone and then stream a random song from Google Play Music. This post is about that specifically. Taking a picture with a web cam and bouncing it off the Google Vision API. I'd call this a part 1 of Part x for this project but not sure I'll remember to post the other parts..ha.


Setting up the API

This was not as easy as I'd hoped. You have to enable billing on your Google Cloud project. Don't fret though unless you plan to do more than 1000 look ups per month.



  • Open up the Google Cloud Console and turn on billing.
  • Then click on the API and Services option. 
  • Search for Vision API and enable it.
Create a new Service account or use an existing one. I created a Compute Engine Default Service account. Download the JSON file and save it in the folder that you will run your script from. 

You can then complete authentication by referencing the JSON file you created.

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'C:\\Python27\\Projects\\emotion\\cred.json'

Python

Ok if you made it this far awesome. Sorry for the lame instructions above.
You will need some Python stuff to make this happen.

  • pip install pillow
  • pip install google-cloud
  • pip install google-cloud-vision
  • Install CV2 
  • Maybe some other stuff

Script

So basically I just hijacked an example to get this to work quick and dirty. I open the camera up, capture a picture, then feed this picture into the defect_faces function. I added code to spit out the results on a web page, to mark up the image with a green box around the recognized face, and to print out the emotion analysis to the console. The script below will handle a lot of different requests, again I am just using the faces portion.


Call the script like so: python c:\Python27\Projects\emotion\emotion.py faces blah

Notice I didn't even take time to fix the arguments thing. So I just override blah in the sample above by hard coding a path...i know i know...lazy bum.

Update

I found an example of how to use Microsoft Face API using Python. It is pretty straight forward so I pasted it here below. The MS Face API returns some extra data. I need to figure out the quotas on the API calls but it seems to be a cool substitute for the google API. It returns approximate age and some other cool details about the image.

You actually upload your file to the web service and then it spits out he results.

Wednesday, January 10, 2018

Party Button 2018 Edition - Python controlled Relay, Momentary Push Button with Key Lockout

Asked for a launch button for Christmas. Thought it would be a cool thing to add to some projects, a silly way to kick something off. The first project I went for was the Party Button 2018 Edition. I made one back in 2016 with an Amazon Dash Button hack: Party Button 2016. Man can't believe that has been almost 2 years.

Santa brought this launch button from Amazon: Launch Button. Taking it apart to review the wiring I quickly realized I had an issue. The green button has an LED in it, but also has a fuse. This light will not come on unless you provide 220V of power...uh...there is no way I am lighting this cheap chinese plastic box up with 220 volts of power to light up a silly LED.

So I stripped down the button and the guts, removing the fuse, feeding some wires through the switch and up to a cheap LED I had laying around. I wired this to the Raspberry Pi GPIO. Then I hooked up the key to another GPIO pin. So I read the pin on the key and if it has power than i turn on the LED.



I snagged a 3V relay for some other ESP8266 project I was working on and headed over to Home Depot to grab an outlet. I used some 3M tape to adhere the relay to an outlet box and juiced up both outlets to the relay. I'd like to control both outlets separately but I hosed up when I ordered the relay and only ordered a 1 Channel Relay.


With most of the hardware components wired up I started on the software side. After several rounds of testing I came up with the python script that would read to see if the Key was in the on position, if so then light up the LED. If you press the Green button it will then fire up the relay and then pick a random song from the folder. If the key is not in the on position then pushing the green button will do nothing and the LED will not light up.


Key and Green Button Code



So finally I needed a way to quickly kill all this. I decided the Red Mushroom button would be the party kill switch. I turn off the LED, turn off the relay and also kill the VLC and Launch Button python script. This will shut the party down hard. I made this a separate script that will listen on a 3rd GPIO port to see when I push the kill switch button.



Pretty happy with how it turned out. Took about 4 days of tinkering to get it right. Here is a Youtube Video of the button in action:




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.