Changeset 182 for rtos_arduino


Ignore:
Timestamp:
Mar 28, 2016, 2:33:15 PM (8 years ago)
Author:
ertl-honda
Message:

更新

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rtos_arduino/trunk/examples/Milkcocoa_basic/rca_app.cpp

    r147 r182  
    44#include "Client_ESP8266.h"
    55
    6 #define WLAN_SSID       ""
    7 #define WLAN_PASSWORD   ""
     6#define SSID       ""
     7#define PASSWORD   ""
    88
    99#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
    1214
    1315#define MILKCOCOA_SERVERPORT  1883
     
    2123
    2224extern void onpush(DataElement *elem);
    23 extern void onpush_chat(DataElement *elem);
    24    
     25
    2526void setup()
    2627{
     28    int ret;
     29   
    2730    Serial.begin(115200);
    28     Serial.println(F("Milkcocoa SDK demo"));
     31    Serial.print("Milkcocoa SDK demo");
    2932
    3033    // Connect to WiFi access point.
    3134    Serial.println(); Serial.println();
    3235    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
    3754    Serial.print("FW Version:");
    3855    Serial.println(wifi.getVersion().c_str());
     
    4461    }
    4562   
    46     if (wifi.joinAP(WLAN_SSID, WLAN_PASSWORD)) {
     63    if (wifi.joinAP(SSID, PASSWORD)) {
    4764        Serial.print("Join AP success\r\n");
    4865        Serial.print("IP: ");
     
    5168        Serial.print("Join AP failure\r\n");
    5269    }
    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   
    5477    if (wifi.disableMUX()) {
    5578        Serial.print("single ok\r\n");
     
    5881    }
    5982   
     83#ifdef MILKCOCOA_ON
    6084    if (milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)){
    6185        Serial.println("milkcocoa on sucesss");   
     
    6589    }
    6690
    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    
    7491    pinMode(13, OUTPUT);
    75    
     92    digitalWrite(13, LOW);   
     93#endif /* MILKCOCOA_ON  */
     94
    7695    Serial.println("setup end\r\n");
    7796}
    7897
    79 int loop_cnt = 0;
    80 int cnt;
     98#ifdef MILKCOCOA_PUSH
     99bool req_led_on = false;
     100#endif /* MILKCOCOA_PUSH */
     101
    81102void 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}
    84152
    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
    98154void 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) {
    104164        Serial.println("LED : ON!");
    105165        digitalWrite(13, HIGH);
    106166    }
    107     else {
     167    else if(strcmp(data, "OFF") == 0) {
    108168        Serial.println("LED : OFF!");
    109169        digitalWrite(13, LOW);
    110170    }
    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.