Friday, January 13, 2017

Home-Made Twitter and Gmail Notifications with PHP and Arduino

I am a little obsessed with Twitter. It's how I communicate with most of the developers I'm not fortunate to live in the same city as. I'm a little less obsessed with IoT projects, but then it's harder to find the time to work on them than it is to check Twitter.

Unless I can do both at the same time.

Ever since the IoT week, I've been meaning to work on a project that will let me know when someone wants to speak to me. Something that looks cool, and is at the same time non-invasive. This is what I've come up with...

Arduino logo

Most of this code can be found on Github. I've tested it using PHP 7.1.

In this post I talk a lot about pins. I use this word to mean the pins coming out of components, as well as the Arduino sockets these pins go into. They're often called terminals or ports. When I talk about pins, I just mean a place you can connect components or wires to. I'm quite specific each time though, so you'll be fine!

The Project

The project we're going to work on is a simple notification system, connecting a single RGB LED, a proximity sensor, and a PHP script to gather notifications. As notifications are received, the RGB LED will cycle through them, fading the LED to the appropriate color for each notification type.

Say someone has mentioned us on Twitter. The LED should periodically cycle to the Twitter blue. Then, if someone sends us an email, the LED should cycle between the Twitter blue and the GMail red. We could extend this to the Fitbit green, the Facebook blue and so on.

If we've seen the Twitter notification, we should be able to wave our hand in front of the proximity sensor, and Twitter blue should be removed from the rotation, leaving GMail red etc.

The Hardware

We can use just about any RGB LED. As long as we can control the color it fades to, we can simulate the social network notifications we're after. Really, just about any common anode RGB LED. If you get one that doesn't include resistors, be sure to add those into your circuit. I'll touch on that later...

The more IoT work you do, the more likely you are to run into the terms anode and cathode. A common anode RGB LED is one that connects one pin to the positive pin on your micro-controller or battery, and three pins to ground, depending on the combination of colors you're after.

Connecting the red pin to ground will close the circuit so that current flows through the red portion of the LED. Connecting the red pin to a Pulse Width Modulation (or PWM) port on our Arduino, and grounding the port by half will reduce the amount of current flowing through the red portion of the LED.

Grounding the various pins by varying amounts will lead to many different colors - as many as you can define as CSS colors.

Next, we need some kind of proximity sensor. The easiest (in my opinion) is an infrared transceiver. You could also use an ultrasonic transceiver, but they're better at mid-range (as they have a minimum sensing distance, so you'll have to be further away from the sensor).

Finally, we need an Arduino. You can use another micro controller, but this post will refer specifically to Arduino, because I have three within arms-reach, and nothing else to test on.

Connecting Things Together

Connecting the circuit

This is a relatively simple circuit, as far as they go. I'm by no means an expert, but I did manage to find the data sheets for the infrared sensor and RGB LED I bought; so connecting them up wasn't too troubling.

The important bits are that our LED pins are connected to PWN ports, so that we can gradually adjust the amounts of red, green, and blue, and that the sensor pin is connected to an analog port (in this case A0).

You may be wondering why we don't connect the LEG pins to analog ports, or why we don't connect the sensor to a PWM port. PWM is designed to simulate degrees of on/off, but the way that is done is by turning something on for a fraction of a second.

With the right frequency of on/off cycles, LEDs only appear to be partially bright. That's a side-effect of our eyesight, in much the same way as thirty static images played in quick succession can appear to represent motion video.

We need the sensor connected to the analog port because we really do need a gradual measurement, and A0 will give us that.

My RGB LED is fine with 3.3v and 200mA of current (before the resistors). So I can connect that to the 3.3v pin, and leave the 5v pin for the sensor's power supply.

My sensor also has a pin to enable/disable the sensor readings. I'll code for this, but keep in mind that your sensor might also have this. If it does, connect it to any unused output pin (like 0-8 or 12-13) and make sure you set that pin to high.

We also need to connect the Arduino USB port to an open USB port on development machine.

The Software

Now let's look at the software we'll use to control things. Before we start telling the Arduino what to do, we should define some services:

