Posted on

Arduino Binary Clock

This post is build on top of the LED matrix in the earlier post. I always wanted to build an Binary clock, and since I had the LED matrix ready, and I found a concept done by other people in their project, I had an idea to re-use my LED matrix and turn it into a binary clock.

Here are the list of material that you will need:

  • Arduino uno/nano
  • Realtime clock DS3231
  • Button cell battery CR2032
  • 8×8 LED matrix
  • 74HC595N shift register
  • Prototyping board or breadboard

Step 1. The Design


Binary Clock
The LED matrix consist of 8×8 LED, so we design the top row to be YYMMDD with a space of empty column between the Year and Month and between Month and Date.

The hour is being displayed at the bottom row in the format of HHMMSS. We also allow a space of empty column in between hour and minute and between minute and second.

The Least significant bit is at the bottom and the Most significant bit is at the top. Each digit is being displayed using four LED, acting like BCD (Binary Coded Decimal) code, which is able to count up to 10. However the hour will only count up to 24 and both the minute and seconds will only count up to 59.

 

 

 

Step 2. The wiring diagram


Leveraging the same circuit to connect the LED matrix to Arduino, we only need to at the RTC (Real Time Clock) DS3231 to the Arduino. To do this we are connecting the SCL line from the DS3231 module to SCL line of Arduino and connecting the SDA line from the DS3231 to SDA line of Arduino. Once this is done we then connect the VCC line to 5V and the ground to Arduino GND.

The detail explanation of the other part of the circuit can be found in my previous instructables post related to 8×8 Matrix Letter game. If you would like to know more details please visit the linked page, you can also asked me if you have any questions related to the connection.

Step 3. Loading the Code

Once this is done, we can start to load the code from the Arduino interface. To initialise the RTC module we need to include the following library.

#include <Time.h>
#include <TimeLib.h>
#include <DS3231.h>

Then to initialise the clock for the first time, add the following code in the setup section of the code.

// Set sketch compiling time
clock.setDateTime(__DATE__, __TIME__);

The above initialisation is to make sure that the DS3231 is sync with the current date and time the first time it is being executed. As long as you had the button cell battery in the RTC clock, it will keep maintaining the clock, and you don’t have to re-initialise it every time.

The following code is the key to displaying the correct binary output:

int day_one = _day % 10;
int day_ten = _day / 10;
int month_one = _month % 10;
int month_ten = _month / 10;
int year_one = _year % 10;
int year_ten = (_year / 10) % 10;
int sec_one = _second % 10;
int sec_ten = _second / 10;
int min_one = _minute % 10;
int min_ten = _minute / 10;
int hour_one = _hour % 10;
int hour_ten = _hour / 10;

We need to separate the one digit and the ten digit of the day, month, year, hour, minute and second by using the division and the mod to get the remainder. Then the output is combined together before being pushed out to the LED matrix. This is done in the same procedure called display_time() as below:

output[0] = day_one << 4 | sec_one;
output[1] = day_ten << 4 | sec_ten;
output[2] = 0;
output[3] = month_one << 4 | min_one;
output[4] = month_ten << 4 | min_ten;
output[5] = 0;
output[6] = year_one << 4 | hour_one;
output[7] = year_ten << 4 | hour_ten;

The day digit is being combined with the second digit, the month digit is being combined with the minutes digit and the year digit is being combined with the hour digit. once this is done we then called the procedure display_char(output) to display all the combination into the LED matrix.

Final Thoughts

You can see the realtime clock in action in the following video.

You can download the full source code in the following location. If you like this post, please leave a comment and also don’t forget to subscribe to receive frequent update and more similar projects like this one. You can easily expanded this project to be able to display Fibonacci clock. I have some concept in my mind already, so when I had the chance I will make a small modification to be able to display the Fibonacci clock.

Posted on

Simple Frankenbot with lights and sound effect

Frankenbot

I was inspired by the cute Frankenbot papercraft from Instructables, so I thought why not make it truly alive by giving it some blinking lights and sounds effect. Here is the plan:

– Connect 2 LEDs for the eyes (different colours of course) and only one eye can be on at a time

– Connect 3 LEDs to the chest so that it can run lights left and right

– Play some scary halloween music for the full effect.

So here goes my Frankenbot ready for Halloween.

Step 1. Create the Frankenbot papercraft using the instruction from Instructables.

