-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1-photon.cpp
56 lines (42 loc) · 1.47 KB
/
1-photon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "Particle.h"
// Test Program #1 for Google Cloud
// Just generates some simple random data once a minute. Also sends up the device name.
#include <math.h> // This is just for cos and M_PI, used in generating random sample data
// Forward declarations
void publishData();
void deviceNameHandler(const char *topic, const char *data);
const unsigned long PUBLISH_PERIOD_MS = 60000;
const unsigned long FIRST_PUBLISH_MS = 10000;
const char *PUBLISH_EVENT_NAME = "test2data";
unsigned long lastPublish = FIRST_PUBLISH_MS - PUBLISH_PERIOD_MS;
int nextValue = 1;
String deviceName;
void setup() {
Serial.begin(9600);
Particle.subscribe("spark/", deviceNameHandler);
Particle.publish("spark/device/name");
}
void loop() {
if (millis() - lastPublish >= PUBLISH_PERIOD_MS) {
lastPublish = millis();
if (deviceName.length() > 0) {
publishData();
}
}
}
void publishData() {
// This just publishes some somewhat random data for testing
// a is a monotonically increasing integer
int a = nextValue++;
// double value b is a cosine, so the values will rise and fall nicely over 360 steps
double b = cos((double)(a % 360) * M_PI / 180.0);
// c is a random integer
int c = rand();
char buf[256];
snprintf(buf, sizeof(buf), "{\"a\":%d,\"b\":%.3f,\"c\":%d,\"n\":\"%s\"}", a, b, c, deviceName.c_str());
Serial.printlnf("publishing %s", buf);
Particle.publish(PUBLISH_EVENT_NAME, buf, PRIVATE);
}
void deviceNameHandler(const char *topic, const char *data) {
deviceName = data;
}