Using RT thread system to realize DIY digital instrument -- synchronous network time

Catalog:

1. Using RT thread system to realize DIY digital instrument (I) -- stm32ubmx5.6 porting touchGFX4.13
2. Using RT thread system to realize DIY digital instrument (2) -- transplanting TouchGFX to RTThread system
3. Using RT thread system to realize DIY digital instrument (3) - obtaining data of temperature and humidity sensor
4. Using RT thread system to realize DIY digital instrument (4) - synchronous network time
5. Use RT thread system to realize DIY digital instrument (5) - link to Alibaba cloud Internet of things platform
6. Use RT thread system to realize DIY digital instrument (6) -- improve TouchGFX instrument interface
7. Using RT thread system to realize DIY digital instrument (7) -- developing wechat applet





Experimental platform:

Hardware: Wildfire challenger STM32F767 V1 development and ESP8266 module
Software: TouchGFXDesigner v4.13 and STM32CubeMX v5.6.0, MDK v5.29, RT thread env tool

Preparation before experiment:

1. Prepare a set of wildfire challenger STM32F767 development version or other core boards
2. Install TouchGFXDesigner v4.13
3. Install STM32CubeMX v5.6.0 and X? Cube? Touchgfx software package
4. Install MDK v5.27 and above
5. Download the RTThread source package https://gitee.com/rtthread/rt-thread



Download:

Continuous code update: github code download address https://gitee.com/Aladdin-Wang/hellotouchGFX.git

Contact author:

Add wechat comment touchgfx, pull in touchgfx rtthread technology exchange group to learn together

1. Add ESP8266 device driver

1.1. Enable C + + components

Before using the at_device package, you need to first open the libc component, and the C + + programming language used by touchgfx. Before using the libc component, you need to first open the C + + component support

1.2 enable libc component

Before using the at? Device package, you need to open the libc component first

1.3 add at? Device package

AT device software package is the transplantation of AT components and AT socket functions. It is necessary to open AT component library and AT socket functions to obtain AT device software package.

RT-Thread online packages —>
IoT - internet of things —>
-*- AT DEVICE: RT-Thread AT component porting or samples for different device
[ ] Enable at device init by thread
AT socket device modules (Not selected, please select) —>
Version (V1.6.0) —>






1.4 open uart3 through cubmx


Increase the RX buffer size:


1.5 open BSD socket:


At this time, net device and file system will also be opened automatically (fd is managed in file system, so file system is required):




2 network test

ifcong test:

ping test:

tcpclient test:
Open the basic sample code tcp client in the package




-> RT-Thread online packages
-> miscellaneous packages
-> samples: kernel and components samples
-> a network_samples package for rt-thread



Since AT Socket protocol stack is used, replace AF INET with AF at




3 get network time

3.1 add netutils tool package

The netutils package contains a collection of all the network gadgets available for RT thread, including NTP tools.
NTP is the network time protocol, which is used to synchronize the time of each computer in the network. After the NTP client on RT thread connects to the network, it can obtain the current UTC time and update it to RTC.


3.2 open RTC

Since the NTP tool needs to synchronize to the local RTC after obtaining the network time, it needs to turn on the local RTC function

Test using the command that came with the NTP Toolkit:



3.3 write power on automatic synchronization time code

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <arpa/inet.h> 
#include <netdev.h>
#include <ntp.h>

/* defined the LED0 pin: PH10 */
#define LED0_PIN    GET_PIN(H, 10)

int main(void)
{
    int count = 1;
	time_t cur_time;
    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
	rt_pin_write(LED0_PIN, PIN_HIGH);
	//Get network card object
	struct netdev* net = netdev_get_by_name("esp0");

	//Block to determine whether the current network is connected normally
	while(netdev_is_internet_up(net) != 1)
	{
	   rt_thread_mdelay(200);
	}
	//Prompt that the current network is ready
	rt_kprintf("network is ok!\n");

	cur_time = ntp_sync_to_rtc(NULL);
	if (cur_time)
	{
		rt_kprintf("Cur Time: %s", ctime((const time_t*) &cur_time));
	}
	else
	{
		rt_kprintf("NTP sync fail.\n");
	}

    while (count++)
    {
        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_thread_mdelay(1000);
        rt_pin_write(LED0_PIN, PIN_LOW);
        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}

3.4 synchronize network time to UI

Use the macro SIMULATOR to distinguish the code of vs simulation and hal Library

#ifndef SIMULATOR
#include "Thread.h"
#include <rtdevice.h>
#endif
void mainView::handleTickEvent()
{
	#ifdef SIMULATOR
	/*Code using VS emulation*/
	tickCounter++;

    if (tickCounter % 60 == 0)
    {
        if (++digitalSeconds >= 60)
        {
            digitalSeconds = 0;
            if (++digitalMinutes >= 60)
            {
                digitalMinutes = 0;
                if (++digitalHours >= 24)
                {
                    digitalHours = 0;
                }
            }
        }

        if (++analogSeconds >= 60)
        {
            analogSeconds = 0;
            if (++analogMinutes >= 60)
            {
                analogMinutes = 0;
                if (++analogHours >= 24)
                {
                    analogHours = 0;
                }
            }
        }

        // Update the clocks
        digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
        analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);
    }
	#else
	/*Code using HAL Library*/
	time_t now;
	struct tm *clock;
	now = time(RT_NULL);
	clock = gmtime(&now);
	
	digitalHours = clock->tm_hour;
	digitalMinutes = clock->tm_min;
	digitalSeconds = clock->tm_sec;
	
	analogHours = clock->tm_hour;
	analogMinutes = clock->tm_min;
	analogSeconds = clock->tm_sec;
	
	digitalClock.setTime24Hour(digitalHours, digitalMinutes, digitalSeconds);
    analogClock.setTime24Hour(analogHours, analogMinutes, analogSeconds);


	#endif

}

4 Effect

Power on acquisition time:

UI synchronization time:



Extended reading: Network framework of RT thread learning notes

Tags: network socket simulator github

Posted on Wed, 08 Apr 2020 00:12:44 -0700 by nutshell