forked from SmingHub/Sming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
84 lines (67 loc) · 2.22 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 you SSID and Password here
#define WIFI_PWD "PleaseEnterPass"
#endif
Timer procTimer;
int sensorValue = 0;
HttpClient thingSpeak;
int onDataSent(HttpConnection& client, bool successful)
{
if(successful)
Serial.println("Success sent");
else
Serial.println("Failed");
String response = client.getResponse()->getBody();
Serial.println("Server response: '" + response + "'");
if(response.length() > 0) {
int intVal = response.toInt();
if(intVal == 0)
Serial.println("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 = "api.thingspeak.com";
url.Path = "/update";
url.Query["key"] = "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("I'm NOT CONNECTED. Need help :(");
// Start soft access point
WifiAccessPoint.config("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
}
void init()
{
spiffs_mount(); // Mount file system, in order to work with files
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);
}