init

Dependencies:   mbed C12832 EthernetInterface MQTT mbed-rtos picojson

Committer:
co838_mgl6
Date:
Thu May 05 14:02:24 2016 +0000
Revision:
3:f809d8f8e572
Parent:
2:a4a8003e29bd
final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co838_mgl6 1:1e45dd2c91fb 1 #include "LCD.h"
co838_mgl6 1:1e45dd2c91fb 2 #include "MathAlgo.hpp"
co838_mgl6 1:1e45dd2c91fb 3
co838_mgl6 1:1e45dd2c91fb 4 #define MQTTCLIENT_QOS2 1
co838_mgl6 1:1e45dd2c91fb 5
co838_mgl6 1:1e45dd2c91fb 6 #include "MQTTEthernet.h"
co838_mgl6 1:1e45dd2c91fb 7 #include "MQTTClient.h"
co838_mgl6 2:a4a8003e29bd 8 #include "picojson.h"
co838_mgl6 0:d094cd2a86fd 9
co838_mgl6 1:1e45dd2c91fb 10 LCD lcd;
co838_mgl6 2:a4a8003e29bd 11 PwmOut spkr(D6);
co838_mgl6 0:d094cd2a86fd 12
co838_mgl6 1:1e45dd2c91fb 13 #define LED2_OFF 1
co838_mgl6 1:1e45dd2c91fb 14 #define LED2_ON 0
co838_mgl6 1:1e45dd2c91fb 15
co838_mgl6 1:1e45dd2c91fb 16 Serial pc(USBTX, USBRX);
co838_mgl6 1:1e45dd2c91fb 17
co838_mgl6 1:1e45dd2c91fb 18 #define printf pc.printf
co838_mgl6 1:1e45dd2c91fb 19
co838_mgl6 1:1e45dd2c91fb 20 int arrivedcount = 0;
co838_mgl6 1:1e45dd2c91fb 21
co838_mgl6 2:a4a8003e29bd 22 enum string_code
co838_mgl6 2:a4a8003e29bd 23 {
co838_mgl6 2:a4a8003e29bd 24 ALERT,
co838_mgl6 2:a4a8003e29bd 25 LAST,
co838_mgl6 2:a4a8003e29bd 26 NOTKNOWN
co838_mgl6 2:a4a8003e29bd 27 };
co838_mgl6 2:a4a8003e29bd 28
co838_mgl6 2:a4a8003e29bd 29 string_code hashit (std::string const& inString)
co838_mgl6 2:a4a8003e29bd 30 {
co838_mgl6 2:a4a8003e29bd 31 if (inString == "/alert") return ALERT;
co838_mgl6 2:a4a8003e29bd 32 if (inString == "/last") return LAST;
co838_mgl6 2:a4a8003e29bd 33 return NOTKNOWN;
co838_mgl6 2:a4a8003e29bd 34 }
co838_mgl6 2:a4a8003e29bd 35
co838_mgl6 2:a4a8003e29bd 36 void alert(const std::string& msg)
co838_mgl6 2:a4a8003e29bd 37 {
co838_mgl6 2:a4a8003e29bd 38 picojson::value v;
co838_mgl6 2:a4a8003e29bd 39 const char * json = msg.c_str();
co838_mgl6 2:a4a8003e29bd 40 picojson::parse(v, json, json + msg.size());
co838_mgl6 2:a4a8003e29bd 41 picojson::array list = v.get("coordinates").get<picojson::array>();
co838_mgl6 2:a4a8003e29bd 42 picojson::array::iterator iter = list.begin();
co838_mgl6 2:a4a8003e29bd 43 double lng = (*iter).get<double>();
co838_mgl6 2:a4a8003e29bd 44 ++iter;
co838_mgl6 2:a4a8003e29bd 45 double lat = (*iter).get<double>();
co838_mgl6 2:a4a8003e29bd 46 printf("ALERT : lng = %f / latt = %f\r\n", lng, lat);
co838_mgl6 2:a4a8003e29bd 47 lcd.print("Alert from [lng:" + mathalgo::stof(lng) + "/lat:" + mathalgo::stof(lat) + "]\n");
co838_mgl6 2:a4a8003e29bd 48 for (float i = 2000.0; i < 10000.0; i += 100) {
co838_mgl6 2:a4a8003e29bd 49 spkr.period(1.0/i);
co838_mgl6 2:a4a8003e29bd 50 spkr=0.5;
co838_mgl6 2:a4a8003e29bd 51 wait(0.02);
co838_mgl6 2:a4a8003e29bd 52 }
co838_mgl6 2:a4a8003e29bd 53 spkr=0.0;
co838_mgl6 2:a4a8003e29bd 54 }
co838_mgl6 2:a4a8003e29bd 55
co838_mgl6 2:a4a8003e29bd 56 void last(const std::string& msg)
co838_mgl6 2:a4a8003e29bd 57 {
co838_mgl6 2:a4a8003e29bd 58 picojson::value v;
co838_mgl6 2:a4a8003e29bd 59 const char * json = msg.c_str();
co838_mgl6 2:a4a8003e29bd 60 picojson::parse(v, json, json + msg.size());
co838_mgl6 2:a4a8003e29bd 61 picojson::array list = v.get("coordinates").get<picojson::array>();
co838_mgl6 2:a4a8003e29bd 62 picojson::array::iterator iter = list.begin();
co838_mgl6 2:a4a8003e29bd 63 double lng = (*iter).get<double>();
co838_mgl6 2:a4a8003e29bd 64 ++iter;
co838_mgl6 2:a4a8003e29bd 65 double lat = (*iter).get<double>();
co838_mgl6 2:a4a8003e29bd 66 ++iter;
co838_mgl6 2:a4a8003e29bd 67 double depth = (*iter).get<double>();
co838_mgl6 2:a4a8003e29bd 68 double mag = v.get("mag").get<double>();
co838_mgl6 2:a4a8003e29bd 69 std::string title = v.get("title").get<std::string>();
co838_mgl6 2:a4a8003e29bd 70 lcd.print("Event[lng:" + mathalgo::stof(lng) + "/lat:" + mathalgo::stof(lat) + "]\n");
co838_mgl6 2:a4a8003e29bd 71 lcd.print("Title: " + title + "\n");
co838_mgl6 2:a4a8003e29bd 72 lcd.print("Magni: " + mathalgo::stof(mag) + "| Depth:" + mathalgo::stof(depth) + "\n");
co838_mgl6 2:a4a8003e29bd 73 }
co838_mgl6 1:1e45dd2c91fb 74
co838_mgl6 1:1e45dd2c91fb 75 void messageArrived(MQTT::MessageData& md)
co838_mgl6 0:d094cd2a86fd 76 {
co838_mgl6 2:a4a8003e29bd 77 MQTT::Message &message = md.message;
co838_mgl6 1:1e45dd2c91fb 78 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
co838_mgl6 1:1e45dd2c91fb 79 printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
co838_mgl6 2:a4a8003e29bd 80 std::string payload ((char*)message.payload, 0, message.payloadlen);
co838_mgl6 2:a4a8003e29bd 81 std::string topic (md.topicName.lenstring.data, 0, md.topicName.lenstring.len);
co838_mgl6 1:1e45dd2c91fb 82 ++arrivedcount;
co838_mgl6 2:a4a8003e29bd 83
co838_mgl6 2:a4a8003e29bd 84 switch (hashit(topic))
co838_mgl6 2:a4a8003e29bd 85 {
co838_mgl6 2:a4a8003e29bd 86 case ALERT: alert(payload); break;
co838_mgl6 2:a4a8003e29bd 87 case LAST: last(payload); break;
co838_mgl6 2:a4a8003e29bd 88 case NOTKNOWN: printf("Command unknown [%s]\r\n", topic.c_str()); break;
co838_mgl6 2:a4a8003e29bd 89 }
co838_mgl6 0:d094cd2a86fd 90 }
co838_mgl6 1:1e45dd2c91fb 91
co838_mgl6 1:1e45dd2c91fb 92 int main(int argc, char* argv[])
co838_mgl6 1:1e45dd2c91fb 93 {
co838_mgl6 1:1e45dd2c91fb 94 char* hostname = "92.222.87.208";
co838_mgl6 1:1e45dd2c91fb 95 int port = 1883;
co838_mgl6 2:a4a8003e29bd 96 char* topic1 = "/alert";
co838_mgl6 2:a4a8003e29bd 97 char* topic2 = "/last";
co838_mgl6 2:a4a8003e29bd 98
co838_mgl6 2:a4a8003e29bd 99 pc.baud(38400);
co838_mgl6 2:a4a8003e29bd 100 MQTTEthernet ipstack = MQTTEthernet();
co838_mgl6 2:a4a8003e29bd 101 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
co838_mgl6 1:1e45dd2c91fb 102 printf("Connecting to %s:%d\r\n", hostname, port);
co838_mgl6 1:1e45dd2c91fb 103 int rc = ipstack.connect(hostname, port);
co838_mgl6 1:1e45dd2c91fb 104 if (rc != 0)
co838_mgl6 1:1e45dd2c91fb 105 printf("rc from TCP connect is %d\r\n", rc);
co838_mgl6 1:1e45dd2c91fb 106
co838_mgl6 1:1e45dd2c91fb 107 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
co838_mgl6 1:1e45dd2c91fb 108 data.MQTTVersion = 3;
co838_mgl6 1:1e45dd2c91fb 109 data.clientID.cstring = "mbed-k64f";
co838_mgl6 1:1e45dd2c91fb 110 if ((rc = client.connect(data)) != 0)
co838_mgl6 1:1e45dd2c91fb 111 printf("rc from MQTT connect is %d\r\n", rc);
co838_mgl6 1:1e45dd2c91fb 112
co838_mgl6 2:a4a8003e29bd 113 if ((rc = client.subscribe(topic1, MQTT::QOS1, messageArrived)) != 0)
co838_mgl6 2:a4a8003e29bd 114 printf("rc from MQTT subscribe /alert is %d\r\n", rc);
co838_mgl6 2:a4a8003e29bd 115 if ((rc = client.subscribe(topic2, MQTT::QOS1, messageArrived)) != 0)
co838_mgl6 2:a4a8003e29bd 116 printf("rc from MQTT subscribe /last is %d\r\n", rc);
co838_mgl6 2:a4a8003e29bd 117
co838_mgl6 1:1e45dd2c91fb 118 printf("GO RECEIVE\r\n");
co838_mgl6 1:1e45dd2c91fb 119 while (1) {
co838_mgl6 1:1e45dd2c91fb 120 client.yield(100);
co838_mgl6 1:1e45dd2c91fb 121 }
co838_mgl6 1:1e45dd2c91fb 122 return 0;
co838_mgl6 1:1e45dd2c91fb 123 }