Design of Smart home based on Linux (5)

Source: Internet
Author: User

4 Software Design

The software design part mainly includes uboot porting, kernel compiling, system porting, device driver programming, application programming (QT programming, MySQL database programming, control system programming), function function of each module (partially implemented in IAR under Windows).

The structure of the Software section is shown in diagram 4-1:


Figure 4-1 Software structure diagram

4.1 U-boot Transplant

[15] Since each operating system must run a small program before it runs, this is usually said to be bootloader, a firmware program similar to the Windows BIOS. [16] This program allows you to initialize a hardware device, establish a memory space map, and introduce the system to a suitable state so that it can eventually invoke the correct environment prepared by the operating system kernel. Bootloader's startup program flowchart 4-2 shows:


Figure 4-2 bootloader Start-Up program flowchart

① Download U-boot source code, to the site Http://www.denx.de/wiki/U-Boot download the required source code uboot1.1.6-2012-09-25.tar.gz
② Build and ARM board-compatible types
Modify the top-level makefile project file, as shown in path 4-3 of the modified compiler:
 

Figure 4-3  the path to the compiler

③ Modify related parameters 
 parameters include the relevant files of the target board, and generic code independent of the processor architecture, such as memory size detection and fault detection, processor-related files, device drivers, and so on. 
④ compile and test 
 run #make Smdk6410_config and # Make command 4-4 shows 
 
 

Diagram 4-4  run #make The Smdk6410_config and #make commands

 compile for a long time, the last generated U-boot file, and use the file U-boot command to view the files, Determine if the u-boot that the arm platform can run is generated. 
 
 

Figure 4-5  View U-boot file properties

 Copy the generated u-boot file to the SD card, and then the board starts with the SD card and enters the Putty console setting u-boot environment variables: 

Diagram 4-6  setting u-boot environment variables

Pre style= "Text-align:left" > here to detect the ability to ping a virtual machine to proceed to the next step:
 using ping 192.168.12.18 

Figure 4-7 Detecting the ability to ping a virtual machine

4.2 Kernel cropping

Although the Linux kernel supports a variety of hardware platforms, there are some features that are not necessary for an embedded device, so there are some clipping [17]. For this platform need to use is touch screen, Gpio port, Ethernet, serial port, camera, Nandflash these several main drivers. It is enough to cut down what is used. Here is the basic idea of kernel clipping:

① download and unzip the source package, this topic uses the Linux-3.0.1 version of the source code;

② Configure the build platform to modify makefile, primarily the definition of architecture and cross-compilation variables. 4-8 is shown below:

Figure modification Makefile

③ clipping kernel

Execute command:

#make Menuconfig

Figure 4-10 Kernel Clipping interface

Go to device drivers and configure the information you want. The spaces represent that there is no need to compile, "*" means that it must be compiled, and "M" is an option that can be loaded. Execute make zimage to create the kernel image, and the resulting kernel image is in the arch/arm/boot/directory:

Burn the kernel image to Nandflash via the TFTP service.

4.3 Making of File system

Use the tools provided by the OK6410 Development command:

#./mkyaffs2image-nand256m FILESYSTEM-YAFFS2 ROOTFS.YAFFS2

Write to Nandflash via the TFTP service file system.

4.4 QT Program Design

The QT program design of this topic has two parts, one is the remote client running on the PC, one is the QT programs running on the touch screen of the Development Board. The QT Development Framework 4-12 shows:

Embedded QT applications are developed from development to application-capable process 4-13:


4.4.1 Interface Design

OK6410 Board programming, the first to do a UI interface, which is used in the Qwidget class design. The Qwidget class is the base class for all user-interface objects. The widget is an atom of the user interface: he accepts the mouse, keyboard, and other events (touch) from the window system, and draws his own performance on the screen [18]. Each widget is a rectangle, and the widget can be covered by other parent windows or partially covered by the front part. Using Qt-disgner for layout design, you will get the desired interface effect, the system is running on the Development Board GUI interface 4-14 is shown.

Figure 4-14 GUI interface

4.4.2 Signal and slot

The signal and slot mechanism is the core mechanism of QT, a high-level interface between signals and slots, used for communication between objects [19]. When the object changes, the signal is emitted by the object (emit) out, directing the object to do all the things, by invoking the qobject object's connect function to associate the signal of an object with the slot function of another object, so that when the emitter emits the signal, the receiver's slot function is called . The function is defined as follows:

BOOL Qobject::connect (Constqobject * sender, const char * signal, const QOBJECT * receiver, const char *member)

The light bulb and temperature and humidity in this subject is triggered by this mechanism, when an event on the touchscreen is pressed, a signal is sent, and the function of the bulb is called. The signal and groove mechanism is shown in flowchart 4-15:


