|
| 1 | +/* |
| 2 | + * This sample publishes a message to an Amazon SNS target (topic or endpoint). |
| 3 | + * |
| 4 | + * The first attempt will always fail because of the clock implementation on the Edison. |
| 5 | + * The library uses the timestamp in the AWS response to seed the clock for subsequent attempts. |
| 6 | + * |
| 7 | + * For this demo to work, add your AWS keys in "awsSecKey" and "awsKeyID", create an SNS topic |
| 8 | + * or endpoint and populate the TARGET_ARN with the topic or endpoint ARN. |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#include <AmazonSNSClient.h> |
| 13 | +#include <EdisonAWSImplementations.h> |
| 14 | +#include <AWSFoundationalTypes.h> |
| 15 | +#include <WiFi.h> |
| 16 | + |
| 17 | +/* AWS credentials */ |
| 18 | +const char* awsSecKey = ""; |
| 19 | +const char* awsKeyID = ""; |
| 20 | + |
| 21 | +/* Wi-Fi */ |
| 22 | +char ssid[] = ""; // your network SSID (name) |
| 23 | +char pass[] = ""; // your network password (use for WPA, or use as key for WEP) |
| 24 | +int status = WL_IDLE_STATUS; |
| 25 | + |
| 26 | +/* Constants for connecting to Amazon SNS. */ |
| 27 | +static const char* TARGET_ARN = ""; // replace each ':' with '%3A' |
| 28 | +static const char* AWS_REGION = ""; // us-west-2 etc |
| 29 | +static const char* AWS_ENDPOINT = "amazonaws.com"; |
| 30 | + |
| 31 | +/* Light the LED while a message is transmitted. */ |
| 32 | +int led = 13; |
| 33 | + |
| 34 | +/* Device independent implementations required for AmazonSNSClient to function. */ |
| 35 | +EdisonHttpClient httpClient; |
| 36 | +EdisonDateTimeProvider dateTimeProvider; |
| 37 | + |
| 38 | +/* AWS objects. */ |
| 39 | +AmazonSNSClient snsClient; |
| 40 | +PublishInput publishInput; |
| 41 | +ActionError actionError; |
| 42 | + |
| 43 | +void setup() { |
| 44 | + /* Begin serial communication. */ |
| 45 | + Serial.begin(9600); |
| 46 | + delay(5000); |
| 47 | + |
| 48 | + pinMode(led, OUTPUT); |
| 49 | + |
| 50 | + /* Require a Wi-Fi shield. */ |
| 51 | + if (WL_NO_SHIELD == WiFi.status()) { |
| 52 | + Serial.println("Wi-Fi shield not present."); |
| 53 | + |
| 54 | + /* Do not continue. */ |
| 55 | + while(true); |
| 56 | + } |
| 57 | + |
| 58 | + String fv = WiFi.firmwareVersion(); |
| 59 | + if (fv != "1.1.0") { |
| 60 | + Serial.println("Please upgrade the Wi-Fi firmware"); |
| 61 | + } |
| 62 | + |
| 63 | + /* Attempt to connect to the Wi-fi network. */ |
| 64 | + while (status != WL_CONNECTED) { |
| 65 | + Serial.print("Attempting to connect to SSID: "); |
| 66 | + Serial.println(ssid); |
| 67 | + |
| 68 | + /* Connect to WPA/WPA2 network. Change this line if using open or WEP network. */ |
| 69 | + status = WiFi.begin(ssid, pass); |
| 70 | + |
| 71 | + /* Wait 10 seconds for connection. */ |
| 72 | + delay(10000); |
| 73 | + } |
| 74 | + |
| 75 | + Serial.println("Connected to wifi"); |
| 76 | + printWifiStatus(); |
| 77 | + |
| 78 | + /* Initialize Amazon SNS client. */ |
| 79 | + snsClient.setAWSRegion(AWS_REGION); |
| 80 | + snsClient.setAWSEndpoint(AWS_ENDPOINT); |
| 81 | + snsClient.setAWSSecretKey(awsSecKey); |
| 82 | + snsClient.setAWSKeyID(awsKeyID); |
| 83 | + snsClient.setHttpClient(&httpClient); |
| 84 | + snsClient.setDateTimeProvider(&dateTimeProvider); |
| 85 | +} |
| 86 | + |
| 87 | +void loop() { |
| 88 | + /* Repeat SNS attempt every 60s. */ |
| 89 | + delay(60000); |
| 90 | + |
| 91 | + /* Light up the LED. */ |
| 92 | + digitalWrite(led, HIGH); |
| 93 | + Serial.println("Start Loop"); |
| 94 | + |
| 95 | + MinimalString dateTime(dateTimeProvider.getDateTime()); |
| 96 | + dateTimeProvider.sync(dateTime.getCStr()); |
| 97 | + |
| 98 | + /* Delivery attempt to Amazon SNS. */ |
| 99 | + publishInput.setMessage(dateTime); |
| 100 | + publishInput.setTargetArn(TARGET_ARN); |
| 101 | + PublishOutput publishOutput = snsClient.publish(publishInput, actionError); |
| 102 | + Serial.println(publishOutput.getMessageId().getCStr()); |
| 103 | + |
| 104 | + /* Finish loop and turn off the LED. */ |
| 105 | + Serial.println(dateTime.getCStr()); |
| 106 | + Serial.println("Finished loop"); |
| 107 | + delay(1000); |
| 108 | + digitalWrite(led, LOW); |
| 109 | +} |
| 110 | + |
| 111 | +void printWifiStatus() { |
| 112 | + // print the SSID of the network you're attached to: |
| 113 | + Serial.print("SSID: "); |
| 114 | + Serial.println(WiFi.SSID()); |
| 115 | + |
| 116 | + // print your WiFi shield's IP address: |
| 117 | + IPAddress ip = WiFi.localIP(); |
| 118 | + Serial.print("IP Address: "); |
| 119 | + Serial.println(ip); |
| 120 | + |
| 121 | + // print the received signal strength: |
| 122 | + long rssi = WiFi.RSSI(); |
| 123 | + Serial.print("signal strength (RSSI):"); |
| 124 | + Serial.print(rssi); |
| 125 | + Serial.println(" dBm"); |
| 126 | +} |
| 127 | + |
0 commit comments