1 | #include "rca.h"
|
---|
2 | #include "ESP8266.h"
|
---|
3 | #include "Milkcocoa.h"
|
---|
4 | #include "Client_ESP8266.h"
|
---|
5 |
|
---|
6 | #define WLAN_SSID ""
|
---|
7 | #define WLAN_PASSWORD ""
|
---|
8 |
|
---|
9 | #define MILKCOCOA_APP_ID ""
|
---|
10 | #define MILKCOCOA_DATASTORE "esp8266"
|
---|
11 | #define MILKCOCOA_DATASTORE_CHAT "chat"
|
---|
12 |
|
---|
13 | #define MILKCOCOA_SERVERPORT 1883
|
---|
14 |
|
---|
15 | ESP8266Client wifi;
|
---|
16 |
|
---|
17 | const char MQTT_SERVER[] PROGMEM = MILKCOCOA_APP_ID ".mlkcca.com";
|
---|
18 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ MILKCOCOA_APP_ID;
|
---|
19 |
|
---|
20 | Milkcocoa milkcocoa = Milkcocoa(&wifi, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
|
---|
21 |
|
---|
22 | extern void onpush(DataElement *elem);
|
---|
23 | extern void onpush_chat(DataElement *elem);
|
---|
24 |
|
---|
25 | void setup()
|
---|
26 | {
|
---|
27 | Serial.begin(115200);
|
---|
28 | Serial.println(F("Milkcocoa SDK demo"));
|
---|
29 |
|
---|
30 | // Connect to WiFi access point.
|
---|
31 | Serial.println(); Serial.println();
|
---|
32 | Serial.print("Connecting to ");
|
---|
33 | Serial.println(WLAN_SSID);
|
---|
34 |
|
---|
35 | wifi.begin(Serial5, 115200);
|
---|
36 |
|
---|
37 | Serial.print("FW Version:");
|
---|
38 | Serial.println(wifi.getVersion().c_str());
|
---|
39 |
|
---|
40 | if (wifi.setOprToStation()) {
|
---|
41 | Serial.print("to station ok\r\n");
|
---|
42 | } else {
|
---|
43 | Serial.print("to station err\r\n");
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (wifi.joinAP(WLAN_SSID, WLAN_PASSWORD)) {
|
---|
47 | Serial.print("Join AP success\r\n");
|
---|
48 | Serial.print("IP: ");
|
---|
49 | Serial.println(wifi.getLocalIP().c_str());
|
---|
50 | } else {
|
---|
51 | Serial.print("Join AP failure\r\n");
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (wifi.disableMUX()) {
|
---|
55 | Serial.print("single ok\r\n");
|
---|
56 | } else {
|
---|
57 | Serial.print("single err\r\n");
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)){
|
---|
61 | Serial.println("milkcocoa on sucesss");
|
---|
62 | }
|
---|
63 | else {
|
---|
64 | Serial.println("milkcocoa on failure");
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (milkcocoa.on(MILKCOCOA_DATASTORE_CHAT, "push", onpush_chat)){
|
---|
68 | Serial.println("milkcocoa on sucesss");
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | Serial.println("milkcocoa on failure");
|
---|
72 | }
|
---|
73 |
|
---|
74 | pinMode(13, OUTPUT);
|
---|
75 |
|
---|
76 | Serial.println("setup end\r\n");
|
---|
77 | }
|
---|
78 |
|
---|
79 | int loop_cnt = 0;
|
---|
80 | int cnt;
|
---|
81 | void loop() {
|
---|
82 | milkcocoa.loop();
|
---|
83 | char buffer[100];
|
---|
84 |
|
---|
85 | sprintf(buffer, "RCA is Alive %d", loop_cnt++);
|
---|
86 | DataElement elem = DataElement();
|
---|
87 | elem.setValue("message", buffer);
|
---|
88 | milkcocoa.push(MILKCOCOA_DATASTORE_CHAT, &elem);
|
---|
89 |
|
---|
90 | // DataElement elem = DataElement();
|
---|
91 | // elem.setValue("v", cnt++);
|
---|
92 | // milkcocoa.push(MILKCOCOA_DATASTORE, elem);
|
---|
93 |
|
---|
94 | Serial.print(".");
|
---|
95 | delay(1000);
|
---|
96 | };
|
---|
97 |
|
---|
98 | void onpush(DataElement *pelem) {
|
---|
99 | int v;
|
---|
100 | Serial.print("onpush : ");
|
---|
101 | v = pelem->getInt("v");
|
---|
102 | Serial.println(v);
|
---|
103 | if (v == 1) {
|
---|
104 | Serial.println("LED : ON!");
|
---|
105 | digitalWrite(13, HIGH);
|
---|
106 | }
|
---|
107 | else {
|
---|
108 | Serial.println("LED : OFF!");
|
---|
109 | digitalWrite(13, LOW);
|
---|
110 | }
|
---|
111 | };
|
---|
112 |
|
---|
113 | void onpush_chat(DataElement *pelem) {
|
---|
114 | char *c;
|
---|
115 | Serial.print("onpush_chat : ");
|
---|
116 | c = pelem->getString("message");
|
---|
117 | Serial.println(c);
|
---|
118 | };
|
---|