Step 2. Cut out the eye holes and insert LEDs

Franken bot eyes

 

I use the recycle LEDs that was joined together with one 220 ohm resistor. Different legs was connected to the same resistor so only one eye will be on at a time, and you will have to switch the polarity to light up the second eye.

Step 3. Prepare the chest holes for LEDs

Chest holesCut out 3 more holes on the chest. It is much easier to cut it out when you had the robot chest disassembled.

Step 4. Prepare the electronics

I am using the recycled electronics from the “Light Activated MP3 player” from the earlier post. You can follow the link to find the instruction on how to build one.

Things to note, the way the light activated will need to be reversed, in this we want the Frankenbot to stay idle when there is sufficient light detected by the sensor, and when the light is off, it will came alive blinking it’s eyes and the chest lights and also playing the horror music at the background. So minor modification to the source code is necessary.

Step 5. Connecting the LEDs

Connecting LEDs

What I had done here is essentially as follows:

– Eyes is connected through pin D2 and D6

– Chest LEDs is connected through pin D3, D4 and D5

So the following additional modification to the code is necessary

const int LDRpin = A0;       // LDR connected to pin A0 const int EyeCommon = 6;    // low = off
const int Eyepin = 2;      // Eye LED pin D23
const int Chest1 = 3;
const int Chest2 = 4;
const int Chest3 = 5;

Step 6. It’s Alive

FrankenbotThere are several final modification to the code is necessary as follows:

In the setup section add the following:

pinMode(LDRpin, INPUT);     // setup LDR
  pinMode(EyeCommon, OUTPUT);
  pinMode(Eyepin, OUTPUT);
  pinMode(Chest1, OUTPUT);
  pinMode(Chest2, OUTPUT);
  pinMode(Chest3, OUTPUT);
mp3.playFolderTrack(7,Song);

in the loop section add the following:

if (ldrStatus <300){
    //digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(EyeCommon, LOW);  // eye LED had different polarity so that 
    digitalWrite(Eyepin, HIGH);	   // only one eyes can be on at one time
    delay(100);
    digitalWrite(EyeCommon, HIGH);
    digitalWrite(Eyepin, LOW);
    timeoutTimer ++;
    //Serial.print("LED Light");
    if (DEBUG) Serial.println(timeoutTimer);
    mp3.start();
    digitalWrite(Chest1, LOW); // chest LED had common cable connected, 	 	
    digitalWrite(Chest2, HIGH); // my common cable is connected to Anode, 
    digitalWrite(Chest3, HIGH);  // so EyeCommon needs to be HIGH
    delay(100);
    digitalWrite(Chest1, HIGH);
    digitalWrite(Chest2, LOW);
    digitalWrite(Chest3, HIGH);
    delay(100);
    digitalWrite(Chest1, HIGH);
    digitalWrite(Chest2, HIGH);
    digitalWrite(Chest3, LOW);
    delay(100);
    digitalWrite(Chest1, HIGH);
    digitalWrite(Chest2, LOW);
    digitalWrite(Chest3, HIGH);
    delay(100);
    
    onStatus = true;
    if (timeoutTimer > 200) {
      if (DEBUG) Serial.println(timeoutTimer);
      timeoutTimer = 0;
      resetCount = 0;
    } 
  }

The section above is when the light intensity is low, so we flicker the eyes LED and the chest LEDs. The following section is when the light is high, we only have one eyes turned on.

} else {
    digitalWrite(EyeCommon, LOW);
    digitalWrite(Eyepin, HIGH);
    digitalWrite(Chest1, HIGH);
    digitalWrite(Chest2, HIGH);
    digitalWrite(Chest3, HIGH);    

    mp3.pause();
    timeoutTimer ++;
 }

And don’t forget to upload the scary scene music to the sd card. In this example I put it in the folder “07” and the song is called “001.mp3”

Now upload the code and test it, if all goes well, when you flick the switch your Frankenbot will come alive. Cover the sensor or turn off the lights, it’s eyes should flicker and the chest LED will flicker too.

Enjoy and Happy Halloween!!!

If you like this you can subscribe for frequent update and leave some comments.

Posted on

IoT Gas Sensor with ESP8266

I had been wanting to get the gas sensor working with my ESP8266 as an additional item on the IoT list. So similar to the IoT Temperature sensor from the previous post, I had created the IoT gas sensor using ESP8266.

