Orange Pi 2G-IoT

From Wiki-Orange Pi
Revision as of 11:36, 28 June 2022 by Admin (talk | contribs) (Connect to Network via GSM)
Jump to: navigation, search

Orange Pi Introduction


What is Orange Pi 2G-IoT?


 It’s an open-source single-board computer. It can run Android 4.4, Ubuntu, Debian, Raspberry Pi image. It uses the RDA8810 Soc, and has 256MB LPDDR2 SDRAM.

What can I do with Orange Pi 2G-IoT?


You can use it to build... 

  • A computer
  • A wireless server
  • Games
  • Music and sounds
  • HD video
  • A speaker
  • Android
  • Scratch
  • Pretty much anything else, because Orange Pi 2G-IoT is open source.


Whom is it for?


 Orange Pi 2G-IoT is for anyone who wants to create with technology– not just consuming. It's a simple, fun, useful tool and you can use it to take control of the world around you.

Hardware specification


colspan=2 Hardware specification
CPU ARM Cortex-A5 32bit
GPU

Separate graphic processor, Vivante's GC860
support OpenGLES1.1/2.0
support OpenVG1.4
support DirectFB
support GDI/DirecShow
30M Triangle/s, 250M Pixel/s

Memory (SDRAM) Integrated 256MB LPDDR2 SDRAM
Onboard Storage TF card / Integrated 500MB 8Bit 1.8V 4K SLC Nand Flash
Onboard WIFI+BT RDA5991, WIFI+BT
2G model

The four frequency single card
GSM/GPRS Dedicated accelerators
SIM card

Video Input

A CSI input connector Camera:
Supports 8-bit YUV422 CMOS sensor interface
Supports CCIR656 protocol for NTSC and PAL
Supports SM pixel camera sensor
Supports video capture solution up to 1080p@30fps

Audio Input MIC, 3.5 mm Jack
Video Outputs LCD
Audio Output 3.5 mm Jack、 FM、SPEAK(Optional)
Power Source

USB OTG input can supply power
Battery input can supply power(Optional)

USB 2.0 Ports One USB 2.0 HOST, One USB 2.0 OTG
Buttons Power Button(SW602)
Low-level peripherals 40 Pins Header, compatible with Raspberry Pi B+
GPIO(1x3) pin UART, ground.
LED Power led
Supported OS Android, Ubuntu, Debian, Rasbian
cospan=2 Interface definition
Product size 67mm × 42mm
Weight 35g
colspan=2 Orange Pi™ is a trademark of the Shenzhen Xunlong Software CO., Limited


Top view

Orange-pi-2g-iot-img1.png


Bottom view

Orange-pi-2g-iot-img2.png


Interface instructions:

Orange-pi-2g-iot-img3.png


Orange-pi-2g-iot-img4.png


GPIO Specifications


 The CSI Camera Connector is a 24-pin FPC connector which can connect external camera module with proper signal pin mappings. The pin of CIS connector can be defined as follows. The connector marked with "CON 1" on the Orange Pi 2G-IOT is camera connector.

Orange-pi-i96-img3.png


OrangePi 2G-IOT-CSI

CON1-P01 NC
CON1-P02 GND
CON1-P03 TWI2-SDA PE13
CON1-P04 VCC-CSI
CON1-P05 TWI2-SCK PE12
CON1-P06 CSI-RESET# PE15
CON1-P07 CSI-VSYNC PE3
CON1-P08 CSI-STBY-EN PE15
CON1-P09 CSI-HSYNC PE2
CON1-P10 VDD1V8-CSI
CON1-P11 VCC-CSI
CON1-P12 CSI-D7 PE11
CON1-P13 CSI-MCLK PE1
CON1-P14 CSI-D6 PE10
CON1-P15 GND
CON1-P16 CSI-D5 PE9
CON1-P17 CSI-PCLK PE0
CON1-P18 CSI-D4 PE8
CON1-P19 CSI-D0 PE4
CON1-P20 CSI-D3 PE7
CON1-P21 CSI-D1 PE5
CON1-P22 CSI-D2 PE6
CON1-P23 GND
CON1-P24 AFVCC-CSI


