1 | #include "r2ca.h"
|
---|
2 |
|
---|
3 | #include "ESP8266.h"
|
---|
4 | #include "Milkcocoa.h"
|
---|
5 | #include "Client_ESP8266.h"
|
---|
6 | #include "../../examples_gdef.h"
|
---|
7 |
|
---|
8 | #include <Wire.h>
|
---|
9 | #include <SeeedOLED.h>
|
---|
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 | if (milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)){
|
---|
80 | // if (milkcocoa.on(MILKCOCOA_DATASTORE, "send", onpush)){
|
---|
81 | Serial.println("milkcocoa on sucesss");
|
---|
82 | }
|
---|
83 | else {
|
---|
84 | Serial.println("milkcocoa on failure");
|
---|
85 | }
|
---|
86 |
|
---|
87 | Wire.begin();
|
---|
88 | SeeedOled.init();
|
---|
89 |
|
---|
90 | SeeedOled.clearDisplay();
|
---|
91 | SeeedOled.setNormalDisplay();
|
---|
92 | SeeedOled.setPageMode();
|
---|
93 | SeeedOled.setTextXY(0,0);
|
---|
94 | SeeedOled.putString("Hello World!");
|
---|
95 |
|
---|
96 | Serial.println("setup end\r\n");
|
---|
97 | }
|
---|
98 |
|
---|
99 | void loop()
|
---|
100 | {
|
---|
101 | int8_t ret;
|
---|
102 | int8_t push_ret;
|
---|
103 |
|
---|
104 | while((ret = milkcocoa.loop(1)) != 0) {
|
---|
105 | Serial.println("Milkcocoa.loop() connection error.");
|
---|
106 | Serial.println(milkcocoa.connectErrorString(ret));
|
---|
107 | Serial.println(ret);
|
---|
108 | Serial.println("Retrying MQTT connection in 5 seconds...");
|
---|
109 | delay(5000);
|
---|
110 | }
|
---|
111 | Serial.print(".");
|
---|
112 |
|
---|
113 | if (Serial.available() > 0) {
|
---|
114 | Serial.read();
|
---|
115 | Serial.print("Pause : Input any char to continue.\n\r");
|
---|
116 | while(!(Serial.available() > 0));
|
---|
117 | Serial.print("Resume.\n\r");
|
---|
118 | Serial.read();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | void onpush(DataElement *pelem)
|
---|
123 | {
|
---|
124 | char *data;
|
---|
125 | static int ln = 1;
|
---|
126 |
|
---|
127 | if(!pelem->getString("TEXT", &data)) {
|
---|
128 | Serial.print("onpush : key TEXT is not found.");
|
---|
129 | return;
|
---|
130 | };
|
---|
131 |
|
---|
132 | Serial.print("onpush : {TEXT, ");
|
---|
133 | Serial.write(data);
|
---|
134 | Serial.println("}.");
|
---|
135 |
|
---|
136 | SeeedOled.setTextXY(ln++%7,0);
|
---|
137 | SeeedOled.putString(data);
|
---|
138 | }
|
---|