Adding SNS(Social Networking Services)function in your device

details

A few years ago, someone developed the dispenser with auto posting the status to weibo in order to let he know the status of dispenser anytime. But this method is relatively primitive and didn’t achieve the dispenser directly connecting to Internet, it just send the status of dispenser to PC through one external device. And then PC will connect to weibo in order to posting it through Internet.

Now, it is the era of IoT(Internet of Things). To let your device achieve connecting to Internet directly is one of easy job.

This article will introduce how to let your device auto posting the status to weibo directly without PC’s help. That means, you just need to connect one Ethernet cable to the dispenser for posting the status to weibo.

How to implement?

Please refer the following:

Hardware environment:

1. We need one W5500EVB as following image:
W5500EVB-hk1

2. W5500EVB basic information as following:

a) MCU: STM32103RC with 256K Flash, 48K SRAM and 2K EEPROM

b) Ethernet controller: W5500 which is using SPI for connecting with MCU

c) Power: USB for supply the power

3. Programming tools for W5500EVB:

IAR for ARM v5.41

PS: This is only the version which our engineer using now.

If you are using other version of IAR, please adjust the library of ST MCU.

4. Other:

a) SINA webio user name and password.

PS: If you don’t have, please register one account of your device.

b) One Mini to USB cable

c) One LAN cable

d) STM32 MCU’s serial programming software

PS: ST provides their official software for downloading the f/w which called “Flash Loader Demo” as following:

 W5500EVB-hk2

Source code implementation:

After introduced the hardware environment, let’s look at the source code.

PS: Visitor can download all reference source code in the attachment.

1  #include “w5500/socket.h”
2  #include “w5500/w5500.h”

#include <stdio.h>
#include <string.h>

3  #define SOCK_WEIBO      2                     //the socket number is used for weibo; it can be a number between 0~7
4  #define WEIBO_SERVER    “61.109.255.136”      //weibo server IP address string
5  #define HTTP_PATH       “/wiznet/”            //HTTP path
6  #define WEIBO_ID        “xxxx@xxxx.xxx”   //your sinaweibo ID
7  #define WEIBO_PWD       “******”             //your sinaweibo password

char tmp_buf[512]={0x00,};                        //a temp buffer to store weibo content and HTTP header
unsigned char post_weibo(char* weibo)
{
8   unsigned char weibo_server_ip[4] = {61,109,255,136};    //Weibo server IP address
9   static unsigned intany_local_port = 1000;              //TCP socket local port nubmer
10 char post_data[385]={0x00,};
//weibo content (140 characters, but one Chinese character will be 2 Bytes!) + ID & Password information. If your sinaweibo ID & Password are too long, please define a bigger buffer
unsigned char ret=0;
unsigned intlen=0;
11 if(socket(SOCK_WEIBO,Sn_MR_TCP,any_local_port++,0)!=1)  //to initialize a TCP socket
{
printf(“Socket initialization failed.\r\n”);
return 0;
}
else
{
12  ret=connect(SOCK_WEIBO,weibo_server_ip,80);       //connect to the weibo server, default TCP port is 80
if(ret!=1)
{
printf(“Connect Weibo server failed.\r\n”);
return 0;
}
else
{
13  while(getSn_SR(SOCK_WEIBO)!=SOCK_ESTABLISHED);  //wait for the TCP connection established!
printf(“Connected with Weibo server.\r\n”);
14  sprintf(post_data,”id=%s&pw=%s&cmd=update&status=%s”,(char*)WEIBO_ID,(char*)WEIBO_PWD,weibo);
15  sprintf(tmp_buf,”POST %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: w5500\r\nContent-Type: application/x-www-form-urlencoded; charset=gb2312\r\nContent-Length: %d\r\n\r\n%s”,(char*)HTTP_PATH,(char*)WEIBO_SERVER,strlen(post_data),post_data);
16  len=send(SOCK_WEIBO,(unsigned char*)tmp_buf,strlen(tmp_buf)); //upload your weibo content
while(1)
{
17        len=getSn_RX_RSR(SOCK_WEIBO);
if(len>0)
{
memset(tmp_buf,0x00,512);
18  len=recv(SOCK_WEIBO, (unsigned char*)tmp_buf, len);       //receive the return result from weibo server
char* p=strstr(tmp_buf,(char*)”\r\n\r\n”)+4;              //get http payload without http header: return value
printf(“%s\r\n”,p);
19  disconnect(SOCK_WEIBO);     //disconnect with weibo server
20  close(SOCK_WEIBO);          //close the socket
21  return 1;                   //sucess! return 1
}
}
}
}
}