Using Method


 You can configure your Orange Pi in a very short period of time and use it according to the following steps. You need to fulfill the several steps before booting your Orange Pi.

Step 1: Prepare Accessories Needed


The first time you use the Orange Pi, you need at least some parts for the following:

No. Items Requirements and Instructions
1 TF card 8GB ; class 10 (for now it only supports 8GB SD card).Branded TF cards which are much more reliable are the good choice
2 Power adapter At lease 5V/2A high quality power adapter, OTG could use as power supply.
3 Keyboard and mouse Any keyboard and mouse with USB port is applicable; Keyboard and mouse are high-power, so a USB concentrator is required.
4 TTL to USB cable Support debug log in.
5 Audio cable (Optional) You can select an audio cable with 3.5mm jack to feel stereo audio.
6 SIM Card (Optional) Support 2G SIM card


Orange-pi-i96-img4.png

TF card

Orange-pi-i96-img5.png

OTG power adapter


Step 2: Prepare a TF Card


 In order to be able to us Orange Pi normally, you must first install the operating system into the TF card or Nand. The following instructions will teach you how to write the operating system image file to the Windows and Linux Platform. For now this board could support boot from TF card with Android and Linux distro, and could support boot from Nand with Android. It will illustrate about how to write image into Nand.

1)Writing image into a SD card on Windows:
a.Inserting the TF card into the computer, the capacity of the card must be larger than the operating system image, usually requires 8GB or bigger capacity.
b.Formatting the TF card.

i.Download tools for formatting TF card, such as TF Formatter, could be download from
https://www.sdcard.org/downloads/formatter_4/eula_windows/
ii.Unzip the downloaded files, and run setup.exe
iii.In the options settings option set the format type option to quick formatting. Logical size adjustment option to open "(ON)”

Orange-pi-i96-img6.png


Orange-pi-i96-img7.png


iv.Make sure the inserted TF card codes are in accordance with the chosen codes.
v.Click the "Format"button.

Download the operating system image file from the download page, the page address is as follows:
http://www.orangepi.cn/downloadresourcescn/
d.Unzip the downloaded file (in addition to the Android system, this method can be used to burn to write, the Android system need another burn, the following will introduce).
e.Right click the downloaded file, select "Unzip file" to write image to TF card.

i Download tools to write image, such as Win32 Diskimager,
http://sourceforge.net/projects/win32diskimager/files/Archive/.
ii Select the path of image file that has been unzipped.

Orange-pi-i96-img8.png


iii Click the "Write" button and wait for the image writing.
iv After the image is written, click the "Exit" button.


2)Writing image into a SD card on Linux:

a.Inserting the TF card into the computer, the capacity of the card must be larger than the operating system image, usually requires 8GB or bigger capacity.
b.Formatting the TF card.

i Run fdisk –l command to make sure TF disk.
ii Run umount /dev/sdxx to uninstall all partitions of TF Card.
iii Run sudo fdisk /dev/sdx command. Use director to delete all partitions of TF Card, and then us n command to add a new partition, finally use w command to save and exit.
iv Run sudo mkfs.vfat /dev/sdx1 command to format the TF card partition set up last step to FAT32 form(according to your TF card disk to replacex ). Or you could skip this step since command in Linux will format TF card automatic.

c.Download the image OS from download page:
http://www.orangepi.cn/`downloadresourcescn/
d.Unzip the downloaded file and right click it, select " Unzip file"
e.Write image into TF card

