|
| 1 | +// This example uses an Arduino/Genuino Zero together with |
| 2 | +// a WiFi101 Shield or a MKR1000 to connect to shiftr.io. |
| 3 | +// |
| 4 | +// IMPORTANT: This example uses the new WiFi101 library. |
| 5 | +// |
| 6 | +// IMPORTANT: You need to install/update the SSL certificates first: |
| 7 | +// https://github.com/arduino-libraries/WiFi101-FirmwareUpdater#to-update-ssl-certificates |
| 8 | +// |
| 9 | +// You can check on your device after a successful |
| 10 | +// connection here: https://shiftr.io/try. |
| 11 | +// |
| 12 | +// by Gilberto Conti |
| 13 | +// https://github.com/256dpi/arduino-mqtt |
| 14 | + |
| 15 | +#include <MKRGSM.h> |
| 16 | +#include <MQTTClient.h> |
| 17 | +#include <ArduinoBearSSL.h> |
| 18 | + |
| 19 | +const char pin[] = ""; |
| 20 | +const char apn[] = "apn"; |
| 21 | +const char login[] = "login"; |
| 22 | +const char password[] = "pass"; |
| 23 | + |
| 24 | +const char server[] = "xxxxxxxxxxxxxx.iot.xx-xxxx-x.amazonaws.com"; |
| 25 | + |
| 26 | +// Get the cert data by: |
| 27 | +// 1) Creating a CSR using the ArduinoBearSSL -> Tools -> ECC508CSR example for key slot 0 |
| 28 | +// 2) Creating a new thing and uploading the CSR for it |
| 29 | +// 3) Downloading the public key for the thing in AWS IoT |
| 30 | +// 4) Convert the base64 encoded cert to binary |
| 31 | +const byte cert[] = { |
| 32 | + // ... |
| 33 | +}; |
| 34 | + |
| 35 | +GPRS gprs; |
| 36 | +GSM gsmAccess; |
| 37 | + |
| 38 | +GSMClient gsmClient; |
| 39 | +BearSSLClient net(gsmClient, 0, cert, sizeof(cert)); |
| 40 | +MQTTClient client; |
| 41 | + |
| 42 | +unsigned long lastMillis = 0; |
| 43 | + |
| 44 | +unsigned long getTime() { |
| 45 | + return gsmAccess.getTime(); |
| 46 | +} |
| 47 | + |
| 48 | +void setup() { |
| 49 | + Serial.begin(115200); |
| 50 | + |
| 51 | + // connection state |
| 52 | + boolean notConnected = true; |
| 53 | + |
| 54 | + // After starting the modem with GSM.begin() |
| 55 | + // attach the shield to the GPRS network with the APN, login and password |
| 56 | + while (notConnected) { |
| 57 | + if ((gsmAccess.begin(pin) == GSM_READY) & |
| 58 | + (gprs.attachGPRS(apn, login, password) == GPRS_READY)) { |
| 59 | + notConnected = false; |
| 60 | + } else { |
| 61 | + Serial.println("Not connected"); |
| 62 | + delay(1000); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Serial.println("GPRS connected"); |
| 67 | + |
| 68 | + ArduinoBearSSL.onGetTime(getTime); |
| 69 | + |
| 70 | + // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. |
| 71 | + // You need to set the IP address directly. |
| 72 | + // |
| 73 | + // MQTT brokers usually use port 8883 for secure connections. |
| 74 | + client.begin(server, 8883, net); |
| 75 | + client.onMessage(messageReceived); |
| 76 | + |
| 77 | + connect(); |
| 78 | +} |
| 79 | + |
| 80 | +void connect() { |
| 81 | + Serial.print("\nconnecting..."); |
| 82 | + while (!client.connect("arduino")) { |
| 83 | + Serial.print("."); |
| 84 | + delay(1000); |
| 85 | + } |
| 86 | + |
| 87 | + Serial.println("\nconnected!"); |
| 88 | + |
| 89 | + client.subscribe("/hello"); |
| 90 | + // client.unsubscribe("/hello"); |
| 91 | +} |
| 92 | + |
| 93 | +void loop() { |
| 94 | + client.loop(); |
| 95 | + |
| 96 | + if (!client.connected()) { |
| 97 | + connect(); |
| 98 | + } |
| 99 | + |
| 100 | + // publish a message roughly every second. |
| 101 | + if (millis() - lastMillis > 1000) { |
| 102 | + lastMillis = millis(); |
| 103 | + client.publish("/hello", "world"); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void messageReceived(String &topic, String &payload) { |
| 108 | + Serial.println("incoming: " + topic + " - " + payload); |
| 109 | +} |
0 commit comments