Using the same idea you don’t have to open port in your router, but leveraging an intermediate layer by publishing the data to a website. Here are the list of components used:

The circuit

We are connecting the Gas sensor analog output to Analog input (AD0) of ESP8266 as shown in the following picture.

ESP8266WithGasSensor

The Buzzer is connected to Pin GND and D3.

We will check the sensor via analog to give much better variation, we can also use the Digital output from the sensor, but you will have to calibrate this properly to ensure that it gives the desired trigger when a certain composition of gas was detected. You can use the prototype board to connect them all up and using Arduino interface you can load the code.

If you are not familiar on how to setup the Arduino IDE (integrated development Environment) I had an instructables post that can help you to get started.

The finished connection should look like something below:

IoT Gas Sensor with ESP8266

Now that you have a setup the circuit, there is one more step to setup the Cloud webserver (optional) if you want the sensor to periodically upload the gas reading to the webserver so that you can monitor this remotely.

Assumption: You have a working webserver, and familiar with transferring files to your webserver.

Web server setup

Transfer the content of “IoTGasSensorwebserver.zip” into the root of your webserver. Let’s assume that your webserver is “http://arduinotestbed.com”

You can put the file in a separate folder, but you need to modify the sketch to point to the correct location of “gasdata_store.php”

In this example we are assuming that the full path to the file are http://arduinotestbed.com/gasdata_store.php

Once you had uploaded the files, you can test that it is working going to the following link

http://arduinotestbed.com/GasData.php

watch out for the character case as most webserver are case sensitive. If all is working, you should see something that looks like the following:

IoT Gas Data

One more thing you will need to make sure is the file “gas.txt” needs to be writeable, so you need to set the permission of this file to “666” by using the following unix command:

chmod 666 gas.txt

This file is where the sensor data will be uploaded into by the ESP8266.

You can download the webserver scripting using this link.

Uploading the arduino sketch

Now you are ready to upload the sketch code to the ESP8266. You should have 2 files:

  • ESP8266GasSensor.ino
  • mainPage.h
  • settings.h

Put them all in the same folder and open the “ESP8266GasSensor.ino” in the Arduino IDE, then make small modification to the code to point to the correct webserver location shown in the following picture:

Arduino code changes

Modify the line

String weburi = "/gasdata_store.php"

if you didn’t put this file in the root of the webserver, this should point to the correct path of the file. Then compiled and load the sketch to ESP8266. If all goes well you should have a working AP (Access point) from the ESP8266 the first time you run this. The AP’s name is called “ESP-GasSensor”.

Try to connect to this AP using your laptop or mobile phone, then find out what is the ip address that was assigned to you, this can be done using “ipconfig” command in windows or “ifconfig” command if you are in linux or mac. If you are using iPhone you can click on the “i” button next to the ESP-GasSensor that you are connected to.

Open your web browser and point to the ESP-GasSensor Ip address, if you are assigned with 192.168.4.10 as your up, the ESP-GasSensor is have the ip of 192.168.4.1, so you can point your web browser to http://192.168.4.1

You should be presented with the settings page where you can enter your wifi configuration. Once you had enter your WiFi access point that connects to the internet, tick the “update Wifi Config” check box, and click on “update” to save the settings to the ESP8266.

The ESP8266 will now restart and trying to connect to your WiFi router. If all goes well you should see the gas data being updated to you webserver on a regular interval. In this example you can point your browser to

http://arduinotestbed.com/GasData.php

Congratulations!! if you manage to reach this part. You should give yourself a pat on the back. Now you can tell your friends about the gas sensor that you have.

You can download the full arduino source code here.

Final adjustment

But wait you might want to re-calibrate the sensor alarm to suit your need. This is not just for show, it should trigger and alarm when the threshold of gas reach a certain level. Depends on the type of sensor that you are using you will need to calibrate this. So go get a lighter, and point the lighter towards the sensor, and without lighting the lighter, press the gas release button on the lighter, so the gas will flow to the sensor. This should trip the buzzer. If not then you need to check whether the reading goes up by looking at the webserver. If this is not working then you need to check the connection, the sensor and the buzzer. If all goes well, the buzzer should make a noise. The threshold in the code is set to 100, you should be able to find it in the following section of the code:

double threshold = 100;