Some of the signals used are as follows:

Signals:

void Islight (bool states); Lamp control Signal

void settempsignal (int temp); Temperature signal

void sethumsignal (int hum); Humidity signal

The slot functions used are as follows:

Private Slots:

void on_btnled4_clicked ();

void on_btnled3_clicked ();

Voidon_btnled2_clicked ();

void on_btnled1_clicked ();

void Readmycom ();

void All_led_on_writecom ();

void All_led_off_writecom ();

void Temphum_writecom ();

void Setlightstates (bool states);

void Settempslot (int temp);

void Sethumslot (int hum);

The connection function of the signal and slot is as follows:

Mainwin::mainwin (qwidget*parent):

Qwidget (parent),

UI (New Ui::mainwin)

{

UI->SETUPUI (this);

Startinit ();

......

Connect (Temptimer, SIGNAL (timeout ()), This,slot (temphum_writecom ()));

Connect (this,signal (islight (BOOL)), This,slot (Setlightstates (bool)));

Connect (this,signal (settempsignal (int)), This,slot (settempslot (int)));

Connect (this,signal (sethumsignal (int)), This,slot (sethumslot (int)));

......

}

4.5 Control design of intelligent electrical apparatus

The subject of indoor air conditioning as an intelligent electrical appliances, set a temperature threshold, when the temperature reached this threshold when the start of air conditioning, such as the general summer indoor temperature of 25 degrees Celsius, if the temperature exceeds 25 degrees Celsius, then the air conditioning will automatically open. By periodically obtaining the current temperature in the room, and to compare the received information with the set value, when the temperature reaches the appropriate temperature, the automatic stop, this can save electricity. The data processing here is DHT11 get the temperature in the environment, send to the OK6410 Development Board, compare with the set temperature, if the temperature exceeds the threshold, then send the corresponding instruction. At the same time, the interface of the touch screen can see the corresponding state of air conditioning. Figure 4-16 is a flowchart for temperature control.


Programming of 4.5.1 Intelligent Home ZigBee Gateway

Because the arm gateway and the ZigBee coordinator is based on RS232 serial communication, so in order to solve the problem of serial communication, this topic uses the baud rate is 115200, the data bit is 8, the stop bit is 1, the data bit is not verified. When it comes to data communication, you have to design a frame format for the frame, which is set to: Frame header + Node ID code + module ID code + data (COMMANDH + Commandl) + frame end.

A detailed description of this data frame can be described using a DHT11 temperature and humidity sensor, such as a temperature data frame:

0xcc,0xee,0x01,0x03,0x01,0x00,0x00,0xff


Module ID is used to distinguish the module from the lamp module, temperature and humidity module, motor module and so on. This data is only sent to the command, then accept the data frame format is somewhat different from the frame header, the data is 0xEE 0xCC For example received a data frame:

0xee,0xcc,0x01,0x03,0x01,0x01,0x01,0xff


QT Serial programming uses the library function programming, using the Qextserialport class, the Qextserialport class reads the serial data the way has two kinds: polling is the polling way, EventDriven refers to the event-driven mode [21]. However, in the Linux system can only use the polling mode, in this way the serial read/write is synchronous, the signal in this state is not available. The advantage of this approach is that the system overhead is small, but you need to define a timer to read the serial data.

Using Qextserialport to realize serial communication, the configuration of serial data communication can be embodied in the following code.

