1 | #include "r2ca.h"
|
---|
2 | #include "ESP8266.h"
|
---|
3 | #include "Milkcocoa.h"
|
---|
4 | #include "Client_ESP8266.h"
|
---|
5 |
|
---|
6 | #include "../examples_gdef.h"
|
---|
7 |
|
---|
8 | //#define MILKCOCOA_PUSH
|
---|
9 | #define MILKCOCOA_ON
|
---|
10 |
|
---|
11 | #define MILKCOCOA_SERVERPORT 1883
|
---|
12 |
|
---|
13 | ESP8266Client wifi_client;
|
---|
14 |
|
---|
15 | const char MQTT_SERVER[] PROGMEM = MILKCOCOA_APP_ID ".mlkcca.com";
|
---|
16 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ MILKCOCOA_APP_ID;
|
---|
17 |
|
---|
18 | Milkcocoa milkcocoa = Milkcocoa(&wifi_client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
|
---|
19 |
|
---|
20 | extern void onpush(DataElement *elem);
|
---|
21 |
|
---|
22 | void setup()
|
---|
23 | {
|
---|
24 | int ret;
|
---|
25 |
|
---|
26 | Serial.begin(115200);
|
---|
27 | Serial.print("Milkcocoa SDK demo");
|
---|
28 |
|
---|
29 | // Connect to WiFi access point.
|
---|
30 | Serial.println(); Serial.println();
|
---|
31 | Serial.print("Connecting to ");
|
---|
32 | Serial.println(STA_SSID);
|
---|
33 |
|
---|
34 | ret = WiFi.begin(Serial5, 115200);
|
---|
35 |
|
---|
36 | if(ret == 1) {
|
---|
37 | Serial.print("Cannot communicate with ESP8266.");
|
---|
38 | while(1);
|
---|
39 | } else if(ret == 2) {
|
---|
40 | Serial.println("FW Version mismatch.");
|
---|
41 | Serial.print("FW Version:");
|
---|
42 | Serial.println(WiFi.getVersion().c_str());
|
---|
43 | Serial.print("Supported FW Version:");
|
---|
44 | Serial.println(ESP8266_SUPPORT_VERSION);
|
---|
45 | while(1);
|
---|
46 | } else {
|
---|
47 | Serial.print("begin ok\r\n");
|
---|
48 | }
|
---|
49 |
|
---|
50 | Serial.print("FW Version:");
|
---|
51 | Serial.println(WiFi.getVersion().c_str());
|
---|
52 |
|
---|
53 | if (WiFi.setOprToStation()) {
|
---|
54 | Serial.print("to station ok\r\n");
|
---|
55 | } else {
|
---|
56 | Serial.print("to station err\r\n");
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (WiFi.joinAP(STA_SSID, STA_PASSWORD)) {
|
---|
60 | Serial.print("Join AP success\r\n");
|
---|
61 | Serial.print("IP: ");
|
---|
62 | Serial.println(WiFi.getLocalIP().c_str());
|
---|
63 | } else {
|
---|
64 | Serial.print("Join AP failure\r\n");
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (WiFi.stopServer()) {
|
---|
68 | Serial.print("Stop server ok\r\n");
|
---|
69 | } else {
|
---|
70 | Serial.print("Stop server err\r\n");
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (WiFi.disableMUX()) {
|
---|
74 | Serial.print("single ok\r\n");
|
---|
75 | } else {
|
---|
76 | Serial.print("single err\r\n");
|
---|
77 | }
|
---|
78 |
|
---|
79 | #ifdef MILKCOCOA_ON
|
---|
80 | if (milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)){
|
---|
81 | Serial.println("milkcocoa on sucesss");
|
---|
82 | }
|
---|
83 | else {
|
---|
84 | Serial.println("milkcocoa on failure");
|
---|
85 | }
|
---|
86 |
|
---|
87 | pinMode(13, OUTPUT);
|
---|
88 | digitalWrite(13, LOW);
|
---|
89 | #endif /* MILKCOCOA_ON */
|
---|
90 |
|
---|
91 | Serial.println("setup end\r\n");
|
---|
92 | }
|
---|
93 |
|
---|
94 | #ifdef MILKCOCOA_PUSH
|
---|
95 | bool req_led_on = false;
|
---|
96 | #endif /* MILKCOCOA_PUSH */
|
---|
97 |
|
---|
98 | void loop() {
|
---|
99 | int8_t ret;
|
---|
100 | int8_t push_ret;
|
---|
101 |
|
---|
102 | #ifdef MILKCOCOA_ON
|
---|
103 | while((ret = milkcocoa.loop(1)) != 0) {
|
---|
104 | Serial.println("Milkcocoa.loop() connection error.");
|
---|
105 | Serial.println(milkcocoa.connectErrorString(ret));
|
---|
106 | Serial.println(ret);
|
---|
107 | Serial.println("Retrying MQTT connection in 5 seconds...");
|
---|
108 | delay(5000);
|
---|
109 | }
|
---|
110 | Serial.print(".");
|
---|
111 | #endif /* MILKCOCOA_ON */
|
---|
112 |
|
---|
113 | #ifdef MILKCOCOA_PUSH
|
---|
114 | DataElement elem = DataElement();
|
---|
115 |
|
---|
116 | if(req_led_on) {
|
---|
117 | Serial.println("Push data to Milkcocoa : Key:LED, Value:ON");
|
---|
118 | elem.setValue("LED", "ON");
|
---|
119 | }else {
|
---|
120 | Serial.println("Push data to Milkcocoa : Key:LED, Value:OFF");
|
---|
121 | elem.setValue("LED", "OFF");
|
---|
122 | }
|
---|
123 |
|
---|
124 | do {
|
---|
125 | push_ret = milkcocoa.push(MILKCOCOA_DATASTORE, &elem);
|
---|
126 | if (push_ret != 0) {
|
---|
127 | Serial.println("Milkcocoa.push() error.");
|
---|
128 | Serial.println(milkcocoa.pushErrorString(push_ret));
|
---|
129 | Serial.println(push_ret);
|
---|
130 | Serial.println("Retrying MQTT push in 5 seconds...");
|
---|
131 | delay(5000);
|
---|
132 | }
|
---|
133 | }while(push_ret != 0);
|
---|
134 |
|
---|
135 | Serial.println("Push success.");
|
---|
136 | req_led_on = (req_led_on)? false : true;
|
---|
137 | delay(2000);
|
---|
138 | #endif /* MILKCOCOA_PUSH */
|
---|
139 |
|
---|
140 | if (Serial.available() > 0) {
|
---|
141 | Serial.read();
|
---|
142 | Serial.print("Pause : Input any char to continue.\n\r");
|
---|
143 | while(!(Serial.available() > 0));
|
---|
144 | Serial.print("Resume.\n\r");
|
---|
145 | Serial.read();
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | #ifdef MILKCOCOA_ON
|
---|
150 | void onpush(DataElement *pelem) {
|
---|
151 | char *data;
|
---|
152 | if(!pelem->getString("LED", &data)) {
|
---|
153 | Serial.print("onpush : key LED is not found.");
|
---|
154 | return;
|
---|
155 | };
|
---|
156 | Serial.print("onpush : {LED, ");
|
---|
157 | Serial.write(data);
|
---|
158 | Serial.println("}.");
|
---|
159 | if (strcmp(data, "ON") == 0) {
|
---|
160 | Serial.println("LED : ON!");
|
---|
161 | digitalWrite(13, HIGH);
|
---|
162 | }
|
---|
163 | else if(strcmp(data, "OFF") == 0) {
|
---|
164 | Serial.println("LED : OFF!");
|
---|
165 | digitalWrite(13, LOW);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | #endif /* MILKCOCOA_ON */
|
---|