[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 | #include <Wire.h>
|
---|
| 9 | #include <Digital_Light_TSL2561.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 | Wire.begin();
|
---|
| 80 | TSL2561.init();
|
---|
| 81 |
|
---|
| 82 | Serial.println("setup end\r\n");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void loop()
|
---|
| 86 | {
|
---|
| 87 | int8_t ret;
|
---|
| 88 | int8_t push_ret;
|
---|
| 89 |
|
---|
| 90 | DataElement elem = DataElement();
|
---|
| 91 |
|
---|
| 92 | Serial.print("Push data to Milkcocoa : Key:LUX, Value:");
|
---|
| 93 | int lux = (int)TSL2561.readVisibleLux();
|
---|
| 94 | Serial.println(lux);
|
---|
| 95 | elem.setValue("LUX", lux);
|
---|
| 96 |
|
---|
| 97 | do {
|
---|
| 98 | push_ret = milkcocoa.push(MILKCOCOA_DATASTORE, &elem);
|
---|
| 99 | if (push_ret != 0) {
|
---|
| 100 | Serial.println("Milkcocoa.push() error.");
|
---|
| 101 | Serial.println(milkcocoa.pushErrorString(push_ret));
|
---|
| 102 | Serial.println(push_ret);
|
---|
| 103 | Serial.println("Retrying MQTT push in 5 seconds...");
|
---|
| 104 | delay(5000);
|
---|
| 105 | }
|
---|
| 106 | }while(push_ret != 0);
|
---|
| 107 |
|
---|
| 108 | Serial.println("Push success.");
|
---|
| 109 | delay(2000);
|
---|
| 110 |
|
---|
| 111 | if (Serial.available() > 0) {
|
---|
| 112 | Serial.read();
|
---|
| 113 | Serial.print("Pause : Input any char to continue.\n\r");
|
---|
| 114 | while(!(Serial.available() > 0));
|
---|
| 115 | Serial.print("Resume.\n\r");
|
---|
| 116 | Serial.read();
|
---|
| 117 | }
|
---|
| 118 | }
|
---|