模組化,中斷服務

Dependencies:   EthernetInterface MQTT RHT03 mbed-rtos mbed

Fork of MyTempuratureMqtt by Charlie Simms

main.cpp

Committer:
bruceyang
Date:
2015-05-21
Revision:
1:8b5f77770753
Parent:
0:5939450e4bef

File content as of revision 1:8b5f77770753:

#include "mbed.h"
#include "EthernetInterface.h"
#include "PubSubClient.h"

Serial pc(USBTX, USBRX);
DigitalOut led(LED1);
DigitalIn button(p21);

char* serverIpAddr = "128.199.127.110";  /*Sever ip address*/
int port = 443; /*Sever Port*/

void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/
PubSubClient mqtt(serverIpAddr, port, callback);
EthernetInterface  eth;

void callback(char* topic, char* payload, unsigned int len)
{
    //Send incoming payloads back to topic "/mbed".
    mqtt.publish("mbed", payload, len);
}

int main() {

    eth.init(); //Use DHCP
    eth.connect();
    pc.printf("IP Address is %s\n", eth.getIPAddress());
    
    pc.printf("MQTTClient Tester");
    
    
    char clientID[] = "mbed";   /*Client nanme show for MQTT server*/
    char pub_topic[] = "mbed";   /*Publish to topic : "/mbed" */
    char sub_topic[] = "mirror";   /*Subscribe to topic : "/mirror" */
    
    if(!mqtt.connect(clientID)){
        pc.printf("\r\nConnect to server failed ..\r\n");
        return -1;
    }
    
    pc.printf("\r\nConnect to server sucessed ..\r\n");
    
    mqtt.publish(pub_topic, "Hello here is mbed...");
    mqtt.subscribe(sub_topic);
       
    
   pc.printf("#### End of the test.. ####");
      
   //eth.disconnect();
    
    while(1) {
    mqtt.loop();
     if (button) {
            mqtt.publish(pub_topic, "ON!!");
            mqtt.publish("control", "ffffff");
            
            led = 1;
        } else {
            mqtt.publish(pub_topic, "OFF");
            mqtt.publish("control", "000000");
            led = 0;
        }
        wait(0.4); // simple debouncing
    }
}