mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-11 04:28:14 +08:00
add:增加mqtt配置
add:device report add:增加未配置mqtt 服务器地址/端口时不尝试连接mqtt服务器 add:udp接收数据后json处理
This commit is contained in:
55
TC1/main.c
55
TC1/main.c
@@ -8,7 +8,7 @@
|
||||
|
||||
#define os_log(format, ...) custom_log("TC1", format, ##__VA_ARGS__)
|
||||
|
||||
char rtc_init=0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
||||
char rtc_init = 0; //sntpУʱ<D0A3>ɹ<EFBFBD><C9B9><EFBFBD>־λ
|
||||
|
||||
char strMac[12] = { 0 };
|
||||
|
||||
@@ -23,21 +23,39 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size )
|
||||
int i, j;
|
||||
UNUSED_PARAMETER( size );
|
||||
|
||||
sprintf( mico_system_context_get( )->micoSystemConfig.name, ZTC1_NAME );
|
||||
user_config_t* userConfigDefault = user_config_data;
|
||||
userConfigDefault->idx = -1;
|
||||
userConfigDefault->version=USER_CONFIG_VERSION;
|
||||
for(i=0;i<PLUG_NUM;i++)
|
||||
if ( strMac[0] == 0 )
|
||||
{
|
||||
userConfigDefault->plug[i].idx=-1;
|
||||
sprintf( userConfigDefault->plug[i].name, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d",i );
|
||||
for(j=0;j<PLUG_TIME_TASK_NUM;j++)
|
||||
IPStatusTypedef para;
|
||||
micoWlanGetIPStatus( ¶, Station );
|
||||
strcpy( strMac, para.mac);
|
||||
}
|
||||
|
||||
unsigned char mac1,mac2;
|
||||
mac1=strtohex(strMac[8],strMac[9]);
|
||||
mac2=strtohex(strMac[10],strMac[11]);
|
||||
|
||||
|
||||
sprintf( mico_system_context_get( )->micoSystemConfig.name, ZTC1_NAME,mac1,mac2 );
|
||||
user_config_t* userConfigDefault = user_config_data;
|
||||
|
||||
userConfigDefault->mqtt_ip[0] = 0;
|
||||
userConfigDefault->mqtt_port = 0;
|
||||
userConfigDefault->mqtt_user[0] = 0;
|
||||
userConfigDefault->mqtt_password[0] = 0;
|
||||
|
||||
userConfigDefault->idx = -1;
|
||||
userConfigDefault->version = USER_CONFIG_VERSION;
|
||||
for ( i = 0; i < PLUG_NUM; i++ )
|
||||
{
|
||||
userConfigDefault->plug[i].idx = -1;
|
||||
sprintf( userConfigDefault->plug[i].name, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d", i );
|
||||
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ )
|
||||
{
|
||||
userConfigDefault->plug[i].task[j].hour=0;
|
||||
userConfigDefault->plug[i].task[j].minute=0;
|
||||
userConfigDefault->plug[i].task[j].repeat=0x80;
|
||||
userConfigDefault->plug[i].task[j].on=0;
|
||||
userConfigDefault->plug[i].task[j].action=1;
|
||||
userConfigDefault->plug[i].task[j].hour = 0;
|
||||
userConfigDefault->plug[i].task[j].minute = 0;
|
||||
userConfigDefault->plug[i].task[j].repeat = 0x80;
|
||||
userConfigDefault->plug[i].task[j].on = 0;
|
||||
userConfigDefault->plug[i].task[j].action = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +94,7 @@ int application_start( void )
|
||||
MicoGpioInitialize( (mico_gpio_t) MICO_GPIO_5, OUTPUT_PUSH_PULL );
|
||||
user_led_set( 0 );
|
||||
|
||||
if (user_config->version!=USER_CONFIG_VERSION || user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23 )
|
||||
if ( user_config->version != USER_CONFIG_VERSION || user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23 )
|
||||
{
|
||||
os_log( "WARNGIN: user params restored!" );
|
||||
err = mico_system_context_restore( sys_config );
|
||||
@@ -97,17 +115,14 @@ int application_start( void )
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
wifi_init( );
|
||||
key_init( );
|
||||
err = user_mqtt_init( );
|
||||
require_noerr( err, exit );
|
||||
err = user_rtc_init();
|
||||
err = user_rtc_init( );
|
||||
require_noerr( err, exit );
|
||||
|
||||
|
||||
err = user_udp_init();
|
||||
err = user_udp_init( );
|
||||
while ( 1 )
|
||||
{
|
||||
// mico_thread_msleep(500);
|
||||
|
||||
29
TC1/main.h
29
TC1/main.h
@@ -4,16 +4,18 @@
|
||||
#include "mico.h"
|
||||
#include "MiCOKit_EXT.h"
|
||||
|
||||
#define ZTC1_NAME "zTC1"
|
||||
#define USER_CONFIG_VERSION 1
|
||||
#define TYPE 1
|
||||
#define TYPE_NAME "zTC1"
|
||||
|
||||
#define ZTC1_NAME "zTC1_%02X%02X"
|
||||
|
||||
#define USER_CONFIG_VERSION 1
|
||||
#define SETTING_MQTT_STRING_LENGTH_MAX 32 //<2F><><EFBFBD><EFBFBD> 4 <20>ֽڶ<D6BD><DAB6>롣
|
||||
|
||||
#define PLUG_NAME_LENGTH 32
|
||||
#define PLUG_NUM 6 //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#define PLUG_TIME_TASK_NUM 5 //ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5<EFBFBD>鶨ʱ<E9B6A8><CAB1><EFBFBD><EFBFBD>
|
||||
|
||||
|
||||
|
||||
#define Led MICO_GPIO_5
|
||||
#define Button MICO_GPIO_23
|
||||
|
||||
@@ -28,10 +30,8 @@
|
||||
#define Relay_5 MICO_GPIO_18
|
||||
#define Relay_NUM PLUG_NUM
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
char hour; //Сʱ
|
||||
char minute; //<2F><><EFBFBD><EFBFBD>
|
||||
uint8_t repeat; //bit7:һ<><D2BB> bit6-0:<3A><><EFBFBD><EFBFBD>-<2D><>һ
|
||||
@@ -40,7 +40,8 @@ typedef struct {
|
||||
|
||||
} user_plug_task_config_t;
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
char name[PLUG_NAME_LENGTH];
|
||||
char idx;
|
||||
user_plug_task_config_t task[PLUG_TIME_TASK_NUM];
|
||||
@@ -48,13 +49,19 @@ typedef struct {
|
||||
} user_plug_config_t;
|
||||
|
||||
//<2F>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
char mqtt_ip[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service ip
|
||||
int mqtt_port; //mqtt service port
|
||||
char mqtt_user[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user
|
||||
char mqtt_password[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user
|
||||
// char mqtt_device_id[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user device name
|
||||
|
||||
char version;
|
||||
char idx;
|
||||
user_plug_config_t plug[PLUG_NUM];
|
||||
} user_config_t;
|
||||
|
||||
|
||||
extern char rtc_init;
|
||||
|
||||
extern char strMac[12];
|
||||
|
||||
@@ -12,7 +12,15 @@ typedef struct _user_json_context_t
|
||||
int8_t val;
|
||||
} user_json_context_t;
|
||||
|
||||
void user_function_cmd_received(struct sockaddr *addr, uint8_t *pusrdata )
|
||||
void user_send( int udp_flag, uint8_t *s )
|
||||
{
|
||||
if ( udp_flag )
|
||||
user_udp_send( s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
else
|
||||
user_mqtt_send( "domoticz/in", s );
|
||||
}
|
||||
|
||||
void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
char *out;
|
||||
@@ -23,40 +31,25 @@ void user_function_cmd_received(struct sockaddr *addr, uint8_t *pusrdata )
|
||||
return;
|
||||
}
|
||||
|
||||
// //<2F><><EFBFBD><EFBFBD>device report
|
||||
// os_printf( "start json:device report\r\n" );
|
||||
// cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" );
|
||||
// if ( p_cmd && cJSON_IsString( p_cmd ) && os_strcmp( p_cmd->valuestring, "device report" ) == 0 )
|
||||
// {
|
||||
//
|
||||
// cJSON *pRoot = cJSON_CreateObject( );
|
||||
// cJSON_AddStringToObject( pRoot, "name", mqtt_device_id );
|
||||
// cJSON_AddStringToObject( pRoot, "mac", strMac );
|
||||
// cJSON_AddNumberToObject( pRoot, "type", TYPE );
|
||||
// cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME );
|
||||
// char *s = cJSON_Print( pRoot );
|
||||
// os_printf( "pRoot: %s\r\n", s );
|
||||
//
|
||||
// if ( addr )
|
||||
// {
|
||||
// if ( addr && pesp_conn->type == ESPCONN_UDP )
|
||||
// {
|
||||
// pesp_conn->type = ESPCONN_UDP;
|
||||
// pesp_conn->proto.udp->remote_port = 10181; //<2F><>ȡ<EFBFBD>˿<EFBFBD>
|
||||
// pesp_conn->proto.udp->remote_ip[0] = 255; //<2F><>ȡIP<49><50>ַ
|
||||
// pesp_conn->proto.udp->remote_ip[1] = 255;
|
||||
// pesp_conn->proto.udp->remote_ip[2] = 255;
|
||||
// pesp_conn->proto.udp->remote_ip[3] = 255;
|
||||
// }
|
||||
// espconn_send( pesp_conn, s, os_strlen( s ) ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// } else
|
||||
// {
|
||||
// user_mqtt_send( "domoticz/in", s );
|
||||
// }
|
||||
// cJSON_free( (void *) s );
|
||||
// cJSON_Delete( pRoot );
|
||||
// // cJSON_Delete(p_cmd);
|
||||
// }
|
||||
//<2F><><EFBFBD><EFBFBD>device report
|
||||
os_log( "start json:device report\r\n" );
|
||||
cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" );
|
||||
if ( p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "device report" ) == 0 )
|
||||
{
|
||||
|
||||
cJSON *pRoot = cJSON_CreateObject( );
|
||||
cJSON_AddStringToObject( pRoot, "name", sys_config->micoSystemConfig.name );
|
||||
cJSON_AddStringToObject( pRoot, "mac", strMac );
|
||||
cJSON_AddNumberToObject( pRoot, "type", TYPE );
|
||||
cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME );
|
||||
char *s = cJSON_Print( pRoot );
|
||||
os_log( "pRoot: %s\r\n", s );
|
||||
|
||||
user_send( udp_flag, s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
free( (void *) s );
|
||||
cJSON_Delete( pRoot );
|
||||
// cJSON_Delete(p_cmd);
|
||||
}
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>idx<64>ֶ<EFBFBD>int<6E><74><EFBFBD><EFBFBD> description<6F>ֶ<EFBFBD>string<6E><67><EFBFBD><EFBFBD> name<6D>ֶ<EFBFBD>string<6E><67><EFBFBD><EFBFBD>
|
||||
cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" );
|
||||
@@ -73,18 +66,32 @@ void user_function_cmd_received(struct sockaddr *addr, uint8_t *pusrdata )
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if (
|
||||
(p_idx && cJSON_IsNumber( p_idx ) && p_idx->valueint == 2) //idx
|
||||
|| (p_description && cJSON_IsString( p_description ) && strcmp( p_description->valuestring, "123" ) == 0) //description mqttid
|
||||
|| (p_name && cJSON_IsString( p_name ) && strcmp( p_name->valuestring, sys_config->micoSystemConfig.name ) == 0) //name
|
||||
)
|
||||
{
|
||||
cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
|
||||
if ( p_nvalue ) led( p_nvalue->valueint );
|
||||
}
|
||||
*/
|
||||
cJSON_Delete( pJsonRoot );
|
||||
free( out );
|
||||
|
||||
}
|
||||
|
||||
unsigned char strtohex( char a, char b )
|
||||
{
|
||||
if ( a >= 0x30 && a <= 0x39 )
|
||||
a -= 0x30;
|
||||
else if ( a >= 0x41 && a <= 0x46 )
|
||||
{
|
||||
a = a + 10 - 0x41;
|
||||
} else if ( a >= 0x61 && a <= 0x66 )
|
||||
{
|
||||
a = a + 10 - 0x61;
|
||||
}
|
||||
|
||||
if ( b >= 0x30 && b <= 0x39 )
|
||||
b -= 0x30;
|
||||
else if ( b >= 0x41 && b <= 0x46 )
|
||||
{
|
||||
b = b + 10 - 0x41;
|
||||
} else if ( b >= 0x61 && b <= 0x66 )
|
||||
{
|
||||
b = b + 10 - 0x61;
|
||||
}
|
||||
|
||||
return a * 16 + b;
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#include "mico.h"
|
||||
#include "MiCOKit_EXT.h"
|
||||
|
||||
void user_function_cmd_received(struct sockaddr *addr,uint8_t *pusrdata);
|
||||
|
||||
void user_function_cmd_received(int udp_flag,uint8_t *pusrdata);
|
||||
unsigned char strtohex(char a, char b);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -33,13 +33,12 @@
|
||||
* Constants
|
||||
******************************************************/
|
||||
|
||||
#define MQTT_CLIENT_ID "MQTT_Client_zTC1"
|
||||
#define MQTT_CLIENT_USERNAME "z"
|
||||
#define MQTT_CLIENT_PASSWORD "2633063"
|
||||
#define MQTT_CLIENT_ID sys_config->micoSystemConfig.name
|
||||
#define MQTT_CLIENT_USERNAME user_config->mqtt_user
|
||||
#define MQTT_CLIENT_PASSWORD user_config->mqtt_password
|
||||
#define MQTT_CLIENT_KEEPALIVE 30
|
||||
#define MQTT_CLIENT_SUB_TOPIC1 "test/mico/test0" // loop msg
|
||||
#define MQTT_CLIENT_SUB_TOPIC2 "domoticz/out" // loop msg
|
||||
#define MQTT_CLIENT_PUB_TOPIC "test/mico/test1"
|
||||
#define MQTT_CLIENT_SUB_TOPIC1 "domoticz/out" // loop msg
|
||||
#define MQTT_CLIENT_PUB_TOPIC "domoticz/in"
|
||||
#define MQTT_CMD_TIMEOUT 5000 // 5s
|
||||
#define MQTT_YIELD_TMIE 5000 // 5s
|
||||
//#define MQTT_CLIENT_SSL_ENABLE // ssl
|
||||
@@ -74,8 +73,8 @@ REyPOFdGdhBY2P1FNRy0MDr6xr+D2ZOwxs63dG1nnAnWZg7qwoLgpZ4fESPD3PkA\r\n\
|
||||
|
||||
#else // ! MQTT_CLIENT_SSL_ENABLE
|
||||
|
||||
#define MQTT_SERVER "47.112.16.98"
|
||||
#define MQTT_SERVER_PORT 1883
|
||||
#define MQTT_SERVER user_config->mqtt_ip
|
||||
#define MQTT_SERVER_PORT user_config->mqtt_port
|
||||
|
||||
#endif // MQTT_CLIENT_SSL_ENABLE
|
||||
|
||||
@@ -247,6 +246,8 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
||||
LinkStatusTypeDef LinkStatus;
|
||||
while ( 1 )
|
||||
{
|
||||
mico_rtos_thread_sleep( 3 );
|
||||
if(MQTT_SERVER[0]<0x20 ||MQTT_SERVER[0]>0x7f ||MQTT_SERVER_PORT<1) continue; //δ<><CEB4><EFBFBD><EFBFBD>mqtt<74><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
micoWlanGetLinkStatus( &LinkStatus );
|
||||
if ( LinkStatus.is_connected != 1 )
|
||||
@@ -259,7 +260,7 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
||||
rc = NewNetwork( &n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings );
|
||||
if ( rc == MQTT_SUCCESS ) break;
|
||||
mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc);
|
||||
mico_rtos_thread_sleep( 3 );
|
||||
|
||||
}
|
||||
|
||||
mqtt_log("MQTT network connection success!");
|
||||
@@ -290,10 +291,6 @@ void mqtt_client_thread( mico_thread_arg_t arg )
|
||||
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." );
|
||||
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", MQTT_CLIENT_SUB_TOPIC1);
|
||||
|
||||
rc = MQTTSubscribe( &c, MQTT_CLIENT_SUB_TOPIC2, QOS0, messageArrived );
|
||||
require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." );
|
||||
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", MQTT_CLIENT_SUB_TOPIC2);
|
||||
|
||||
/* 5. client loop for recv msg && keepalive */
|
||||
while ( 1 )
|
||||
{
|
||||
@@ -394,7 +391,7 @@ OSStatus user_recv_handler( void *arg )
|
||||
require( p_recv_msg, exit );
|
||||
|
||||
app_log("user get data success! from_topic=[%s], msg=[%ld][%s].\r\n", p_recv_msg->topic, p_recv_msg->datalen, p_recv_msg->data);
|
||||
user_function_cmd_received(NULL, p_recv_msg->data );
|
||||
user_function_cmd_received(0, p_recv_msg->data );
|
||||
free( p_recv_msg );
|
||||
|
||||
exit:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#define os_log(format, ...) custom_log("UDP", format, ##__VA_ARGS__)
|
||||
|
||||
#include "main.h"
|
||||
#include "user_function.h"
|
||||
|
||||
#define LOCAL_UDP_PORT 10182
|
||||
#define REMOTE_UDP_PORT 10181
|
||||
@@ -42,7 +43,7 @@ void udp_thread( void *arg )
|
||||
OSStatus err;
|
||||
struct sockaddr_in addr;
|
||||
fd_set readfds;
|
||||
struct timeval t = { 0, 5000 * 1000 }; //5s
|
||||
|
||||
socklen_t addrLen = sizeof(addr);
|
||||
int udp_fd = -1, len;
|
||||
p_udp_send_msg_t p_send_msg = NULL;
|
||||
@@ -87,8 +88,10 @@ void udp_thread( void *arg )
|
||||
require_action( len >= 0, exit, err = kConnectionErr );
|
||||
|
||||
strcpy( ip_address, inet_ntoa( addr.sin_addr ) );
|
||||
if(len<1024) buf[len]=0;
|
||||
os_log( "udp recv from %s:%d, len:%d :%s", ip_address,addr.sin_port, len ,buf);
|
||||
sendto( udp_fd, buf, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in) );
|
||||
user_function_cmd_received(1,buf);
|
||||
// sendto( udp_fd, buf, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in) );
|
||||
}
|
||||
|
||||
/* recv msg from user worker thread to be sent to server */
|
||||
|
||||
Reference in New Issue
Block a user