i Run sudo fdisk –l command to make sure the TF card disk
ii Make sure the image file hash key is the same as download page offered(optional) :
sha1sum [path]/[imagename]
Here will be output some number which should be same as the image page line of "SHA-1"
iii Run umount /dev/sdxx command to uninstall all partitions in TF Card
iv Run the command of sudo dd bs=4M if=[path]/[imagename] of=/dev/sdx to write image file and wait for it finished. You can run sudo pkill –USR1 –n –x dd command to check the procedure.


Step 3: Start your Orange Pi


  • Insert the TF card with written image into the TF card slot
Orange-pi-2g-iot-img5.png


  • Make sure the toggle switch is showing like the following, booting from SD card.
Orange-pi-2g-iot-img6.png


  • Insert the keyboard or mouse into the USB port.
  • Connect wifi antenna and base-band antenna
Orange-pi-2g-iot-img7.png


  • Connect LCD and Camera
Orange-pi-2g-iot-img8.png


Orange-pi-2g-iot-img9.png


Orange-pi-2g-iot-img10.png


  • Connect TTL cable, you could refer to the Debug method in this instruction.

Android and Linux use different Baud rate, please note the Baud rate setting. Android Baud rate is 921600, Linux Baud rate is 921600
Serial port uses TTL to USB cable to connect.

Orange-pi-2g-iot-img11.png


  • It is the power input interface on the right side for connecting a 5V and at least 2A or bigger than 2A power adapter. Avoid using smaller power GSM mobile phone charger, it is not able to output 2A even if it marked "5V/2A".
Orange-pi-2g-iot-img12.png


 If the above steps are successful, the OrangePi will start in a few minutes. The monitor Graphical interface of display system. It may take a long time to start the first time, please wait patiently. The next time will boot very fast.

Step 4: Turn off your Orange Pi correctly


 You can use the shutdown button on the interface to safety close the Orange Pi. You can also close the system by entering command in the shell:
sudo halt
or
sudo shutdown –h
 It will be safety to turn off the Orange Pi. If directly use the power button to shut down the system may damage the file system on TF Card. After the system is closed, the power can be cut off by more than 5 seconds’ press. If all the above steps run, then your Orange Pi could shut down.

Initialize settings for your Linux system


 You need to make some basic settings when it is you first time to use Linux on Orange Pi 2G-IOT, like wifi setting, audio setting, user setting.

1)Wifi setting on serial port

 In the use of serial login system, enter the login password the system will prompt you to use the OrangePi_Settings tool to make some basic setting, including wifi setting. You could use the following command in the order line:
sudo orangepi_config
> wifi settings

 This setting include the functions of WIFI statue setting, wifi searching and connect to AP. You could use this method to set wifi.

2)Use ssh to connect wifi

 You need to use two cellphones if you want to use this function. Please refer to this:
 Orange Pi 2G-IOT is defaulted to connect the hotspot of orangepi, the password is orangepi. Use another cellphone’s hotspot function, setting the hot spot name as orangepi, password as orangepi. It will connect to orangepi hotspot default after booting the system. After that, use another cellphone to connect the hotspot, and use “wifi assistant” to check the IP of Orange Pi 2G-IOT.
 After getting the IP of Orange Pi 2G-IOT, you could use SSH remote login in Linux PC or Windows PC. Command as following:
 ssh orangepi@192.168.xxx.xxx
 Password: orangepi
 After enter the system via ssh, run the following command to connect to router: sudo orangepi_config

Write Android into Nand


 Orange Pi 2G-IOT is supported boot from Nand, and also supported update Android in Nand.

1)Boot Android from NAND

 Switching the boot mode into NAND via short jumper cap.

Orange-pi-2g-iot-img13.png


 Power it on, Orange Pi 2G-IOT will boot from NAND.

2)Update Android in NAND

  • Short jumper cap to switch the system to boot from NAND, set toggle switch into 1234 UP, 5678 Down like the following:


Orange-pi-2g-iot-img14.png


3)Install writing tool on Windows

 For now Nand writing tool could only support working on Windows, you could download the tool from official website: http://www.orangepi.org/downloadresources/

Orange-pi-i96-img18.png


