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

Dependencies:   mbed

Committer:
lixianyu
Date:
Fri Jun 24 02:06:43 2016 +0000
Revision:
1:e34100dd6532
Parent:
0:740c1eb2df13
?Arduino??????????0~255??????LPC824????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:740c1eb2df13 1 /**
lixianyu 0:740c1eb2df13 2 * @file BlynkApiArduino.h
lixianyu 0:740c1eb2df13 3 * @author Volodymyr Shymanskyy
lixianyu 0:740c1eb2df13 4 * @license This project is released under the MIT License (MIT)
lixianyu 0:740c1eb2df13 5 * @copyright Copyright (c) 2015 Volodymyr Shymanskyy
lixianyu 0:740c1eb2df13 6 * @date Mar 2015
lixianyu 0:740c1eb2df13 7 * @brief
lixianyu 0:740c1eb2df13 8 *
lixianyu 0:740c1eb2df13 9 */
lixianyu 0:740c1eb2df13 10
lixianyu 0:740c1eb2df13 11 #ifndef BlynkApiArduino_h
lixianyu 0:740c1eb2df13 12 #define BlynkApiArduino_h
lixianyu 0:740c1eb2df13 13
lixianyu 0:740c1eb2df13 14 #include "mbed.h"
lixianyu 0:740c1eb2df13 15 #include <Blynk/BlynkApi.h>
lixianyu 0:740c1eb2df13 16
lixianyu 0:740c1eb2df13 17
lixianyu 0:740c1eb2df13 18 static Timer blynk_millis_timer;
lixianyu 0:740c1eb2df13 19 static Ticker blynk_waker;
lixianyu 0:740c1eb2df13 20
lixianyu 0:740c1eb2df13 21 static
lixianyu 0:740c1eb2df13 22 void blynk_wake() {
lixianyu 0:740c1eb2df13 23 //pc.puts("(...)");
lixianyu 0:740c1eb2df13 24 }
lixianyu 0:740c1eb2df13 25
lixianyu 0:740c1eb2df13 26 static
lixianyu 0:740c1eb2df13 27 void delay(unsigned long ms)
lixianyu 0:740c1eb2df13 28 {
lixianyu 0:740c1eb2df13 29 wait_ms(ms);
lixianyu 0:740c1eb2df13 30 }
lixianyu 0:740c1eb2df13 31
lixianyu 0:740c1eb2df13 32 static
lixianyu 0:740c1eb2df13 33 unsigned long millis(void)
lixianyu 0:740c1eb2df13 34 {
lixianyu 0:740c1eb2df13 35 return blynk_millis_timer.read_ms();
lixianyu 0:740c1eb2df13 36 }
lixianyu 0:740c1eb2df13 37
lixianyu 0:740c1eb2df13 38 template<class Proto>
lixianyu 0:740c1eb2df13 39 void BlynkApi<Proto>::Init()
lixianyu 0:740c1eb2df13 40 {
lixianyu 0:740c1eb2df13 41 blynk_waker.attach(&blynk_wake, 2.0);
lixianyu 0:740c1eb2df13 42 blynk_millis_timer.start();
lixianyu 0:740c1eb2df13 43 }
lixianyu 0:740c1eb2df13 44
lixianyu 0:740c1eb2df13 45 template<class Proto>
lixianyu 0:740c1eb2df13 46 BLYNK_FORCE_INLINE
lixianyu 0:740c1eb2df13 47 millis_time_t BlynkApi<Proto>::getMillis()
lixianyu 0:740c1eb2df13 48 {
lixianyu 0:740c1eb2df13 49 return blynk_millis_timer.read_ms();
lixianyu 0:740c1eb2df13 50 }
lixianyu 0:740c1eb2df13 51
lixianyu 0:740c1eb2df13 52 #ifdef BLYNK_NO_INFO
lixianyu 0:740c1eb2df13 53
lixianyu 0:740c1eb2df13 54 template<class Proto>
lixianyu 0:740c1eb2df13 55 BLYNK_FORCE_INLINE
lixianyu 0:740c1eb2df13 56 void BlynkApi<Proto>::sendInfo() {}
lixianyu 0:740c1eb2df13 57
lixianyu 0:740c1eb2df13 58 #else
lixianyu 0:740c1eb2df13 59
lixianyu 0:740c1eb2df13 60 template<class Proto>
lixianyu 0:740c1eb2df13 61 BLYNK_FORCE_INLINE
lixianyu 0:740c1eb2df13 62 void BlynkApi<Proto>::sendInfo()
lixianyu 0:740c1eb2df13 63 {
lixianyu 0:740c1eb2df13 64 static const char profile[] BLYNK_PROGMEM =
lixianyu 0:740c1eb2df13 65 BLYNK_PARAM_KV("ver" , BLYNK_VERSION)
lixianyu 0:740c1eb2df13 66 BLYNK_PARAM_KV("h-beat" , TOSTRING(BLYNK_HEARTBEAT))
lixianyu 0:740c1eb2df13 67 BLYNK_PARAM_KV("buff-in", TOSTRING(BLYNK_MAX_READBYTES))
lixianyu 0:740c1eb2df13 68 #ifdef BLYNK_INFO_DEVICE
lixianyu 0:740c1eb2df13 69 BLYNK_PARAM_KV("dev" , BLYNK_INFO_DEVICE)
lixianyu 0:740c1eb2df13 70 #endif
lixianyu 0:740c1eb2df13 71 #ifdef BLYNK_INFO_CPU
lixianyu 0:740c1eb2df13 72 BLYNK_PARAM_KV("cpu" , BLYNK_INFO_CPU)
lixianyu 0:740c1eb2df13 73 #endif
lixianyu 0:740c1eb2df13 74 #ifdef BLYNK_INFO_CONNECTION
lixianyu 0:740c1eb2df13 75 BLYNK_PARAM_KV("con" , BLYNK_INFO_CONNECTION)
lixianyu 0:740c1eb2df13 76 #endif
lixianyu 0:740c1eb2df13 77 BLYNK_PARAM_KV("build" , __DATE__ " " __TIME__)
lixianyu 0:740c1eb2df13 78 ;
lixianyu 0:740c1eb2df13 79 const size_t profile_len = sizeof(profile)-1;
lixianyu 0:740c1eb2df13 80
lixianyu 0:740c1eb2df13 81 #ifdef BLYNK_HAS_PROGMEM
lixianyu 0:740c1eb2df13 82 char mem[profile_len];
lixianyu 0:740c1eb2df13 83 memcpy_P(mem, profile, profile_len);
lixianyu 0:740c1eb2df13 84 static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, mem, profile_len);
lixianyu 0:740c1eb2df13 85 #else
lixianyu 0:740c1eb2df13 86 static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_INFO, 0, profile, profile_len);
lixianyu 0:740c1eb2df13 87 #endif
lixianyu 0:740c1eb2df13 88 return;
lixianyu 0:740c1eb2df13 89 }
lixianyu 0:740c1eb2df13 90
lixianyu 0:740c1eb2df13 91 #endif
lixianyu 0:740c1eb2df13 92
lixianyu 0:740c1eb2df13 93 template<class Proto>
lixianyu 0:740c1eb2df13 94 BLYNK_FORCE_INLINE
lixianyu 0:740c1eb2df13 95 void BlynkApi<Proto>::processCmd(const void* buff, size_t len)
lixianyu 0:740c1eb2df13 96 {
lixianyu 0:740c1eb2df13 97 BlynkParam param((void*)buff, len);
lixianyu 0:740c1eb2df13 98 BlynkParam::iterator it = param.begin();
lixianyu 0:740c1eb2df13 99 if (it >= param.end())
lixianyu 0:740c1eb2df13 100 return;
lixianyu 0:740c1eb2df13 101 const char* cmd = it.asStr();
lixianyu 0:740c1eb2df13 102 const uint16_t cmd16 = *(uint16_t*)cmd;
lixianyu 0:740c1eb2df13 103
lixianyu 0:740c1eb2df13 104 if (++it >= param.end())
lixianyu 0:740c1eb2df13 105 return;
lixianyu 0:740c1eb2df13 106
lixianyu 0:740c1eb2df13 107 #if defined(analogInputToDigitalPin)
lixianyu 0:740c1eb2df13 108 // Good! Analog pins can be referenced on this device by name.
lixianyu 0:740c1eb2df13 109 const uint8_t pin = (it.asStr()[0] == 'A') ?
lixianyu 0:740c1eb2df13 110 analogInputToDigitalPin(atoi(it.asStr()+1)) :
lixianyu 0:740c1eb2df13 111 it.asInt();
lixianyu 0:740c1eb2df13 112 #else
lixianyu 0:740c1eb2df13 113 const uint8_t pin = it.asInt();
lixianyu 0:740c1eb2df13 114 #endif
lixianyu 0:740c1eb2df13 115
lixianyu 0:740c1eb2df13 116 switch(cmd16) {
lixianyu 0:740c1eb2df13 117
lixianyu 0:740c1eb2df13 118 #ifndef BLYNK_NO_BUILTIN
lixianyu 0:740c1eb2df13 119
lixianyu 0:740c1eb2df13 120 case BLYNK_HW_PM: {
lixianyu 0:740c1eb2df13 121 while (it < param.end()) {
lixianyu 0:740c1eb2df13 122 ++it;
lixianyu 0:740c1eb2df13 123 if (!strcmp(it.asStr(), "in")) {
lixianyu 0:740c1eb2df13 124 //pinMode(pin, INPUT);
lixianyu 0:740c1eb2df13 125 } else if (!strcmp(it.asStr(), "out") || !strcmp(it.asStr(), "pwm")) {
lixianyu 0:740c1eb2df13 126 //pinMode(pin, OUTPUT);
lixianyu 0:740c1eb2df13 127 } else {
lixianyu 0:740c1eb2df13 128 #ifdef BLYNK_DEBUG
lixianyu 0:740c1eb2df13 129 BLYNK_LOG4(BLYNK_F("Invalid pin "), pin, BLYNK_F(" mode "), it.asStr());
lixianyu 0:740c1eb2df13 130 #endif
lixianyu 0:740c1eb2df13 131 }
lixianyu 0:740c1eb2df13 132 ++it;
lixianyu 0:740c1eb2df13 133 }
lixianyu 0:740c1eb2df13 134 } break;
lixianyu 0:740c1eb2df13 135 case BLYNK_HW_DR: {
lixianyu 0:740c1eb2df13 136 DigitalIn p((PinName)pin);
lixianyu 0:740c1eb2df13 137 char mem[16];
lixianyu 0:740c1eb2df13 138 BlynkParam rsp(mem, 0, sizeof(mem));
lixianyu 0:740c1eb2df13 139 rsp.add("dw");
lixianyu 0:740c1eb2df13 140 rsp.add(pin);
lixianyu 0:740c1eb2df13 141 rsp.add(int(p));
lixianyu 0:740c1eb2df13 142 static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
lixianyu 0:740c1eb2df13 143 } break;
lixianyu 0:740c1eb2df13 144 case BLYNK_HW_DW: {
lixianyu 0:740c1eb2df13 145 // Should be 1 parameter (value)
lixianyu 0:740c1eb2df13 146 if (++it >= param.end())
lixianyu 0:740c1eb2df13 147 return;
lixianyu 0:740c1eb2df13 148
lixianyu 0:740c1eb2df13 149 //BLYNK_LOG("digitalWrite %d -> %d", pin, it.asInt());
lixianyu 0:740c1eb2df13 150 DigitalOut p((PinName)pin);
lixianyu 0:740c1eb2df13 151 p = it.asInt() ? 1 : 0;
lixianyu 0:740c1eb2df13 152 } break;
lixianyu 0:740c1eb2df13 153 case BLYNK_HW_AR: {
lixianyu 0:740c1eb2df13 154 AnalogIn p((PinName)pin);
lixianyu 0:740c1eb2df13 155 char mem[16];
lixianyu 0:740c1eb2df13 156 BlynkParam rsp(mem, 0, sizeof(mem));
lixianyu 0:740c1eb2df13 157 rsp.add("aw");
lixianyu 0:740c1eb2df13 158 rsp.add(pin);
lixianyu 0:740c1eb2df13 159 rsp.add(int(p.read() * 1024));
lixianyu 0:740c1eb2df13 160 static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, rsp.getBuffer(), rsp.getLength()-1);
lixianyu 0:740c1eb2df13 161 } break;
lixianyu 0:740c1eb2df13 162 case BLYNK_HW_AW: {
lixianyu 0:740c1eb2df13 163 // TODO: Not supported yet
lixianyu 0:740c1eb2df13 164 } break;
lixianyu 0:740c1eb2df13 165
lixianyu 0:740c1eb2df13 166 #endif
lixianyu 0:740c1eb2df13 167
lixianyu 0:740c1eb2df13 168 case BLYNK_HW_VR: {
lixianyu 0:740c1eb2df13 169 BlynkReq req = { pin };
lixianyu 0:740c1eb2df13 170 WidgetReadHandler handler = GetReadHandler(pin);
lixianyu 0:740c1eb2df13 171 if (handler && (handler != BlynkWidgetRead)) {
lixianyu 0:740c1eb2df13 172 handler(req);
lixianyu 0:740c1eb2df13 173 } else {
lixianyu 0:740c1eb2df13 174 BlynkWidgetReadDefault(req);
lixianyu 0:740c1eb2df13 175 }
lixianyu 0:740c1eb2df13 176 } break;
lixianyu 0:740c1eb2df13 177 case BLYNK_HW_VW: {
lixianyu 0:740c1eb2df13 178 ++it;
lixianyu 0:740c1eb2df13 179 char* start = (char*)it.asStr();
lixianyu 0:740c1eb2df13 180 BlynkParam param2(start, len - (start - (char*)buff));
lixianyu 0:740c1eb2df13 181 BlynkReq req = { pin };
lixianyu 0:740c1eb2df13 182 WidgetWriteHandler handler = GetWriteHandler(pin);
lixianyu 0:740c1eb2df13 183 if (handler && (handler != BlynkWidgetWrite)) {
lixianyu 0:740c1eb2df13 184 handler(req, param2);
lixianyu 0:740c1eb2df13 185 } else {
lixianyu 0:740c1eb2df13 186 BlynkWidgetWriteDefault(req, param2);
lixianyu 0:740c1eb2df13 187 }
lixianyu 0:740c1eb2df13 188 } break;
lixianyu 0:740c1eb2df13 189 default:
lixianyu 0:740c1eb2df13 190 BLYNK_LOG2(BLYNK_F("Invalid HW cmd: "), cmd);
lixianyu 0:740c1eb2df13 191 static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_RESPONSE, static_cast<Proto*>(this)->currentMsgId, NULL, BLYNK_ILLEGAL_COMMAND);
lixianyu 0:740c1eb2df13 192 }
lixianyu 0:740c1eb2df13 193 }
lixianyu 0:740c1eb2df13 194
lixianyu 0:740c1eb2df13 195 #endif