namespace Notifier;

interface Service
{
    /**
     * Queries the service to trigger notification
     * alerts. Returns true if there are new
     * notifications to show.
     *
     * @return bool
     */
    public function query();

    /**
     * Marks the most recent notifications as seen.
     */
    public function dismiss();

    /**
     * Returns the name of this service.
     *
     * @return string
     */
    public function name();

    /**
     * Returns an array of pin color values,
     * for a common-anode RGB LED.
     *
     * @return int[]
     */
    public function colors();
}

This is from src/Service.php

Each service we connect to needs to have a way for us to query for new notifications. Once we dismiss a notification type, we'll also need to let the related service know.

Continue reading %Home-Made Twitter and Gmail Notifications with PHP and Arduino%


by Christopher Pitt via SitePoint

Erlang and Elixir, Part 2: Data Types

Elixir has a wealth of data types available. The usual basic types integer, float, boolean, and string are here, but so are the atom / symbol, list, tuple, and anonymous functions. You'll learn all about them in this tutorial.

Before we get started: We will be running these examples in Elixir's interactive mode. Type iex in your terminal to enter interactive mode. For more information, read Part 1 of this guide. (Windows users run iex.bat --werl.)

Preface

Elixir is built with meta-programming in mind. All of it is built upon macros. If you are not familiar with macros, they are simply a single instruction which performs a particular task. Not very hard to get your head around maybe, but here are some real-life examples:

if here is a macro for the standard conditional if ... structure we all know. Elixir will compile the result from the macro for you internally.

Meta-Programming?

Fundamental to Elixir is the manipulation of quoted expressions

Meta-programming essentially means you can create code from code (programs have the ability to treat their own code as data essentially... a huge step indeed.) 

An Elixir program can be represented as its own data structure. 

For example, the building blocks of Elixir are represented by a tuple with three elements (more on tuples later). The function call sum(1,2,3) is defined as so:

You can retrieve the representation of any macro via usage of the quote macro:

Note: Additional to the quote macro is the unquote macro—you can read more on the topic in the Elixir documentation.

So we see the first element is the function name of :sum, the second is the keyword list (more on this later) which contains metadata (currently blank in the example), and the third is the arguments list. 

All macros are built using these data structures, so this means our code can have the innate ability to modify and recompile its own code. So your application can now write its own code, check for faults in code and fix them, or even scan a plugin being added and check the code inside and modify it on the fly. 

The implications for artificial intelligence are obviously huge—but first, in this guide we must continue with the basics and get a strong footing in the various data types used by Elixir before we begin delving deeper.

Basic Types 

Now that you are in the interactive console, let's look at Booleans

The standard behaviours of true and false are supported. To check the value, we use the function provided by Elixir, is_boolean.

Each type also has one of these predicate functions. For example, is_integer/1is_float/1, or is_number/1 all will check if an argument is an integer, a float, or either. 

Elixir always refers to functions this way, with a slash followed by a number to signify the number of arguments the function takes. So is_boolean/1 requires 1 argument, e.g. is_boolean(1).

Note: If you want to access help at any time in the interactive shell, just type h and you will be able to access information on how to use the Elixir shell. Also, you can find information on any of Elixir's operators or functions by using h is_integer/1 to receive documentation on is_integer/1.

Mathematically, we can also perform functions like so:

In Elixir, when division is performed on an integer, the return is always a float:

If we want to get the remainder of the division or do integer division, we can use the functions div/2 and rem/2 as so:

Note: Parentheses are not required for function calls.

You can also just enter any binary, octal, or hexadecimal numbers into iex.

Atoms (Symbols)

These are like constants, but their name is their own value. Some languages refer to this as symbols. Booleans true and false are also examples of symbols.

Tuples 

Similar to lists and defined by curly braces, they can contain any data like so:

Lists

Similar to tuples, you define a list with square brackets like so:

Two lists can be concatenated and subtracted also:

To return the start of a list or the end of the list, we use the hd and tl functions (short for head and tail).

What's the Difference Between Lists and Tuples?