4)Install USB driver on Windows
 Unzip the tool file, install the USB driver, here is the path:

  • /OrangePi_2G-IOT_Toolschain/USB_Driver/USB-driver/

 You should install it according to your PC, if your PC is 32bit, then install x86 USB driver, if it is 64bit, then is x64 USB driver.

5)Download Android Nand image  Here is the ink for Orange Pi 2G-IOT Nand version image:
 http://www.orangepi.org/downloadresources/

Orange-pi-i96-img19.png


6)Use writing tool

Use writing tool to write NAND:

  • /OrangePi_2G-IOT_Toolschain/OrangePi_2G-IOT_NandUpdate_Tools/OrangePi_2G-IOT_Update.exe.
Orange-pi-i96-img20.png


 Click “load BIN” to import the image of NAND version into writing tool. After that, click Download button to download the image. Meanwhile, the tool is waiting for the download link of Orange Pi 2G-IOT.

7)Download Image

 Prepare an Android USB to DC cable, first connect to the OTG port of Orange Pi 2G-IOT, then push on the power button for 5s, and connect the cable to the Windows PC. Meanwhile, the screen will indicate that connect successful and downloading. It will take around 3min to finished downloaded, after that, reboot the system and then the system will run on the update Android.
 Note: If it could not download, please check the the shorting cap and switch.

Orange-pi-i96-img21.png


Android in no screen ADB mode


  • ADB setting: Set the toggle switch into 1234 “UP”, 5678 “Down”, the system will switch into adb model, in this model, the USB is unable.
Orange-pi-2g-iot-img16.png


  • Connect to the OTG port of Oragne Pi 2G-IOT with the USB to DC cable, the other side connect to PC, push the power button and then the system will be Android.
  • If the PC haven’t set on adb, then please refer to the teaching method of Ubuntu and Windows adb in internet. Use adb command in the PC terminal to connect the adb:adb shell
  • After connect to OrangePi 2G-IOT via adb, you could refer to the adb debug method from the internet to enter into Orange Pi 2G-IOT

We would recommend you use Plug-in Vysor in Chrome browser, this tool could enter Android via adb:

Orange-pi-i96-img23.png


Orange-pi-i96-img24.png


Universal software configuration


1)Change default account

 The default log-in account and password is orangepi/orangepi or root/orangepi. It is recommended to modify the default orangepi account to your own account for secure sake. Take changing into Zhangsan as a sample. Steps are as follows:
a.Use root account to login Orange Pi
b.$ usermod -l zhangsan orangepi 
Change account of orangepi into Zhangsan

Orange-pi-i96-img25.png


c.$ groupmod -n zhangsan orangepi 
    Change group

Orange-pi-i96-img26.png


d.$ mv /home/ornagepi  /home/zhangsan
  Change directory of original orangepi

Orange-pi-i96-img27.png


e.$ usermod -d /home/orangepi  orangepi 
Set this directory into orangepi user's home directory

Orange-pi-i96-img28.png


f.$ cat /etc/passwd
It should be shown as following:

Orange-pi-i96-img29.png


 After the modification of the above steps, you could use the new account Zhangsan to log in.

2)System source configuration

 This instruction will take Ubuntu as an example:
a.Open the source file
$ sudo vi /etc/apt/sources.list

Orange-pi-i96-img30.png


b.Edit source file
 Replace the source file with your favourite source. Take an example of Ubuntu 16.04 on Zhonkeda source:
 deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
 deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
 deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-proposed main multiverse restricted universe
 deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
 deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
 deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
 deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
 deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-proposed main multiverse restricted universe
 deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
 deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
Note: xenial is the version of the code name in this source, if the other version of Ubuntu needs to replace the corresponding version code which can be found on the internet.

3)Enter the system via SSH

 You could refer to the previous charter 5. 2)Use SSH to connect Wifi.

