Changeset 182 for rtos_arduino
- Timestamp:
- Mar 28, 2016, 2:33:15 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
rtos_arduino/trunk/examples/Milkcocoa_basic/rca_app.cpp
r147 r182 4 4 #include "Client_ESP8266.h" 5 5 6 #define WLAN_SSID ""7 #define WLAN_PASSWORD ""6 #define SSID "" 7 #define PASSWORD "" 8 8 9 9 #define MILKCOCOA_APP_ID "" 10 #define MILKCOCOA_DATASTORE "esp8266" 11 #define MILKCOCOA_DATASTORE_CHAT "chat" 10 #define MILKCOCOA_DATASTORE "" 11 12 #define MILKCOCOA_PUSH 13 #define MILKCOCOA_ON 12 14 13 15 #define MILKCOCOA_SERVERPORT 1883 … … 21 23 22 24 extern void onpush(DataElement *elem); 23 extern void onpush_chat(DataElement *elem); 24 25 25 26 void setup() 26 27 { 28 int ret; 29 27 30 Serial.begin(115200); 28 Serial.print ln(F("Milkcocoa SDK demo"));31 Serial.print("Milkcocoa SDK demo"); 29 32 30 33 // Connect to WiFi access point. 31 34 Serial.println(); Serial.println(); 32 35 Serial.print("Connecting to "); 33 Serial.println(WLAN_SSID); 34 35 wifi.begin(Serial5, 115200); 36 36 Serial.println(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 37 54 Serial.print("FW Version:"); 38 55 Serial.println(wifi.getVersion().c_str()); … … 44 61 } 45 62 46 if (wifi.joinAP( WLAN_SSID, WLAN_PASSWORD)) {63 if (wifi.joinAP(SSID, PASSWORD)) { 47 64 Serial.print("Join AP success\r\n"); 48 65 Serial.print("IP: "); … … 51 68 Serial.print("Join AP failure\r\n"); 52 69 } 53 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 54 77 if (wifi.disableMUX()) { 55 78 Serial.print("single ok\r\n"); … … 58 81 } 59 82 83 #ifdef MILKCOCOA_ON 60 84 if (milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)){ 61 85 Serial.println("milkcocoa on sucesss"); … … 65 89 } 66 90 67 if (milkcocoa.on(MILKCOCOA_DATASTORE_CHAT, "push", onpush_chat)){68 Serial.println("milkcocoa on sucesss");69 }70 else {71 Serial.println("milkcocoa on failure");72 }73 74 91 pinMode(13, OUTPUT); 75 92 digitalWrite(13, LOW); 93 #endif /* MILKCOCOA_ON */ 94 76 95 Serial.println("setup end\r\n"); 77 96 } 78 97 79 int loop_cnt = 0; 80 int cnt; 98 #ifdef MILKCOCOA_PUSH 99 bool req_led_on = false; 100 #endif /* MILKCOCOA_PUSH */ 101 81 102 void loop() { 82 milkcocoa.loop(); 83 char buffer[100]; 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 } 84 152 85 sprintf(buffer, "RCA is Alive %d", loop_cnt++); 86 DataElement elem = DataElement(); 87 elem.setValue("message", buffer); 88 milkcocoa.push(MILKCOCOA_DATASTORE_CHAT, &elem); 89 90 // DataElement elem = DataElement(); 91 // elem.setValue("v", cnt++); 92 // milkcocoa.push(MILKCOCOA_DATASTORE, elem); 93 94 Serial.print("."); 95 delay(1000); 96 }; 97 153 #ifdef MILKCOCOA_ON 98 154 void onpush(DataElement *pelem) { 99 int v; 100 Serial.print("onpush : "); 101 v = pelem->getInt("v"); 102 Serial.println(v); 103 if (v == 1) { 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) { 104 164 Serial.println("LED : ON!"); 105 165 digitalWrite(13, HIGH); 106 166 } 107 else {167 else if(strcmp(data, "OFF") == 0) { 108 168 Serial.println("LED : OFF!"); 109 169 digitalWrite(13, LOW); 110 170 } 111 }; 112 113 void onpush_chat(DataElement *pelem) { 114 char *c; 115 Serial.print("onpush_chat : "); 116 c = pelem->getString("message"); 117 Serial.println(c); 118 }; 171 } 172 #endif /* MILKCOCOA_ON */
Note:
See TracChangeset
for help on using the changeset viewer.