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
GND - GND
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 http://elinux.org/RPi_USB_Webcams

I use Windows OS to do below tasks:
- Download Raspbian Jessie image from here https://www.raspberrypi.org/downloads/raspbian/ and extract the zip.
- Write the image file to the microSD card using this guide https://www.raspberrypi.org/documentation/installation/installing-images/windows.md

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
address 192.168.1.101 
netmask 255.255.255.0
gateway 192.168.1.1
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
update_config=1
network={
        ssid="wifiname
        psk="wifipassword
        proto=RSN
        key_mgmt=WPA-PSK
        pairwise=CCMP
        auth_alg=OPEN
}

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.
start_motion_daemon=yes
This is to ensure motion service running when Pi starts. Save and exit.

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

Open the browser on your smartphone or PC within the same wifi network with Pi.
Open http://192.168.1.101:8081 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 http://homecam.duckdns.org:81
- 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
GND to GND
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 192.168.1.2)
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.

Nov 6, 2015

Multichannel 12-bit ADC with Bluetooth Link

This module read the voltage from 4 channels through gain adjustment trimmer potensiometer . Internally, ADC range is 0.0-3.3V, to make it possible to read 0.0-6.0V potentiometers are added. Then voltage input is filtered by 10nF capacitor and clamped by 3.3 V Zenner diode to protect the mcu from over voltage and reverse polarity voltage. Bluetooth module send the data serially to a remote PC (0-20m away) for data logging. Power supply module input can vary 12-24VDC and the output is stable 5.0 VDC. I need this module for monitoring a QCS (Quality Control System) sensor voltage stability. As it is scanning on paper web, I need wireless device to connect it to unmoving PC. You can use it for other purpose as well, data logging can be used for different voltage range or non-wireless communication.  Minor modification on hardware and software would fit your application.

Board











Schematic











Donate and get the code here.