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
Line 
1#include "rca.h"
2#include "ESP8266.h"
3#include "Milkcocoa.h"
4#include "Client_ESP8266.h"
5
6#define STA_SSID ""
7#define STA_PASSWORD ""
8
9#define MILKCOCOA_APP_ID ""
10#define MILKCOCOA_DATASTORE ""
11
12//#define MILKCOCOA_PUSH
13#define MILKCOCOA_ON
14
15#define MILKCOCOA_SERVERPORT 1883
16
17ESP8266Client wifi_client;
18
19const char MQTT_SERVER[] PROGMEM = MILKCOCOA_APP_ID ".mlkcca.com";
20const char MQTT_CLIENTID[] PROGMEM = __TIME__ MILKCOCOA_APP_ID;
21
22Milkcocoa milkcocoa = Milkcocoa(&wifi_client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
23
24extern void onpush(DataElement *elem);
25
26void setup()
27{
28 int ret;
29
30 Serial.begin(115200);
31 Serial.print("Milkcocoa SDK demo");
32
33 // Connect to WiFi access point.
34 Serial.println(); Serial.println();
35 Serial.print("Connecting to ");
36 Serial.println(STA_SSID);
37
38 ret = WiFi.begin(Serial5, 115200);
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:");
46 Serial.println(WiFi.getVersion().c_str());
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
54 Serial.print("FW Version:");
55 Serial.println(WiFi.getVersion().c_str());
56
57 if (WiFi.setOprToStation()) {
58 Serial.print("to station ok\r\n");
59 } else {
60 Serial.print("to station err\r\n");
61 }
62
63 if (WiFi.joinAP(STA_SSID, STA_PASSWORD)) {
64 Serial.print("Join AP success\r\n");
65 Serial.print("IP: ");
66 Serial.println(WiFi.getLocalIP().c_str());
67 } else {
68 Serial.print("Join AP failure\r\n");
69 }
70
71 if (WiFi.stopServer()) {
72 Serial.print("Stop server ok\r\n");
73 } else {
74 Serial.print("Stop server err\r\n");
75 }
76
77 if (WiFi.disableMUX()) {
78 Serial.print("single ok\r\n");
79 } else {
80 Serial.print("single err\r\n");
81 }
82
83#ifdef MILKCOCOA_ON
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);
92 digitalWrite(13, LOW);
93#endif /* MILKCOCOA_ON */
94
95 Serial.println("setup end\r\n");
96}
97
98#ifdef MILKCOCOA_PUSH
99bool req_led_on = false;
100#endif /* MILKCOCOA_PUSH */
101
102void loop() {
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
118 DataElement elem = DataElement();
119
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}
152
153#ifdef MILKCOCOA_ON
154void onpush(DataElement *pelem) {
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) {
164 Serial.println("LED : ON!");
165 digitalWrite(13, HIGH);
166 }
167 else if(strcmp(data, "OFF") == 0) {
168 Serial.println("LED : OFF!");
169 digitalWrite(13, LOW);
170 }
171}
172#endif /* MILKCOCOA_ON */
Note: See TracBrowser for help on using the repository browser.