TCP/IP Embedded Ethernet Controller Connects to Tencent Cloud

TCP/IP Embedded Ethernet Controller Connects to Tencent Cloud
ORIGINAL POST
By Fu Ganjiang
components
Hardware Components
W5500
X 1
Software Apps and online services
TENCENT
details

ucc_june_3.PNG

1. Introduction to W5500

      The W5500 is an all-hardware TCP/IP embedded Ethernet controller that provides an easier Internet connection solution for embedded systems. The W5500 integrates TCP/IP protocol stack, 10/100M Ethernet data link layer (MAC) and physical layer (PHY), enabling users to expand network connectivity in their applications with a single chip.
      The market-proven WIZnet full hardware TCP/IP protocol stack supports TCP, UDP, IPv4, ICMP, ARP, IGMP and PPPoE protocols. The W5500 embeds 32K bytes of on-chip cache for Ethernet packet processing. If you use the W5500, you only need some simple Socket programming to implement Ethernet applications. This will be faster and easier than other embedded Ethernet solutions. Users can use 8 hardware sockets for independent communication at the same time.
      W5500 provides SPI (Peripheral Serial Interface) for easier integration with peripheral MCUs. Moreover, W5500 uses a new high-efficiency SPI protocol to support 80MHz rate, which can better realize high-speed network communication. In order to reduce system power consumption, W5500 provides Wake-on-LAN (WOL) and Power-down modes for customers to choose from.

2. W5500 underlying driver writing and transplantation

W5500 low-level low-level driver porting reference example: http://www.elecfans.com/d/1843264.html?track_id=myCenter&mod=article&share
STM32 connection Tencent cloud reference example: http://www.elecfans.com/d/1842055. html?track_id=myCenter&mod=article&share

Tencent IoT Network Platform Connection

#include "stm32f10x.h"
#include "usart.h"
#include "timer.h"
#include "aliyun_mqtt.h"

/*w5500相关头文件*/
#include "w5500api.h"
#include "dhcp.h"
#include "dns.h"
#define SERVER_IP "LA57WTHWL6.iotcloud.tencentdevices.com"//服务器IP
#define SERVER_PORT 1883 //端口号
//客户端ID:{产品ID}{设备名}
#define ClientID "LA57WTHWL6Smart_home"
//用户名和密码可使用密码生成工具完成
#define Username "LA57WTHWL2Smart_home;120310126;HK8V9;1635948714"
#define Password "2a915cae4489b591ce556e71f4e9f1ka21c354a37d12fee7c46f56abee1f048a;hmacsha256"//密文 

//订阅题:$thing/down/property/{ProductID}/{DeviceName} ---{ProductID}产品ID,{DeviceName}设备名
#define SET_TOPIC  "$thing/down/property/Lb57WTHWL3/Smart_home"//订阅
//发布主题:$thing/up/property/{ProductID}/{DeviceName}
#define POST_TOPIC "$thing/up/property/Lb57WTHWL3/Smart_home"//发布

