YX ZHANG / Mbed 2 deprecated EthW5500

Dependencies:   MQTT WIZnet_Library mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "networking.h"
00003 
00004 Serial pc(PA_9,PA_10);
00005 DigitalIn BTN(PC_13);
00006 DigitalOut LED(PB_8);
00007 
00008 void on_control_cmd(const char* actuator_name, const char* control_value)
00009 {
00010     pc.printf("Received CMD %s %s\r\n", actuator_name, control_value);
00011     if(strcmp(actuator_name, "led") == 0)
00012         LED = atoi(control_value);
00013 }
00014 
00015 int main() {
00016     
00017     MQTTSocket sock;
00018     MClient client(sock);
00019     
00020     //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
00021     const char* sensors[][2] = {
00022         "test", " ",
00023         "button", "V",
00024         NULL, NULL //最后一行以空指针作为结束标记
00025     };
00026 
00027     //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
00028     const char* actuators[][2] = {
00029         "led", "int",
00030         NULL, NULL //最后一行以空指针作为结束标记
00031     };
00032     networking_init(sock, client, "tdxls-iot.xicp.net", sensors, actuators, on_control_cmd);
00033 
00034     bool btn = 0;
00035     while(1){
00036         bool newBTN = BTN;
00037         if(newBTN != btn){
00038             char buf[16];
00039             int value = (bool)newBTN;
00040             
00041             sprintf(buf, "%d mV", value);
00042             publish_value(client,"button",buf);
00043             
00044             btn = newBTN;
00045         }else{
00046             client.yield(1000);
00047             publish_value(client,"test","hello world");
00048         }
00049     }
00050 }