void Mainwin::opencom () {    mycom = new Posix_qextserialport (COM, qextserialbase::P olling);    Here Qextserialbase::querymode should use Qextserialbase::P olling    isOpen = Mycom->open (qiodevice::readwrite);    if (isOpen) {       //qmessagebox::information (this, TR ("open successfully"), TR ("successfully opened serial port") + COM, Qmessagebox::ok);} else{        qmessagebox::critical (this, tr ("Open Failed"), TR ("Failed to open serial port") + COM + TR ("\ n the serial device does not exist or has been occupied"), Qmessagebox::ok);        return;    }    Set baud rate: 115200    mycom->setbaudrate (BAUD115200);    Set Data bits: 8-bit    mycom->setdatabits (data_8);    Set checksum: No    mycom->setparity (par_none);    Set Stop bit    mycom->setstopbits (stop_1);    Turn on the read Timer    timer->start (timerdly);    Set the data flow control    Mycom->setflowcontrol (Flow_off);    Set delay    mycom->settimeout (time_out);}

Design of 4.5.2 ZigBee

ZigBee's program design uses the Iarworkbench development tool, realizes the serial port communication, the bulb control, the fan air conditioning control, the curtain control, the sensor data transmission function.

The basic steps of using the serial port:

① initializes the serial port, which includes baud rate, check bit, interrupt, clock frequency and so on. The specific code is as follows:

/****************************************************************    serial port initialization function     ***************************** /void Inituart () {    clkconcmd &= ~0x40;//Set the system clock source to 32MHZ oscillator while    (Clkconsta & 0x40);                     Wait for crystal stability     clkconcmd &= ~0x47;                 Set the system main clock frequency to 32MHZ    percfg = 0x00;        Position 1 P0 port     p0sel = 0x3c;        P0_2,p0_3,p0_4,p0_5 used as serial port, the second function     p2dir &= ~0xc0;      P0 priority as UART0, priority    U0CSR |= 0x80;       UART mode     U0GCR |= one;           U0GCR and U0baud cooperate         U0baud |= 216;       Baud rate set to 115200     utx0if = 0;          UART0 TX Interrupt Flag initial position 0     u0csr|=0x40;            Allow to receive    ien0|=0x84;           Open total interrupt, receive interrupt}

② sends data to the send buffer or reads from the receive buffer. The specific code is as follows:

/**************************************************************** serial port sending data function    ********************************* /void Uart_send_string (Uchar *data,int len) {    int J;     for (j=0;j<len;j++)     {       u0dbuf = *data++;       while (utx0if = = 0);       utx0if = 0;     } }/******************* serial receive function ***********************/#pragma vector = urx0_vector   __interrupt void Uart0_isr (void )  {     urx0if = 0;    Clear Interrupt flag     command = U0DBUF;     Rxdata[p]=command;    p++;    if (p = = 8)    {      p = 0;    }}

Sensor module, motor module, Electric light control is mainly used in the IO Port control programming, using a configuration similar to STM32 configuration method, the following steps:

The ① feature sets the Register P1sel, which is the normal IO port by default. 0 indicates normal IO port, 1 indicates second function

② input/Output configuration p1dir,0 represents input, 1 indicates output

③io Port input Circuit mode configuration, 0 means pull-up/drop-down, 1 means high impedance state.

The specific code is illustrated by the example of a light bulb:

void Ledabcd_init (void) {  p1sel &= ~0x30;//Select Port  p1dir |= 0x30;  Set to output mode  p1inp &= ~0x30;  Open Pull-up}

4.6 Making the embedded platform QT program

Since the screen on the OK6410 board is not the same as the screen on the PC, the size and font settings need to be set. Because the touch screen used is the 480*272 resolution, the Chinese font supported here is "Wenquanyi", so add the following code to the main.cpp:

int main (int argc, char *argv[]) {    qapplication A (argc, argv);    Mainwin W; #if defined (_WS_QWS) | | Defined (Q_WS_QWS)    w.showfullscreen ();    Qfont font ("Wenquanyi", 12,qfont::bold);    A.setfont (font), #else    w.resize (480, 272), #endif ... w.show (); ...}

Use the cross-compilation tool in the terminal to compile and create a shell script in conjunction with the scripting language, using the command:

#vim myshell.sh

Add the following content to the file:

#/bin/bash

Qmake-project

/opt/qt-4.7.1/bin/qmake

Make

CP smartlife/nfsroot/filesystem-yaffs2/

Then change the properties of the myshell.sh using the command:

#chmod +777 myshell.sh

Executing myshell.sh can implement cross-compilation and will generate files that can be run on arm.

The resulting smartlife is a file that can be executed on an arm platform that is used in putty./smartlife–qws to perform the build SmartLife.

4.7 Programs for remote clients

Users need to use the user name and password to log in, the cute penguin as a background image, real-time query all the situation in the home, so that the network can be remote control of the situation in the home.

The login interface uses the dialog box mode to write the call to the dialog class, the user name and password verification using the ciphertext method, the specific code is as follows:

void dialog::on_loginbtn_clicked () {    qtextcodec::setcodecfortr (Qtextcodec::codecforlocale ());    if (Ui->usredit->text () ==tr ("Zhanghao") &&ui->pwdedit->text () ==tr ("123456"))    {        Accept ();    }    else    {    qmessagebox::warning (this,tr ("Warning"), tr ("User name or passwd wrong"), tr ("Yes"), tr ("no"));    If not, pop-up warning dialog box    ui->usredit->clear ();//empty User name Input box    ui->pwdedit->clear ();//Empty password input box    ui- >usredit->setfocus ();//move the cursor to the user name input box    }}

The addition of the background picture uses the drawing tool to place the background picture under the image of the project directory, implemented by calling the PaintEvent () function, with the specific code:

void Dialog::p aintevent (qpaintevent *)     //interface image of the manned, in the header file to declare this member {    qpainter painter (this);    QPIXMAP pix;    Pix.load (":/image/8.jpg");    Painter.drawpixmap (0,0,400,350,pix);}

Client-related UI designs are shown in Figures 4-17, 4-18, 4-19, and 4-20:


5 Commissioning and summary

5.1 Debugging of Home gateways

Debugging the idea is: the use of USB to TTL serial port, the USB to TTL serial port RX and OK6410 Development on the TXD, will be USB to the serial port of the TX and OK6410 Development Board RXD, Special note is to be on the OK6410 Development Board GND and USB to TTL serial port GND to join together (it is very important), before because of this problem wasted a lot of time, then the lab classmate told me this must be common, or most of the time data transmission will appear error. Then use the Serial debugging assistant on the PC to see whether the command sent with their own on the touch screen, save the received data in the TXT document, with Notepad to open the saved TXT document, and then the previous operation of the corresponding command compared to the discovery of this TXT document compared to the discovery of the exact same. It can be concluded that 1: the serial port of the arm gateway of Smart home can send instructions to the outside world.

The same in the Serial debugging assistant to send the corresponding data, if the TFT screen can achieve the corresponding display results then the ARM Gateway serial port can receive external data sent. It can be concluded that 2: the serial port of the smart home ARM gateway can accept the data correctly and make corresponding response.

Conclusion 1 and Conclusion 2 can conclude that the serial procedure of ARM gateway is correct.

5.2 ZigBee Debugging

On the ZigBee serial port to determine the data is correct, the data sent here is each through the serial port, the first to determine the data sent is correct, the use of the serial debugging assistant, the data will be sent to the serial debugging assistant on the data displayed on the comparison, if there is no error, Then this serial port sends the data to be no problem. After several debugging and modification code found that the serial port data transmission can finally be sent in the format required to send data. The next is to receive the data, the method is to send the received data out on the serial debugging assistant display. However, when using the serial port function, the data sent to the processing must be screened out, otherwise the data sent for processing is unpredictable.

5.3 The communication between ZigBee and arm

The debugging between ZigBee and arm some problems, the wireless transmission of data will send the wrong information, the reason may be the problem of the code, because the use of wireless transmission to use the protocol stack, and this ZigBee does not run the protocol stack, so wireless transmission will definitely have problems. Using the wireless serial number transmission module CC1101 to debug the discovery or there is a problem, the reason may be the timing problem, if you want to completely solve this problem, there is still a lot of difficulties, the topic is only wired data transmission. So this wireless transmission failure ended, can only use wired data transmission to debug, so as to achieve the desired effect.

5.4 Effect of physical operation

Smart home model is made of PVC board, in the model has: LED made of lamps, DC motor and PVC material made of air conditioning model, gear motor and cloth made of curtains, light collection module (photoresistors), temperature and humidity acquisition module (DHT11), Access control module (single-chip microcomputer +rfid+ Servo + buzzer).

Smart home operation (lighting all open, Access card success, temperature and humidity within the set range) 1. 5-1 is shown below:


Development Board running effect (4 LED lights all lit, you can see the normal current display of temperature and humidity: 26 degrees Celsius, humidity of 35%, set the temperature threshold of 27 degrees Celsius, humidity is 70%, the current temperature and humidity have not exceeded the threshold, so the air conditioning does not open) 5-2 is shown:




Conclusion

In this design, the present situation and the future development trend of the IoT intelligent Home system at home and abroad are analyzed in detail. Based on the existing intelligent home system solutions, this paper proposes a combination of processor and wireless technology, and integrates the Internet communication technology to realize the real internet of things and to monitor and manage the intelligent home system in real time. The following are the main tasks in this article:

(1) Based on the existing intelligent home system solutions, the design scheme of the intelligent Home system is put forward, including the Home Gateway Master Controller, the terminal controlled equipment, the home control network and the external communication network four parts.

(2) The family gateway and its core function module are analyzed in detail, and the hardware of the power supply module, System reset circuit, serial interface circuit, interface, interface, Ethernet interface circuit, storage circuit and interface circuit are completed.

(3) in the software design is mainly to carry out the porting of the boot program, and then on the basis of the migration of the kernel and the corresponding configuration work, finally, the completion of the file system production and migration and embedded server migration.

(4) The Family Internal Control network module analysis, completed the living room lighting switch control, home temperature and humidity monitoring and control, intelligent curtain control.

(5) On the basis of the protocol stack of wireless network technology and the application development interface function, the communication between home control networks is realized, and the initial realization of remote control and query function is carried out.

(6) on the basis of radio frequency (RFID), it realizes the control function of intelligent door access.

In this paper, many of the areas can be extended, the database can be implemented in the home Environment storage query statistics and other functions. At the same time, the mobile phone message can be accepted in the way of the abnormal situation in the home to alarm, in addition to Android development and iOS development.

Due to my limited level, there are some deficiencies and negligence in the text, I hope that the comments and welcome criticism and exchanges.




















Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design of Smart home based on Linux (5)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.