-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
application.cpp
84 lines (67 loc) · 2.2 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
#include <SmingCore.h>
// 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
{
SimpleTimer procTimer;
int sensorValue;
HttpClient thingSpeak;
int onDataSent(HttpConnection& client, bool successful)
{
Serial.println(successful ? _F("Success sent") : _F("Failed"));
String response = client.getResponse()->getBody();
Serial << _F("Server response: '") << response << '\'' << endl;
if(response.length() > 0) {
int intVal = response.toInt();
if(intVal == 0) {
Serial.println(_F("Sensor value wasn't accepted. May be we need to wait a little?"));
}
}
return 0;
}
void sendData()
{
// Read our sensor value :)
sensorValue++;
/*
Here is an alternative method of URL construction, which is helpful for more complex URLs.
The resulting URL string is equivalent to:
"http://api.thingspeak.com/update?key=7XXUJWCWYTMXKN3L&field1=" + String(sensorValue)
*/
Url url;
url.Scheme = URI_SCHEME_HTTP;
url.Host = F("api.thingspeak.com");
url.Path = F("/update");
url.Query["key"] = F("7XXUJWCWYTMXKN3L");
url.Query["field1"] = String(sensorValue);
thingSpeak.downloadString(url, onDataSent);
}
// Will be called when WiFi station timeout was reached
void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
{
Serial.println(_F("I'm NOT CONNECTED. Need help :("));
// Start soft access point
WifiAccessPoint.config(F("CONFIG ME PLEEEEASE..."), "", AUTH_OPEN);
WifiAccessPoint.enable(true);
// .. some you code for configuration ..
}
void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)
{
// Start send data loop
procTimer.initializeMs<25 * 1000>(sendData).start(); // every 25 seconds
}
} // namespace
void init()
{
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
Serial.systemDebugOutput(false); // Disable 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.onStationDisconnect(connectFail);
WifiEvents.onStationGotIP(gotIP);
}