Using a momentary switch with an arduino

I purchased an arduino a little while ago and got the starter kit with it.
It comes with most of the stuff you need to get into some fun projects and understanding how the whole thing works.

20m 66ft 20awg Extension Cable Wire Cord
HeroNeo 10pcs Connector Adapter Cable
JACKYLED New DC 12V 2A 2.0A
SunFounder 8 Channel 5V Relay Shield Module
Sunfounder Project Super Starter Kit with Tutorial Book
Arduino Mega 2560 Board Model
16mm High Round Cap Momentary Metal Push Button Switch
Triangle Bulbs 3528-IP65-White-60L Pure White LED Strip Light, Waterproof LED Flexible Light Strip 12V with 300 SMD LED

Well my first real project was to get led lights to work. I asked for the led lights for christmas as a stocking stuff.
After that I bought the relay and power supply as a gift to myself for my birthday.

Then it was off to wiring it up.
Plug the 12v into one side of the bread board.
Plug the negative for the LEDs into the same row as the power supply.
Then run a wire from the positive to the Common (center) plug of the relay you would like to use.
Take the positive of the LEDs and plug that into the Normally Closed channel of the relay.
The idea with that is when the relay is triggered the circuit if created.

Now to wire up the relay to the arduino.
Run a wire from the 5v power into the VCC of the relay. This is the part that actually powers the physical switch/magnet to close the circuit on the other side of the relay.
Run a wire from the ground power into the ground of the relay.
Use any of the none PWM channels on the arduino since PWM is NOT what we want for controlling the relay.
I picked ch 22 and plug that into ch 1 of the relay.
Note this relay has all the resistors and circuitry setup so you don’t need to do any of that yourself. You can purchase a relay by itself and do all that yourself.

We now have a fully wired up circuit to control the relay.

Lets try this first code to make sure it worked:

// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(22, OUTPUT);

// pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(22, HIGH);
delay(2000);

digitalWrite(22, LOW);
}

That worked so we know can now save that file incase we need it later.
Now we can do the logic for the momentary switch. I picked a momentary switch because of the look in the physical world. It returns to a flat state after its pushed instead of like a light switch that moves and stays in the position you put it on. The problem with the momentary is now you have to keep track of states, the time it takes to push the button and how it needs to be wired.

Continue reading Using a momentary switch with an arduino

Redis Pub/Sub PHP and Node.js

Redis’ pub/sub is a super simple way of communicating events between parts of applications or completely separate applications.

This turned out to be a great solution for a project that had a background php worker that was generating calculations but had a front-end web socket in node.js that was pushing those out to the client.

A simple timer on the node.js was the intermediate solution. The issue was the timer in node.js verse the cron tab run would never match up and would take an extra cycle in the timer to get the data so the live data was still aways behind.

Pub/Sub was the solution since both apps were already using Redis to store and retrieve the data (along with other resources on their own). But there was a problem. The message part of the Publish(Key,Message) can only be a string. The data that was being sent was already complex and couldn’t be appended naturally with out a lot of code. So instead of trying to add to the array that was being stored as json originally, I took the data and added it into a stdClass object that contained the other info that I needed.

<?php $key = 'importantInfo1';
$message = new \stdClass();
$message->who = 'client1';
$message->what = 'thing2';
$message->payload = $arrayOfData;

$redis->publish($key, json_encode($message));

This allowed for me to identify the data without have to inject it in the original array and remove it later.

On the node.js side, I found another “issue” but a quick google search solved it. (http://stackoverflow.com/questions/7330496/redis-node-js-2-clients-1-pub-sub-causing-issues-with-writes). The issue is a node.js redis connection can be a getter/setter and a pub/sub connection at the same time. I don’t know the reason but the solution is easy. Make 2 connections, name one “redisDbGetSet” and the other “redisDbPubSub”.

There is also another step before you can do the “on” step for receiving those messages.
The connection has to subscribe, which makes sense due to what its doing and so a standard connection isn’t flooded with messages it doesnt want.

redisDbPubSub.subscribe('importantInfo1', 'importantInfo2');

redisDbPubSub.on("message", function (channel, message) {
    switch(channel){
        case 'importantInfo1':
            functionOne(JSON.parse(message));
            break;
        case 'importantInfo2':
            fucntionTwon(JSON.parse(message));
            break;
    }
}

Continue reading Redis Pub/Sub PHP and Node.js