HaaS EDU scenario-based application learning-home page information screen

1. Introduction to the experiment

This experiment mainly introduces the realization of the home page information screen. As the first page, the home page information implements a mobile-like interface, which contains rich content.

1 ) Product name

2) Version information

3) System status, system time, WIFI connection, Bluetooth connection.

4) IP address

 

display effect

The interface display effect is as follows:

 

This experiment contains a lot of content. Through the study of this experiment, you can learn a lot of knowledge points, such as OLED drive display, ADC to obtain power information, Wi-Fi connection status and so on.

 

2. Involving knowledge points

  • ADC configuration and use
  • Use of OLED
  • Wi-Fi distribution network
  • System clock acquisition (NTP network time synchronization)

 

3. Preparation of software and hardware environment

3.1, hardware

One HaaS EDU K1 development board.

All the modules mentioned above are already included on our EDU board.

 

3.2 Software

All the codes of the homepage information screen have been implemented and are included in the release version, which can be seen every time the system is restarted.

 

3.2.1, firmware version

Version number: VER 1.0.0

 

3.2.2, code path

Download code

git clone https://github.com/alibaba/AliOS-Things.git -b dev_3.1.0_haas

For domestic users, in order to avoid slow downloading from github, you can download from gitee.

git clone https://gitee.com/alios-things/AliOS-Things.git -b dev_3.1.0_haas

 

Compile

Enter the top-level directory of the code such as AliOS-Things to compile it. You can directly compile the demo app in the application/example/ directory, or the app developed by yourself. Take compiling helloworld_demo as an example below.

aos make distclean

aos make edu_demo@haaseduk1 -c config

aos make

 

The relative paths of the files needed on the home screen are as follows:

application/example/edu_demo/k1_apps/homepage/homepage.c

application/example/edu_demo/k1_apps/homepage/homepage.h

 

Burning method

See the development environment chapter

 

3.2.3, get started

Version number display

The version number is displayed in the middle of the screen.

The format is VER: xxx

 

Configure Wi-Fi

Command line format currently used for Wi-Fi configuration

For example, the current Wi-Fi name is haas-open, and the password is 12345678.

 netmgr -t wifi -c haas-open 12345678

After connecting, the obtained IP address will be displayed, as shown in the figure below:

Power display

The battery level will be displayed to the icon in the upper right corner, divided into five levels.

 

4. Introduction to each experiment

4.1, OLED development and display

4.1.1 Introduction to OLED background

OLED , namely Organic Light-Emitting Diode (Organic Light-Emitting Diode), also known as Organic Electroluminesence Display (OELD). OLED has excellent characteristics such as self-luminous, no backlight, high contrast, thin thickness, wide viewing angle, fast response speed, can be used for flexible panels, wide operating temperature range, simple structure and manufacturing process, etc., so it is considered to be The next-generation flat panel display emerging application technology.

LCDs require backlighting, but OLEDs do not, because it is self-luminous. For the same display, the OLED effect should be better. With current technology, it is difficult to increase the size of OLEDs, but the resolution can indeed be very high.

 

4.1.2, hardware design

In this experiment, the OLED is fixed on the front panel as a small board, and the middle is connected to the main board through a cable.

The OLED screen used in this experiment has the following characteristics:

1 ) The module is a monochrome display with white characters on a black background

2 ) The display size is 1.3 inches

3 ) High resolution, the resolution of this module is 132*64

4 ) The hardware interface adopts SPI bus interface

 

Appearance of onboard OLED:

OLED outline drawing

The schematic diagram is as follows:

         

Schematic diagram of EDU OLED part

4.1.3, software design

For the detailed introduction and use of OLED, please refer to Chapter 3 OLED. Here we mainly introduce how to use it. According to the schematic diagram, the SPI0 of the motherboard connected to the OLED adopts the 4-wire SPI mode.

 

initialization

First initialize SPI0, this can be found in the total entry function:

application/example/edu_demo/app_entry.c

sh1106_init();

sh1106_init initialization includes SPI0 initialization and GPIO initialization.

uint8_t sh1106_init(void)

{

    uint8_t err_code;

    err_code = hardware_init();

    if (err_code != 0)

    {

        return err_code;

    }

    command_list();

    return err_code;

}

Display part

The code is located in application/example/edu_demo/k1_apps/homepage/homepage.c