Let’s look from the beginning. Line 1~2 is for quote the library of W5500 and Socket. Line 3~7 is define part. Line 3 is use for select one socket number for weibo as W5500 support 8 independent sockets simultaneously. So users just need to make sure that you select the socket number is not use for other function. For this source code, we use Socket 2. Line 4~5 is for define the server IP address(weibo) and HTTP path which need to access. Please don’t modify this part. Line 6~7 is define your sinaweibo user name and password. Please notice that we only support sinaweibo. So user should make sure that you input the correct user name and password. If not, it can’t post anything to your weibo. In additional, please don’t worry that we will not keep user’s password in our server.

Line 8 is use for connect to the server IP by input in the array. Line 9 is use for define a local port number for this socket. It can be any number from 0~65535. Our server can accept the data submitted in HTTP format, Line 10 define the array for save the format for weibo contents temporarily; May be you will ask the following question: Why need to define a 385 bytes array as weibo only can accept 140 characters? It is because of this array is not only use for save the contents of weibo and also save the weibo user name and password. In the other hand, if weibo contents included Chinese character, the length of all Chinese weibo contents will be 280 bytes as one Chinese character will use 2 bytes. Therefore, if your sinaweibo ID & Password are too long, please define a bigger buffer.

Following is socket operation: Line 11 is use for initialize a TCP socket. Line 12 is request TCP Server for connection. Line 13 is waiting for connection set up continuously. After connect to the TCP Server, Line 14~15 will build the weibo HTTP data package and line 16 will send it to server.

After receive the return from server, line 17 will read the data length which received by W5500. If the data length is larger than 0, line 18 will read the data from W5500’s RX buffer to tmp_buf. Since the data package included the

HTTP header, the second line of line 18 will cut the HTTP header as we just care about the return result from server.  If received the return from server, it means that the connection with server is successful. But even connection is success; weibo is not 100% send out. Please refer the following table for the different type of server result.

Server return data

Explanation & Troubleshooting

1: weibo id or password errorSINA weibo user name or password is empty. Please check you have input the correct user name or password of your weibo in line 6~7 of source code. And also check the data package of line 14 is correct or not.
2: login failedLogin failed.Please check you have input the correct user name or password of your weibo in line 6~7 of source code.

3: weibo content(status) is invalid

The contents of weibo is empty.

Please make sure that you write the 140 characters for posting to weibo correctly when call the function of post_weibo().

4: too frequent message ###.###Sending weibo message too frequently.In order to control the flow of the server, we require each Weib account only can sent the message at intervals of two minutes. If the intervals of existing Weibo posting message is smaller than 2 minutes compare to last Weibo message, user will receive the error message. ###.### means the intervals between existing Weibo message and last Weibo message. Unit is second.
255: okSINA Weibo posting successfully.
Other messageUnknown Error

 

Lastly, line 19 is use for disconnect the connection with server. And line 20 will close the socket. If it success to communicate with server, line 21 will return 1.

This is all of the source code. You can compile it and download into MCU. Then power ON, see the serial debug message: “255:ok”. It means you success to let your device auto posting the status to weibo. Please access your weibo to check the data really post which you typing in the source code.

The following is one of example:
W5500EVB-hk3

Is it easy to implement? Is it more powerful when the dispenser directly connecting to Internet? Please quick try to let your device connecting to Internet directly!!!!

Article from : http://wizwiki.net/forum/viewtopic.php?f=91&t=266(Chines)

