Broker https://moodle.kent.ac.uk/2015/pluginfile.php/226454/mod_resource/content/2/co657_assessment_5.pdf

Dependencies:   C12832 EthernetInterface MQTT mbed-rtos mbed

Committer:
co657_fh98
Date:
Mon Jan 25 18:14:34 2016 +0000
Revision:
1:7f748889f3c9
Parent:
0:c46614e52b7c
Broker;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co657_fh98 0:c46614e52b7c 1 /**
co657_fh98 0:c46614e52b7c 2 CO657 Assessment 5 - Aggregation and Visualisation - SmartPlant
co657_fh98 0:c46614e52b7c 3 https://moodle.kent.ac.uk/2015/mod/resource/view.php?id=190889
co657_fh98 0:c46614e52b7c 4
co657_fh98 0:c46614e52b7c 5 Broker Mid Point Device
co657_fh98 0:c46614e52b7c 6 Receives temperature from Xbee and sends to MQTT.
co657_fh98 0:c46614e52b7c 7 Subscribes to commands MQTT and sends any received commands over Xbee.
co657_fh98 0:c46614e52b7c 8
co657_fh98 0:c46614e52b7c 9 Copyright (C) Frederick Harrington(fh98) and Harry Jones(hj80), University Of Kent
co657_fh98 0:c46614e52b7c 10 24/01/2016 **/
co657_fh98 0:c46614e52b7c 11
co657_fh98 0:c46614e52b7c 12 /** Libraries used **/
co657_fh98 0:c46614e52b7c 13 #include "mbed.h"
co657_fh98 0:c46614e52b7c 14 #include "C12832.h"
co657_fh98 0:c46614e52b7c 15 #include "PubSubClient.h"
co657_fh98 0:c46614e52b7c 16 #include "EthernetInterface.h"
co657_fh98 0:c46614e52b7c 17 #include <iostream>
co657_fh98 0:c46614e52b7c 18
co657_fh98 0:c46614e52b7c 19 /** Libraries used **/
co657_fh98 0:c46614e52b7c 20 C12832 lcd (D11, D13, D12, D7, D10);
co657_fh98 0:c46614e52b7c 21 Serial xbee (D1, D0);
co657_fh98 0:c46614e52b7c 22 EthernetInterface eth;
co657_fh98 0:c46614e52b7c 23 Serial pc (USBTX, USBRX);
co657_fh98 0:c46614e52b7c 24
co657_fh98 0:c46614e52b7c 25
co657_fh98 0:c46614e52b7c 26 /** Hardware declarations used **/
co657_fh98 0:c46614e52b7c 27 char* topicTopic = "unikent/users/fh98/topics";
co657_fh98 0:c46614e52b7c 28 char* tempTopic = "unikent/users/fh98/a5/plant";
co657_fh98 0:c46614e52b7c 29 char* commandsTopic = "unikent/users/fh98/a5/commands";
co657_fh98 0:c46614e52b7c 30 char* clientID = "fh98";
co657_fh98 0:c46614e52b7c 31 //char* ip = "129.12.3.210"; Not using doughnut.
co657_fh98 0:c46614e52b7c 32 char* ip = "85.119.83.194"; //test.mosquitto.org
co657_fh98 0:c46614e52b7c 33 int port = 1883;
co657_fh98 0:c46614e52b7c 34
co657_fh98 0:c46614e52b7c 35 //Callback for incoming messages.
co657_fh98 0:c46614e52b7c 36 void callback(char* topic, char* payload, unsigned int len){
co657_fh98 0:c46614e52b7c 37 lcd.printf("Topic: %s\r\n", topic);
co657_fh98 0:c46614e52b7c 38 lcd.printf("Payload: %s\r\n", payload);
co657_fh98 0:c46614e52b7c 39 xbee.printf(payload);
co657_fh98 0:c46614e52b7c 40 }
co657_fh98 0:c46614e52b7c 41
co657_fh98 0:c46614e52b7c 42 //MQTT declarations
co657_fh98 0:c46614e52b7c 43 PubSubClient mqtt (ip, port, callback);
co657_fh98 0:c46614e52b7c 44
co657_fh98 0:c46614e52b7c 45 //Main Method
co657_fh98 0:c46614e52b7c 46 int main()
co657_fh98 0:c46614e52b7c 47 {
co657_fh98 0:c46614e52b7c 48 //Function based declarations.
co657_fh98 0:c46614e52b7c 49 char str[10]={10};
co657_fh98 0:c46614e52b7c 50 eth.init();
co657_fh98 0:c46614e52b7c 51 eth.connect();
co657_fh98 0:c46614e52b7c 52
co657_fh98 0:c46614e52b7c 53 lcd.printf("Ethernet Working \r\n");
co657_fh98 0:c46614e52b7c 54 lcd.printf("IP: %s\r\n\r\n", eth.getIPAddress());
co657_fh98 0:c46614e52b7c 55 lcd.printf("Connecting to: \r\n");
co657_fh98 0:c46614e52b7c 56 lcd.printf("IP: %s\r\n", ip);
co657_fh98 0:c46614e52b7c 57 lcd.printf("Port: %d\r\n", port);
co657_fh98 0:c46614e52b7c 58 lcd.printf("\r\n");
co657_fh98 0:c46614e52b7c 59 wait(1.0);
co657_fh98 0:c46614e52b7c 60 lcd.cls();
co657_fh98 0:c46614e52b7c 61
co657_fh98 0:c46614e52b7c 62 bool connected = false;
co657_fh98 0:c46614e52b7c 63 int attempts = 10;
co657_fh98 0:c46614e52b7c 64
co657_fh98 0:c46614e52b7c 65 //While loops and if else for connecting to the MQTT server.
co657_fh98 0:c46614e52b7c 66 while(!connected){
co657_fh98 0:c46614e52b7c 67 if(!mqtt.connect(clientID)){
co657_fh98 0:c46614e52b7c 68 lcd.printf("Error connecting to MQTT, try:%d\r\n", attempts);
co657_fh98 0:c46614e52b7c 69 attempts--;
co657_fh98 0:c46614e52b7c 70 }
co657_fh98 0:c46614e52b7c 71 else{
co657_fh98 0:c46614e52b7c 72 lcd.printf("Connected to MQTT\r\n\r\n");
co657_fh98 0:c46614e52b7c 73 connected = true;
co657_fh98 0:c46614e52b7c 74 }
co657_fh98 0:c46614e52b7c 75 if(attempts == 0){
co657_fh98 0:c46614e52b7c 76 lcd.printf("Failed to connect to MQTT, aborting.\r\n");
co657_fh98 0:c46614e52b7c 77 return -1;
co657_fh98 0:c46614e52b7c 78 }
co657_fh98 0:c46614e52b7c 79 }
co657_fh98 0:c46614e52b7c 80
co657_fh98 0:c46614e52b7c 81 //Publishing to the topic based topic.
co657_fh98 0:c46614e52b7c 82 char buffer[150];
co657_fh98 0:c46614e52b7c 83 sprintf(buffer, "%s\n%s\n", tempTopic, commandsTopic);
co657_fh98 0:c46614e52b7c 84 mqtt.publish(topicTopic, buffer);
co657_fh98 0:c46614e52b7c 85
co657_fh98 0:c46614e52b7c 86 //Subscribe to the commands MQTT topic.
co657_fh98 0:c46614e52b7c 87 mqtt.subscribe(commandsTopic);
co657_fh98 0:c46614e52b7c 88
co657_fh98 0:c46614e52b7c 89 //Main loop
co657_fh98 0:c46614e52b7c 90 while (true) {
co657_fh98 0:c46614e52b7c 91 //Reading from Xbee.
co657_fh98 0:c46614e52b7c 92 xbee.scanf("%s", str);
co657_fh98 0:c46614e52b7c 93 //LCD display.
co657_fh98 0:c46614e52b7c 94 lcd.cls();
co657_fh98 0:c46614e52b7c 95 lcd.locate(1,1);
co657_fh98 0:c46614e52b7c 96 lcd.printf("Broker");
co657_fh98 0:c46614e52b7c 97 lcd.locate(32,12);
co657_fh98 0:c46614e52b7c 98 lcd.printf("Last Published");
co657_fh98 0:c46614e52b7c 99 lcd.locate(40,21);
co657_fh98 0:c46614e52b7c 100 lcd.printf("%sC", str);
co657_fh98 0:c46614e52b7c 101 //Publish to the temperature topic.
co657_fh98 0:c46614e52b7c 102 mqtt.publish(tempTopic, str);
co657_fh98 0:c46614e52b7c 103 wait(5.0);
co657_fh98 0:c46614e52b7c 104 //Loop the subscribed topics.
co657_fh98 0:c46614e52b7c 105 mqtt.loop();
co657_fh98 0:c46614e52b7c 106 }
co657_fh98 0:c46614e52b7c 107 }