source: rtos_arduino/trunk/examples/Milkcocoa_NCESIoT/r2ca_app.cpp@ 260

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

マクロ名を更新.
実行モデルを変更.

File size: 3.9 KB
Line 
1#include "r2ca.h"
2#include "ESP8266.h"
3#include "Milkcocoa.h"
4#include "Client_ESP8266.h"
5
6#include "../examples_gdef.h"
7
8#define MILKCOCOA_SERVERPORT 1883
9
10ESP8266Client wifi_client;
11
12const char MQTT_SERVER[] PROGMEM = MILKCOCOA_APP_ID ".mlkcca.com";
13const char MQTT_CLIENTID[] PROGMEM = __TIME__ MILKCOCOA_APP_ID;
14
15Milkcocoa milkcocoa = Milkcocoa(&wifi_client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
16
17#include <Servo.h>
18Servo myservo;
19
20#include "Ultrasonic.h"
21Ultrasonic ultrasonic(3);
22
23extern void onpush(DataElement *elem);
24
25void setup()
26{
27 int ret;
28
29 Serial.begin(115200);
30 Serial.print("Milkcocoa NCES IoT 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 if (milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)){
83 Serial.println("milkcocoa on sucesss");
84 }
85 else {
86 Serial.println("milkcocoa on failure");
87 }
88
89 myservo.attach(2);
90
91 Serial.println("setup end\r\n");
92}
93
94void loop() {
95 int8_t ret;
96 int8_t push_ret;
97
98 long RangeInCentimeters;
99 RangeInCentimeters = ultrasonic.MeasureInCentimeters();
100 Serial.print(RangeInCentimeters);//0~400cm
101 Serial.println(" cm");
102
103 DataElement elem = DataElement();
104 elem.setValue("RangeInCentimeters", (int)RangeInCentimeters);
105
106 do {
107 push_ret = milkcocoa.push(MILKCOCOA_DATASTORE, &elem);
108 if (push_ret != 0) {
109 Serial.println("Milkcocoa.push() error.");
110 Serial.println(milkcocoa.pushErrorString(push_ret));
111 Serial.println(push_ret);
112 Serial.println("Retrying MQTT push in 5 seconds...");
113 delay(5000);
114 }
115 }while(push_ret != 0);
116
117 Serial.println("Push success.");
118 delay(2000);
119
120 if (Serial.available() > 0) {
121 Serial.read();
122 Serial.print("Pause : Input any char to continue.\n\r");
123 while(!(Serial.available() > 0));
124 Serial.print("Resume.\n\r");
125 Serial.read();
126 }
127}
128
129void onpush(DataElement *pelem) {
130 char *data;
131 int pos;
132 if(!pelem->getString("SERVO", &data)) {
133 return;
134 };
135
136 pos = atoi(data);
137
138 if ((pos >= 0) && (pos <= 180)) {
139 myservo.write(pos);
140 Serial.print("Ser Servo : ");
141 Serial.println(pos);
142 }
143}
144
145
146void loop1() {
147 int8_t ret;
148 while((ret = milkcocoa.loop(1)) != 0) {
149 Serial.println("Milkcocoa.loop() connection error.");
150 Serial.println(milkcocoa.connectErrorString(ret));
151 Serial.println(ret);
152 Serial.println("Retrying MQTT connection in 5 seconds...");
153 delay(5000);
154 }
155 delay(100);
156}
Note: See TracBrowser for help on using the repository browser.