A few years ago, someone developed the dispenser with auto posting the status to weibo in order to let he know the status of dispenser anytime. But this method is relatively primitive and didn’t achieve the dispenser directly connecting to Internet, it just send the status of dispenser to PC through one external device. And then PC will connect to weibo in order to posting it through Internet.

Now, it is the era of IoT(Internet of Things). To let your device achieve connecting to Internet directly is one of easy job.

This article will introduce how to let your device auto posting the status to weibo directly without PC’s help. That means, you just need to connect one Ethernet cable to the dispenser for posting the status to weibo.

How to implement?

Please refer the following:

Hardware environment:

1. We need one W5500EVB as following image:
W5500EVB-hk1

2. W5500EVB basic information as following:

a) MCU: STM32103RC with 256K Flash, 48K SRAM and 2K EEPROM

b) Ethernet controller: W5500 which is using SPI for connecting with MCU

c) Power: USB for supply the power

3. Programming tools for W5500EVB:

IAR for ARM v5.41

PS: This is only the version which our engineer using now.

If you are using other version of IAR, please adjust the library of ST MCU.

4. Other:

a) SINA webio user name and password.

PS: If you don’t have, please register one account of your device.

b) One Mini to USB cable

c) One LAN cable

d) STM32 MCU’s serial programming software

PS: ST provides their official software for downloading the f/w which called “Flash Loader Demo” as following:

 W5500EVB-hk2

Source code implementation:

After introduced the hardware environment, let’s look at the source code.

PS: Visitor can download all reference source code in the attachment.

1  #include “w5500/socket.h”
2  #include “w5500/w5500.h”

#include <stdio.h>
#include <string.h>

3  #define SOCK_WEIBO      2                     //the socket number is used for weibo; it can be a number between 0~7
4  #define WEIBO_SERVER    “61.109.255.136”      //weibo server IP address string
5  #define HTTP_PATH       “/wiznet/”            //HTTP path
6  #define WEIBO_ID        “xxxx@xxxx.xxx”   //your sinaweibo ID
7  #define WEIBO_PWD       “******”             //your sinaweibo password

char tmp_buf[512]={0x00,};                        //a temp buffer to store weibo content and HTTP header
unsigned char post_weibo(char* weibo)
{
8   unsigned char weibo_server_ip[4] = {61,109,255,136};    //Weibo server IP address
9   static unsigned intany_local_port = 1000;              //TCP socket local port nubmer
10 char post_data[385]={0x00,};
//weibo content (140 characters, but one Chinese character will be 2 Bytes!) + ID & Password information. If your sinaweibo ID & Password are too long, please define a bigger buffer
unsigned char ret=0;
unsigned intlen=0;
11 if(socket(SOCK_WEIBO,Sn_MR_TCP,any_local_port++,0)!=1)  //to initialize a TCP socket
{
printf(“Socket initialization failed.\r\n”);
return 0;
}
else
{
12  ret=connect(SOCK_WEIBO,weibo_server_ip,80);       //connect to the weibo server, default TCP port is 80
if(ret!=1)
{
printf(“Connect Weibo server failed.\r\n”);
return 0;
}
else
{
13  while(getSn_SR(SOCK_WEIBO)!=SOCK_ESTABLISHED);  //wait for the TCP connection established!
printf(“Connected with Weibo server.\r\n”);
14  sprintf(post_data,”id=%s&pw=%s&cmd=update&status=%s”,(char*)WEIBO_ID,(char*)WEIBO_PWD,weibo);
15  sprintf(tmp_buf,”POST %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: w5500\r\nContent-Type: application/x-www-form-urlencoded; charset=gb2312\r\nContent-Length: %d\r\n\r\n%s”,(char*)HTTP_PATH,(char*)WEIBO_SERVER,strlen(post_data),post_data);
16  len=send(SOCK_WEIBO,(unsigned char*)tmp_buf,strlen(tmp_buf)); //upload your weibo content
while(1)
{
17        len=getSn_RX_RSR(SOCK_WEIBO);
if(len>0)
{
memset(tmp_buf,0x00,512);
18  len=recv(SOCK_WEIBO, (unsigned char*)tmp_buf, len);       //receive the return result from weibo server
char* p=strstr(tmp_buf,(char*)”\r\n\r\n”)+4;              //get http payload without http header: return value
printf(“%s\r\n”,p);
19  disconnect(SOCK_WEIBO);     //disconnect with weibo server
20  close(SOCK_WEIBO);          //close the socket
21  return 1;                   //sucess! return 1
}
}
}
}
}

