[mw_shl_code=c,true]/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "delay.h"
#include "spi.h"
#include "socket.h" // Just include one header for WIZCHIP
#include "Internet/DNS/dns.h"
#include "timer.h"
#include <string.h>
#include <math.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define SOCK_TCPS 0
#define SOCK_DNS 1
#define DATA_BUF_SIZE 2048
/* Private macro -------------------------------------------------------------*/
uint8_t gDATABUF[DATA_BUF_SIZE];
uint8_t domain_ip[4]={115,29,170,230};
uint8_t domain_name[]="tlink.io";
// Default Network Configuration
wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
.ip = {192, 168, 11, 123},
.sn = {255,255,255,0},
.gw = {192, 168, 11, 1},
.dns = {114,114,114,114},
.dhcp = NETINFO_STATIC };
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void platform_init(void); // initialize the dependent host peripheral
void network_init(void); // Initialize Network information and display it
uint8_t tlink_post(const char *sensors_id,float value);
/**
* @brief ?®???ò??????
* @param None
* @retval None
*/
int main(void)
{
uint8_t tmp;
int32_t ret = 0,i=0,t=0;
uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
int32_t value_table[8]={45,90,135,180,225,270,315};
float value;
//Host dependent peripheral initialized
platform_init();
// First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP
/* Critical section callback */
reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit); //×??á??????????
/* Chip selection call back */
#if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//×??áSPI????????????
#elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect); // CS must be tried with LOW.
#else
#if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
#error "Unknown _WIZCHIP_IO_MODE_"
#else
reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
#endif
#endif
/* SPI Read & Write callback function */
reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte); //×??á????????
/* WIZCHIP SOCKET Buffer initialize */
if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
printf("WIZCHIP Initialized fail.\r\n");
while(1);
}
/* PHY link status check */
do{
if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
printf("Unknown PHY Link stauts.\r\n");
}
}while(tmp == PHY_LINK_OFF);
/* Network initialization */
network_init();
/* DNS client initialization */
DNS_init(SOCK_DNS, gDATABUF);
Timer_Start();
/* DNS procssing */
if ((ret = DNS_run(gWIZNETINFO.dns, domain_name, domain_ip)) > 0){ // try to 1st DNS
printf("> 1st DNS Reponsed\r\n");
}else if(ret == -1){
printf("> MAX_DOMAIN_NAME is too small. Should be redefined it.\r\n");
Timer_Stop();
while(1);
}else{
printf("> DNS Failed\r\n");
Timer_Stop();
while(1);
}
//DNS????????
if(ret > 0){
printf("> Translated %s to %d.%d.%d.%d\r\n",domain_name,domain_ip[0],domain_ip[1],domain_ip[2],domain_ip[3]);
}
Timer_Stop();
while(1)
{
value=100*sin(3.1*(value_table[i++]/180.0));
tlink_post("10980",value);//????????????????
// if(i > 7)
// {
// i = 0;
// }
}
}
void network_init(void)
{
uint8_t tmpstr[6];
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
// Display Network Information
ctlwizchip(CW_GET_ID,(void*)tmpstr);
printf("\r\n=== %s NET CONF ===\r\n",(char*)tmpstr);
printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],
gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
printf("SIP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
printf("GAR: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
printf("SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
printf("DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
printf("======================\r\n");
}
void platform_init(void)
{
SystemInit();//?????±????????
USART_Configuration();//?®??1??????
SPI_Configuration();
delay_init();
Timer_Config();//??????DNS?ù?è?????¨?±?÷
}
uint8_t tlink_post(const char *sensors_id,float value)
{
int ret;
char* presult;
char remote_server[] = "www.tlink.io";
char str_tmp[128] = {0};
// ???ó?????????ì????????
static char http_request[DATA_BUF_SIZE] = {0}; //?ù?÷??????±?????·???????????
static char http_response[DATA_BUF_SIZE] = {0}; //?ù?÷??????±?????·???????????
// Http??????±í??????
char http_content[32] = {0};
sprintf(str_tmp,"");
// ?·?¨HTTP±í???á?????? {"value":20}
sprintf( http_content , "{\"deviceNo\":\"S2H3MS38G4KFG9EJ\",\"sensorDatas\":[{\"sensorsId\":\"%s\",\"value\":\"%f\"}]}" ,sensors_id,value);
// ?·?¨ HTTP???ó?×??
// ????POST /v1.0/device/98d19569e0474e9abf6f075b8b5876b9/1/1/datapoints/add HTTP/1.1\r\n
sprintf( http_request , "POST http://www.tlink.io/tlink_interface/api/device/createDataPonit.htm HTTP/1.1\r\n");
// ???????? ???? Host: api.machtalk.net\r\n
sprintf( str_tmp , "Host:www.tlink.io\r\n");
strcat( http_request , str_tmp);
strcat( http_request , "Accept: */*\r\n");
// ?????á??±í?????????¤?? ???? Content-Length:12\r\n
sprintf( str_tmp , "Content-Length:%d\r\n" ,strlen(http_content) );
strcat( http_request , str_tmp);
// ????±í??±à?????? Content-Type:application/x-www-form-urlencoded\r\n
strcat( http_request , "Content-Type:application/x-www-form-urlencoded\r\n");
strcat( http_request , "Connection:close\r\n");
// HTTP?×????HTTP???? ·?????·?
strcat( http_request , "\r\n");
// HTTP????????
strcat( http_request , http_content);
printf("%s",http_request);
//???????¨??TCP·???????
//???¨????Socket??°ó?¨±???????5000
ret = socket(SOCK_TCPS,Sn_MR_TCP,5000,0x00);
if(ret != SOCK_TCPS){
printf("%d:Socket Error\r\n",SOCK_TCPS);
while(1);
}else{
printf("%d:Opened\r\n",SOCK_TCPS);
}
//????TCP·????÷
ret = connect(SOCK_TCPS,domain_ip,80);
if(ret != SOCK_OK){
printf("%d:Socket Connect Error\r\n",SOCK_TCPS);
while(1);
}
//·??????ó
ret = send(SOCK_TCPS,(unsigned char *)http_request,strlen(http_request));
if(ret != strlen(http_request)){
printf("%d:Socket Send Error\r\n",SOCK_TCPS);
while(1);
}
// ?????ì??
ret = recv(SOCK_TCPS,(unsigned char *)http_response,DATA_BUF_SIZE);
if(ret <= 0){
printf("%d:Socket Get Error\r\n",SOCK_TCPS);
while(1);
}
http_response[ret] = '\0';
//??????·?????HTTP OK
presult = strstr( (const char *)http_response , (const char *)"200 OK\r\n");
if( presult != NULL ){
printf("Http Response OK\r\n");
}else{
printf("Http Response Error\r\n");
printf("%s",http_response);
}
close(SOCK_TCPS);
return 1;
}
/*********************************END OF FILE**********************************/
[/mw_shl_code]
tlink_post函数只上电执行一次,怎么解决,没看出来哪里出问题了
|