char mqtt_message[200];//上报数据缓存区
u8 rx_buff[200];
void init_Net(void)
{
	u8 try_times=0;
	init_W5500();/*W5500初始化*/
	init_dhcp_client();//动态分配IP
	while(check_DHCP_state(SOCK_DHCP)!=DHCP_RET_UPDATE)
  {
			try_times++;
			if(try_times==255)//超时 重启芯片
      {							
				Reset_W5500();
				init_dhcp_client();
				try_times=0;
			}
			delay_ms(10);
		}
    while(!set_Network());
  /*判断数据是否写入成功*/
  u8 ipaddr[4];//ip地址
  u8 subnet[4];//子网掩码
  u8 gateway[4];//网关
	getSIPR (ipaddr);			
	printf(" W5500 IP地址   : %d.%d.%d.%drn", ipaddr[0],ipaddr[1],ipaddr[2],ipaddr[3]);
	getSUBR(subnet);
	printf(" W5500 子网掩码 : %d.%d.%d.%drn", subnet[0],subnet[1],subnet[2],subnet[3]);
	getGAR(gateway);
	printf(" W5500 网关     : %d.%d.%d.%drn", gateway[0],gateway[1],gateway[2],gateway[3]);  
}
/*连接服务器*/
uint8 TCP_Connect(SOCKET Socket,uint8 *sip,uint16 sport,uint16 lport)
{
  static uint8 CONNECT_FLAG = 0;
  switch(getSn_SR(Socket))
  {
    case SOCK_INIT:
			 CONNECT_FLAG=connect(Socket,sip,sport);/*连接服务器*/
       break;
    case SOCK_CLOSE_WAIT:
      disconnect(Socket);
      close(Socket);
      break;
    case SOCK_CLOSED:
			 CONNECT_FLAG=0;
			 socket(Socket,Sn_MR_TCP,lport,Sn_MR_ND);
			 break;
  }
  return CONNECT_FLAG;
}
int main()
{
  u16 i=0;
  u8 stat=0;
  u16 time=0,cnt=0;
  float temp=10;
  u16 rlen;
  USART1_Init(115200);
  printf("W5500 SPI模拟时序寄存器版,连接腾讯云VER1.0 rn");
  init_Net();/*W5500硬件初始化,动态分配IP*/
  printf("W5500 硬件初始化成功rn");
  while(!do_dns((u8 *)SERVER_IP)){}/*域名解析*/
  while(1)
  {
    stat=TCP_Connect(MQTT_SOCK,NET_CONFIG.rip,1883,5500);/*连接服务器*/
    printf("stat=%drn",stat);
    if(stat)break;
    delay_ms(1000);
  } 
  printf("腾讯物联网平台连接中....n");
  while(1)
  {
    if(getSn_SR(MQTT_SOCK)==SOCK_ESTABLISHED)
    {
       if(getSn_IR(MQTT_SOCK)&Sn_IR_CON)
       {
         setSn_IR(MQTT_SOCK,Sn_IR_CON);
       }
       MQTT_Init();
       if(MQTT_Connect(ClientID,Username,Password)==0)
       {
          printf("腾讯云平台连接成功rn");
         break;
       }
    }
  }
  stat=MQTT_SubscribeTopic(SET_TOPIC,0,1);
  if(stat)printf("订阅失败rn"); 
  while(1)
  {
    /*接收数据*/
    rlen=getSn_RX_RSR(MQTT_SOCK);
    if(rlen>0)
    {
      rlen=recv(MQTT_SOCK,rx_buff,rlen);
      printf("len=%drn",rlen);
      for(i=0;i=5000)
    {
      time=0;
      MQTT_SentHeart();//发送心跳包
    }
    if(cnt>=2000)
    {
      temp+=1.5;
      if(temp>=85)temp=-15;
      cnt=0;
      sprintf(mqtt_message,"{"method":"report","clientToken":"123","params":{"LED1":1,"temp":%.2f,"L":356}}",temp);//温度
      MQTT_PublishData(POST_TOPIC,mqtt_message,0);
    }    
  }
}

;i++)>

3. Operation effect

insert image description herepYYBAGKV5dqARxLmAAFgecWP9iw664.pngpoYBAGKV5euAbEYzAAHmv-GPGTk926.png

ucc_june_3.PNG

1. Introduction to W5500

      The W5500 is an all-hardware TCP/IP embedded Ethernet controller that provides an easier Internet connection solution for embedded systems. The W5500 integrates TCP/IP protocol stack, 10/100M Ethernet data link layer (MAC) and physical layer (PHY), enabling users to expand network connectivity in their applications with a single chip.
      The market-proven WIZnet full hardware TCP/IP protocol stack supports TCP, UDP, IPv4, ICMP, ARP, IGMP and PPPoE protocols. The W5500 embeds 32K bytes of on-chip cache for Ethernet packet processing. If you use the W5500, you only need some simple Socket programming to implement Ethernet applications. This will be faster and easier than other embedded Ethernet solutions. Users can use 8 hardware sockets for independent communication at the same time.
      W5500 provides SPI (Peripheral Serial Interface) for easier integration with peripheral MCUs. Moreover, W5500 uses a new high-efficiency SPI protocol to support 80MHz rate, which can better realize high-speed network communication. In order to reduce system power consumption, W5500 provides Wake-on-LAN (WOL) and Power-down modes for customers to choose from.

2. W5500 underlying driver writing and transplantation

W5500 low-level low-level driver porting reference example: http://www.elecfans.com/d/1843264.html?track_id=myCenter&mod=article&share
STM32 connection Tencent cloud reference example: http://www.elecfans.com/d/1842055. html?track_id=myCenter&mod=article&share

Tencent IoT Network Platform Connection

#include "stm32f10x.h"
#include "usart.h"
#include "timer.h"
#include "aliyun_mqtt.h"

/*w5500相关头文件*/
#include "w5500api.h"
#include "dhcp.h"
#include "dns.h"
#define SERVER_IP "LA57WTHWL6.iotcloud.tencentdevices.com"//服务器IP
#define SERVER_PORT 1883 //端口号
//客户端ID:{产品ID}{设备名}
#define ClientID "LA57WTHWL6Smart_home"
//用户名和密码可使用密码生成工具完成
#define Username "LA57WTHWL2Smart_home;120310126;HK8V9;1635948714"
#define Password "2a915cae4489b591ce556e71f4e9f1ka21c354a37d12fee7c46f56abee1f048a;hmacsha256"//密文 