Lists are stored in memory as linked lists, a value pair list that is iterated and accessed in a linear operation. So updating is fast as long as we are appending to the list, but if we are modifying inside the list, the operation will be slower.

Tuples, on the other hand, are stored contiguously in memory. This means that getting the tuple total size or accessing just one element is fast. But here, in comparison to the lists, appending to an existing tuple is slow and requires copying the whole tuple in memory internally.

Anonymous Functions

We can define a function by using the fn and end keywords.

Elixir's manual refers to Anonymous Functions as “first class citizens”. This means that you can pass arguments to other functions the same way as integers or strings can. 

So, in our example, we have declared the anonymous function in the variable myFunc to the is_function(myFunc) checking function, which correctly returned true. This means we have successfully created our function. 

We can also check the number of arguments of our myFunc function by calling is_function(myFunc, 2).

Note: Using a dot (.) between the variable and parenthesis is required to call an anonymous function.

Strings

We define Strings in Elixir between double quotes:

Interpolation is supported also with the # sign:

For concatenation, use <>.

To get the length of a string, use the String.length function:

To split a string based on a pattern, you can use the String.split method:

Keyword Lists

Commonly used in programming, a key-value pair of two data entries, essentially two item tuples, can be created like so where the key is an atom:

Elixir has a syntax for defining lists as [key: value]. We can then use any of the other operators available in Elixir such as ++ to append to the back or front of the list.

Keyword lists have three important points to remember:

  1. Keys must be atoms.
  2. Keys are ordered, as specified by the developer.
  3. Keys can be given more than once.

For database queries, the Ecto Library makes use of this when performing a query like so:

Elixir's if macro also incorporates this into its design:

In general, when a list is an argument to a function, the square brackets are optional.

Maps

Similar to Keyword lists, maps are there to help your key-value pair needs. They're created via the %{} syntax as so:

They're similar to keyword lists, but not identical—there are two differences:

  1. Maps allow any value as a key.
  2. Maps’ keys do not follow any ordering.

Note: When you set all the keys in map as atoms, the keyword syntax can be very convenient:

Matching

Maps are very good for pattern matching, unlike keyword lists. When a map is used in a pattern, it will always match on a subset of a given value.

A pattern will always match as long as the keys exist in the provided map. So to match all, we use an empty map.

To take maps further, the Map Module provides a powerful API to manipulate maps:

Nested Data Structures

Data often requires hierarchies and structuring. Elixir provides support for maps inside maps, or keyword lists inside maps and so on. Manipulation of this is made convenient using the put_in and update_in macros, which can be used as follows:

Let's say we have the following:

So now we have some data on the table to play with, and each keyword list of users has a map containing the name, age, and some other information. If we want to access John's age, we can do the following:

It happens we can also use this same syntax for updating the value:

RegEx

Elixir uses Erlang's :re module, which is based on the PCRE. You can find more information in the documentation.

To create a Regular expression in Elixir, use the Regex.compile method or the special short-hand forms ~r or ~R.

The Regex module has a plethora of useful functions that you can use to validate regex expressions, compile them, and escape correctly. Here are some examples:

You can also run a regex over a map and retrieve the results with the named_captures method:

Conclusion

Elixir is a fully featured meta-programming environment based on the usage of macros fundamentally. It can help to bring you as a developer to a new level of application design and data structuring thanks to its powerful lists, maps, and anonymous functions when used in conjunction with its meta-programming capabilities.

We can look at specific solutions in our approaches, which in non-macro languages may have taken many lines or classes of code to create, due to the powerful wealth offered to Elixir in the form of its adoption of DSL (Domain Specific Language) and Erlang's Modules. 

Key-pair value store manipulation, list and basic data manipulation are just a fraction of the full spectrum offered to developers. We'll cover all of this in more detail as we continue in the next parts of the series.


by Tom Whitbread via Envato Tuts+ Code

Heydays

One Pager for Oslo-based design agency, Heydays. Minimal approach to an agency portfolio featuring just a quick trigger background slideshow of their work. Nice touch with the slideshow pause when hovering over the center letter.