Feel free to change the threshold to higher or lower depends on your need. I hope you like this project. If you do please drop me a line, and subscribe to my blog for more projects.

 

Posted on

Arduino Dice simulator

I was looking at my kids playing snake and ladder tonight, and came an idea in my mind “why don’t I make a dice simulator?”. I already had the LED matrix from the previous post here. So I created the Arduino Dice simulator using my previous circuit.

So I added a permanent button to the circuit that I had using a pull-down resistor of 10K ohm which connects pin 2 to the ground.

Then with little modification to the code as following

// LED matrix with latch 74HC595N
// the program random display the character
// the button is added and the character will only be displayed if after the button is pressed
// added code to simulate a dice

int latchPin = 4; // pis connected to shift registors
int clockPin = 5;
int dataPin = 3;
int pins [8] = {6, 7, 8, 9, 10, 11, 12, 13}; // common cathode pins
int button = 2; // connect button and 10K resistor to GND (normal LOW), the other end to 5V
byte A[8] = {   B00000000, // Letters are defined
                B00011000,// you can create your own
                B00100100,
                B01000010,
                B01111110,
                B01000010,
                B01000010,
                B00000000
            };
byte B[8] = {0x0, 0x7c, 0x42, 0x42, 0x7c, 0x42, 0x42, 0x7c}; 
byte C[8] = {0x0, 0x3c, 0x42, 0x40, 0x40, 0x40, 0x42, 0x3c};
byte D[8] = {0x0, 0x7c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x7c};
byte E[8] = {0x0, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x7e};
byte F[8]={0x0, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x40};
byte G[8]={0x0, 0x3c, 0x42, 0x40, 0x4e, 0x42, 0x42, 0x3c};
byte H[8]={0x0, 0x42, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42};
byte I[8]={0x0, 0x70, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70};
byte J[8]={0x0, 0x7c, 0x4, 0x4, 0x4, 0x4, 0x44, 0x38};
byte K[8]={0x0, 0x44, 0x48, 0x50, 0x60, 0x50, 0x48, 0x44};
byte L[8]={0x0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7c};
byte M[8]={0x0, 0x42, 0x66, 0x5a, 0x42, 0x42, 0x42, 0x42};
byte N[8]={0x0, 0x42, 0x62, 0x72, 0x5a, 0x4e, 0x46, 0x42};
byte O[8]={0x0, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c};
byte P[8]={0x0, 0x7c, 0x42, 0x42, 0x7c, 0x40, 0x40, 0x40};
byte Q[8]={0x0, 0x3c, 0x42, 0x42, 0x42, 0x4a, 0x44, 0x3a};
byte R[8]={0x0, 0x7c, 0x42, 0x42, 0x7c, 0x48, 0x44, 0x42};
byte S[8]={0x0, 0x3e, 0x40, 0x40, 0x3c, 0x2, 0x2, 0x7c};
byte T[8]={0x0, 0x7c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10};
byte U[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c};
byte V[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x42, 0x24, 0x18};
byte W[8]={0x0, 0x42, 0x42, 0x42, 0x42, 0x5a, 0x66, 0x42};
byte X[8]={0x0, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x0};
byte Y[8]={0x0, 0x44, 0x44, 0x28, 0x10, 0x10, 0x10, 0x10};
byte Z[8]={0x0, 0x7e, 0x4, 0x8, 0x10, 0x20, 0x40, 0x7e};
byte X1[8]={0x0, 0x0, 0x18, 0x3c, 0x3c, 0x18, 0x0, 0x0};
byte X2[8]={0x0, 0x20, 0x70, 0x20, 0x4, 0xe, 0x4, 0x0};
byte X3[8]={0x40, 0xe0, 0x40, 0x18, 0x18, 0x2, 0x7, 0x2};
byte X4[8]={0x42, 0xe7, 0x42, 0x0, 0x0, 0x42, 0xe7, 0x42};
byte X5[8]={0x42, 0xe7, 0x42, 0x18, 0x18, 0x42, 0xe7, 0x42};
byte X6[8]={0x66, 0x66, 0x0, 0x66, 0x66, 0x0, 0x66, 0x66};
byte blank[8] = { B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000};
long randNumber;
int buttonpressed = 0;
long time = 0; // last time the button pressed
long debounce = 50;

