mqtt_iot_explorer_tc_pm25_oled.c 7.0 KB
Newer Older
S
supowang 已提交
1 2 3 4 5
#include "tos_k.h"
#include "esp8266_tencent_firmware.h"
#include "tencent_firmware_module_wrapper.h"
#include "pm2d5_parser.h"
#include "oled.h"
6
#include "math.h"
S
supowang 已提交
7

S
supowang 已提交
8 9 10
#define WIFI_SSID               "Tencent-GuestWiFi"
#define WIFI_PASSWD             ""
#define PRODUCT_ID              "ZLC54JVTAH"
11 12
#define DEVICE_NAME             "node_20DEBC0A0000"
#define DEVICE_KEY              "QXiswx4I1T+bZyHOrHvaqg=="
S
supowang 已提交
13

S
supowang 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
//#define REPORT_DATA_TEMPLATE1    "{\"method\":\"report\",\"clientToken\":\"%s\"," \
//    "\"params\":{\"PM1_CF1\":%d,"	\
//    "\"PM2d5_CF1\":%d," \
//    "\"PM10_CF1\":%d," \
//    "\"PM1\":%d," \
//    "\"PM2d5\":%d," \
//    "\"PM10\":%d," \
//    "\"particles_0d3\":%d," \
//    "\"particles_0d5\":%d,"	\
//    "\"particles_1\":%d," \
//    "\"particles_2d5\":%d," \
//    "\"particles_5\":%d," \
//    "\"particles_10\":%d," \
//    "\"version\":%d," \
//    "\"Error\":%d" \
//    "}}"
S
supowang 已提交
30

S
supowang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
#define REPORT_DATA_TEMPLATE    "{\"method\":\"report\",\"clientToken\":\"%s\"," \
    "\"params\":{\"a\":%d,"	\
    "\"b\":%d," \
    "\"c\":%d," \
    "\"d\":%d," \
    "\"e\":%d," \
    "\"f\":%d," \
    "\"g\":%d," \
    "\"h\":%d,"	\
    "\"i\":%d," \
    "\"j\":%d," \
    "\"k\":%d," \
    "\"l\":%d," \
S
supowang 已提交
44 45
    "\"v\":%d," \
    "\"ec\":%d" \
46
    "}}"
S
supowang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

void default_message_handler(mqtt_message_t* msg)
{
    printf("callback:\r\n");
    printf("---------------------------------------------------------\r\n");
    printf("\ttopic:%s\r\n", msg->topic);
    printf("\tpayload:%s\r\n", msg->payload);
    printf("---------------------------------------------------------\r\n");
}

char payload[256] = {0};
static char report_topic_name[TOPIC_NAME_MAX_SIZE] = {0};
static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0};

k_mail_q_t mail_q;
pm2d5_data_u pm2d5_value;
uint8_t pm2d5_value_pool[3 * sizeof(pm2d5_data_u)];

65 66 67 68 69 70 71 72 73
void generate_client_token(char* buffer, size_t size)
{
    long client_token_value;
    
    memset(buffer, 0, size);
    client_token_value = ((long)tos_systick_get()) % (long)(pow(10, size));
    snprintf(buffer, size, "%ld", client_token_value);
}