Take the display version information as an example:

OLED_Clear(); // 清屏函数      

OLED_Show_String(40, (12 + 4) * 1, "HaaS EDU", 12, 1); / 将字符串填入显示缓存

snprintf(image_version, 21, "VER: %s", BUILD_VERSION); // 格式化字符串

OLED_Show_String(33, (12 + 4) * 2, image_version, 12, 1); // 将格式化后的字符串-

                                                                                                                    //版本信息填入缓存

OLED_Refresh_GRAM(); // 刷新显存到屏幕上

4.2, ADC operation and power acquisition

4.2.1. Background introduction

ADC , or analog-to-digital converter (English: Analog-to-digital converter), is a type of device used to convert continuous signals in analog form into discrete signals in digital form. The opposite device is called a digital-to-analog converter (DAC).

A typical analog-to-digital converter converts an analog signal into a digital signal representing a certain proportional voltage value. However, some analog-to-digital converters are not pure electronic devices, such as rotary encoders, which can also be regarded as analog-to-digital converters.

4.2.2, hardware design

The schematic diagram is as follows:

 

The voltage detection mainly needs to pay attention to the following points:

1. The voltage detection uses the GADC1 channel of the MCU.

2. With USB power supply, the voltage of the detection point is constant between 4.8V and 5.2V. When switching to battery power supply (the USB power supply is disconnected), the voltage floats between 3.65V and 4.2V. Variety.

3. Because of the limited range of GADC, the voltage detection adopts frequency division. As shown in the schematic diagram, the measured value of ADC is about 1/3 of VOLT, plus the loss of ADC internal resistance, the actual ratio is 3.208.

 

4.2.3, software design

Driver initialization

adc_dev_t adc = {1, 1000, 0x12345678};   //初始化ADC1

ret = hal_adc_init(&adc);

if (ret)

{

  printf("\r\n=====adc test : adc init failed===\r\n");

  return -1;

}

Get voltage value

1. Read ten times,

2. Remove the maximum and minimum values, and then take the average

 for (int32_t i = 0; i < 10; i++)

    {

        hal_adc_value_get(&adc, &output, 200);

        test_sum += output;



        /* the min sampling voltage */

        if (test_min >= output)

        {

            test_min = output;

        }

        /* the max sampling voltage */

        if (test_max <= output)

        {

            test_max = output;

        }

        osDelay(1);

    }



    hal_adc_finalize(&adc);



    test_avrg = (test_sum - test_min - test_max) >> 3;

    //printf("\r\n=====adc test : the samping volage is:%dmv===\r\n", test_avrg);

    test_avrg *= 3.208;

    *volage = test_avrg;

Return battery level

The actual voltage value is ADC*3.208, and then according to this value, different levels are returned, which are mainly divided into five levels, namely 0%, 25%, 50
%, 75%, and 100%.

 if (test_avrg > 4100)

    {

        *level = 4;

    }

    else if ((test_avrg > 3980) && (test_avrg < 4100))

    {

        *level = 3;

    }

    else if ((test_avrg > 3850) && (test_avrg < 3980))

    {

        *level = 2;

    }

    else if ((test_avrg > 3700) && (test_avrg < 3850))

    {

        *level = 1;

    }

    else if (test_avrg < 3700)

    {

        *level = 0;

    }

Many people who see the code may wonder why 4100 mV to 4200 mV represents 100%, and 4100 mV to 3980 mV represents 75%, which is not a linear value. In fact, the reason is very simple, the battery discharge curve of lithium batteries is not a linear.

 

Display icon

The codes for displaying different monochrome battery icons are as follows:

if (0 == get_battery(&battery_level))

{

  //printf("get_battery success %d\n", battery_level);

  OLED_Icon_Draw(110, 0, &icon_battery_20_12[battery_level], 0);

}

4.3. System Wi-Fi and network time synchronization

4.3.1. Background introduction

Wi-Fi

The term Wi-Fi must be familiar to everyone, and it is synonymous with networking. Simply put, Wi-Fi (WirelessFidelity) is a network transmission standard. Like Bluetooth technology, it belongs to short-range wireless technology. With the popularization of network applications, it has brought great convenience to people, and therefore has been widely used. Wi-Fi makes it possible for us to surf the Internet anytime, anywhere.