//订阅题:$thing/down/property/{ProductID}/{DeviceName} ---{ProductID}产品ID,{DeviceName}设备名
#define SET_TOPIC  "$thing/down/property/Lb57WTHWL3/Smart_home"//订阅
//发布主题:$thing/up/property/{ProductID}/{DeviceName}
#define POST_TOPIC "$thing/up/property/Lb57WTHWL3/Smart_home"//发布

char mqtt_message[200];//上报数据缓存区
u8 rx_buff[200];
void init_Net(void)
{
	u8 try_times=0;
	init_W5500();/*W5500初始化*/
	init_dhcp_client();//动态分配IP
	while(check_DHCP_state(SOCK_DHCP)!=DHCP_RET_UPDATE)
  {
			try_times++;
			if(try_times==255)//超时 重启芯片
      {							
				Reset_W5500();
				init_dhcp_client();
				try_times=0;
			}
			delay_ms(10);
		}
    while(!set_Network());
  /*判断数据是否写入成功*/
  u8 ipaddr[4];//ip地址
  u8 subnet[4];//子网掩码
  u8 gateway[4];//网关
	getSIPR (ipaddr);			
	printf(" W5500 IP地址   : %d.%d.%d.%drn", ipaddr[0],ipaddr[1],ipaddr[2],ipaddr[3]);
	getSUBR(subnet);
	printf(" W5500 子网掩码 : %d.%d.%d.%drn", subnet[0],subnet[1],subnet[2],subnet[3]);
	getGAR(gateway);
	printf(" W5500 网关     : %d.%d.%d.%drn", gateway[0],gateway[1],gateway[2],gateway[3]);  
}
/*连接服务器*/
uint8 TCP_Connect(SOCKET Socket,uint8 *sip,uint16 sport,uint16 lport)
{
  static uint8 CONNECT_FLAG = 0;
  switch(getSn_SR(Socket))
  {
    case SOCK_INIT:
			 CONNECT_FLAG=connect(Socket,sip,sport);/*连接服务器*/
       break;
    case SOCK_CLOSE_WAIT:
      disconnect(Socket);
      close(Socket);
      break;
    case SOCK_CLOSED:
			 CONNECT_FLAG=0;
			 socket(Socket,Sn_MR_TCP,lport,Sn_MR_ND);
			 break;
  }
  return CONNECT_FLAG;
}
int main()
{
  u16 i=0;
  u8 stat=0;
  u16 time=0,cnt=0;
  float temp=10;
  u16 rlen;
  USART1_Init(115200);
  printf("W5500 SPI模拟时序寄存器版,连接腾讯云VER1.0 rn");
  init_Net();/*W5500硬件初始化,动态分配IP*/
  printf("W5500 硬件初始化成功rn");
  while(!do_dns((u8 *)SERVER_IP)){}/*域名解析*/
  while(1)
  {
    stat=TCP_Connect(MQTT_SOCK,NET_CONFIG.rip,1883,5500);/*连接服务器*/
    printf("stat=%drn",stat);
    if(stat)break;
    delay_ms(1000);
  } 
  printf("腾讯物联网平台连接中....n");
  while(1)
  {
    if(getSn_SR(MQTT_SOCK)==SOCK_ESTABLISHED)
    {
       if(getSn_IR(MQTT_SOCK)&Sn_IR_CON)
       {
         setSn_IR(MQTT_SOCK,Sn_IR_CON);
       }
       MQTT_Init();
       if(MQTT_Connect(ClientID,Username,Password)==0)
       {
          printf("腾讯云平台连接成功rn");
         break;
       }
    }
  }
  stat=MQTT_SubscribeTopic(SET_TOPIC,0,1);
  if(stat)printf("订阅失败rn"); 
  while(1)
  {
    /*接收数据*/
    rlen=getSn_RX_RSR(MQTT_SOCK);
    if(rlen>0)
    {
      rlen=recv(MQTT_SOCK,rx_buff,rlen);
      printf("len=%drn",rlen);
      for(i=0;i=5000)
    {
      time=0;
      MQTT_SentHeart();//发送心跳包
    }
    if(cnt>=2000)
    {
      temp+=1.5;
      if(temp>=85)temp=-15;
      cnt=0;
      sprintf(mqtt_message,"{"method":"report","clientToken":"123","params":{"LED1":1,"temp":%.2f,"L":356}}",temp);//温度
      MQTT_PublishData(POST_TOPIC,mqtt_message,0);
    }    
  }
}

;i++)>

3. Operation effect

insert image description herepYYBAGKV5dqARxLmAAFgecWP9iw664.pngpoYBAGKV5euAbEYzAAHmv-GPGTk926.png

COMMENTS

Please Login to comment
  Subscribe  
Notify of
POSTED BY
Reusable S/W