4)Modify the size of ext4 file system

 It could promote system performance via expanding the rootfs partitions of file system after writing image, which could avoid the problems caused by insufficient space.
 Expanding rootfs partitions on TF card of PC:
 Using GParted to adjust the size:
 Select the specified letter, right-click the corresponding letter, select "change the size" to adjust into the desired size, click "adjust the size", close the dialog box and click "apply to all operations", select the "apply" to complete the expansion operation.

a.Expand file system
 i Boot to Linux, umount /dev/sdb1 and /dev/sdb2, if it prompts disk busy, then use fuser to clean the using disk(we will recommend using another Linux booting disk to lead the system).
 ii Use fdisk /dev/sdb to adjust the partition size, after into it, enter p, and keep in mind about the initial position of needed extending size partition.
 iii Enter d to delete the partition need to change the size(my file system is /dev/sdb2, which is the 2 partition ).
 iv Enter n to build a new partition, make sure the initial position is the same as you deleted, and enter the number as you desire.
 v Enter w to save the partition data.
 vi Use the following command to check the file system(make sure it is a right file system)
  e2fsck -f /dev/sdb2
 vii Adjust the partition size
  resize2fs /dev/sdb2
 viii It could mount a disk partition, you could check whether it has changed.

b.Shrink file system
 i Boot to Linux, umount /dev/sdb1 and /dev/sdb2, if it prompts disk busy, then use fuser to clean the using disk(we will recommend using another Linux booting disk to lead the system).
 ii Use the following command to check the file system(make sure it is a right file system)
  e2fsck -f /dev/sdb2
 iii Modify the size of file system(Use resize2fs)
  resize2fs /dev/sdb2 900M
 The "s"after the number represents specifying the size of file system via the sectors(every sector calculated by 512 bite). You could also specify it into K(KB), M(MB), G(GB), etc.
 iv Use fdisk /dev/sdb to adjust the partition size, after into it, enter p, and keep in mind about the initial position of needed extending size partition. You need to first delete the partition then build a new one because the fdisk could not modify the size dynamic(you need to calculate the size, it have to enough to contain the file system adjusted in last step).
v Enter d to delete the partition need to change the size(my file system is /dev/sdb2, which is the 2 partition ).
 vi Enter n to build a new partition, make sure the initial position is the same as you deleted, and enter the number as you desire. Besides, if it is boot-able partition you want to change, note that need to keep the boot-able mark in case cannot boot.
 The above illustration is using fdisk and resize2fs to modify partition and file system, you could also use gparted. Gparted has graphical interface and it could help you to re-size file system at the same time of re-sizing partition. Goarted is much easier to use and reduce the change to make mistake. For now our official Lubuntu and Raspbian could not use it.

Usage of GPIO and WiringPi


1)Use WiringPi on OrangePi 2G-IOT to connect network

Orange-pi-2g-iot-img17.png


OrangePi 2G-IOT could support WiringPi, you could have a try according to the introduction on this section. The following is 40 Pin of OrangePi.

a.Download the latest WiringPi source code of Orange Pi 2G-IOT
Please confirm that the Orange Pi 2G-IOT has connect to wifi or network successfully, if no, then you need to first make it connect to wifi or network. If you connect to network, then you also need to install some essential tools:

sudo apt-get install git gcc make

b.Download the latest source code
You could download the latest WiringPi source code from official website: www.orangepi.org
You could also download files from github: https://github.com/OrangePiLibra/WiringPi.git with the following command:
env GIT_SSL_NO_VERIFY=true git clone
c.Compile and install WiringPi
Use the following command to compile and install the WiringPi after get the latest source code.
cd WiringOP/
./build OrangePi_2G-IOT 
./build OrangePi_2G-IOT install
d.Test WiringPi with gpio command
You could use gpio command to test GPIO on 40pin on Orange Pi 2G-IOT with WiringPi installed.
i Use "gpio readall" command to print out all WiringPi pin mapping as following.BCM line represents the actual hardware GPIO, there are 4 Groups GPIO and each Group have 32pins, and the serial number start from Group PA. PA0 corresponds to BCM colum number 0.
wPi line represents pins of wiringPi, you could use this group of data when use C library and gpio command on wiringPi. For example, number 37 pin is corresponding to number 25 pin of wiringPi, you could operate the 37 via operating 25pin.
Name line represents the definition name of Pin.
Mode line represents the mode of pin, it could but both input and output.
V line represents the voltage value of the current pin.
Physical line represents the actual hardware number.