void setup() {
  Serial.begin(9600); // Serial begin
  pinMode(latchPin, OUTPUT); // Pin configuration
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  for (int i = 0; i < 8; i++) { // for loop is used to configure common cathodes
    pinMode(pins[i], OUTPUT);
    digitalWrite(pins[i], HIGH);
  }
  pinMode(button, INPUT);   // set button input 
  randomSeed(analogRead(0));
}

void loop() {
  // read the button
  buttonpressed = digitalRead(button);
  if (buttonpressed == HIGH && millis() - time > debounce){
    time = millis();
    randNumber = random(1,6);
    Serial.println(randNumber);
    
    for (int k = 0; k < 1000; k++) { // showing each letter for 1 second
      getChar(randNumber);
  
    }       
  } else {
    display_char(X1);delay(20);
    display_char(X2);delay(20);
    display_char(X3);delay(20);  
    display_char(X4);delay(20);
    display_char(X5);delay(20);
    display_char(X6);delay(20);
 
  }


  // add more letters show method here

}

void getChar(int num){
  switch (num){
    case 1: display_char(X1); break;
    case 2: display_char(X2); break;
    case 3: display_char(X3); break;
    case 4: display_char(X4); break;
    case 5: display_char(X5);break;
    case 6: display_char(X6);break;
    default:
    break;
  }

}
void display_char(byte ch[8]) { // Method do the multiplexing
  for (int j = 0; j < 8; j++) {
    digitalWrite(latchPin, LOW);
    digitalWrite(pins[j], LOW);

    shiftOut(dataPin, clockPin, LSBFIRST, ch[j]);
    digitalWrite(latchPin, HIGH);
    //delay(1);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, B00000000); // to get rid of flicker when
    digitalWrite(latchPin, HIGH);
    digitalWrite(pins[j], HIGH);

  }
}


I hope you enjoyed this short post. You can view the result in the following video

If you like what I post, don’t forget to subscribe.

Posted on

RFID enabled tag

This RFID enabled tag tutorial show a simple RFID circuit that connects to Arduino. The circuit will detect the RFID card and play a simple tone on the speaker when the registered card is tapped and will play another tone when un-registered card is tapped.

RFID with speaker1

I had stumble across a simple circuit to simulate RFID lock and unlock via Instructables from educ8s and his website. So I used his code with little modification and added my own code from the previous post on simple Super mario speaker song, and this circuit was born.

Continue reading RFID enabled tag

Posted on

The simplest Mario speaker song circuit

The other day I stumble upon a simple circuit to create a speaker song. So naturally I try it out and it seems to be quite simple, but it was the sponge bob theme song, so I thought what a boring song. I want a more fun song, and what is more fun and nostalgic than the super Mario theme song, and I manage to find another blog post on Super mario theme song. I analysed the code and found a few improvement that can be made, so naturally I modify the code and factoring he function call. So the Mario speaker song was born.

So here are the circuit connection diagram:

You only need to connect the speaker to GND and another through 1K resistor through pin 3 of the arduino. Once done, you can upload the following code. Continue reading The simplest Mario speaker song circuit

Posted on

Disco Light using LED Strip

My Daughter had a disco night to attend, so what could be better than equipped her with and LED Strip which will flash through the night and making it a blast.

It is also fortunate that the LED strip that I had order through Aliexpress arrived on time along with the Arduino Nano that I had ordered.

Components

Ok, so lets go through the list of components that we need for this:

  • Arduino Uno or nano
  • 5 V addressable LED Strip (WS2812B)
  • Switch
  • Battery pack
  • Cables
  • Recycled belt or back pack strip

Continue reading Disco Light using LED Strip

Posted on

PS3 CFW 4.82

I am trying to use my PS3 today and it seems that my son had accidentally upgrade the firmware to the latest Official Firmware 4.82. Prior to this I am having CFW 4.55 and I haven’t upgrade it cause all the games are still working. so ARRGGGHH!!!

Keep Calm and search google, so I stumble across some fine instruction to flash back the my PS3 CFW 4.82. I use the following link for the instruction

https://gbatemp.net/threads/guide-ps3-4-82-cfw-installation-for-dummies.491117/

The exploit tools patch can be found in the following location.

http://www.psx-place.com/threads/ps3xploit-tools-v2-0-improved-flash-writers-dumpers-even-easier-to-install-cfw-on-4-82-ofw.16139/

To obtain the legit information after being con from few other various sites with firmware that was password protected and trying to get you to sign up for survey or something similar and selling your email address, mobile phone number and the like.

