1 | #include "ESP8266.h"
|
---|
2 | #include "Milkcocoa.h"
|
---|
3 | #include "Client_ESP8266.h"
|
---|
4 |
|
---|
5 | /************************* WiFi Access Point *********************************/
|
---|
6 |
|
---|
7 | #define WLAN_SSID "...SSID..."
|
---|
8 | #define WLAN_PASS "...PASS..."
|
---|
9 |
|
---|
10 |
|
---|
11 | /************************* Your Milkcocoa Setup *********************************/
|
---|
12 |
|
---|
13 | #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..."
|
---|
14 | #define MILKCOCOA_DATASTORE "esp8266"
|
---|
15 |
|
---|
16 | /************* Milkcocoa Setup (you don't need to change this!) ******************/
|
---|
17 |
|
---|
18 | #define MILKCOCOA_SERVERPORT 1883
|
---|
19 |
|
---|
20 | /************ Global State (you don't need to change this!) ******************/
|
---|
21 |
|
---|
22 | // Create an ESP8266Client class to connect to the MQTT server.
|
---|
23 | ESP8266Client wifi;
|
---|
24 |
|
---|
25 | const char MQTT_SERVER[] PROGMEM = MILKCOCOA_APP_ID ".mlkcca.com";
|
---|
26 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ MILKCOCOA_APP_ID;
|
---|
27 |
|
---|
28 | Milkcocoa milkcocoa = Milkcocoa(&wifi, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
|
---|
29 |
|
---|
30 | int cnt;
|
---|
31 |
|
---|
32 | void setup() {
|
---|
33 | Serial.begin(115200);
|
---|
34 | delay(10);
|
---|
35 |
|
---|
36 | Serial.println(F("Milkcocoa SDK demo"));
|
---|
37 |
|
---|
38 | // Connect to WiFi access point.
|
---|
39 | Serial.println(); Serial.println();
|
---|
40 | Serial.print("Connecting to ");
|
---|
41 | Serial.println(WLAN_SSID);
|
---|
42 |
|
---|
43 | wifi.begin(Serial5, 115200);
|
---|
44 |
|
---|
45 | Serial.print("FW Version:");
|
---|
46 | Serial.println(wifi.getVersion().c_str());
|
---|
47 |
|
---|
48 | if (wifi.setOprToStation()) {
|
---|
49 | Serial.print("to station ok\r\n");
|
---|
50 | } else {
|
---|
51 | Serial.print("to station err\r\n");
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (wifi.joinAP(WLAN_SSID, WLAN_PASS)) {
|
---|
55 | Serial.print("Join AP success\r\n");
|
---|
56 | Serial.print("IP: ");
|
---|
57 | Serial.println(wifi.getLocalIP().c_str());
|
---|
58 | } else {
|
---|
59 | Serial.print("Join AP failure\r\n");
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (wifi.disableMUX()) {
|
---|
63 | Serial.print("single ok\r\n");
|
---|
64 | } else {
|
---|
65 | Serial.print("single err\r\n");
|
---|
66 | }
|
---|
67 |
|
---|
68 | if(milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)){
|
---|
69 | Serial.println("milkcocoa on sucesss");
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | Serial.println("milkcocoa on failure");
|
---|
73 | }
|
---|
74 |
|
---|
75 | cnt = 0;
|
---|
76 | };
|
---|
77 |
|
---|
78 | void loop() {
|
---|
79 | milkcocoa.loop();
|
---|
80 |
|
---|
81 | DataElement elem = DataElement();
|
---|
82 | elem.setValue("v", cnt++);
|
---|
83 | milkcocoa.push(MILKCOCOA_DATASTORE, elem);
|
---|
84 |
|
---|
85 | delay(7000);
|
---|
86 | };
|
---|
87 |
|
---|
88 | void onpush(DataElement elem) {
|
---|
89 | Serial.println("onpush");
|
---|
90 | Serial.println(elem.getInt("v"));
|
---|
91 | };
|
---|
92 |
|
---|