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 |
|
---|
10 | ESP8266Client wifi_client;
|
---|
11 |
|
---|
12 | const char MQTT_SERVER[] PROGMEM = MILKCOCOA_APP_ID ".mlkcca.com";
|
---|
13 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ MILKCOCOA_APP_ID;
|
---|
14 |
|
---|
15 | Milkcocoa milkcocoa = Milkcocoa(&wifi_client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
|
---|
16 |
|
---|
17 | #include <Servo.h>
|
---|
18 | Servo myservo;
|
---|
19 |
|
---|
20 | #include "Ultrasonic.h"
|
---|
21 | Ultrasonic ultrasonic(3);
|
---|
22 |
|
---|
23 | extern void onpush(DataElement *elem);
|
---|
24 |
|
---|
25 | void 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 |
|
---|
94 | void 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 |
|
---|
129 | void 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 |
|
---|
146 | void 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 | }
|
---|