by Rob Hope via One Page Love

Madison + Vine

Madison + Vine

Minimal One Pager for 'Madison + Vine' studio featuring a centrally divided layout - left linking to their branded showreel - right linking to their originals.

by Rob Hope via One Page Love

The “Most Loved” One Page Websites from December 2016

one-page-love-hosting-reviews-bh-unlimitedDecember’s “Most Loved” One Page website round-up is brought to you by hosting provider, Bluehost.

Bluehost is the most affordable hosting option to host your One Page websites. They have an incredible $2.95/month deal exclusive to One Page Readers where you can host your website with 50GB diskspace and unlimited bandwidth. They also throw in a free domain!

If you want to receive these “Most Loved” awards in your inbox each month, subscribe to our Inspiration Newsletter.

Below are the 6 One Page websites we awarded “Most Loved” in December – hope you enjoy!

Epicurrence No.5 (Event)

Another banger One Pager by Dann Petty and his team for the fifth Epicurrence event, this time in Jackson Hole, WY. The Single Page website features a wonderfully subtle parallax effect with the layered mountains and logo – all while snow falls and clouds drift by – stunning. Other highlights include a slick colorful submission form and a unique speaker infographic that impressively adapts to a grid on mobile.

Launch Website
Full Review

Mariah Or Messiah? (Game)

Funny (and well executed) online quiz where you decide if these famous quotes where from Jesus or Mariah Carey. The One Pager features a changing background gradient, lovely load transitions and a subtle pixel snowfall effect.

Launch Website
Full Review

10×16 (Music Related, Experimental)

Exceptional One Page website for 10×16 – a fun collaborative project where you could follow as 19 designers countdown their top 10 albums of 2016, each with reimagined cover art. The transitions are beautiful and the minimalist artwork redesigns are just gorgeous. A lovely touch how the website background color changes to correlate with the current artwork you’re browsing.

Launch Website
Full Review

Campaign Monitor 2016 Year in Review (Annual Report)

Colorful One Pager reporting on a fantastic year at Campaign Monitor. The long-scrolling Single Page site features impressive infographics (and stats), lovely load transitions, a scroll progress bar and a fun interactive Ping Pong game.

Launch Website
Full Review

Ueno. (Portfolio)

Stunning, spacious design with gorgeous transitions in this new One Page website for Ueno. The unique intro starts with a full screen video that zooms out while the main project title loads. As you continue to scroll the other project images zoom out within the same image dimension (while subtly moving up) creating a remarkable first impression. Lastly, the Single Page site ends with a big footer area that changes background color to correlate with the (fun!) team videos that also change. Exceptional.

Launch Website
Full Review

The Wedding of Jessica and Brennan (Wedding)

Beautiful long scrolling One Pager announcing the wedding of Jessica and Brennan – both designers of course. The Single Page website features a parallax scrolling rose-overlay that’s perfectly subtle with no parallax on text. There is also good whitespace, lovely typography and last little shoutout to that navigation “roll out” animation, awesome stuff.

Launch Website
Full Review


Hope you enjoyed these beautiful One Pagers from December! Big love to hosting provider Bluehost for sponsoring the round up:)


by Rob Hope via One Page Love

Building a Business on the Back of Interviews

Do you interview people for your blog or podcast? Want to discover different ways to repurpose those interviews? To explore how he built his business through expert interviews, I interview Nathan Chan. More About This Show The Social Media Marketing podcast is an on-demand talk radio show from Social Media Examiner. It’s designed to help [...]

This post Building a Business on the Back of Interviews first appeared on .
- Your Guide to the Social Media Jungle


by Michael Stelzner via

HTML5 Page Structure Basics

The following is an extract from our book, HTML5 & CSS3 for the Real World, 2nd Edition, written by Alexis Goldstein, Louis Lazaris, and Estelle Weyl. Copies are sold in stores worldwide, or you can buy it in ebook form here. Now that we’ve broken down the basics of our template, let’s start adding some […]

Continue reading %HTML5 Page Structure Basics%


by Louis Lazaris via SitePoint