Orange-pi-i96-img32.png


ii Use "gpio export pin mode" to explore wiringPi GPIO to the directory of /sys/class/gpio and set the GPIO mode into mode.
According to the above WiringPi pin mapping, hardware pin number 37 is corresponding to WiringPi pin number GPIO 25, explore the number 25 and set it into output mode.

Orange-pi-2g-iot-img18.png


Orange-pi-2g-iot-img19.png


iii Use "gpio unexport pin" to cancel explore pin to /sys/class/gpio. For example: orangepi# gpio unexport 25

Orange-pi-2g-iot-img20.png


iv Use "gpio exports" to check the current explored gpio. For expample: orangepi# gpio exports

Orange-pi-2g-iot-img21.png


v Use "gpio mode pin mode" command to configure wiringPi pin mode. For example:
Configure pin wiringPi 25 as output mode
orangepi# gpio mode 25 out
Configure pin wiringPi 26 as input mode
orangepi# gpio mode 26 in

vi.Use "gpio write pin value" to write value of output mode pin, for example:
Configure pin wiringPi 25 as output pin:
orangepi# gpio mode 25 out
Write 0 on wiringPi 25
orangepi# gpio write 25 0
Write 1 wiringPi 25
orangepi# gpio write 25 1

vii.Use "gpio read pin" command to read the value of input mode pin, for example:
Configure pin wiringPi 25 as input pin
orangepi# gpio mode 25 in
read the value from wiringPi 25
orangepi# gpio read 25

Orange-pi-i96-img33.png


viii.If you want to learn more "gpio" command, you could refer to "gpio -h" obtain.

e.Use WiringPi C Library

WiringPi support C library and python library, you could use C language to operation GPIO port, the example source code is on the directory of /example/OrangePi/.
Here is an example for C library usage on GPIO:
Complie GPIO LED

Orange-pi-i96-img34.png


Usage of C library on wiringPi:
In order to use wiringPi C library, first you need to import file of "wiringPi.h". You need to initialize wiringPi before using GPIO with function wiringPiSetup(). And then you could configure pin mode into INPUT or OUTPUT. And please note that the pin number should corresponding to wiringPi. Finally you could use function digitalWrite() and digitalRead() to read and write.

Connect to Network via GSM


1)Use 2G-IOT to send message

For now the version of Ubuntu,Debian and Raspbian could support SMS sending message. Before using the SMS function, please make sure the SIM card you use is active.

i.Prepare

First you need to prepare an activated Micro SIM card and insert into Orange Pi 2G-IOT, the Orange PI 2G-IOT could support the following frequency:
Frequency Range: 850,900,1800,1900
Standard: GSM800

Orange-pi-2g-iot-img23.png


ii.Install SIM card

Please note the direction of when inserting SIM card.

Orange-pi-2g-iot-img25.png


iii.Login Linux system

After inserting SIM card, power on and enter into Linux system. You could login the system via serial port or SSH. If you are going to use serial port to login, please be remember to set the baud rate into 921600.

Orange-pi-2g-iot-img26.png


iv.SMS Usage

There are many ways to use SMS, here will introduce the way with serial port and C language.

a.With Serial Port

You need to install the tool of minicom on Orange Pi 2G-IOT before using SMS. Please make sure the board has already connect to wifi.
You could install minicom with the following command:

sudo apt-get install minico


