![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
1
main.cpp@100:642bee2ce173, 2022-01-20 (annotated)
- Committer:
- awali
- Date:
- Thu Jan 20 09:44:11 2022 +0000
- Revision:
- 100:642bee2ce173
- Parent:
- 91:dab9882e2b49
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
awali | 100:642bee2ce173 | 1 | /* EXAMPLE */ |
awali | 100:642bee2ce173 | 2 | #include "mbed.h" |
awali | 100:642bee2ce173 | 3 | #include "hcsr04.h" |
awali | 100:642bee2ce173 | 4 | |
awali | 100:642bee2ce173 | 5 | //D12 TRIGGER D11 ECHO |
awali | 100:642bee2ce173 | 6 | HCSR04 sensor(D12, D11); |
awali | 100:642bee2ce173 | 7 | |
awali | 100:642bee2ce173 | 8 | |
awali | 100:642bee2ce173 | 9 | |
awali | 100:642bee2ce173 | 10 | #include "MQTTmbed.h" |
awali | 100:642bee2ce173 | 11 | |
awali | 100:642bee2ce173 | 12 | #include <MQTTClientMbedOs.h> |
awali | 100:642bee2ce173 | 13 | |
awali | 100:642bee2ce173 | 14 | #include <cstdio> |
awali | 100:642bee2ce173 | 15 | |
mbed_official | 0:857719181846 | 16 | |
mbed_official | 0:857719181846 | 17 | #include "mbed.h" |
awali | 100:642bee2ce173 | 18 | #include <LiquidCrystal_I2C.h> |
mbed_official | 0:857719181846 | 19 | |
awali | 100:642bee2ce173 | 20 | // Set the LCD address to 0x27 for a 16 chars and 2 line display |
awali | 100:642bee2ce173 | 21 | LiquidCrystal_I2C lcd(0x4E, 16, 2); |
awali | 100:642bee2ce173 | 22 | |
awali | 100:642bee2ce173 | 23 | |
awali | 100:642bee2ce173 | 24 | int main(int argc, char *argv[]) |
awali | 100:642bee2ce173 | 25 | { |
awali | 100:642bee2ce173 | 26 | |
awali | 100:642bee2ce173 | 27 | /* EXAMPLE */ |
awali | 100:642bee2ce173 | 28 | long distance = sensor.distance(); |
awali | 100:642bee2ce173 | 29 | printf("distance %d \n",distance); |
awali | 100:642bee2ce173 | 30 | // initialize the LCD |
awali | 100:642bee2ce173 | 31 | lcd.begin(); |
awali | 100:642bee2ce173 | 32 | |
awali | 100:642bee2ce173 | 33 | |
awali | 100:642bee2ce173 | 34 | // initialize the LCD |
awali | 100:642bee2ce173 | 35 | lcd.begin(); |
mbed_official | 21:b2f2f6a840b4 | 36 | |
awali | 100:642bee2ce173 | 37 | // Turn on the blacklight and print a message. |
awali | 100:642bee2ce173 | 38 | lcd.backlight(); |
awali | 100:642bee2ce173 | 39 | lcd.print("Hello, world!"); |
awali | 100:642bee2ce173 | 40 | |
awali | 100:642bee2ce173 | 41 | |
awali | 100:642bee2ce173 | 42 | |
awali | 100:642bee2ce173 | 43 | //lcd.printf("Hi mbed World!\n"); |
awali | 100:642bee2ce173 | 44 | |
awali | 100:642bee2ce173 | 45 | printf("Starting IBM MQTT demo:\n"); |
awali | 100:642bee2ce173 | 46 | |
awali | 100:642bee2ce173 | 47 | |
awali | 100:642bee2ce173 | 48 | |
awali | 100:642bee2ce173 | 49 | TCPSocket socket; |
awali | 100:642bee2ce173 | 50 | |
awali | 100:642bee2ce173 | 51 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
awali | 100:642bee2ce173 | 52 | |
awali | 100:642bee2ce173 | 53 | if (!net) { |
awali | 100:642bee2ce173 | 54 | |
awali | 100:642bee2ce173 | 55 | printf("Error! No network inteface found.\n"); |
awali | 100:642bee2ce173 | 56 | |
awali | 100:642bee2ce173 | 57 | return 0; |
awali | 100:642bee2ce173 | 58 | |
awali | 100:642bee2ce173 | 59 | } |
mbed_official | 0:857719181846 | 60 | |
awali | 100:642bee2ce173 | 61 | printf("Connecting to the network...\r\n"); |
awali | 100:642bee2ce173 | 62 | |
awali | 100:642bee2ce173 | 63 | |
awali | 100:642bee2ce173 | 64 | |
awali | 100:642bee2ce173 | 65 | nsapi_size_or_error_t rc = net->connect(); |
awali | 100:642bee2ce173 | 66 | |
awali | 100:642bee2ce173 | 67 | if (rc != 0) { |
awali | 100:642bee2ce173 | 68 | |
awali | 100:642bee2ce173 | 69 | printf("Error! _net->connect() returned: %d\r\n", rc); |
mbed_official | 0:857719181846 | 70 | |
awali | 100:642bee2ce173 | 71 | return -1; |
awali | 100:642bee2ce173 | 72 | |
awali | 100:642bee2ce173 | 73 | } |
awali | 100:642bee2ce173 | 74 | |
awali | 100:642bee2ce173 | 75 | |
mbed_official | 0:857719181846 | 76 | |
awali | 100:642bee2ce173 | 77 | rc = socket.open(net); |
awali | 100:642bee2ce173 | 78 | |
awali | 100:642bee2ce173 | 79 | if (rc != 0) { |
awali | 100:642bee2ce173 | 80 | |
awali | 100:642bee2ce173 | 81 | printf("Error! _socket.open() returned: %d\r\n", rc); |
mbed_official | 0:857719181846 | 82 | |
awali | 100:642bee2ce173 | 83 | return -1; |
awali | 100:642bee2ce173 | 84 | |
awali | 100:642bee2ce173 | 85 | } |
mbed_official | 66:2cf02c7d430c | 86 | |
awali | 100:642bee2ce173 | 87 | |
awali | 100:642bee2ce173 | 88 | |
awali | 100:642bee2ce173 | 89 | SocketAddress address; |
awali | 100:642bee2ce173 | 90 | |
awali | 100:642bee2ce173 | 91 | net->gethostbyname("sandbox.rightech.io", |
mbed_official | 0:857719181846 | 92 | |
awali | 100:642bee2ce173 | 93 | &address); |
awali | 100:642bee2ce173 | 94 | |
awali | 100:642bee2ce173 | 95 | address.set_port(1883); |
awali | 100:642bee2ce173 | 96 | |
awali | 100:642bee2ce173 | 97 | |
mbed_official | 66:2cf02c7d430c | 98 | |
awali | 100:642bee2ce173 | 99 | printf("Opening connection to remote %s port %d\r\n", |
awali | 100:642bee2ce173 | 100 | |
awali | 100:642bee2ce173 | 101 | address.get_ip_address(), address.get_port()); |
awali | 100:642bee2ce173 | 102 | |
awali | 100:642bee2ce173 | 103 | rc = socket.connect(address); |
mbed_official | 66:2cf02c7d430c | 104 | |
awali | 100:642bee2ce173 | 105 | if (rc != 0) { |
awali | 100:642bee2ce173 | 106 | |
awali | 100:642bee2ce173 | 107 | printf("Error! _socket.connect() returned: %d\r\n", rc); |
awali | 100:642bee2ce173 | 108 | |
awali | 100:642bee2ce173 | 109 | return -1; |
awali | 100:642bee2ce173 | 110 | |
awali | 100:642bee2ce173 | 111 | } |
awali | 100:642bee2ce173 | 112 | |
awali | 100:642bee2ce173 | 113 | printf("Connected socket\n"); |
mbed_official | 0:857719181846 | 114 | |
awali | 100:642bee2ce173 | 115 | |
awali | 100:642bee2ce173 | 116 | |
awali | 100:642bee2ce173 | 117 | MQTTClient client(&socket); |
awali | 100:642bee2ce173 | 118 | |
awali | 100:642bee2ce173 | 119 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
awali | 100:642bee2ce173 | 120 | |
awali | 100:642bee2ce173 | 121 | data.MQTTVersion = 3; |
awali | 100:642bee2ce173 | 122 | |
awali | 100:642bee2ce173 | 123 | data.clientID.cstring = "mqtt-rudikrafikov-11r3lh"; |
awali | 100:642bee2ce173 | 124 | |
awali | 100:642bee2ce173 | 125 | data.username.cstring = "1234"; |
mbed_official | 0:857719181846 | 126 | |
awali | 100:642bee2ce173 | 127 | data.password.cstring = "1234"; |
awali | 100:642bee2ce173 | 128 | |
awali | 100:642bee2ce173 | 129 | if ((rc = client.connect(data)) != 0) |
awali | 100:642bee2ce173 | 130 | |
awali | 100:642bee2ce173 | 131 | printf("rc from MQTT connect is %d\r\n", rc); |
mbed_official | 67:ebff4a8d228d | 132 | |
awali | 100:642bee2ce173 | 133 | |
awali | 100:642bee2ce173 | 134 | |
awali | 100:642bee2ce173 | 135 | |
awali | 100:642bee2ce173 | 136 | |
awali | 100:642bee2ce173 | 137 | //{clientId:"vali_client_id",userName:"vali_user_name",password:"fkQiW3AJFcM38ED"} |
awali | 100:642bee2ce173 | 138 | |
awali | 100:642bee2ce173 | 139 | |
awali | 100:642bee2ce173 | 140 | |
awali | 100:642bee2ce173 | 141 | |
mbed_official | 0:857719181846 | 142 | |
awali | 100:642bee2ce173 | 143 | MQTT::Message message; |
awali | 100:642bee2ce173 | 144 | |
awali | 100:642bee2ce173 | 145 | char *topic = "temp"; |
awali | 100:642bee2ce173 | 146 | |
awali | 100:642bee2ce173 | 147 | char buf[100]; |
mbed_official | 71:a0fbcc153b55 | 148 | |
awali | 100:642bee2ce173 | 149 | int temp = 33; |
awali | 100:642bee2ce173 | 150 | |
awali | 100:642bee2ce173 | 151 | int press = 2; |
awali | 100:642bee2ce173 | 152 | |
awali | 100:642bee2ce173 | 153 | int hum = 3; |
awali | 100:642bee2ce173 | 154 | |
awali | 100:642bee2ce173 | 155 | // sprintf(buf, |
awali | 100:642bee2ce173 | 156 | |
awali | 100:642bee2ce173 | 157 | // "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%d,\"Pressure\":" |
awali | 100:642bee2ce173 | 158 | |
awali | 100:642bee2ce173 | 159 | // "%d,\"Humidity\":%d}}", temp, press, hum); |
awali | 100:642bee2ce173 | 160 | |
awali | 100:642bee2ce173 | 161 | // sprintf(buf, |
awali | 100:642bee2ce173 | 162 | |
awali | 100:642bee2ce173 | 163 | // "{\"temperature\": %d}", temp); |
awali | 100:642bee2ce173 | 164 | sprintf(buf, |
awali | 100:642bee2ce173 | 165 | |
awali | 100:642bee2ce173 | 166 | "{\"distance\": %d}", distance); |
awali | 100:642bee2ce173 | 167 | |
awali | 100:642bee2ce173 | 168 | |
mbed_official | 0:857719181846 | 169 | |
awali | 100:642bee2ce173 | 170 | printf("Sending message: \n%s\n", buf); |
awali | 100:642bee2ce173 | 171 | |
awali | 100:642bee2ce173 | 172 | message.qos = MQTT::QOS0; |
awali | 100:642bee2ce173 | 173 | |
awali | 100:642bee2ce173 | 174 | message.retained = false; |
awali | 100:642bee2ce173 | 175 | |
awali | 100:642bee2ce173 | 176 | message.dup = false; |
awali | 100:642bee2ce173 | 177 | |
awali | 100:642bee2ce173 | 178 | message.payload = (void *)buf; |
mbed_official | 0:857719181846 | 179 | |
awali | 100:642bee2ce173 | 180 | message.payloadlen = strlen(buf); |
awali | 100:642bee2ce173 | 181 | |
awali | 100:642bee2ce173 | 182 | rc = client.publish(topic, message); |
awali | 100:642bee2ce173 | 183 | |
awali | 100:642bee2ce173 | 184 | printf("Message sent\n"); |
mbed_official | 0:857719181846 | 185 | |
awali | 100:642bee2ce173 | 186 | printf("Demo concluded successfully \r\n"); |
mbed_official | 0:857719181846 | 187 | |
awali | 100:642bee2ce173 | 188 | |
awali | 100:642bee2ce173 | 189 | |
awali | 100:642bee2ce173 | 190 | return 0; |
awali | 100:642bee2ce173 | 191 | } |