earthquake finder

Dependencies:   C12832 EthernetInterface MQTT mbed-rtos mbed picojson

Committer:
co838_ab981
Date:
Mon May 02 14:59:41 2016 +0000
Revision:
0:bdc16d97b0cb
earthquake finder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co838_ab981 0:bdc16d97b0cb 1 /* Include mbed library */
co838_ab981 0:bdc16d97b0cb 2 #include "mbed.h"
co838_ab981 0:bdc16d97b0cb 3
co838_ab981 0:bdc16d97b0cb 4 /* Include LCD library */
co838_ab981 0:bdc16d97b0cb 5 #include "C12832.h"
co838_ab981 0:bdc16d97b0cb 6
co838_ab981 0:bdc16d97b0cb 7 /* include MQTT library */
co838_ab981 0:bdc16d97b0cb 8 #include "MQTTEthernet.h"
co838_ab981 0:bdc16d97b0cb 9 #include "MQTTClient.h"
co838_ab981 0:bdc16d97b0cb 10
co838_ab981 0:bdc16d97b0cb 11 /* Include Json Library */
co838_ab981 0:bdc16d97b0cb 12 #include "picojson.h"
co838_ab981 0:bdc16d97b0cb 13
co838_ab981 0:bdc16d97b0cb 14
co838_ab981 0:bdc16d97b0cb 15 /* Communication with the host (serial over USB) */
co838_ab981 0:bdc16d97b0cb 16 Serial host (USBTX, USBRX);
co838_ab981 0:bdc16d97b0cb 17
co838_ab981 0:bdc16d97b0cb 18 /* LED on main card */
co838_ab981 0:bdc16d97b0cb 19 DigitalOut gpo(D0);
co838_ab981 0:bdc16d97b0cb 20 DigitalOut ledR(LED_RED);
co838_ab981 0:bdc16d97b0cb 21 DigitalOut ledG(LED_GREEN);
co838_ab981 0:bdc16d97b0cb 22
co838_ab981 0:bdc16d97b0cb 23
co838_ab981 0:bdc16d97b0cb 24 /* LED on shield card */
co838_ab981 0:bdc16d97b0cb 25 PwmOut red(D5);
co838_ab981 0:bdc16d97b0cb 26 PwmOut green(D9);
co838_ab981 0:bdc16d97b0cb 27
co838_ab981 0:bdc16d97b0cb 28 /* LCD on the shield (128x32) */
co838_ab981 0:bdc16d97b0cb 29 C12832 shlcd (D11, D13, D12, D7, D10);
co838_ab981 0:bdc16d97b0cb 30
co838_ab981 0:bdc16d97b0cb 31 /* Joystics */
co838_ab981 0:bdc16d97b0cb 32 DigitalIn select(D4);
co838_ab981 0:bdc16d97b0cb 33
co838_ab981 0:bdc16d97b0cb 34 /* Speaker */
co838_ab981 0:bdc16d97b0cb 35 PwmOut speaker(D6);
co838_ab981 0:bdc16d97b0cb 36
co838_ab981 0:bdc16d97b0cb 37 static volatile int Check = 0;
co838_ab981 0:bdc16d97b0cb 38 static int err = 0;
co838_ab981 0:bdc16d97b0cb 39
co838_ab981 0:bdc16d97b0cb 40 /* Put all led off */
co838_ab981 0:bdc16d97b0cb 41 void ledOff()
co838_ab981 0:bdc16d97b0cb 42 {
co838_ab981 0:bdc16d97b0cb 43 ledR = 1;
co838_ab981 0:bdc16d97b0cb 44 ledG = 1;
co838_ab981 0:bdc16d97b0cb 45 red.write(1.0f);
co838_ab981 0:bdc16d97b0cb 46 green.write(1.0f);
co838_ab981 0:bdc16d97b0cb 47 }
co838_ab981 0:bdc16d97b0cb 48
co838_ab981 0:bdc16d97b0cb 49 /* Show hostname and port on lcd */
co838_ab981 0:bdc16d97b0cb 50 void showHostPort(char *host, int port)
co838_ab981 0:bdc16d97b0cb 51 {
co838_ab981 0:bdc16d97b0cb 52 shlcd.cls();
co838_ab981 0:bdc16d97b0cb 53 shlcd.locate(40,2);
co838_ab981 0:bdc16d97b0cb 54 shlcd.printf("Welcome");
co838_ab981 0:bdc16d97b0cb 55 shlcd.locate(5,10);
co838_ab981 0:bdc16d97b0cb 56 shlcd.printf("Connecting to : ");
co838_ab981 0:bdc16d97b0cb 57 shlcd.locate(5,18);
co838_ab981 0:bdc16d97b0cb 58 shlcd.printf("%s:%d", host, port);
co838_ab981 0:bdc16d97b0cb 59 wait(0.2f);
co838_ab981 0:bdc16d97b0cb 60 }
co838_ab981 0:bdc16d97b0cb 61
co838_ab981 0:bdc16d97b0cb 62 /* Show error msg on lcd */
co838_ab981 0:bdc16d97b0cb 63 void showErrorMsg(int rc)
co838_ab981 0:bdc16d97b0cb 64 {
co838_ab981 0:bdc16d97b0cb 65 shlcd.cls();
co838_ab981 0:bdc16d97b0cb 66 shlcd.locate(5,2);
co838_ab981 0:bdc16d97b0cb 67 shlcd.printf("An error occured,");
co838_ab981 0:bdc16d97b0cb 68 shlcd.locate(5,10);
co838_ab981 0:bdc16d97b0cb 69 shlcd.printf("pls restart the application");
co838_ab981 0:bdc16d97b0cb 70 shlcd.locate(5,18);
co838_ab981 0:bdc16d97b0cb 71 if (rc == 1)
co838_ab981 0:bdc16d97b0cb 72 shlcd.printf("Connection fail to the host");
co838_ab981 0:bdc16d97b0cb 73 else if (rc == 2)
co838_ab981 0:bdc16d97b0cb 74 shlcd.printf("Client: Invalid information");
co838_ab981 0:bdc16d97b0cb 75 else
co838_ab981 0:bdc16d97b0cb 76 shlcd.printf("Fail to subscribe");
co838_ab981 0:bdc16d97b0cb 77 wait(4.0f);
co838_ab981 0:bdc16d97b0cb 78 }
co838_ab981 0:bdc16d97b0cb 79
co838_ab981 0:bdc16d97b0cb 80 /* Show subscribe msg on lcd */
co838_ab981 0:bdc16d97b0cb 81 void showSubscribe(char *topic)
co838_ab981 0:bdc16d97b0cb 82 {
co838_ab981 0:bdc16d97b0cb 83 shlcd.cls();
co838_ab981 0:bdc16d97b0cb 84 shlcd.locate(40,2);
co838_ab981 0:bdc16d97b0cb 85 shlcd.printf("Welcome");
co838_ab981 0:bdc16d97b0cb 86 shlcd.locate(5,10);
co838_ab981 0:bdc16d97b0cb 87 shlcd.printf("Subscribe to : ");
co838_ab981 0:bdc16d97b0cb 88 shlcd.locate(5,18);
co838_ab981 0:bdc16d97b0cb 89 shlcd.printf("%s", topic);
co838_ab981 0:bdc16d97b0cb 90 wait(1.0f);
co838_ab981 0:bdc16d97b0cb 91 }
co838_ab981 0:bdc16d97b0cb 92
co838_ab981 0:bdc16d97b0cb 93 /* Show wait for data on lcd */
co838_ab981 0:bdc16d97b0cb 94 void showWaitData()
co838_ab981 0:bdc16d97b0cb 95 {
co838_ab981 0:bdc16d97b0cb 96 shlcd.cls();
co838_ab981 0:bdc16d97b0cb 97 shlcd.locate(40,2);
co838_ab981 0:bdc16d97b0cb 98 shlcd.printf("Welcome");
co838_ab981 0:bdc16d97b0cb 99 shlcd.locate(15,10);
co838_ab981 0:bdc16d97b0cb 100 shlcd.printf("Pls wait for data ...");
co838_ab981 0:bdc16d97b0cb 101 }
co838_ab981 0:bdc16d97b0cb 102
co838_ab981 0:bdc16d97b0cb 103 /* Put on/off the light and active the speaker. (Danger 1 = Higth danger, Danger 3 = Low danger) */
co838_ab981 0:bdc16d97b0cb 104 void danger1()
co838_ab981 0:bdc16d97b0cb 105 {
co838_ab981 0:bdc16d97b0cb 106 for (float i=2000.0; i<(float)10000.0; i+=(float)50.0) {
co838_ab981 0:bdc16d97b0cb 107 speaker.period((float)1.0/i);
co838_ab981 0:bdc16d97b0cb 108 speaker = 0.06;
co838_ab981 0:bdc16d97b0cb 109 ledR = ledR ? 0 : 1;
co838_ab981 0:bdc16d97b0cb 110 if (red.read() == 0)
co838_ab981 0:bdc16d97b0cb 111 red.write(1.0f);
co838_ab981 0:bdc16d97b0cb 112 else
co838_ab981 0:bdc16d97b0cb 113 red.write(0.0f);
co838_ab981 0:bdc16d97b0cb 114 wait(0.03);
co838_ab981 0:bdc16d97b0cb 115 }
co838_ab981 0:bdc16d97b0cb 116 speaker=0.0;
co838_ab981 0:bdc16d97b0cb 117 ledR = 0;
co838_ab981 0:bdc16d97b0cb 118 red.write(0.0f);
co838_ab981 0:bdc16d97b0cb 119 }
co838_ab981 0:bdc16d97b0cb 120
co838_ab981 0:bdc16d97b0cb 121 /* Put on/off the light and active the speaker. (Danger 1 = Higth danger, Danger 3 = Low danger) */
co838_ab981 0:bdc16d97b0cb 122 void danger2()
co838_ab981 0:bdc16d97b0cb 123 {
co838_ab981 0:bdc16d97b0cb 124 for (float i=2000.0; i<(float)10000.0; i+=(float)75.0) {
co838_ab981 0:bdc16d97b0cb 125 speaker.period((float)1.0/i);
co838_ab981 0:bdc16d97b0cb 126 speaker = 0.06;
co838_ab981 0:bdc16d97b0cb 127 ledR = ledR ? 0 : 1;
co838_ab981 0:bdc16d97b0cb 128 if (green.read() == 0)
co838_ab981 0:bdc16d97b0cb 129 green.write(1.0f);
co838_ab981 0:bdc16d97b0cb 130 else
co838_ab981 0:bdc16d97b0cb 131 green.write(0.0f);
co838_ab981 0:bdc16d97b0cb 132 if (red.read() == 0)
co838_ab981 0:bdc16d97b0cb 133 red.write(1.0f);
co838_ab981 0:bdc16d97b0cb 134 else
co838_ab981 0:bdc16d97b0cb 135 red.write(0.0f);
co838_ab981 0:bdc16d97b0cb 136 wait(0.03);
co838_ab981 0:bdc16d97b0cb 137 }
co838_ab981 0:bdc16d97b0cb 138 speaker=0.0;
co838_ab981 0:bdc16d97b0cb 139 ledG = 0;
co838_ab981 0:bdc16d97b0cb 140 ledR = 0;
co838_ab981 0:bdc16d97b0cb 141 red.write(0.0f);
co838_ab981 0:bdc16d97b0cb 142 green.write(0.0f);
co838_ab981 0:bdc16d97b0cb 143 }
co838_ab981 0:bdc16d97b0cb 144
co838_ab981 0:bdc16d97b0cb 145 /* Put on/off the light and active the speaker. (Danger 1 = Higth danger, Danger 3 = Low danger) */
co838_ab981 0:bdc16d97b0cb 146 void danger3()
co838_ab981 0:bdc16d97b0cb 147 {
co838_ab981 0:bdc16d97b0cb 148 for (float i=2000.0; i<(float)10000.0; i+=(float)100.0) {
co838_ab981 0:bdc16d97b0cb 149 speaker.period((float)1.0/i);
co838_ab981 0:bdc16d97b0cb 150 speaker = 0.06;
co838_ab981 0:bdc16d97b0cb 151 ledG = ledG ? 0 : 1;
co838_ab981 0:bdc16d97b0cb 152 if (green.read() == 0)
co838_ab981 0:bdc16d97b0cb 153 green.write(1.0f);
co838_ab981 0:bdc16d97b0cb 154 else
co838_ab981 0:bdc16d97b0cb 155 green.write(0.0f);
co838_ab981 0:bdc16d97b0cb 156 wait(0.03);
co838_ab981 0:bdc16d97b0cb 157 }
co838_ab981 0:bdc16d97b0cb 158 speaker=0.0;
co838_ab981 0:bdc16d97b0cb 159 ledG = 0;
co838_ab981 0:bdc16d97b0cb 160 green.write(0.0f);
co838_ab981 0:bdc16d97b0cb 161 }
co838_ab981 0:bdc16d97b0cb 162
co838_ab981 0:bdc16d97b0cb 163 /* Get msg from mqtt server, parse the json, show data on lcd and call the function danger. */
co838_ab981 0:bdc16d97b0cb 164 void messageArrived(MQTT::MessageData& md)
co838_ab981 0:bdc16d97b0cb 165 {
co838_ab981 0:bdc16d97b0cb 166 MQTT::Message &message = md.message;
co838_ab981 0:bdc16d97b0cb 167 picojson::value res;
co838_ab981 0:bdc16d97b0cb 168 string json((char*)message.payload);
co838_ab981 0:bdc16d97b0cb 169 const char *value = json.c_str();
co838_ab981 0:bdc16d97b0cb 170
co838_ab981 0:bdc16d97b0cb 171 picojson::parse(res, value, value + strlen(value));
co838_ab981 0:bdc16d97b0cb 172 if (Check == (10 * (int)res.get("nb").get<double>())) {
co838_ab981 0:bdc16d97b0cb 173 ledOff();
co838_ab981 0:bdc16d97b0cb 174 return ;
co838_ab981 0:bdc16d97b0cb 175 }
co838_ab981 0:bdc16d97b0cb 176 shlcd.cls();
co838_ab981 0:bdc16d97b0cb 177 shlcd.locate(-4,0);
co838_ab981 0:bdc16d97b0cb 178 shlcd.printf("%s", res.get("l").get<string>().c_str());
co838_ab981 0:bdc16d97b0cb 179 shlcd.locate(0,8);
co838_ab981 0:bdc16d97b0cb 180 shlcd.printf("%s", res.get("t").get<string>().c_str());
co838_ab981 0:bdc16d97b0cb 181 shlcd.locate(0,16);
co838_ab981 0:bdc16d97b0cb 182 shlcd.printf("Distance: %s, M: %s", res.get("di").get<string>().c_str(), res.get("m").get<string>().c_str());
co838_ab981 0:bdc16d97b0cb 183 ledOff();
co838_ab981 0:bdc16d97b0cb 184 if ((int)res.get("da").get<double>() == 1)
co838_ab981 0:bdc16d97b0cb 185 danger1();
co838_ab981 0:bdc16d97b0cb 186 else if ((int)res.get("da").get<double>() == 2)
co838_ab981 0:bdc16d97b0cb 187 danger2();
co838_ab981 0:bdc16d97b0cb 188 else
co838_ab981 0:bdc16d97b0cb 189 danger3();
co838_ab981 0:bdc16d97b0cb 190 Check = 10 * (int)res.get("nb").get<double>();
co838_ab981 0:bdc16d97b0cb 191 }
co838_ab981 0:bdc16d97b0cb 192
co838_ab981 0:bdc16d97b0cb 193
co838_ab981 0:bdc16d97b0cb 194 int main()
co838_ab981 0:bdc16d97b0cb 195 {
co838_ab981 0:bdc16d97b0cb 196 char *topic = "ab981_a5_iot";
co838_ab981 0:bdc16d97b0cb 197 char *hostname = "test.mosquitto.org";
co838_ab981 0:bdc16d97b0cb 198 int port = 1883;
co838_ab981 0:bdc16d97b0cb 199
co838_ab981 0:bdc16d97b0cb 200 host.baud (38400); /* set baud rate */
co838_ab981 0:bdc16d97b0cb 201 ledOff();
co838_ab981 0:bdc16d97b0cb 202 showHostPort(hostname, port);
co838_ab981 0:bdc16d97b0cb 203 MQTTEthernet ipstack = MQTTEthernet();
co838_ab981 0:bdc16d97b0cb 204 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
co838_ab981 0:bdc16d97b0cb 205 if (ipstack.connect(hostname, port) != 0) /* Connecting to server */
co838_ab981 0:bdc16d97b0cb 206 showErrorMsg(1);
co838_ab981 0:bdc16d97b0cb 207 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
co838_ab981 0:bdc16d97b0cb 208 data.MQTTVersion = 3;
co838_ab981 0:bdc16d97b0cb 209 data.clientID.cstring = "mbed-a5";
co838_ab981 0:bdc16d97b0cb 210 data.keepAliveInterval = 20;
co838_ab981 0:bdc16d97b0cb 211 data.cleansession = 1;
co838_ab981 0:bdc16d97b0cb 212 if (client.connect(data) != 0) /* send data client */
co838_ab981 0:bdc16d97b0cb 213 showErrorMsg(2);
co838_ab981 0:bdc16d97b0cb 214 showSubscribe(topic);
co838_ab981 0:bdc16d97b0cb 215 if (client.subscribe(topic, MQTT::QOS2, messageArrived) != 0) /* subscribe to channel */
co838_ab981 0:bdc16d97b0cb 216 showErrorMsg(3);
co838_ab981 0:bdc16d97b0cb 217 showWaitData();
co838_ab981 0:bdc16d97b0cb 218 while (!select || err != 1) {
co838_ab981 0:bdc16d97b0cb 219 client.yield(100);
co838_ab981 0:bdc16d97b0cb 220 }
co838_ab981 0:bdc16d97b0cb 221 shlcd.cls();
co838_ab981 0:bdc16d97b0cb 222 shlcd.locate(40,8);
co838_ab981 0:bdc16d97b0cb 223 shlcd.printf("Goodbye");
co838_ab981 0:bdc16d97b0cb 224 client.unsubscribe(topic);
co838_ab981 0:bdc16d97b0cb 225 client.disconnect();
co838_ab981 0:bdc16d97b0cb 226 ipstack.disconnect();
co838_ab981 0:bdc16d97b0cb 227 }