The AP core and Modem will communicate via serial port. There will be a modem0 node after booting into Linux system. You could use serial port tool to connect to the /dev/modem0 node, which we recommend you use minicom, you could also try other tools.
If you are using the minicom or other tools on serial port, please refer to the following: You could configure with the following command to connect /dev/modem0:

sudo minicmo -s


After input the command, you will enter the configure interface, select "Serial port setup"

Orange-pi-2g-iot-img27.png


Push A to modify Serial Device into /dev/modem0

Orange-pi-2g-iot-img28.png


Use AT command to send message

First you need to input AT command to check the situation of Modem, if Modem replies OK, then means Modem is connect correctly.

Orange-pi-2g-iot-img29.png


Input command of AT +cfun=1 to open Modem full function

Orange-pi-2g-iot-img30.png


Orange-pi-2g-iot-img31.png


Orange-pi-2g-iot-img32.png


Orange-pi-2g-iot-img33.png


Initialize serial port:

Orange-pi-2g-iot-img34.png


Send AT command function

Orange-pi-2g-iot-img35.png


AT send message

Orange-pi-2g-iot-img36.png


Detail steps you could refer to the following: “OrangePi_2G_IOT_GSM_Demo.c
Here is the reflect of running C:

Orange-pi-2g-iot-img37.png


Orange-pi-2g-iot-img41.png


Orange-pi-2g-iot-img38.png


Orange-pi-2g-iot-img42.png


Orange-pi-2g-iot-img39.png


Finally you will receive the message from OrangePi 2G-IOT

Orange-pi-2g-iot-img40.png


  • OrangePi_2G_IOT_GSM_Demo.c


/*
* OrangePi 2G-IOT GSM Demo
*  (C) Copyright 2017 OrangePi
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>

#define NR_CITY  30
#define MODEM_PATH  "/dev/modem0"
#define VERSION     "0.1.0"

struct Centry_number {
    char *city;
    char *number;
} City_Number[NR_CITY] = {
    { "ShenZhen",           "13010888500" },
    { "Beijing",            "13010112500" },
    { "Shanghai",           "13010314500" },
    { "Shandong",           "13010171500" },
    { "Jiangsu" ,           "13010341500" },
    { "Zhejiang",           "13010360500" },
    { "Fujian",             "13010380500" },
    { "Sichuan",            "13010811500" },
    { "Chongqing",          "13010831500" },
    { "Hainan" ,            "13010501500" },
    { "Heilongjiang",       "13010980500" },
    { "Jilin",              "13010911500" },
    { "Tianjin",            "13010130500" },
    { "Hebei",              "13010180500" },
    { "Inner Mongolia",     "13010950500" },
    { "Shanxi",             "13010701500" },
    { "Anhui",              "13010305500" },
    { "Xinjiang",           "13010969500" },
    { "Qinghai",            "13010776500" },
    { "Gansu",              "13010879500" },
    { "Ningxia",            "13010796500" },
    { "Guizhou",            "13010788500" },
    { "Yunnan",             "13010868500" },
    { "Hunan",              "13010731500" },
    { "Hubei",              "13010710500" },
    { "Guangdong",          "13010200500" },
    { "Guangxi",            "13010591500" },
    { "Henan",              "13010761500" },
    { "Jiangxi",            "13010720500" },
    { "Liaoning",           "13010240500"},
};

/*
*Initialize serial
*/
void serial_init(int fd)
{
    struct termios options;

    tcgetattr(fd, &options);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~CSIZE;
    options.c_cflag &= ~CRTSCTS;
    options.c_cflag |= CS8;
    options.c_cflag &= ~CSTOPB;
    options.c_iflag |= IGNPAR;
    options.c_oflag = 0;
    options.c_lflag = 0;
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    tcsetattr(fd, TCSANOW, &options);
}

void display_message(int direction, const char *message)
{
    if (direction) {
        printf("Send Message ------> %s\n", MODEM_PATH);
        printf(">> %s\n", message);
    } else {
        printf("Rece Message <------ %s\n", MODEM_PATH);
        printf("<< %s\n", message);
    }
}