S
supowang 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
void mqtt_demo_task(void)
{
    int ret = 0;
    int size = 0;
    int  i = 0;    
    char *product_id = PRODUCT_ID;
    char *device_name = DEVICE_NAME;
    char *key = DEVICE_KEY;
    
    device_info_t dev_info;
    memset(&dev_info, 0, sizeof(device_info_t));
    char str[16];   
    size_t mail_size;
87 88
    uint8_t report_error_count = 0;
    char client_token[10];
S
supowang 已提交
89 90 91 92 93 94 95 96

    /**
     * Please Choose your AT Port first, default is HAL_UART_2(USART2)
    */
    ret = esp8266_tencent_firmware_sal_init(HAL_UART_PORT_2);
    
    if (ret < 0) {
        printf("esp8266 tencent firmware sal init fail, ret is %d\r\n", ret);
97
        return;
S
supowang 已提交
98 99
    }
    
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    OLED_Clear();
    sprintf(str, "Connecting WIFI...");
    OLED_ShowString(0, 0, (uint8_t*)str, 16);
    while (1) {
        if ( esp8266_tencent_firmware_join_ap(WIFI_SSID, WIFI_PASSWD) == 0) {
            printf("module WIFI connect success\n");
            break;
        }
        printf("module WIFI connect fail\n");
        tos_sleep_ms(2000);
    }
    
    OLED_Clear();
    sprintf(str, "Connecting IoT Explorer...");
    OLED_ShowString(0, 0, (uint8_t*)str, 16);
S
supowang 已提交
115 116 117 118 119
    strncpy(dev_info.product_id, product_id, PRODUCT_ID_MAX_SIZE);
    strncpy(dev_info.device_name, device_name, DEVICE_NAME_MAX_SIZE);
    strncpy(dev_info.device_serc, key, DEVICE_SERC_MAX_SIZE);
    tos_tf_module_info_set(&dev_info, TLS_MODE_PSK);
    mqtt_param_t init_params = DEFAULT_MQTT_PARAMS;
120 121 122 123 124 125 126
    while (1) {
        if (tos_tf_module_mqtt_conn(init_params) == 0) {
            printf("module mqtt connect success\n");
            break;
        }
        printf("module mqtt connect fail\n");
        tos_sleep_ms(5000);
S
supowang 已提交
127 128
    }
    
129
    /* 开始订阅topic */
S
supowang 已提交
130 131 132 133 134 135 136 137 138 139 140
    size = snprintf(report_reply_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/down/property/%s/%s", product_id, device_name);

    if (size < 0 || size > sizeof(report_reply_topic_name) - 1) {
        printf("sub topic content length not enough! content size:%d  buf size:%d", size, (int)sizeof(report_reply_topic_name));
    }
    if (tos_tf_module_mqtt_sub(report_reply_topic_name, QOS0, default_message_handler) != 0) {
        printf("module mqtt sub fail\n");
    } else {
        printf("module mqtt sub success\n");
    }
    
141
    memset(report_topic_name, 0, sizeof(report_topic_name));
S
supowang 已提交
142 143 144 145 146 147
    size = snprintf(report_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/up/property/%s/%s", product_id, device_name);

    if (size < 0 || size > sizeof(report_topic_name) - 1) {
        printf("pub topic content length not enough! content size:%d  buf size:%d", size, (int)sizeof(report_topic_name));
    }
    
148
    /* 创建邮箱 */
S
supowang 已提交
149 150 151 152 153 154 155 156 157 158
    tos_mail_q_create(&mail_q, pm2d5_value_pool, 3, sizeof(pm2d5_data_u));
    
    HAL_NVIC_DisableIRQ(USART3_4_IRQn);
    
    if (pm2d5_parser_init() == -1) {
        printf("pm2d5 parser init fail\r\n");
        return;
    }
  
    while (1) {
159
        /* 通过接收邮件来读取数据 */
S
supowang 已提交
160 161 162 163
        HAL_NVIC_EnableIRQ(USART3_4_IRQn);
        tos_mail_q_pend(&mail_q, (uint8_t*)&pm2d5_value, &mail_size, TOS_TIME_FOREVER);
        HAL_NVIC_DisableIRQ(USART3_4_IRQn);
        
164
        //收到之后打印信息
S
supowang 已提交
165 166 167 168 169
        printf("\r\n\r\n\r\n");
        for (i = 0; i < 13; i++) {
            printf("data[%d]:%d ug/m3\r\n", i+1, pm2d5_value.data[i]);
        }
        
170
        /* 显示PM2.5的值 */
S
supowang 已提交
171
        OLED_Clear();
S
supowang 已提交
172
        sprintf(str, "PM2.5:%4d ug/m3", pm2d5_value.pm2d5_data.data2);
S
supowang 已提交
173 174
        OLED_ShowString(0,0,(uint8_t*)str,16);
        
175
        /* 上报值 */
176
        generate_client_token(client_token, sizeof(client_token));
S
supowang 已提交
177
        memset(payload, 0, sizeof(payload));
S
supowang 已提交
178
        snprintf(payload, sizeof(payload), REPORT_DATA_TEMPLATE, client_token,
179 180
                pm2d5_value.pm2d5_data.data1, pm2d5_value.pm2d5_data.data2,
                pm2d5_value.pm2d5_data.data3, pm2d5_value.pm2d5_data.data4,
S
supowang 已提交
181
                pm2d5_value.pm2d5_data.data5, pm2d5_value.pm2d5_data.data6,                
182 183 184
                pm2d5_value.pm2d5_data.data7, pm2d5_value.pm2d5_data.data8,
                pm2d5_value.pm2d5_data.data9, pm2d5_value.pm2d5_data.data10,
                pm2d5_value.pm2d5_data.data11, pm2d5_value.pm2d5_data.data12,
S
supowang 已提交
185
                2, pm2d5_value.pm2d5_data.err_code);
186 187 188 189
        
        if (tos_tf_module_mqtt_publ(report_topic_name, QOS0, payload) != 0) {
            report_error_count++;
            printf("module mqtt publ fail, count: %d\n", report_error_count);
S
supowang 已提交
190
            sprintf(str, "# report fail");
191
            OLED_ShowString(0,2,(uint8_t*)str,16);
S
supowang 已提交
192
        } else {
193 194
            report_error_count = 0;
            printf("module mqtt publ success\n");
S
supowang 已提交
195
            sprintf(str, "# report ok");
196 197 198
            OLED_ShowString(0,2,(uint8_t*)str,16);
        }
        
S
supowang 已提交
199
        tos_sleep_ms(5000);
S
supowang 已提交
200
        
S
supowang 已提交
201 202 203 204 205
    }
}

void application_entry(void *arg)
{
S
supowang 已提交
206
    char *str = "TencentOS-Tiny";
S
supowang 已提交
207
    
208
    /* 初始化OLED */
S
supowang 已提交
209 210 211 212 213 214 215 216 217 218
    OLED_Init();
    OLED_Clear();
    OLED_ShowString(0, 0, (uint8_t*)str, 16);
    
    mqtt_demo_task();
    while (1) {
        printf("This is a mqtt demo!\r\n");
        tos_task_delay(1000);
    }
}