not running

Dependencies:   TextLCD MQTT

Committer:
hyan99
Date:
Tue Dec 10 22:29:09 2019 +0000
Revision:
0:3b4906b8a747
Child:
1:19c3299ea83a
error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hyan99 0:3b4906b8a747 1 #include "Communication.h"
hyan99 0:3b4906b8a747 2
hyan99 0:3b4906b8a747 3 Communication* Communication::singleton = NULL;
hyan99 0:3b4906b8a747 4 int Communication::speeds[5] = {0, 0, 0, 0, 0};
hyan99 0:3b4906b8a747 5 int Communication::sync = 0;
hyan99 0:3b4906b8a747 6 int Communication::flags[5] = {0x01, 0x02, 0x04, 0x08, 0x10};
hyan99 0:3b4906b8a747 7 EventFlags Communication::control_flags;
hyan99 0:3b4906b8a747 8 EventFlags Communication::sync_flags;
hyan99 0:3b4906b8a747 9
hyan99 0:3b4906b8a747 10 Serial pc_comm(USBTX, USBRX);
hyan99 0:3b4906b8a747 11
hyan99 0:3b4906b8a747 12 Communication* Communication::getInstance() {
hyan99 0:3b4906b8a747 13 if (singleton == NULL) {
hyan99 0:3b4906b8a747 14 Communication();
hyan99 0:3b4906b8a747 15 }
hyan99 0:3b4906b8a747 16 return singleton;
hyan99 0:3b4906b8a747 17 }
hyan99 0:3b4906b8a747 18
hyan99 0:3b4906b8a747 19 Communication::Communication()
hyan99 0:3b4906b8a747 20 {
hyan99 0:3b4906b8a747 21 singleton = this;
hyan99 0:3b4906b8a747 22 setup_wifi();
hyan99 0:3b4906b8a747 23 if (wifi == NULL) {
hyan99 0:3b4906b8a747 24 pc_comm.printf("Failed to set up Wifi.");
hyan99 0:3b4906b8a747 25 }
hyan99 0:3b4906b8a747 26 MQTTNetwork network(wifi);
hyan99 0:3b4906b8a747 27 setup_mqtt(network);
hyan99 0:3b4906b8a747 28 if (client == NULL) {
hyan99 0:3b4906b8a747 29 pc_comm.printf("Failed to set up Client.");
hyan99 0:3b4906b8a747 30 }
hyan99 0:3b4906b8a747 31 }
hyan99 0:3b4906b8a747 32
hyan99 0:3b4906b8a747 33 void Communication::setup_wifi() {
hyan99 0:3b4906b8a747 34 // Get a handle to the WiFi module
hyan99 0:3b4906b8a747 35 wifi = WiFiInterface::get_default_instance();
hyan99 0:3b4906b8a747 36
hyan99 0:3b4906b8a747 37 // Connect the module to the wifi, based on the SSID and password
hyan99 0:3b4906b8a747 38 // specified in the mbed_app.json configuration file
hyan99 0:3b4906b8a747 39 // If you are using AirPennNet-Device, this will not succeed until the MAC
hyan99 0:3b4906b8a747 40 // address (printed shortly after this) is registered
hyan99 0:3b4906b8a747 41 pc_comm.printf("Connecting to wifi\r\n");
hyan99 0:3b4906b8a747 42 int rc = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
hyan99 0:3b4906b8a747 43
hyan99 0:3b4906b8a747 44 // Print out the MAC address of the wifi module. The MAC address is
hyan99 0:3b4906b8a747 45 // needed to register the device with AirPennNet-Device, so that you
hyan99 0:3b4906b8a747 46 // can use the campus wifi
hyan99 0:3b4906b8a747 47 pc_comm.printf("MAC Address: ");
hyan99 0:3b4906b8a747 48 pc_comm.printf(wifi->get_mac_address());
hyan99 0:3b4906b8a747 49 pc_comm.printf("\r\n");
hyan99 0:3b4906b8a747 50
hyan99 0:3b4906b8a747 51 if (rc != 0) {
hyan99 0:3b4906b8a747 52 pc_comm.printf("Problem connecting to wifi\r\n");
hyan99 0:3b4906b8a747 53 return;
hyan99 0:3b4906b8a747 54 } else {
hyan99 0:3b4906b8a747 55 pc_comm.printf("Wifi connected\r\n");
hyan99 0:3b4906b8a747 56 }
hyan99 0:3b4906b8a747 57 }
hyan99 0:3b4906b8a747 58
hyan99 0:3b4906b8a747 59
hyan99 0:3b4906b8a747 60 void Communication::setup_mqtt(MQTTNetwork &network) {
hyan99 0:3b4906b8a747 61 // the hostname and port point to a Google Cloud MQTT server we setup for
hyan99 0:3b4906b8a747 62 // this project
hyan99 0:3b4906b8a747 63 const char* hostname = "34.68.206.11";
hyan99 0:3b4906b8a747 64 int port = 1883;
hyan99 0:3b4906b8a747 65
hyan99 0:3b4906b8a747 66 // Create the underlying network connection to the MQTT server
hyan99 0:3b4906b8a747 67 pc_comm.printf("Connecting to %s:%d\r\n", hostname, port);
hyan99 0:3b4906b8a747 68 int rc = network.connect(hostname, port);
hyan99 0:3b4906b8a747 69 if (rc != 0) {
hyan99 0:3b4906b8a747 70 pc_comm.printf("There was an error with the TCP connect: %d\r\n", rc);
hyan99 0:3b4906b8a747 71 return;
hyan99 0:3b4906b8a747 72 }
hyan99 0:3b4906b8a747 73
hyan99 0:3b4906b8a747 74 pc_comm.printf("Connected to %s:%d\r\n", hostname, port);
hyan99 0:3b4906b8a747 75
hyan99 0:3b4906b8a747 76 // Connect the MQTT client to the server
hyan99 0:3b4906b8a747 77 client = new MQTT::Client<MQTTNetwork, Countdown>(network);
hyan99 0:3b4906b8a747 78 rc = client->connect();
hyan99 0:3b4906b8a747 79 if (rc != 0) {
hyan99 0:3b4906b8a747 80 pc_comm.printf("There was an error with the MQTT connect: %d\r\n", rc);
hyan99 0:3b4906b8a747 81 return;
hyan99 0:3b4906b8a747 82 }
hyan99 0:3b4906b8a747 83
hyan99 0:3b4906b8a747 84 pc_comm.printf("MQTT connect successful!\r\n");
hyan99 0:3b4906b8a747 85 }
hyan99 0:3b4906b8a747 86
hyan99 0:3b4906b8a747 87 void Communication::message_arrived(MQTT::MessageData& md)
hyan99 0:3b4906b8a747 88 {
hyan99 0:3b4906b8a747 89 MQTT::Message &message = md.message;
hyan99 0:3b4906b8a747 90 pc_comm.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
hyan99 0:3b4906b8a747 91 pc_comm.printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
hyan99 0:3b4906b8a747 92 }
hyan99 0:3b4906b8a747 93
hyan99 0:3b4906b8a747 94 void Communication::control_arrived(MQTT::MessageData& md) {
hyan99 0:3b4906b8a747 95 MQTT::Message &message = md.message;
hyan99 0:3b4906b8a747 96
hyan99 0:3b4906b8a747 97 char* payload = (char*) message.payload;
hyan99 0:3b4906b8a747 98
hyan99 0:3b4906b8a747 99 int id = (int) payload[0];
hyan99 0:3b4906b8a747 100 int speed = (int) payload[1];
hyan99 0:3b4906b8a747 101
hyan99 0:3b4906b8a747 102 speeds[id-1] = speed;
hyan99 0:3b4906b8a747 103 control_flags.set(flags[id-1]);
hyan99 0:3b4906b8a747 104 }
hyan99 0:3b4906b8a747 105
hyan99 0:3b4906b8a747 106 void Communication::sync_arrived(MQTT::MessageData& md) {
hyan99 0:3b4906b8a747 107 MQTT::Message &message = md.message;
hyan99 0:3b4906b8a747 108
hyan99 0:3b4906b8a747 109 char* payload = (char*) message.payload;
hyan99 0:3b4906b8a747 110 sync = (int) payload[0];
hyan99 0:3b4906b8a747 111 sync_flags.set(0x01);
hyan99 0:3b4906b8a747 112 }
hyan99 0:3b4906b8a747 113
hyan99 0:3b4906b8a747 114 int Communication::send_message(char* topic) {
hyan99 0:3b4906b8a747 115 pc_comm.printf("Sending a message!\r\n");
hyan99 0:3b4906b8a747 116 MQTT::Message message;
hyan99 0:3b4906b8a747 117
hyan99 0:3b4906b8a747 118 char buf[100];
hyan99 0:3b4906b8a747 119 sprintf(buf, "Hello World! This is a test message!\r\n");
hyan99 0:3b4906b8a747 120 int rc = client->publish(topic, (char*) buf, strlen(buf)+1, MQTT::QOS1);
hyan99 0:3b4906b8a747 121
hyan99 0:3b4906b8a747 122 if (rc != 0) {
hyan99 0:3b4906b8a747 123 return -1;
hyan99 0:3b4906b8a747 124 } else {
hyan99 0:3b4906b8a747 125 pc_comm.printf("Message sent!\r\n");
hyan99 0:3b4906b8a747 126 return 0;
hyan99 0:3b4906b8a747 127 }
hyan99 0:3b4906b8a747 128 }
hyan99 0:3b4906b8a747 129
hyan99 0:3b4906b8a747 130 int Communication::subscribe_to_topic_position(char* topic) {
hyan99 0:3b4906b8a747 131 int rc = client->subscribe(topic, MQTT::QOS1, message_arrived);
hyan99 0:3b4906b8a747 132 if (rc != 0) {
hyan99 0:3b4906b8a747 133 pc_comm.printf("There was a problem subscribing: %d\r\n", rc);
hyan99 0:3b4906b8a747 134
hyan99 0:3b4906b8a747 135 // client->disconnect();
hyan99 0:3b4906b8a747 136 return -1;
hyan99 0:3b4906b8a747 137 } else {
hyan99 0:3b4906b8a747 138 pc_comm.printf("Subscribed!\r\n");
hyan99 0:3b4906b8a747 139 }
hyan99 0:3b4906b8a747 140 return rc;
hyan99 0:3b4906b8a747 141 }
hyan99 0:3b4906b8a747 142
hyan99 0:3b4906b8a747 143 int Communication::subscribe_to_topic_control(char* topic) {
hyan99 0:3b4906b8a747 144 int rc = client->subscribe(topic, MQTT::QOS1, control_arrived);
hyan99 0:3b4906b8a747 145 if (rc != 0) {
hyan99 0:3b4906b8a747 146 pc_comm.printf("There was a problem subscribing: %d\r\n", rc);
hyan99 0:3b4906b8a747 147
hyan99 0:3b4906b8a747 148 // client->disconnect();
hyan99 0:3b4906b8a747 149 return -1;
hyan99 0:3b4906b8a747 150 } else {
hyan99 0:3b4906b8a747 151 pc_comm.printf("Subscribed!\r\n");
hyan99 0:3b4906b8a747 152 }
hyan99 0:3b4906b8a747 153 return rc;
hyan99 0:3b4906b8a747 154 }
hyan99 0:3b4906b8a747 155
hyan99 0:3b4906b8a747 156 int Communication::subscribe_to_topic_sync(char* topic) {
hyan99 0:3b4906b8a747 157 int rc = client->subscribe(topic, MQTT::QOS1, message_arrived);
hyan99 0:3b4906b8a747 158 if (rc != 0) {
hyan99 0:3b4906b8a747 159 pc_comm.printf("There was a problem subscribing: %d\r\n", rc);
hyan99 0:3b4906b8a747 160
hyan99 0:3b4906b8a747 161 // client->disconnect();
hyan99 0:3b4906b8a747 162 return -1;
hyan99 0:3b4906b8a747 163 } else {
hyan99 0:3b4906b8a747 164 pc_comm.printf("Subscribed!\r\n");
hyan99 0:3b4906b8a747 165 }
hyan99 0:3b4906b8a747 166 return rc;
hyan99 0:3b4906b8a747 167 }
hyan99 0:3b4906b8a747 168
hyan99 0:3b4906b8a747 169 void Communication::publish_car(char* topic, int speed, int position) {
hyan99 0:3b4906b8a747 170 message_t *message = mpool.alloc();
hyan99 0:3b4906b8a747 171 message->topic = topic;
hyan99 0:3b4906b8a747 172 message->speed = speed;
hyan99 0:3b4906b8a747 173 message->position = position;
hyan99 0:3b4906b8a747 174 q_car.put(message);
hyan99 0:3b4906b8a747 175 }
hyan99 0:3b4906b8a747 176
hyan99 0:3b4906b8a747 177 void Communication::publish_road(char* topic, int num) {
hyan99 0:3b4906b8a747 178 message_t *message = mpool.alloc();
hyan99 0:3b4906b8a747 179 message->topic = topic;
hyan99 0:3b4906b8a747 180 message->speed = -1; // indicate this is for sync
hyan99 0:3b4906b8a747 181 message->position = num;
hyan99 0:3b4906b8a747 182 q_car.put(message);
hyan99 0:3b4906b8a747 183 }
hyan99 0:3b4906b8a747 184
hyan99 0:3b4906b8a747 185 void Communication::start() {
hyan99 0:3b4906b8a747 186 while(1) {
hyan99 0:3b4906b8a747 187 osEvent evt = q_car.get();
hyan99 0:3b4906b8a747 188 if (evt.status == osEventMessage) {
hyan99 0:3b4906b8a747 189 message_t *message = (message_t*)evt.value.p;
hyan99 0:3b4906b8a747 190 char* topic = message->topic;
hyan99 0:3b4906b8a747 191
hyan99 0:3b4906b8a747 192 if (message->speed == -1) {
hyan99 0:3b4906b8a747 193 char buf[1];
hyan99 0:3b4906b8a747 194 buf[0] = (char) message->position; // update number
hyan99 0:3b4906b8a747 195
hyan99 0:3b4906b8a747 196 int rc = client->publish(topic, (char*) buf, strlen(buf)+1, MQTT::QOS1);
hyan99 0:3b4906b8a747 197 if (rc != 0) {
hyan99 0:3b4906b8a747 198 pc_comm.printf("Failed to publish Road info: %s", topic);
hyan99 0:3b4906b8a747 199 } else {
hyan99 0:3b4906b8a747 200 pc_comm.printf("Message sent!\r\n");
hyan99 0:3b4906b8a747 201 }
hyan99 0:3b4906b8a747 202 } else {
hyan99 0:3b4906b8a747 203 char buf[2];
hyan99 0:3b4906b8a747 204 buf[0] = (char) message->position;
hyan99 0:3b4906b8a747 205 buf[1] = (char) message->speed;
hyan99 0:3b4906b8a747 206
hyan99 0:3b4906b8a747 207 int rc = client->publish(topic, (char*) buf, strlen(buf)+1, MQTT::QOS1);
hyan99 0:3b4906b8a747 208 if (rc != 0) {
hyan99 0:3b4906b8a747 209 pc_comm.printf("Failed to publish AccCar info: %s", topic);
hyan99 0:3b4906b8a747 210 } else {
hyan99 0:3b4906b8a747 211 pc_comm.printf("Message sent!\r\n");
hyan99 0:3b4906b8a747 212 }
hyan99 0:3b4906b8a747 213 }
hyan99 0:3b4906b8a747 214 mpool.free(message);
hyan99 0:3b4906b8a747 215 client->yield(100); // wait for 100ms
hyan99 0:3b4906b8a747 216 }
hyan99 0:3b4906b8a747 217 }
hyan99 0:3b4906b8a747 218 }
hyan99 0:3b4906b8a747 219
hyan99 0:3b4906b8a747 220 void Communication::reset() {
hyan99 0:3b4906b8a747 221 control_flags.clear();
hyan99 0:3b4906b8a747 222
hyan99 0:3b4906b8a747 223 if (thread != NULL) {
hyan99 0:3b4906b8a747 224 thread->terminate();
hyan99 0:3b4906b8a747 225 }
hyan99 0:3b4906b8a747 226
hyan99 0:3b4906b8a747 227 thread = new Thread();
hyan99 0:3b4906b8a747 228 thread->start( callback(this, &Communication::start) );
hyan99 0:3b4906b8a747 229 }
hyan99 0:3b4906b8a747 230
hyan99 0:3b4906b8a747 231 void Communication::stop() {
hyan99 0:3b4906b8a747 232 if (thread != NULL) {
hyan99 0:3b4906b8a747 233 thread->terminate();
hyan99 0:3b4906b8a747 234 }
hyan99 0:3b4906b8a747 235 }