void Send_AT(int fd, const char *str1, const char *str2, const char *str3)
{
    char buff[128];
    char answer[128];

    memset(buff, 0, sizeof(buff));
    if (str1 != NULL)
        strcpy(buff, str1);
    if (str2 != NULL)
        strcat(buff, str2);
    if (str3 != NULL)
        strcat(buff, str3);
    write(fd, buff, strlen(buff));
    display_message(1, buff);

    memset(answer, 0, sizeof(answer));
    sleep(1);
    read(fd, answer, sizeof(answer));
    display_message(0, answer);

}

int send(int fd, char *cmgf, char *cmgs, char *csca, char *message)
{
    /* AT Test */
    Send_AT(fd, "AT\r", NULL, NULL);
    /* Set Modem Full Function */
    Send_AT(fd, "AT +CFUN=", "1", "\r");
    /* Set CMGF */
    Send_AT(fd, "AT +CMGF=", cmgf, "\r");
    /* Set Message Centr Number */
    Send_AT(fd, "AT +CSCA=", csca, "\r");
    /* Set Receive Number */
    Send_AT(fd, "AT +CMGS=", cmgs, "\r");
    /* Send Message */
    Send_AT(fd, message, NULL, NULL);
}

int Send_Message(int fd)
{
    char buff[128];
    char num1[64];
    char num2[64];
    int i;
    int choice;

    printf("********* City Select **********\n");
    for (i = 0; i < NR_CITY; i++)
        printf("[%2d] %s\n", i, City_Number[i].city);
    printf("Please select your City!\n");
    scanf("%d", &choice);
    do {
        memset(num1, 0, sizeof(num1));
        printf("\nPlease Entry Receive phone number:\n");
        scanf("%s", num1);
    } while (strlen(num1) != 11);

    sleep(1);
    memset(buff, 0, sizeof(buff));
    printf("Please input Meesage:\n");
    scanf("%s", buff);

    /* Restruct buff */
    i = strlen(buff);
    buff[i] = 0x1A;
    buff[i+1] = '\r';
    buff[i+2] = '\0';

    memset(num2, 0, sizeof(num2));
    strcpy(num2, "+86");
    strcat(num2, num1);

    memset(num1, 0, sizeof(num1));
    strcpy(num1, "+86");
    strcat(num1, City_Number[choice].number);

    send(fd, "1", num2, num1, buff);
}

/*
* Call Phone.
*/
void Call_Phone(int fd)
{
    char buff[128];
    char number[20];

    do {
        memset(number, 0, sizeof(number));
        printf("\nPlease input phone number:");
        scanf("%s", number);
    } while (strlen(number) != 11);

    memset(buff, 0, sizeof(buff));
    strcpy(buff, "+86");
    strcat(buff, number);
    strcat(buff, ";");

    /* AT Test */
    Send_AT(fd, "AT\r", NULL, NULL);
    /* Call */
    Send_AT(fd, "AT", " DT ", buff);
}

int main(int argc, char *argv[])
{
    int fd;
    char choice;

    fd = open(MODEM_PATH, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd < 0) {
        printf("Can't open %s\n", MODEM_PATH);
        return -1;
    }

    /* Initialize /dev/modem0 */
    serial_init(fd);

    printf("************************************************\n");
    printf("\tWelcome to OrangePi 2G-IOT\n");
    printf("\tModem version %s\n", VERSION);
    printf("************************************************\n");
    printf("Entry your select:\n");
    printf("1. Send Message\n");
    printf("2. Call Phone\n");
    printf("3. Exit\n");
    choice = getchar();

    switch (choice) {
    case '1':
            Send_Message(fd);
            break;
    case '2':
            Call_Phone(fd);
            break;
    default:
            break;

    }
    close(fd);

    return 0;
}