* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkApiMbed.h Source File

BlynkApiMbed.h

00001 /**
00002  * @file       BlynkApiArduino.h
00003  * @author     Volodymyr Shymanskyy
00004  * @license    This project is released under the MIT License (MIT)
00005  * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
00006  * @date       Mar 2015
00007  * @brief
00008  *
00009  */
00010 
00011 #ifndef BlynkApiMbed_1_h
00012 #define BlynkApiMbed_1_h
00013 
00014 #include "mbed.h"
00015 #include <Blynk/BlynkApi.h>
00016 #include "microduino_util.h"
00017 
00018 Timer g_BlynkTimer;
00019 extern Serial pc;
00020 
00021 template<class Proto>
00022 void BlynkApi<Proto>::Init()
00023 {
00024     g_BlynkTimer.start();
00025     //pc.printf("Enter BlynkApi<Proto>::Init()\r\n");
00026 }
00027 
00028 template<class Proto>
00029 BLYNK_FORCE_INLINE
00030 millis_time_t BlynkApi<Proto>::getMillis()
00031 {
00032 // TODO: Remove workaround for Intel Curie
00033 // https://forum.arduino.cc/index.php?topic=391836.0
00034 #ifdef ARDUINO_ARCH_ARC32
00035     noInterrupts();
00036     uint64_t t = millis();
00037     interrupts();
00038     return t;
00039 #else
00040     return g_BlynkTimer.read_ms();
00041 #endif
00042 }
00043 
00044 #ifdef BLYNK_NO_INFO
00045 ffff
00046 template<class Proto>
00047 BLYNK_FORCE_INLINE
00048 void BlynkApi<Proto>::sendInfo() {}
00049 
00050 #else
00051 
00052 template<class Proto>
00053 BLYNK_FORCE_INLINE
00054 void BlynkApi<Proto>::sendInfo()
00055 {
00056     static const char profile[] BLYNK_PROGMEM =
00057         BLYNK_PARAM_KV("ver"    , BLYNK_VERSION)
00058         BLYNK_PARAM_KV("h-beat" , TOSTRING(BLYNK_HEARTBEAT))
00059         BLYNK_PARAM_KV("buff-in", TOSTRING(BLYNK_MAX_READBYTES))
00060 #ifdef BLYNK_INFO_DEVICE
00061         BLYNK_PARAM_KV("dev"    , BLYNK_INFO_DEVICE)
00062 #endif
00063 #ifdef BLYNK_INFO_CPU
00064         BLYNK_PARAM_KV("cpu"    , BLYNK_INFO_CPU)
00065 #endif
00066 #ifdef BLYNK_INFO_CONNECTION
00067         BLYNK_PARAM_KV("con"    , BLYNK_INFO_CONNECTION)
00068 #endif
00069         BLYNK_PARAM_KV("build"  , __DATE__ " " __TIME__)
00070         ;
00071     const size_t profile_len = sizeof(profile)-1;
00072 
00073 #ifdef BLYNK_HAS_PROGMEM
00074     char mem[profile_len];
00075     memcpy_P(mem, profile, profile_len);
00076     static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, mem, profile_len);
00077 #else
00078     static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, profile, profile_len);
00079 #endif
00080     return;
00081 }
00082 
00083 #endif
00084 
00085 static gpio_t g_gpio;
00086 static analogin_t _adc;
00087 //dac_t _dac;
00088 template<class Proto>
00089 BLYNK_FORCE_INLINE
00090 void BlynkApi<Proto>::processCmd(const void* buff, size_t len)
00091 {
00092     BlynkParam param((void*)buff, len);
00093     BlynkParam::iterator it = param.begin();
00094     if (it >= param.end())
00095         return;
00096     const char* cmd = it.asStr();
00097 #if defined(MPIDE)
00098     uint16_t cmd16;
00099     memcpy(&cmd16, cmd, sizeof(cmd16));
00100 #else
00101     const uint16_t cmd16 = *(uint16_t*)cmd;
00102 #endif
00103     if (++it >= param.end())
00104         return;
00105 
00106 #if defined(analogInputToDigitalPin)
00107     // Good! Analog pins can be referenced on this device by name.
00108     const uint8_t pin = (it.asStr()[0] == 'A') ?
00109                         analogInputToDigitalPin(atoi(it.asStr()+1)) :
00110                         it.asInt();
00111 #else
00112 #warning "analogInputToDigitalPin not defined => Named analog pins will not work"
00113     //const uint8_t pin = it.asInt();
00114     const uint16_t pin = MUtil::getRealPin(it.asInt());
00115     const uint8_t pin1 = it.asInt();
00116 #endif
00117 
00118     switch(cmd16) {
00119 #ifndef BLYNK_NO_BUILTIN
00120         case BLYNK_HW_PM: {
00121             while (it < param.end()) {
00122                 ++it;
00123                 if (!strcmp(it.asStr(), "in")) {
00124                     //pinMode(pin, INPUT);
00125                     gpio_init_in(&g_gpio, (PinName)pin);
00126                 } else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) {
00127                     //pinMode(pin, OUTPUT);
00128                     gpio_init_out(&g_gpio, (PinName)pin);
00129 #ifdef INPUT_PULLUP
00130                 } else if (!strcmp(it.asStr(), "pu")) {
00131                     //pinMode(pin, INPUT_PULLUP);
00132                     gpio_init_in_ex(&g_gpio, pin, PullUp);
00133 #endif
00134 #ifdef INPUT_PULLDOWN
00135                 } else if (!strcmp(it.asStr(), "pd")) {
00136                     //pinMode(pin, INPUT_PULLDOWN);
00137                     gpio_init_in_ex(&g_gpio, pin, PullDown);
00138 #endif
00139                 } else {
00140 #ifdef BLYNK_DEBUG
00141                     BLYNK_LOG4(BLYNK_F("Invalid pin "), pin, BLYNK_F(" mode "), it.asStr());
00142 #endif
00143                 }
00144                 ++it;
00145             }
00146         }
00147         break;
00148         
00149         case BLYNK_HW_DR: {
00150             //DigitalIn p((PinName)pin);
00151             char mem[16];
00152             BlynkParam rsp(mem, 0, sizeof(mem));
00153             rsp.add("dw");
00154             rsp.add(pin1);
00155             //rsp.add(int(p));
00156             rsp.add(gpio_read(&g_gpio));
00157             static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
00158         }
00159         break;
00160         
00161         case BLYNK_HW_DW: {
00162             // Should be 1 parameter (value)
00163             if (++it >= param.end())
00164                 return;
00165 
00166 #ifdef ESP8266
00167             // Disable PWM...
00168             analogWrite(pin, 0);
00169 #endif
00170 #ifndef BLYNK_MINIMIZE_PINMODE_USAGE
00171             //pinMode(pin, OUTPUT);
00172             gpio_init_out(&g_gpio, (PinName)pin);
00173 #endif
00174             //digitalWrite(pin, it.asInt() ? HIGH : LOW);
00175             gpio_write(&g_gpio, it.asInt() ? 1 : 0);
00176         }
00177         break;
00178         
00179         case BLYNK_HW_AR: {
00180             analogin_init(&_adc, (PinName)pin);
00181             char mem[16];
00182             BlynkParam rsp(mem, 0, sizeof(mem));
00183             rsp.add("aw");
00184             rsp.add(pin1);
00185             //rsp.add(analogRead(pin));
00186             rsp.add(int(analogin_read(&_adc)*1024));
00187             static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
00188         }
00189         break;
00190         
00191         case BLYNK_HW_AW: {
00192             #if 0 // TODO: Not supported yet
00193             analogout_init(&_dac, pin);
00194             // Should be 1 parameter (value)
00195             if (++it >= param.end())
00196                 return;
00197 
00198 #ifndef BLYNK_MINIMIZE_PINMODE_USAGE
00199             //pinMode(pin, OUTPUT);
00200 #endif
00201             //analogWrite(pin, it.asInt());
00202             analogout_write(&_dac, float(it.asInt()/1024));
00203             #endif
00204         }
00205         break;
00206 #endif
00207 
00208         case BLYNK_HW_VR: {
00209             BlynkReq req = { pin };
00210             WidgetReadHandler handler = GetReadHandler(pin);
00211             if (handler && (handler != BlynkWidgetRead)) {
00212                 handler(req);
00213             } else {
00214                 BlynkWidgetReadDefault(req);
00215             }
00216         }
00217         break;
00218         
00219         case BLYNK_HW_VW: {
00220             ++it;
00221             char* start = (char*)it.asStr();
00222             BlynkParam param2(start, len - (start - (char*)buff));
00223             BlynkReq req = { pin };
00224             WidgetWriteHandler handler = GetWriteHandler(pin);
00225             if (handler && (handler != BlynkWidgetWrite)) {
00226                 handler(req, param2);
00227             } else {
00228                 BlynkWidgetWriteDefault(req, param2);
00229             }
00230         }
00231         break;
00232         
00233         default:
00234             BLYNK_LOG2(BLYNK_F("Invalid HW cmd: "), cmd);
00235             static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_RESPONSE, static_cast<Proto*>(this)->currentMsgId, NULL, BLYNK_ILLEGAL_COMMAND);
00236     }
00237 }
00238 
00239 #endif
00240