source: rtos_arduino/trunk/examples/Milkcocoa_NCESIoT/rca_app.cpp@ 227

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

NCES IoT と Milkcocoaを組み合わせたサンプル

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