Skip to content

Commit 8d0d317

Browse files
author
Jinesh Varia
committed
2 parents 71e186c + a8e7781 commit 8d0d317

8 files changed

+8809
-0
lines changed

samples/EdisonSNSSample.ino

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+

samples/SparkSNSSample.ino

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
This sample publishes a message to an Amazon SNS target (topic or endpoint).
3+
*/
4+
5+
#include "AmazonSNSClient.h"
6+
#include "SparkAWSImplementations.h"
7+
#include "AWSFoundationalTypes.h"
8+
9+
/* AWS credentials */
10+
const char* awsKeyID = "";
11+
const char* awsSecKey = "";
12+
13+
/* Constants for connecting to Amazon SNS. */
14+
static const char* TARGET_ARN = ""; // replace each ':' with '%3A'
15+
static const char* AWS_REGION = ""; // us-west-2 etc
16+
static const char* AWS_ENDPOINT = "amazonaws.com";
17+
18+
/* Light the LED while a message is transmitted. */
19+
int led = 13;
20+
21+
/* Device independent implementations required for AmazonSNSClient to function. */
22+
SparkHttpClient httpClient;
23+
SparkDateTimeProvider dateTimeProvider;
24+
25+
/* AWS objects. */
26+
AmazonSNSClient snsClient;
27+
PublishInput publishInput;
28+
ActionError actionError;
29+
30+
void setup() {
31+
/* Begin serial communication. */
32+
Serial.begin(9600);
33+
delay(5000);
34+
35+
pinMode(led, OUTPUT);
36+
37+
/* Initialize Amazon SNS client. */
38+
snsClient.setAWSRegion(AWS_REGION);
39+
snsClient.setAWSEndpoint(AWS_ENDPOINT);
40+
snsClient.setAWSSecretKey(awsSecKey);
41+
snsClient.setAWSKeyID(awsKeyID);
42+
snsClient.setHttpClient(&httpClient);
43+
snsClient.setDateTimeProvider(&dateTimeProvider);
44+
}
45+
46+
void loop() {
47+
/* Repeat SNS attempt every 60s. */
48+
delay(60000);
49+
50+
/* Light up the LED. */
51+
digitalWrite(led, HIGH);
52+
Serial.println("Start Loop");
53+
54+
MinimalString dateTime(dateTimeProvider.getDateTime());
55+
dateTimeProvider.sync(dateTime.getCStr());
56+
57+
/* Delivery attempt to Amazon SNS. */
58+
publishInput.setMessage(dateTime);
59+
publishInput.setTargetArn(TARGET_ARN);
60+
PublishOutput publishOutput = snsClient.publish(publishInput, actionError);
61+
Serial.println(publishOutput.getMessageId().getCStr());
62+
63+
/* Finish loop and turn off the LED. */
64+
Serial.println(dateTime.getCStr());
65+
Serial.println("Finished loop");
66+
delay(1000);
67+
digitalWrite(led, LOW);
68+
}

0 commit comments

Comments
 (0)