source: rtos_arduino/trunk/examples/Milkcocoa_basic/rca_app.cpp@ 213

Last change on this file since 213 was 213, checked in by ertl-honda, 8 years ago

クライアントの変更に対応

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