[147] | 1 | #include "rca.h"
|
---|
| 2 | #include "ESP8266.h"
|
---|
| 3 | #include "Milkcocoa.h"
|
---|
| 4 | #include "Client_ESP8266.h"
|
---|
| 5 |
|
---|
[235] | 6 | #include "../examples_gdef.h"
|
---|
[147] | 7 |
|
---|
[213] | 8 | //#define MILKCOCOA_PUSH
|
---|
[182] | 9 | #define MILKCOCOA_ON
|
---|
| 10 |
|
---|
[147] | 11 | #define MILKCOCOA_SERVERPORT 1883
|
---|
| 12 |
|
---|
[213] | 13 | ESP8266Client wifi_client;
|
---|
[147] | 14 |
|
---|
| 15 | const char MQTT_SERVER[] PROGMEM = MILKCOCOA_APP_ID ".mlkcca.com";
|
---|
| 16 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ MILKCOCOA_APP_ID;
|
---|
| 17 |
|
---|
[213] | 18 | Milkcocoa milkcocoa = Milkcocoa(&wifi_client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
|
---|
[147] | 19 |
|
---|
| 20 | extern void onpush(DataElement *elem);
|
---|
[182] | 21 |
|
---|
[147] | 22 | void setup()
|
---|
| 23 | {
|
---|
[182] | 24 | int ret;
|
---|
| 25 |
|
---|
[147] | 26 | Serial.begin(115200);
|
---|
[182] | 27 | Serial.print("Milkcocoa SDK demo");
|
---|
[147] | 28 |
|
---|
| 29 | // Connect to WiFi access point.
|
---|
| 30 | Serial.println(); Serial.println();
|
---|
| 31 | Serial.print("Connecting to ");
|
---|
[213] | 32 | Serial.println(STA_SSID);
|
---|
[182] | 33 |
|
---|
[213] | 34 | ret = WiFi.begin(Serial5, 115200);
|
---|
[182] | 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:");
|
---|
[213] | 42 | Serial.println(WiFi.getVersion().c_str());
|
---|
[182] | 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 |
|
---|
[147] | 50 | Serial.print("FW Version:");
|
---|
[213] | 51 | Serial.println(WiFi.getVersion().c_str());
|
---|
[147] | 52 |
|
---|
[213] | 53 | if (WiFi.setOprToStation()) {
|
---|
[147] | 54 | Serial.print("to station ok\r\n");
|
---|
| 55 | } else {
|
---|
| 56 | Serial.print("to station err\r\n");
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[213] | 59 | if (WiFi.joinAP(STA_SSID, STA_PASSWORD)) {
|
---|
[147] | 60 | Serial.print("Join AP success\r\n");
|
---|
| 61 | Serial.print("IP: ");
|
---|
[213] | 62 | Serial.println(WiFi.getLocalIP().c_str());
|
---|
[147] | 63 | } else {
|
---|
| 64 | Serial.print("Join AP failure\r\n");
|
---|
| 65 | }
|
---|
[182] | 66 |
|
---|
[213] | 67 | if (WiFi.stopServer()) {
|
---|
[182] | 68 | Serial.print("Stop server ok\r\n");
|
---|
| 69 | } else {
|
---|
| 70 | Serial.print("Stop server err\r\n");
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[213] | 73 | if (WiFi.disableMUX()) {
|
---|
[147] | 74 | Serial.print("single ok\r\n");
|
---|
| 75 | } else {
|
---|
| 76 | Serial.print("single err\r\n");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[182] | 79 | #ifdef MILKCOCOA_ON
|
---|
[147] | 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);
|
---|
[182] | 88 | digitalWrite(13, LOW);
|
---|
| 89 | #endif /* MILKCOCOA_ON */
|
---|
| 90 |
|
---|
[147] | 91 | Serial.println("setup end\r\n");
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[182] | 94 | #ifdef MILKCOCOA_PUSH
|
---|
| 95 | bool req_led_on = false;
|
---|
| 96 | #endif /* MILKCOCOA_PUSH */
|
---|
| 97 |
|
---|
[147] | 98 | void loop() {
|
---|
[182] | 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
|
---|
[147] | 114 | DataElement elem = DataElement();
|
---|
| 115 |
|
---|
[182] | 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 | }
|
---|
[147] | 148 |
|
---|
[182] | 149 | #ifdef MILKCOCOA_ON
|
---|
[147] | 150 | void onpush(DataElement *pelem) {
|
---|
[182] | 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) {
|
---|
[147] | 160 | Serial.println("LED : ON!");
|
---|
| 161 | digitalWrite(13, HIGH);
|
---|
| 162 | }
|
---|
[182] | 163 | else if(strcmp(data, "OFF") == 0) {
|
---|
[147] | 164 | Serial.println("LED : OFF!");
|
---|
| 165 | digitalWrite(13, LOW);
|
---|
| 166 | }
|
---|
[182] | 167 | }
|
---|
| 168 | #endif /* MILKCOCOA_ON */
|
---|