Raspberry Pi: Attach A Sensor To Raspberry Pi and Receive Mobile Alerts

In this particular scenario, my goal was to attach a motion sensing device to Raspberry Pi Zero W and have it send me alerts.   Now that this basic functionality is achieved, I can modify and reuse it as I wish. 

I used the following: 

Hardware: 
  1. Raspberry Pi Zero W 

  1. PIR motion sensor (EMY 5 X HC-SR501 Adjust Ir Pyroelectric Infrared PIR Motion Sensor Detector) 

Software: 
  1. (Operating system: Debian) 

  1. Programming Language:  Node.js 

  1. Communication medium:  IFTTT.com 

STEP ONE:  ATTACHING THE PIR 
This PIR component has three pins (Power, Output, Ground.)  Using female-to-male cables, I attached as follows: 
PIR PINS 
RASPBERRY PI GPIO PINS 
POWER 
PIN 2 (5v) 
OUTPUT 
PIN 7 
GROUND 
PIN 6 
STEP TWO: 
  1. If you don’t have one already, set up an account at ifttt.com .  Personally, I do not recommend this site for confidential actions but it is fun to play with! 

  1.  Navigate to “My Applets” and click on “New Applet” 

  1. Click the “Plus” image and you should be taken to a page “Choose a service” 

  1. Search for “web”  and choose “Maker Webhooks” 

  1. Select “Receive a web request” as a trigger 

  1. Enter the following tag:  “motion_detected” to create a trigger 

  1. You should be re-directed to a page that reads “If (webhook) then” Plus sign. 

  1. Select the “SMS” service. 

  1. Select “Send me an SMS” 

  1. Enter your tag  “motion_detected” to create an action and then add the phone number for SMS 

  1. You should receive a special “key”.  Save this. 

  1. Review changes and save. 

STEP THREE: INSTALLING SOFTWARE ON PI: 
  1. Create a folder for project and ensure internet is connected. 

  1. Open a terminal window, type the following: 

  1. Test to see if Node.js is installed) 

 Node –v 
  1. If no version is returned or version is below v6, get latest: 

                      curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash – 
  1. Once version is downloaded, then install:   

sudo apt-get install –y nodejs 
  1. Test to see if latest version of Node.js is installed: 

Node –v 
  1. Download this code into your folder from here:  pir_motion_trigger.js 

  1. Open code (pir_motion_trigger.js) in text editor.  You will change the following lines of code to customize for your IFTTT applet: 

     // IFTTT data 
     var key = “enter key here” 
     var eventName = “motion_detected” 
  1. Save code and close 

  1. Once again, open a terminal window and navigate to your folder.   

  1. Type the following: 

sudo node pir_motion_trigger.js 
  1. Wait a minute or two then wave your hand in front.  Soon you should get a text message. 

NOTE:  This particular model of PIR has the ability to adjust sensitivity for motion delay and distance.  You can manipulate by adjusting the two yellow/orange screws or programmatically. 

Raspberry Pi: Remotely Connect To Raspberry Pi From Windows

Remote Desktop Connection:  Connecting one computer to another gaining the ability to take control of the remote computer. 

The Raspberry Pi Zero can be especially difficult to program for Windows users.  The tiny microcomputer runs on a Linux-based operating system.  Hooking up a small computer to an external monitor is not a problem but adding keyboard or mouse can get painful without the proper resources and experienceInstead,  below is what we can do. 

First, an external monitor and keyboard will be needed for the initial setup.  If you haven’t done this,  then there is good documentation here. 

Second, your main PC will need to be on the same network as your Raspberry Pi. On the Pi, you will need to get it’s IP Address.  This can be done by opening the command terminal and typing “ifconfig”  This should return a list of network information.  Locate a line for “wlan*” and in here you will jot down the address following the entry “inet6 addr: ”    It should look something like:   192.168.1.12.  You will use this address to connect.  Note that your Raspberry Pi will most likely want you to enter UserId and Password. 

Third, follow these well-written instructions here to walkthrough the process:   https://www.raspberrypi.org/documentation/remote-access/vnc/ 

If all works as expected, you will be able to see the desktop of your Raspberry Pi and control it from your PC. 


.NET: Using A Single log4net Configuration File Across A Multi-Tier Web Application

log4net is
a tool to help the programmer output log statements to a variety of output
targets.



This
example is written in Visual Studio 2015 for a WebAPI2 solution.  The sample solution consists of three projects
that include a project for data, models, unit tests, etc.  Source code can be found here:



https://github.com/cjsander/Log4netInMultiTierWebApp

SETUP:
1.     
Create
your project and then import the following Nuget package:        
https://www.nuget.org/packages/log4net/
2.     
For
each project, add reference to log4net.dll assembly.  Though all configuration will be in the  main
API project, other projects will require the assembly reference as well.
3.     
Configuration
files to be included are:
a.       Log4net.config
b.      Web.config
c.       Settings.xml
d.      Global.asax.cs
4.     
Note
that the <appsettings> node of the web.config file has been abstracted
into a separate file — Settings.xml.  
These settings will be referenced in the web.config in the following
line of code:

         <appSettings file=c:webappslog4sampleconfigsettings.xml/>

APPLICATION:
1.       In your web.config file, enter the
following:
  <configSections>
    <section name=log4net type=log4net.Config.Log4NetConfigurationSectionHandler,
log4net
/>
  </configSections>
2.       Now, add the logging configuration
values.  These can be added in your
web.config(or app.config) but in this scenario, it has its own configuration
file – log4net.config:
<log4net>
 
<
appender name=RollingFile type=log4net.Appender.RollingFileAppender>…..
</log4net>

3.       Then,
you will want to add the path to the file in your <appSettings> node:
<add key=logPath value=c:webappslog4sampleconfiglog4net.config />
4.       In your
Global.asax file, add the following to the Application_Start() method:
//log4net setup:
var logPath = (ConfigurationManager.AppSettings[“logPath”]);

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(logPath));

EXTRA:
You can also
add a helper method to customize your logging and add details that would help
with your debugging:
public class Log4NetHelper
    {
public string LogMsg(Message m, [Optional] string exceptionMsg, [CallerMemberName] string methodName = “”)
        {
            string msg = $”{methodName} {m} {exceptionMsg};
            return msg;
        }
}

               To
reference:
private readonly Log4NetHelper _msg = new Log4NetHelper();
_logger.Debug(_msg.LogMsg(Log4NetHelper.Message.InitialRequestStart));


SAMPLE FOLDER STRUCTURE: