LWIP 学习(dhcp,mqtt)

1 .dhcp 成功判断和获取地址打印


extern struct netif gnetif;
void StartMainTask(void const * argument){
	MX_LWIP_Init();	

	for(;;){
		osDelay(1000);
		struct dhcp *dhcp;		
		dhcp = netif_dhcp_data(&gnetif);
		if(dhcp->state == DHCP_STATE_BOUND){
			u8 *p;
			p = (u8*)(&gnetif.ip_addr.addr);
			printf("dhcp ok %d.%d.%d.%d!\n",p[0],p[1],p[2],p[3]);
			break;
		}else{
			printf("waiting dhcp\n");
		}
	}

	for(;;){
		osDelay(1000);
	}
}

2. mqtt 使用例子

定时器不够用增加2个

#if !defined MEMP_NUM_SYS_TIMEOUT || defined __DOXYGEN__
#define MEMP_NUM_SYS_TIMEOUT            (LWIP_TCP+2 + IP_REASSEMBLY + LWIP_ARP + (2*LWIP_DHCP) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + (PPP_SUPPORT*6*MEMP_NUM_PPP_PCB) + (LWIP_IPV6 ? (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD) : 0))
#endif
 

 

#include "main.h"
#include "common.h"

#include "lwip.h"
#include "lwip/init.h"
#include "lwip/netif.h"
#if defined ( __CC_ARM )  /* MDK ARM Compiler */
#include "lwip/sio.h"
#endif /* MDK ARM Compiler */

#include "lwip/apps/mqtt.h"


mqtt_client_t static_client;
ip_addr_t svrip;

void mqttConnect( ip_addr_t *ip_addr,mqtt_client_t *client);
u8 tempbuf[256];

static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags)
{
  printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags);

  if(flags & MQTT_DATA_FLAG_LAST) {
  	if(len<256){
	   memcpy(tempbuf,data,len);
	   tempbuf[len] = 0;
	   printf("mqtt_incoming_data_cb: %s\n", (const char *)tempbuf);	   
    }
  }
}


static void mqtt_sub_request_cb(void *arg, err_t result)
{
  printf("Subscribe result: %d\n", result);
}

static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len)
{
  printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len);
}

static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
{
  err_t err;
  if(status == MQTT_CONNECT_ACCEPTED) {
    printf("mqtt_connection_cb: Successfully connected\n");
    mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg);
	// subscribe topic   
    err = mqtt_subscribe(client, "hello", 1, mqtt_sub_request_cb, arg);
    if(err != ERR_OK) {
      printf("mqtt_subscribe return: %d\n", err);
    }
  } else {
    printf("mqtt_connection_cb: Disconnected, reason: %d\n", status);    
	// try again;
    mqttConnect(&svrip,client);
  }  
}


  
void mqttConnect( ip_addr_t *ip_addr,mqtt_client_t *client)
{
   struct mqtt_connect_client_info_t ci;
   err_t err;
   memset(&ci, 0, sizeof(ci));   
   ci.client_id = "lwip_test";
   err = mqtt_client_connect(client,ip_addr , MQTT_PORT, mqtt_connection_cb, 0, &ci);   
   if(err != ERR_OK) {
	 printf("mqtt_connect return %d\n", err);
   }
}



/* Called when publish is complete either with sucess or failure */
static void mqtt_pub_request_cb(void *arg, err_t result)
{
  if(result != ERR_OK) {
    printf("Publish result: %d\n", result);
  }
}

void mqttPublish(mqtt_client_t *client,char *content)
{
  err_t err;
  u8_t qos = 0; /* 0 1 or 2, see MQTT specification  0->only send 1 time*/
  u8_t retain = 0; /* No don't retain such crappy payload... */
  err = mqtt_publish(client, "iwanttosay", content, strlen(content), qos, retain, mqtt_pub_request_cb, NULL);
  if(err != ERR_OK) {
    printf("Publish err: %d\n", err);
  }
}


u8 sendbuf[256];

void mqttTest(){
	svrip.addr =0x3903010a;
	mqttConnect(&svrip,&static_client);
	int count =1;
	while(count<10){		
		osDelay(5000);
		sprintf(sendbuf,"I have said hello for %d times\n",count++);
		mqttPublish(&static_client,sendbuf);
	}
	// close connection
	mqtt_disconnect(client);
}

// disconnect 
//



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值