Nov 21, 2016

Digital Clock with DS3231, TM1637 and STM32F103C8

It is really simple to do. Only 4 wires for DS3231 (the real time clock), 4 wires for TM1637 (in this case is 4 digit display), 2 wires for STM32F103C8 (32-bit microcontroller).
DS3231 wires are: VCC, GND, SCL (goes to PA.7), SDA (goes to PA.6)
TM1637 wires are: VCC, GND, CLK (goes to PA.0), DIO (goes to PA.1)
STM32F103C8 wires are: VCC and GND.

DS3231 equipped with 3V battery to keep RTC keep counting the time when power off. There's no push buttons for time setup yet, time setup is done in the software. After running program with time setup, give comment sign (//) to the time setup line and reload program. You can get the code here, download to the microcontroller using ST-LINK2.

Aug 2, 2016

4-Digit Seven Segment Display with TM1637

TM1367 is a LED drive and keyboard scan controller but in this application I use it for driving 4 digit seven segment display. There are 4 wires connecting the display and the mcu:
Display - MCU STM32F103C8
VCC - 5V
DIO - Port A1
CLK - Port A0

It communicate with I2C protocol that's why the code has following procedures:
- TM1637Start(void);
- TM1637Stop(void);
- TM1637ReadAck(void);

The example code init the mcu, init the TM1367 and then display integer counting from 0-9999 repeatedly. Get the code here.

Jun 3, 2016

Raspberry Pi 3 Wifi IP Camera

There are many wifi IP cameras now available in the market with various features. I use Raspberry Pi 3 because it's a computer which is programmable, customize-able and fertile.

Hardware you need:
- Raspberry Pi 3 (RPi3 or simply Pi)
- 16GB or 32GB microSD card
- USB camera
- 5V 3A PSU
- The followings are for configuring only: HDMI monitor, USB mouse, USB keyboard , microSD card reader. It's called a reader but it can function as a writer too ;-)

Why do I use RPi3 instead of RPi2 dan RPi1? It's more powerful, faster with internal wifi and bluetooth interface, and the latest OS Raspberry Jessie is equipped with more drivers so I needn't install the drivers manually. Most of USB webcam can work with Pi including unbranded camera but you can read this for reference

I use Windows OS to do below tasks:
- Download Raspbian Jessie image from here and extract the zip.
- Write the image file to the microSD card using this guide

Insert microSD card into socket on the Pi. The socket is not with push-release mechanism anymore to prevent unintentional card release as we experienced on Pi1 and Pi2. Plug in webcam, mouse, keyboard, HDMI and lastly the 5V power supply to micro USB connector. Pi will start and show Raspbian desktop on HDMI display.
Open a terminal and configure the Pi by using command sudo raspi-config
- Expand Filesystem, this is to give more space of SD card space for user
- Change User Password (necessary if you like to manage your Pi from internet)
- Internationalization Options to correct locale, timezone, keyboard, etc.
- in Advanced Options, change hostname (default name is raspberrypi). This require restart. Restart using command shutdown -r now or sudo reboot.

Exit from raspi-config and configure the IP address by editing the file using command:
sudo nano /etc/network/interfaces
and edit the file to be like below. After editing, press ctrl-O, enter for saving, then ctrl-X for exit.

source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Static IP is necessary for port forwarding so the camera will be accessible from the internet. 
Then edit wifi access point setting using command:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
and make sure below text in the file.  After editing, press ctrl-O, enter, ctrl-X.
This setting also can be done graphically on desktop by clicking network icon on top-right of the screen.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

Reset the Pi by disconnecting and reconnecting the power supply and make sure it connects to your wifi access point with internet access.
Update the Pi using command: sudo apt-get update
Install motion using command: sudo apt-get install motion
Edit motion configuration by using command: sudo nano /etc/motion/motion.conf
In my case I change:
- daemon is on to enable it to run
- webcam localhost is off because I view it remotely
- the resolution of camera to 640x480
- frame rate is 5 fps to reduce recording space requirement
- use video device /dev/video0 
- use pallete YUV 17
- use recording format swf or mpeg4
- put some text on left-bottom and right-bottom
- text double size is on for bigger font
- keep the recording video in the directory /etc/motion/swf
- authentication for video streaming username:password
Other settings remain default.

Now edit daemon setting by using command: sudo nano /etc/default/motion
change the text from no to yes.
This is to ensure motion service running when Pi starts. Save and exit.

Run the service by using command: 
sudo service motion start
sudo service motion restart

Open the browser on your smartphone or PC within the same wifi network with Pi.
Open for testing the camera.
When everything is working, you can disconnect HDMI, mouse and keyboard connectors.

TO DO: (It's done on my project but I need time for typing it here).
- Port Forwarding to enable viewing the camera from the internet.
- Dynamic DNS to create fixed URL of the camera, for example
- Add terminal based program for turning on/off light through GPIO and view it directly on camera.
- Install vlc player for playing back the recorder motion.
- Install tightvncserver to view the desktop remotely.
- Install ftp server for remote copying the video files.

Feb 22, 2016

ESP8266 Serial Wifi Module

Let's move on, from bluetooth to wifi.
In my previous posts I always use bluetooth to open the door, now ESP8266 will give a breakthrough.

ESP8266 pinout is shown below.

Wire them as below:
RX to TX of your MCU or USB/serial of PC
VCC to 3.3V
Let GPIO 0 and GPIO 2 unconnected
Let RESET unconnected
CH_PD to VCC to enable the module
TX to RX of your MCU or USB/serial of PC
There are many warnings on the web that the module is not 5V tolerant, so use only 3.3V MCU or USB/serial 3.3V.

Serial port speed is 9600 bps (you can try 115200 if it doesn't work).
The module use AT command for communication protocol and use [CR] and [LF] for line ending.
To check the speed use command AT+CIOBAUD?[CR][LF]. [CR][LF] equivalent to \r\n in C.
I use the module in AP (Access Point) mode, meaning other PC or mobile phone can connect to it directly without wifi switch/router. The command is AT+CWMODE=2[CR][LF]
Then to enable multiple connection AT+CIPMUX=1[CR][LF]
To check the IP address of AP, use command AT+CIFSR[CR][LF] (example
To enabel listener on port 23, AT+CIPSERVER=1,23[CR][LF]
In Android mobile phone open TCP/UDP terminal application or Hercules terminal on PC, and connect to the IP above, connect and send something. It will be received by the module.
To send text from the module use command AT+CIPSEND=0,8[CR][LF]
0 is the channel and 8 is the length of the characters.
Then send 8 characters.

That's it. TCP and UDP difference in connection-oriented and connection-less
For detail sending and receiving format refer to the ESP8266 manual.

I wish I have time to create an Android Apps which can connect to a certain wifi AP programmatically and send password for open/lock something.