The Custom firmware should be obtained from the following.

REBUG_4.82.1_LITE_c5da00d59e4d1a7aa599e5d0e21415f8 _PS3UPDAT.PUP

I am patiently waiting for the exploit to work its magic, this is the second try and it still not successful.

After gazillion tries, the following steps is the one that is successful.

Use the HDD method instead of USB & follow each of these steps to the letter:

A) Flash NOR
It’s highly recommended to install the 4.82 official firmware twice on your console before using ps3xploit flash writer v2.0.
If your ps3 is already on ofw 4.82 then just install the 4.82 system update PUP again.

Once done, open your ps3 browser, set the homepage to http://ps3xploit.com/writer/index_nor_hdd.html
Restart the browser.
Follow the on screen instructions ie
1. download the flash_482.jpg file
2. initialize exploitation
3. patch flash

When patching is finished, you should get a success message.
To protect your console against any risk of bricking, do NOT turn off or reboot the console just yet, you should validate the Flash memory first.

B) Dump NOR
Now set your homepage to http://ps3xploit.com/dumper/index_nor_hdd.html
Restart your browser.
Follow the on screen instructions ie
1. download the dump.jpg file
2. initialize exploitation
3. dump nor flash

When the dump is done you should get a success message.

C) Validate NOR
Insert a fat32 USB key in ps3.
Go to the Photo colum on XMB, select the dump.jpg file & press the Triangle button. Choose to copy the file to USB device.
On PC, rename the file to dump.hex & use PyPS3checker tool to validate the dump.
You can get this tool from the dev’s github repo:
https://github.com/littlebalup/PyPS3tools
Read PyPS3checker_README.txt for tool requirements & usage syntax. You will need to install python 2.7.15, the installer is available for download on python.org.

In the PyPS3checker validation result screen, if you get either ros0 or ros1 valid with PS3Xploit 4.82 then the patch was successful, you can reboot & install the 4.82 cfw of your choosing. ;)
If the dump isn’t validated, ie you get more than 1 ros warning, do not switch off your console & post here to get further guidance.

Hope it helps.

Posted on

BTCmarket ticker using ESP8266

BTCmarket Ticker with ESP8266

With the recent fall of Bitcoin and other crypto currency and my continue interest in learning more about Arduino, after reading several other instruction of using OLED display, I had combine it all to create a BTCmarket ticker using ESP8266. The reason why I use BTCmarket as the sample is because this is the one that I am currently using, but you can adapt that to any market ticker providing they have API to support data exchange.

Step 1: Gather all the materials needed

The materials used in this are:

  • ESP8266
  • 128×64 0.96″ OLED display

Step 2: Connect the ESP8266 to the OLED display using the following :

  • Connect the SCL of OLED display to D2 of ESP8266
  • Connect the SDA of OLED display to D4 of ESP8266
  • Connect the VCC to 3.3V
  • connect the GND to ground

This is shown in the figure below:

ESP8266 connection diagram
ESP8266 Connection

Step 3: Load library to Arduino

The following instruction assume that you are already familiar with Arduino interface and know where the library is located. For Mac users the Arduino library is located in Documents/Arduino/Libraries.

You will need the ESP8266RestClient library, TimeLib library and the ESP8266_and_ESP32_Oled_Driver_for_SSD1306_display library. Credit goes to their respective author for creating such an awesome library.

Step 4: Load the following code in Arduino interface and upload it to the ESP8266

// BTCMarket Crypto currency ticker
//
// 128x64 OLED pinout:
// GND goes to ground
// Vin goes to 3.3V
// Data to I2C SDA (GPIO 4)
// Clk to I2C SCL (GPIO 2)
#include <TimeLib.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include "RestClient.h"
#include <SSD1306.h>
#include <SSD1306Wire.h>


char ssid[] = "INSERT YOUR SSID HERE"; // your network SSID (name)
char pass[] = "INSERT YOUR NETWORK PASSWORD"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

RestClient restclient = RestClient("api.btcmarkets.net",443,1); 
SSD1306 display(0x3c, 4, 2); //0x3d for the Adafruit 1.3" OLED, 0x3C being the usual address of the OLED


