MQTT Client example program. Ethernet connection is via an ENC28J60 module.
Dependencies: UIPEthernet MQTTClient
Diff: main.cpp
- Revision:
- 1:49fb5754df16
- Parent:
- 0:158b106f3cfc
- Child:
- 2:6cd2390302ac
diff -r 158b106f3cfc -r 49fb5754df16 main.cpp --- a/main.cpp Mon Sep 15 12:51:58 2014 +0000 +++ b/main.cpp Sat Dec 20 12:04:36 2014 +0000 @@ -1,4 +1,4 @@ -// In this example an MQTT client is created using an ENC28J60 chip/board +// In this example an MQTT client is created using an ENC28J60 // It is publishing a simple 'example/hello' message 'Hello World.' // and subscribes to 'outdoor/temperature' messages // UIPEthernet library is used to drive the ENC28J60 @@ -23,16 +23,18 @@ UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs #endif -Serial pc(PA_9, PA_10); +//Serial pc(PA_9, PA_10); +Serial pc(USBTX, USBRX); + // Make sure that the MAC number is unique within the connected network. const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; // IP address must be unique and compatible with your network too. Change as appropriate. const IPAddress MY_IP(192,168,1,181); char message_buff[100]; -void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); IPAddress serverIP(192,168,1,30); // MQTT server (e.g. 'mosquitto' running on a Ubuntu PC or Raspberry Pi) EthernetClient ethernetClient; +void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient); int main() @@ -45,8 +47,8 @@ time_t lastTime = t; // initialize the ethernet device - Ethernet.begin(MY_MAC, MY_IP); -// IPAddress localIP = Ethernet.localIP(); + UIPEthernet.begin(MY_MAC, MY_IP); +// IPAddress localIP = UIPEthernet.localIP(); // pc.printf("IP = "); // for(uint8_t i = 0; i < 3; i++) // pc.printf("%d.", localIP[i]);