-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
application.cpp
110 lines (88 loc) · 2.92 KB
/
application.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <SmingCore.h>
#include <JsonObjectStream.h>
/*** Direct PUSH notifications on your mobile phone!
*
* At first you should create new event in Instapush
* 0. Go to http://instapush.im
* 1. Create Application: "SmingTest"
* 2. Add event:
* - Event Title: "notify"
* - Trackers: title [Tab] text
* - Push Message: "{title}! Details: {text}"
* 3. Update Application ID and Application Secret below:
*/
// If you want, you can define WiFi settings globally in Eclipse Environment Variables
#ifndef WIFI_SSID
#define WIFI_SSID "PleaseEnterSSID" // Put your SSID and password here
#define WIFI_PWD "PleaseEnterPass"
#endif
namespace
{
DEFINE_FSTR(APP_ID, "55719abba4c48a802c881205")
DEFINE_FSTR(APP_SECRET, "5300adbe3f906938950fc0cdbc301986")
using InstapushTrackers = HashMap<String, String>;
class InstapushApplication : protected HttpClient
{
public:
InstapushApplication(const String& appId, const String& appSecret) : app(appId), secret(appSecret)
{
}
void notify(const String& event, InstapushTrackers& trackersInfo)
{
debugf("preparing request");
HttpRequest* request = new HttpRequest(String(url));
HttpHeaders requestHeaders;
requestHeaders[HTTP_HEADER_CONTENT_TYPE] = toString(MIME_JSON);
requestHeaders[F("x-instapush-appid")] = app;
requestHeaders[F("x-instapush-appsecret")] = secret;
auto stream = new JsonObjectStream(1024);
auto root = stream->getRoot();
root["event"] = event;
JsonObject trackers = root.createNestedObject("trackers");
for(auto info : trackersInfo) {
debugf("%s: %s", info.key().c_str(), info.value().c_str());
trackers[info.key()] = info.value();
}
request->setBody(stream);
request->onRequestComplete(RequestCompletedDelegate(&InstapushApplication::processed, this));
send(request);
}
int processed(HttpConnection& client, bool successful)
{
Serial.copyFrom(client.getResponse()->stream);
return 0;
}
private:
String app;
String secret;
DEFINE_FSTR_LOCAL(url, "http://api.instapush.im/v1/post");
};
SimpleTimer procTimer;
InstapushApplication pusher(APP_ID, APP_SECRET);
// Publish our message
void publishMessage()
{
Serial.println(_F("Push message now!"));
InstapushTrackers trackers;
trackers["title"] = F("Sming Framework");
trackers["text"] = F("New test was successfully launched");
pusher.notify("notify", trackers); // event name, trackers
}
// Will be called when WiFi station was connected to AP
void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
{
Serial << _F("I'm CONNECTED to ") << ip << endl;
// Start publishing loop
procTimer.initializeMs<10 * 1000>(publishMessage).start();
}
} // namespace
void init()
{
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
Serial.systemDebugOutput(true); // Debug output to serial
WifiStation.config(WIFI_SSID, WIFI_PWD);
WifiStation.enable(true);
WifiAccessPoint.enable(false);
// Run our method when station was connected to AP (or not connected)
WifiEvents.onStationGotIP(gotIP);
}