void setup() {
 //Initialize serial and wait for port to open:
 Serial.begin(115200);
 //Setup Rest Client
 WiFi.begin(ssid, pass);

 Wire.pins(4, 2); // Start the OLED with GPIO 4 and 2 on ESP-01
 Wire.begin(4, 2); // 4=sda, 2=scl
 display.init();
 display.flipScreenVertically();
 display.drawString(0, 24, "Connected.");
 display.display();
 delay(1000);
}

void processticker(){
 String response = "";
 //Get the ETH ticker
 int statusCode = restclient.get("/market/ETH/AUD/tick", &response);
 // for some reason sometimes the response is not clean
 String temp = response.substring(response.lastIndexOf("{"), response.lastIndexOf("}")+1);
 //Serial.println("status:" + String(statusCode) + " response:" + temp);
 int rlength = temp.length();
 //Serial.println("Response Length=" + String(rlength));
 char jvar[rlength+1];
 
 temp.toCharArray(jvar, rlength+1);
 //jvar[rlength] = 0;
 StaticJsonBuffer<300> jsonBuffer;
 Serial.println("jvar:" + String(jvar));
 JsonObject& root = jsonBuffer.parseObject(jvar);
if (!root.success()){
 Serial.println(F("Parsing failed!"));
 Serial.println("temp:" + temp);
 } else{
 String Buy = root["bestBid"];
 String Sell = root["bestAsk"];
 String LastTrade = root["lastPrice"];
 time_t tStamp = root["timestamp"];
 tStamp = tStamp + 600 * 60; // GMT + 10
 String TheDate = String(String(day(tStamp)) + "/" + String(month(tStamp)));
 String TheTime = String(String(hour(tStamp)) + ":" + String(minute(tStamp)));
 Serial.println("Buy:"+Buy+"; Sell:"+Sell + "; Last:"+LastTrade );
 display.clear();
 display.drawString(0,0, "ETH/AUD");
 display.drawString(0,13, TheDate +" " + TheTime);
 display.drawString(0,23, "Buy: "+Buy);
 display.drawString(0,33, "Sell: "+Sell);
 display.display();
 }
 
 delay(1000);
}

int timer =0;

void loop() {

if (timer > 5){ // so that we don't call it too frequently we add delay of 5 second
 timer = 0;
 //process ticker
 processticker(); 
 }else{ // handle http client while waiting
 timer++;
 }

delay(1000);
}

You should be able to have the Ethereum ticker now that you can expand to give you an alert they way you want it.

If you like this post, please give a comment or share with your friends. You can also subscribe to my blog for regular updates.

Posted on

IoT Motion Sensor with light

This is the extension of the earlier post IoT Temperature Sensor with ESP8266. The extension is to additional IoT Motion Sensor with light.

Here are the concept picture.

We are going to connect the PIR sensor (motion sensor) to the other free port of the ESP8266. The PIR sensor consist of 3 legs, shown in the following picture:

As you can see, there are tiny marking of +,0,- on the PCB next to the legs. So connect the “+” to 3.3V, “-” to the ground and the middle pin “0” to pin D6 of ESP8266.

To detect the motion we added the following code to our Arduino program:

void handlePIR(){
  DebugLn("handlePIR");
  motion = digitalRead(PIRpin) == HIGH;
  if (motion){
    digitalWrite(LED_BUILTIN, LOW);
    if (locklow){
      // make sure we wait for a transition to LOW before any further output
      locklow = false;
      Serial.print("motion detected at ");
      Serial.print(millis()/1000);
      Serial.println(" sec");
      delay(50);
    }
    takeLowTime = true;
 } else {
   digitalWrite(LED_BUILTIN, HIGH);
   if (takeLowTime){
     lowIn = millis(); // save the time of the transition from high to LOW
     takeLowTime = false; // make sure this is only done at the start of LOW phase
   }
   // if the sensor is low for more than the given pause, assume no more motion is going to happen
   if (!locklow && millis() - lowIn > pause){
   // make sure this block is executed again after new motion sequence had been detected
     locklow = true;
     Serial.print("motion ended at ");
     Serial.print((millis() - pause)/1000);
     Serial.print(" sec");
     delay(50);
   }
 }
 // post to webserver
 data = "pir_sensor=" + String(motion);
 postData(data);
}

The code above will light up the internal LED (Internal LED active LOW) when there is a motion being detected by the sensor.

In the next step we will connect the LED light to the circuit to allow us to control the light from the Internet.