I often see words like 802.11b/g/n 2.4Ghz in the packaging of routers, so what is this coming from? This is a standard for wireless network communication. IEEE 802.11 is a common standard for wireless local area networks today. It is defined by the Institute of Electrical and Electronics Engineers (IEEE). The following suffixes are the version numbers of the protocol iteration.

 

2.4GHz band

  • The 802.11b/g/n Zhongguidong Wi-Fi radio can transmit in the 2.4 GHz frequency band, with a total of 14 available channels, and the operating frequency range is 2.402GHz-2.483Ghz. 13 channels (1-13) are available in China. The bandwidth between every two adjacent channels is 5Mhz. If channel 1 is used and the bandwidth is 20Mhz, then channels 2, 3, 4, and 5 are all occupied.
  • The bandwidth of each channel is 22MHz, but the effective bandwidth is only 20MHz. In order to reduce the interference of adjacent channels, 1MHz bandwidth boundaries are reserved on both sides.
  • Only three channels (1, 6, and 11) do not share frequency space.

 

5GHz band

  • 802.11a/n/ac Wi-Fi radios can be transmitted in the 5GHz frequency band, with a total of 25 available channels. The center frequency range is 5.150GHz-5.850GMz. The channels available in China include low channels 36, 40, 44, 48, 52, 56, 60, 64, and high channels 149, 153, 157, 161, 165. There is no overlap between channels.
  • Each bandwidth is 20MHz. If channel 149 is used, when 80Mhz is to be used, 153, 157, and 161 must be occupied.

 

System clock acquisition (SNTP network time synchronization)

The current system time is displayed in the upper left corner of the interface. HaaS EDU K1 does not have an RTC chip, so the time cannot be saved automatically.

So how to get the accurate time? Here you need to use the SNTP protocol, SNTP is based on the NTP protocol.

 

SNTP protocol

Simple Network Time Protocol, adapted from NTP, is mainly used to synchronize computer clocks on the Internet. Defined in RFC2030.

The SNTP protocol adopts a client/server working mode and can operate in unicast (point-to-point) or broadcast (point-to-multipoint) mode. The SNTP server receives GPS signals or its own atomic clock as the time reference of the system. In the unicast mode, the SNTP client can obtain accurate time information by regularly accessing the SNTP server, which is used to adjust the time of the system where the client is located to achieve the purpose of time synchronization. In broadcast mode, the SNTP server periodically sends messages to the specified IP unicast address or IP multicast address. The SNTP client obtains time information by listening to these addresses.

 

4.3.2, hardware design

The Wi-Fi module used in this experiment is already included in the MCU and no additional provision is required.

 

4.3.3, software design

Wi-Fi module

The Wi-Fi part of AliOS Things has been encapsulated in netmgr, and edu_demo has been initialized by default.

The code is located in application/example/edu_demo/app_entry.c

netmgr_init();

netmgr_start(true);

aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);

netmgr_init starts tasks related to wifi_service. It includes Wi-Fi hardware initialization and wifi_service module initialization.

netmgr_start starts the task of Wi-Fi automatic connection back.

aos_register_event_filter(EV_WIFI, wifi_service_event, NULL); here is the callback function that registered wifi_event.

 

Use Wi-Fi

Currently, the Wi-Fi name (SSID) and password can be configured into the EDU through code configuration or command line configuration.

Such as the command line

netmgr -t wifi -c haas-open 12345678

After the connection is successful, it will be automatically saved in the file system. If the new WIFI connection fails or is powered on again, it will automatically connect back to the WIFI.

 

Network time synchronization (SNTP)

After edu is connected to the network, it will automatically obtain the network time. And update to the local clock.

If you need to get the system time at this time, just call the system function clock_gettime directly.

struct tm *info;

struct timespec tv;



/* 获取 GMT 时间 */

clock_gettime(CLOCK_REALTIME, &tv);

info = gmtime(&tv);



snprintf(tmp, 21, "%2d:%02d", (info->tm_hour + 8) % 24, info->tm_min);

OLED_Show_String(0, 12 * 0, tmp, 12, 1);

 

5. Developer technical support

If you need more technical support, you can join the DingTalk developer group or follow the WeChat public account

For more technology and solution introduction, please visit the Aliyun AIoT homepage https://iot.aliyun.com/

Guess you like

Origin blog.csdn.net/HaaSTech/article/details/114003904
Recommended