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