Let’s look from the beginning. Line 1~2 is for quote the library of W5500 and Socket. Line 3~7 is define part. Line 3 is use for select one socket number for weibo as W5500 support 8 independent sockets simultaneously. So users just need to make sure that you select the socket number is not use for other function. For this source code, we use Socket 2. Line 4~5 is for define the server IP address(weibo) and HTTP path which need to access. Please don’t modify this part. Line 6~7 is define your sinaweibo user name and password. Please notice that we only support sinaweibo. So user should make sure that you input the correct user name and password. If not, it can’t post anything to your weibo. In additional, please don’t worry that we will not keep user’s password in our server.

Line 8 is use for connect to the server IP by input in the array. Line 9 is use for define a local port number for this socket. It can be any number from 0~65535. Our server can accept the data submitted in HTTP format, Line 10 define the array for save the format for weibo contents temporarily; May be you will ask the following question: Why need to define a 385 bytes array as weibo only can accept 140 characters? It is because of this array is not only use for save the contents of weibo and also save the weibo user name and password. In the other hand, if weibo contents included Chinese character, the length of all Chinese weibo contents will be 280 bytes as one Chinese character will use 2 bytes. Therefore, if your sinaweibo ID & Password are too long, please define a bigger buffer.

Following is socket operation: Line 11 is use for initialize a TCP socket. Line 12 is request TCP Server for connection. Line 13 is waiting for connection set up continuously. After connect to the TCP Server, Line 14~15 will build the weibo HTTP data package and line 16 will send it to server.

After receive the return from server, line 17 will read the data length which received by W5500. If the data length is larger than 0, line 18 will read the data from W5500’s RX buffer to tmp_buf. Since the data package included the

HTTP header, the second line of line 18 will cut the HTTP header as we just care about the return result from server.  If received the return from server, it means that the connection with server is successful. But even connection is success; weibo is not 100% send out. Please refer the following table for the different type of server result.

Server return data

Explanation & Troubleshooting

1: weibo id or password errorSINA weibo user name or password is empty. Please check you have input the correct user name or password of your weibo in line 6~7 of source code. And also check the data package of line 14 is correct or not.
2: login failedLogin failed.Please check you have input the correct user name or password of your weibo in line 6~7 of source code.

3: weibo content(status) is invalid

The contents of weibo is empty.

Please make sure that you write the 140 characters for posting to weibo correctly when call the function of post_weibo().

4: too frequent message ###.###Sending weibo message too frequently.In order to control the flow of the server, we require each Weib account only can sent the message at intervals of two minutes. If the intervals of existing Weibo posting message is smaller than 2 minutes compare to last Weibo message, user will receive the error message. ###.### means the intervals between existing Weibo message and last Weibo message. Unit is second.
255: okSINA Weibo posting successfully.
Other messageUnknown Error

 

Lastly, line 19 is use for disconnect the connection with server. And line 20 will close the socket. If it success to communicate with server, line 21 will return 1.

This is all of the source code. You can compile it and download into MCU. Then power ON, see the serial debug message: “255:ok”. It means you success to let your device auto posting the status to weibo. Please access your weibo to check the data really post which you typing in the source code.

The following is one of example:
W5500EVB-hk3

Is it easy to implement? Is it more powerful when the dispenser directly connecting to Internet? Please quick try to let your device connecting to Internet directly!!!!

Article from : http://wizwiki.net/forum/viewtopic.php?f=91&t=266(Chines)

COMMENTS

Please Login to comment
  Subscribe  
Notify of