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