Category Archives: Programming

Ubuntu Tech Snippet #13 – Get your public IP in terminal

Sometimes when you’re working on a project or are SSHed into a remote server, you need to find out the public IP of the device, without using a GUI web-browser.

I had to do this while working on setting up a script on my server, and came across this neat little command that will grab your public IP and print it nicely out for you to use!

All you have to do is run echo $(curl -s and you’re good to go!

Output from echo $(curl -c

Output from echo $(curl -c

You can also do this programatically. In python you can run this code to get the result as a JSON object:

import requests
session = requests.Session()
ret = session.get('',
print ("Public IP Address:",ret.json()['ip'])

You’ll need to install the Python Requests module. This can be done either via pip. Just run sudo pip install requests. You can technically install it using easy_install, but please, please don’t. Just use pip.

And that’s it!

Thanks for reading! If you have any tips of your own, leave them as a comment down below and I’ll be sure to take a look at them!

Tagged , , , ,

Using Buttons with Arduino


This tutorial shows you how to use a simple push-button with your Arduino to light up an LED, not unlike the Flashing an LED tutorial on this site except we are flashing it only when the button is pressed.

Buttons are pretty important in Arduino, not to mention breadboarding and DIYing in general. By the end of this tutorial you should know how to wire a push-button for use with your Arduino.

This is a nice and easy first-time project for you to try out on your brand new Arduino!

For this tutorial you’ll need:

Finally, few pieces of hookup wire, or breadboard jumper wires – just to hook everything together.

Please note, I am not affiliated or being paid in any way with/by Foxytronics. It’s simply a good store that provides quality components at great prices and my experience has been only good with them.


The wiring for this tutorial is pretty simple, just follow this graphic:

Using Buttons with Arduino Wiring – Image made with Fritzing

First, connect 3.3V and GND to their respective rails on the breadboard.

Second, place the button. You have to place the button straddling the center gap on the breadboard to correctly place it. Just follow the orientation shown above and you’ll be fine.

Next, place the LED. You’ll want the bent leg facing towards the button, the straight leg facing away.

Next, you’ll need to place two 4.7kΩ resistors. One will be connected to the button, on the lower-right-hand corner. The second will be connected to the non-bent leg of the LED. The reason we’ve got a resistor in front of the button is because we’d be getting the full 3.3V through the button and feeding that right back into the Arduino, and we don’t want that. As for why we’re placing a resistor after the LED, if we didn’t have that resistor we’d be putting 3.3V through our LED, which would burn out the LED quite quickly.

Finally, complete the wiring for the button and LED. Nothing too complicated here, just connecting the button to the ground rail to complete that circuit, connecting the button to pin 7 so we can get input from it, and then connecting the LED to the Arduino.

If you followed the diagram correctly, that’s all the Electrical work you need to do, time to get programming!


This code is fairly simple. Just copy/paste or type it all into your Arduino IDE, explanation comes after.

/* Button Tutorial Code
 * Version: 1.0.3
 * Author: RPiAwesomeness
 * Modified: 12/9/14
// First define some constants for the pins
 #define PIN_BUTTON 7
 #define PIN_LED 2
// Define some variable that we'll be using later
 int buttonState = 0; // Keeps track of button state
 // 0 = NOT pressed, 1 = IS pressed
 * setup() is called when the Arduino is turned on or resets
 void setup()
   Serial.begin(9600); // Start the Serial console at a baud rate of 9600
   pinMode(PIN_BUTTON, INPUT); // Declare the 7 digital pin's mode as input
   pinMode(PIN_LED, OUTPUT); // Declare the 2 digital pin's mode as output
 * void loop() loops constantly while the Arduino is powered
 void loop()
   checkButtonState(); // Check the state of the button
 * This function checks the state of the button
 void checkButtonState()
 // Read the state of the button into the buttonState variable
 buttonState = digitalRead(PIN_BUTTON);
// Check if the pushbutton is pressed
 // if it is, the button's state will be HIGH
 if (buttonState == !HIGH) {
 // Turn our LED on
 digitalWrite(PIN_LED, HIGH);
 else {
 // Turn our LED off
 digitalWrite(PIN_LED, LOW);

What this code does is checks whether or not the button is pressed and turns on the LED if it is.

First off, we’ve got the #define PIN_BUTTON 7 line and the #define PIN_LED 2 statements. These simply set the variables PIN_BUTTON and PIN_LED to equal 7 and 2 respectively, which allows us to easily reference the pin we want in the future and helps keep our code readable and easily understandable.

Next, we declare the buttonState variable as an integer (int) and set its default value to equal 0 – meaning that the button is NOT pressed.

Next up is the setup() function. Any and every Arduino program must have this function and it’s what is called automatically when the Arduino first starts up. We’re going to start the Serial console at a 9600 baud rate (don’t worry about this, that’s just what you should default to for now.) We’re also going to set the pin mode for the PIN_BUTTON (pin 7) as an input pin and PIN_LED (pin 2) as an output pin.

The loop() function is continuously called automatically and is yet another required function. In our case we’re simply calling the checkButtonState() and nothing else.

Finally, we’ve got the checkButtonState() function. This function gets the current state of the button and stores that in the buttonState variable. It then checks that variable to see whether or not the button is pressed and if the button IS pressed, then it lights up the LED. 1

And that’s all there is to it! Upload your code to your Arduino:


and once it’s uploaded, go ahead and press the button on the breadboard. If you wired everything up correctly and your code doesn’t have any bugs from copying, compiling, or uploading you should  see the LED turn on when you press it!

Congratulations! You’ve just completed the Using Buttons with Arduino tutorial! The ability to use buttons in your projects is incredibly useful and is definitely something you will most definitely use down the road if you continue working with Arduino!

1 One thing you may notice about the code here is that we’re using what looks like a DOES NOT EQUAL statement that got typed in incorrectly. However, this isn’t the case. When I coded if (buttonState == !HIGH) { statement, I meant that. You see, there’s the NOT operator in C++/Arduino code, and it’s expressed via the exclamation mark (!).
In a nutshell, it takes whatever the normal result would be (has to be a true/false result) and inverts it. If you want more explanation, read here.
Tagged , , , ,
%d bloggers like this: