-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicAdafruitWifiFraschetta.h
99 lines (90 loc) · 2.78 KB
/
BasicAdafruitWifiFraschetta.h
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
//#define ARDUINO_UNOWIFIR4
#ifdef ARDUINO_UNOWIFIR4
#ifndef BasicAdafruitWifiFraschetta_h
#define BasicAdafruitWifiFraschetta_h
#include "WifiFraschetta.h"
//#define BasicAdafruitDebugF
class BasicAdafruitWifiF: public WifiClientF{
private:
const char server[16] = "io.adafruit.com";
BasicAdafruitWifiF(const BasicAdafruitWifiF&);
BasicAdafruitWifiF& operator= (const BasicAdafruitWifiF&);
BasicAdafruitWifiF():WifiClientF(){link = ""; ReceivedString=""; Readable=false; Update=2000; STU=0;}
protected:
String link;
unsigned long Update,STU;
public:
String ReceivedString;
bool Readable;
static BasicAdafruitWifiF* GetInstance(){
static BasicAdafruitWifiF i;
return &i;
}
void AdafruitBegin(String Username,String APIKey,String Feed,unsigned long Update=800){
link = "/api/v2/" + Username + "/feeds/" + Feed + "/data?x-aio-key=" + APIKey + "&limit=1";
this->Update=Update;
}
bool connect(){return WifiClientF::connect(server,80);}
void Download(){
#ifdef BasicAdafruitDebugF
Serial.println("download value...");
#endif
BasicAdafruitWifiF::connect();
printMethod("GET",link);
printHost(server);
printAccept("application/json");
printConnection("Keep-Alive");
printClose();
#ifdef BasicAdafruitDebugF
Serial.println("request sent");
if(OK())Serial.println("request successful");
else{stop(); return;}
#else
if(!OK()){stop(); return;}
#endif
if(StreamFilter("value")){
read(); read(); read(); read();
char f ='"';
ReceivedString="";
Readable=StreamFilter(&f,&ReceivedString);
#ifdef BasicAdafruitDebugF
Serial.print("value: "); Serial.print('"'); Serial.print(ReceivedString); Serial.println('"');
#endif
return;
}
#ifdef BasicAdafruitDebugF
Serial.println("value not found");
#endif
}
bool Upload(String Value){
#ifdef BasicAdafruitDebugF
Serial.print("upload: "); Serial.print('"'); Serial.print(Value); Serial.println('"');
#endif
String Json = "{\"value\":\""+Value+"\"}";
connect();
printMethod("POST",link);
printHost(server);
println("content-type: application/json; charset=utf-8");
printConnection("Keep-Alive");
printJson(Json.c_str());
#ifdef BasicAdafruitDebugF
if(OK()){Serial.println("request successful"); return 1;}
#else
return OK();
#endif
}
void ReceiveString(){
if(millis() - STU >= Update){
STU = millis();
#ifdef BasicAdafruitDebugF
Serial.println("Updating...");
#endif
Download();
}
}
void Send(String s){Upload(s);}
};
#endif
#else
#error This library requires an Arduino board with WiFi support.
#endif