feat: 全量同步 254 个常用的 Arduino 扩展库文件

This commit is contained in:
yczpf2019
2026-01-24 16:05:38 +08:00
parent c665ba662b
commit 397b9a23a3
6878 changed files with 2732224 additions and 1 deletions

View File

@@ -0,0 +1,370 @@
#ifndef BLINKER_AIR202_LP_H
#define BLINKER_AIR202_LP_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
#include "../Functions/BlinkerAIR202.h"
#include "../Functions/BlinkerHTTPAIR202.h"
#ifndef ARDUINOJSON_VERSION_MAJOR
#include "../modules/ArduinoJson/ArduinoJson.h"
#endif
class BlinkerAIR202LP : public BlinkerStream
{
public :
BlinkerAIR202LP()
: stream(NULL), isConnect(false)
{}
int connect() { return isGPRSinit ? true : false; }
int connected() { return isGPRSinit ? true : false; }
void disconnect() { delay(1); }
void ping() { delay(1); }
int available()
{
dataGet();
if (isAvail)
{
isAvail = false; return true;
}
else return false;
}
char * lastRead() { if (isFresh) return msgBuf; return ""; }
void flush() { if (isFresh) { free(msgBuf); isFresh = false; isAvail = false; } }
void dataGet();
int print(char * data, bool needCheck = true);
void begin(const char* _key, const char* _deviceType, String _imei);
void initStream(Stream& s, bool state, blinker_callback_t func);
char * deviceName() { return _deviceName; }
char * authKey() { return _authKey; }
char * token() { if (!isGPRSinit) return ""; else return _authKey; }
int init() { return isGPRSinit; }
int deviceRegister() { return connectServer(); }
private :
bool isGPRSinit = false;
int connectServer();
protected :
Stream* stream;
char* msgBuf;
bool isConnect;
bool isHWS = false;
char* imei;
bool isAvail = false;
bool isFresh = false;
bool _isAuthKey = false;
const char* _vipKey;
const char* _deviceType;
char* _authKey;
char* _deviceName;
blinker_callback_t listenFunc = NULL;
};
int BlinkerAIR202LP::print(char * data, bool needCheck)
{
String msg = BLINKER_F("{\"deviceName\":\"");
msg += _deviceName;
msg += BLINKER_F("\",\"key\":\"");
msg += _authKey;
msg += BLINKER_F("\",\"data\":");
msg += data;
msg += BLINKER_F("}");
String host = BLINKER_F(BLINKER_SERVER_HTTPS);
String url_iot = BLINKER_F("/api/v1/user/device/lowpower/data");
BLINKER_LOG_ALL(BLINKER_F("HTTPS begin: "), host + url_iot);
BlinkerHTTPAIR202 http(*stream, isHWS, listenFunc);
String conType = BLINKER_F("Content-Type");
String application = BLINKER_F("application/json;charset=utf-8");
http.begin(host, url_iot);
String payload;
if (http.POST(msg, conType, application))
{
BLINKER_LOG(BLINKER_F("[HTTP] ... success"));
payload = http.getString();
}
else
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... failed"));
return false;
}
BLINKER_LOG_ALL(BLINKER_F("reply was:"));
BLINKER_LOG_ALL(BLINKER_F("=============================="));
BLINKER_LOG_ALL(payload);
BLINKER_LOG_ALL(BLINKER_F("=============================="));
// DynamicJsonBuffer jsonBuffer;
// JsonObject& data_rp = jsonBuffer.parseObject(payload);
DynamicJsonDocument jsonBuffer(1024);
DeserializationError error = deserializeJson(jsonBuffer, payload);
JsonObject data_rp = jsonBuffer.as<JsonObject>();
// if (data_rp.success())
if (!error)
{
uint16_t msg_code = data_rp[BLINKER_CMD_MESSAGE];
if (msg_code != 1000)
{
String _detail = data_rp[BLINKER_CMD_DETAIL];
BLINKER_ERR_LOG(_detail);
}
else
{
payload = data_rp[BLINKER_CMD_DETAIL][BLINKER_CMD_DATA].as<String>();
}
}
BLINKER_LOG_ALL(BLINKER_F("payload: "), payload);
return true;
}
void BlinkerAIR202LP::dataGet()
{
String host = BLINKER_F(BLINKER_SERVER_HTTPS);
String uri = "";
uri += BLINKER_F("/api/v1/user/device/lowpower/data?deviceName=");
uri += _deviceName;
uri += BLINKER_F("&key=");
uri += _authKey;
BLINKER_LOG_ALL(BLINKER_F("HTTPS begin: "), host + uri);
BlinkerHTTPAIR202 http(*stream, isHWS, listenFunc);
http.begin(host, uri);
String payload;
if (http.GET())
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... success"));
payload = http.getString();
}
else
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... failed"));
return;
}
BLINKER_LOG_ALL(BLINKER_F("reply was:"));
BLINKER_LOG_ALL(BLINKER_F("=============================="));
BLINKER_LOG_ALL(payload);
BLINKER_LOG_ALL(BLINKER_F("=============================="));
// DynamicJsonBuffer jsonBuffer;
// JsonObject& data_rp = jsonBuffer.parseObject(payload);
DynamicJsonDocument jsonBuffer(1024);
DeserializationError error = deserializeJson(jsonBuffer, payload);
JsonObject data_rp = jsonBuffer.as<JsonObject>();
// if (data_rp.success())
if (!error)
{
uint16_t msg_code = data_rp[BLINKER_CMD_MESSAGE];
if (msg_code != 1000)
{
String _detail = data_rp[BLINKER_CMD_DETAIL];
BLINKER_ERR_LOG(_detail);
}
else
{
payload = data_rp[BLINKER_CMD_DETAIL][BLINKER_CMD_DATA].as<String>();
if (isFresh) free(msgBuf);
if (payload != "{}")
{
msgBuf = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(msgBuf, payload.c_str());
isFresh = true;
isAvail = true;
BLINKER_LOG_ALL(BLINKER_F("isAvail"));
}
}
}
BLINKER_LOG_ALL(BLINKER_F("payload: "), payload);
}
void BlinkerAIR202LP::begin(const char* _key, const char* _type, String _imei)
{
_vipKey = _key;
_deviceType = _type;
BLINKER_LOG_ALL(BLINKER_F("PRO deviceType: "), _type);
imei = (char*)malloc((_imei.length() + 1)*sizeof(char));
strcpy(imei, _imei.c_str());
// authKey = (char*)malloc((strlen(_deviceType) + 1)*sizeof(char));
// strcpy(authKey, _deviceType);
}
void BlinkerAIR202LP::initStream(Stream& s, bool state, blinker_callback_t func)
{
stream = &s;
stream->setTimeout(BLINKER_STREAM_TIMEOUT);
isHWS = state;
listenFunc = func;
}
int BlinkerAIR202LP::connectServer()
{
String host = BLINKER_F(BLINKER_SERVER_HTTPS);
String uri = "";
if (!_isAuthKey)
{
// uri += BLINKER_F("/api/v1/user/device/register?deviceType=");
// uri += _deviceType;
// uri += BLINKER_F("&deviceName=");
// uri += imei;
uri += BLINKER_F("/api/v1/user/device/pro/lowpower/auth/get?deviceType=");
uri += _deviceType;
uri += BLINKER_F("&vipKey=");
uri += _vipKey;
uri += BLINKER_F("&deviceName=");
uri += imei;
BLINKER_LOG_ALL(BLINKER_F("HTTPS begin: "), host + uri);
BlinkerHTTPAIR202 http(*stream, isHWS, listenFunc);
http.begin(host, uri);
String payload;
if (http.GET())
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... success"));
payload = http.getString();
// return true;
}
else
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... failed"));
return false;
}
BLINKER_LOG_ALL(BLINKER_F("reply was:"));
BLINKER_LOG_ALL(BLINKER_F("=============================="));
BLINKER_LOG_ALL(payload);
BLINKER_LOG_ALL(BLINKER_F("=============================="));
// DynamicJsonBuffer jsonBuffer;
// JsonObject& root = jsonBuffer.parseObject(payload);
DynamicJsonDocument jsonBuffer(1024);
DeserializationError error = deserializeJson(jsonBuffer, payload);
JsonObject root = jsonBuffer.as<JsonObject>();
if (STRING_contains_string(payload, BLINKER_CMD_NOTFOUND) || error ||
!STRING_contains_string(payload, BLINKER_CMD_AUTHKEY)) {
// while(1) {
BLINKER_ERR_LOG(("Please make sure you have register this device!"));
// ::delay(60000);
return false;
// }
}
String _getAuthKey = root[BLINKER_CMD_DETAIL][BLINKER_CMD_AUTHKEY];
_authKey = (char*)malloc((_getAuthKey.length()+1)*sizeof(char));
strcpy(_authKey, _getAuthKey.c_str());
BLINKER_LOG_ALL(BLINKER_F("===================="));
BLINKER_LOG_ALL(BLINKER_F("_authKey: "), _authKey);
BLINKER_LOG_ALL(BLINKER_F("===================="));
_isAuthKey = true;
}
String url_iot = BLINKER_F("/api/v1/user/device/pro/lowpower/auth?authKey=");
url_iot += _authKey;
BLINKER_LOG_ALL(BLINKER_F("HTTPS begin: "), host + url_iot);
BlinkerHTTPAIR202 http(*stream, isHWS, listenFunc);
http.begin(host, url_iot);
String payload;
if (http.GET())
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... success"));
payload = http.getString();
// return true;
}
else
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... failed"));
return false;
}
BLINKER_LOG_ALL(BLINKER_F("reply was:"));
BLINKER_LOG_ALL(BLINKER_F("=============================="));
BLINKER_LOG_ALL(payload);
BLINKER_LOG_ALL(BLINKER_F("=============================="));
// DynamicJsonBuffer jsonBuffer;
// JsonObject& root = jsonBuffer.parseObject(payload);
DynamicJsonDocument jsonBuffer(1024);
DeserializationError error = deserializeJson(jsonBuffer, payload);
JsonObject root = jsonBuffer.as<JsonObject>();
if (STRING_contains_string(payload, BLINKER_CMD_NOTFOUND) || error ||
!STRING_contains_string(payload, BLINKER_CMD_DEVICENAME)) {
// while(1) {
BLINKER_ERR_LOG(("Please make sure you have register this device!"));
// ::delay(60000);
return false;
// }
}
String _device_name = root[BLINKER_CMD_DETAIL][BLINKER_CMD_DEVICENAME];
if (isGPRSinit)
{
free(_deviceName);
isGPRSinit = false;
}
_deviceName = (char*)malloc((_device_name.length()+1)*sizeof(char));
strcpy(_deviceName, _device_name.c_str());
isGPRSinit = true;
return true;
}
#endif

View File

@@ -0,0 +1,343 @@
#ifndef BLINKER_BLE_H
#define BLINKER_BLE_H
#if defined(ESP32)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerBLE : public BlinkerStream, public BLEServerCallbacks, public BLECharacteristicCallbacks
{
public :
BlinkerBLE()
: deviceConnected(false), isAvail(false)
{}
void begin();
int available();
int read();
int timedRead();
char * lastRead();// { return _isFresh ? BLEBuf : ""; }
void flush();
// bool print(String s, bool needCheck = true);
int print(char * data, bool needCheck = true);
int connect() { return deviceConnected; }
void disconnect() { deviceConnected = false; }
int connected() { return deviceConnected; }
private :
bool deviceConnected;
char* BLEBuf;//[BLINKER_MAX_READ_SIZE];
bool _isFresh = false;
bool isAvail;
bool isFresh;
uint32_t _bufLen;
uint32_t freshTime;
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;
BLEAdvertising *pAdvertising;
BLEAdvertisementData pAdvertisementData;
uint8_t respTimes = 0;
uint32_t respTime = 0;
// bool isAvailBLE = false;
// uint8_t* bleReadBuf;//[20];
// uint32_t bleReadBufLen = 0;
// uint32_t getNum = 0;
// bool isNewLine = false;
void onConnect(BLEServer* pServer);
void onDisconnect(BLEServer* pServer);
void onWrite(BLECharacteristic *pCharacteristic);
int checkTimeOut();
int checkPrintSpan();
};
void BlinkerBLE::begin()
{
BLEDevice::init("Blinker");
pServer = BLEDevice::createServer();
pService = pServer->createService(BLEUUID((uint16_t)0xffe0));//SERVICE_UUID
pServer->setCallbacks(this);
pCharacteristic = pService->createCharacteristic(
BLEUUID((uint16_t)0xffe1),//CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_WRITE_NR
);
pCharacteristic->setCallbacks(this);
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic->setValue("Blinker");
pService->start();
pAdvertising = pServer->getAdvertising();
BLEAddress otherAddress = BLEDevice::getAddress();
esp_bd_addr_t ble_m_address;
memcpy(ble_m_address, otherAddress.getNative(), ESP_BD_ADDR_LEN);
char macStr[9] = { 0 };
macStr[0] = 0x48;
macStr[1] = 0X4d;
for (uint8_t cpynum = 0; cpynum < 6; cpynum++) {
macStr[cpynum+2] = (char)ble_m_address[cpynum];
}
pAdvertisementData.setManufacturerData(macStr);
pAdvertising->setAdvertisementData(pAdvertisementData);
pAdvertising->addServiceUUID(BLEUUID((uint16_t)0xffe0));
pAdvertising->start();
_bufLen = 0;
}
int BlinkerBLE::available()
{
// checkTimeOut();
if (_bufLen)
{
bool isNewLine = false;
for (uint8_t num = 0; num < _bufLen; num++)
{
if (BLEBuf[num] == '\n')
{
BLEBuf[num] = '\0';
isNewLine = true;
}
}
if (isNewLine)
{
BLINKER_LOG_ALL(BLINKER_F("GET1: "), BLEBuf);
isAvail = true;
_isFresh = true;
// for (uint8_t num = 0; num < _bufLen; num++)
// {
// Serial.print(BLEBuf[num]);
// }
// Serial.println();
}
else
{
if (checkTimeOut())
{
BLEBuf[_bufLen] = '\0';
BLINKER_LOG_ALL(BLINKER_F("GET: "), BLEBuf);
isAvail = true;
_isFresh = true;
}
}
}
if (isAvail)
{
BLINKER_LOG_ALL(BLINKER_F("handleBLE: "), BLEBuf);
BLINKER_LOG_FreeHeap_ALL();
isAvail = false;
return true;
}
else {
return false;
}
return false;
}
// int BlinkerBLE::read()
// {
// uint32_t num = getNum;
// if (num < bleReadBufLen)
// {
// getNum++;
// return bleReadBuf[num];
// }
// else
// {
// return -1;
// }
// }
// int BlinkerBLE::timedRead()
// {
// int c;
// uint32_t _startMillis = millis();
// do {
// c = read();
// if (c >= 0) return c;
// } while(millis() - _startMillis < 1000);
// return -1;
// }
char * BlinkerBLE::lastRead()
{
if (_isFresh) return BLEBuf;
else return "";
}
void BlinkerBLE::flush()
{
if (_isFresh || _bufLen)
{
BLINKER_LOG_ALL(BLINKER_F("flush"));
free(BLEBuf); //isFresh = false;
isAvail = false; _isFresh = false;
_bufLen = 0; //isNewLine = false;
}
}
// bool BlinkerBLE::print(String s, bool needCheck)
int BlinkerBLE::print(char * data, bool needCheck)
{
if (needCheck)
{
if (!checkPrintSpan())
{
respTime = millis();
return false;
}
}
String s = data;
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("Response: "), s);
if (connected())
{
BLINKER_LOG_ALL(BLINKER_F("Success..."));
s += BLINKER_CMD_NEWLINE;
String s_send;
uint8_t parts = s.length()/20 + 1;
for (uint8_t num = 0; num < parts; num++)
{
if ((num + 1) == parts)
s_send = s.substring(num*(20), s.length());
else
s_send = s.substring(num*(20), (num+1)*20);
// BLINKER_LOG_ALL("s_send: ", s_send);
pCharacteristic->setValue(s_send.c_str());
pCharacteristic->notify();
delay(5);
}
return true;
}
else
{
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
void BlinkerBLE::onConnect(BLEServer* pServer)
{
deviceConnected = true;
BLINKER_LOG_ALL("BLE connect");
}
void BlinkerBLE::onDisconnect(BLEServer* pServer)
{
deviceConnected = false;
BLINKER_LOG_ALL("BLE disconnect");
pServer->startAdvertising();
}
void BlinkerBLE::onWrite(BLECharacteristic *pCharacteristic)
{
std::string value = pCharacteristic->getValue();
int vlen = value.length();
if (vlen > 0)
{
int data;
uint8_t num;
if (!_bufLen) BLEBuf = (char*)malloc(BLINKER_MAX_READ_SIZE*sizeof(char));
for (num = 0; num < vlen; num++)
{
data = value[num];
if (data == '\n')
{
BLEBuf[_bufLen+num] = data;
num++;
BLINKER_LOG_ALL(BLINKER_F("GET \\n"));
break;
}
BLEBuf[_bufLen+num] = (char)data;
}
if (_bufLen) _bufLen += num;
else _bufLen = num;
freshTime = millis();
BLINKER_LOG_ALL(BLINKER_F("vlen: "), vlen);
BLINKER_LOG_ALL(BLINKER_F("_bufLen: "), _bufLen);
}
}
int BlinkerBLE::checkTimeOut()
{
::delay(10);
uint32_t timeout_ms = millis() - freshTime;
// BLINKER_LOG_ALL(BLINKER_F("timeout_ms: "), timeout_ms);
return timeout_ms > 1000;
}
int BlinkerBLE::checkPrintSpan()
{
if (millis() - respTime < BLINKER_PRINT_MSG_LIMIT)
{
if (respTimes > BLINKER_PRINT_MSG_LIMIT)
{
BLINKER_ERR_LOG("DEVICE NOT CONNECT OR MSG LIMIT");
return false;
}
else
{
respTimes++;
return true;
}
}
else
{
respTimes = 0;
return true;
}
}
#endif
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,196 @@
#ifndef BLINKER_SERIAL_H
#define BLINKER_SERIAL_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
// #include "../Adapters/BlinkerSerial.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerialBLE;
#elif defined (__AVR__) || defined(ESP8266)
#include <SoftwareSerial.h>
SoftwareSerial *SSerialBLE;
#endif
class BlinkerSerial : public BlinkerStream
{
public :
BlinkerSerial()
: stream(NULL), isConnect(false)
{}
int available();
void begin(Stream& s, bool state);
int timedRead();
char * lastRead() { if (isFresh) return streamData; else return ""; }
void flush();
int print(char * data, bool needCheck = true);
int connect() { isConnect = true; return connected(); }
int connected() { return isConnect; }
void disconnect() { isConnect = false; }
protected :
Stream* stream;
char* streamData;
bool isFresh;
bool isConnect;
bool isHWS = false;
uint8_t respTimes = 0;
uint32_t respTime = 0;
int checkPrintSpan();
};
int BlinkerSerial::available()
{
if (!isHWS)
{
#if defined(__AVR__) || defined(ESP8266)
if (!SSerialBLE->isListening())
{
SSerialBLE->listen();
::delay(100);
}
#endif
}
if (stream->available())
{
if (isFresh) free(streamData);
streamData = (char*)malloc(1*sizeof(char));
int16_t dNum = 0;
int c_d = timedRead();
while (dNum < BLINKER_MAX_READ_SIZE &&
c_d >=0 && c_d != '\n')
{
if (c_d != '\r')
{
streamData[dNum] = (char)c_d;
dNum++;
streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
}
c_d = timedRead();
}
dNum++;
streamData = (char*)realloc(streamData, dNum*sizeof(char));
streamData[dNum-1] = '\0';
stream->flush();
BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData);
// BLINKER_LOG_FreeHeap_ALL();
if (strlen(streamData) < BLINKER_MAX_READ_SIZE)
{
if (streamData[strlen(streamData) - 1] == '\r')
streamData[strlen(streamData) - 1] = '\0';
isFresh = true;
return true;
}
else
{
free(streamData);
return false;
}
}
else
{
return false;
}
}
void BlinkerSerial::begin(Stream& s, bool state)
{
stream = &s;
stream->setTimeout(BLINKER_STREAM_TIMEOUT);
isHWS = state;
}
int BlinkerSerial::timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
void BlinkerSerial::flush()
{
if (isFresh)
{
free(streamData); isFresh = false;
}
}
// int BlinkerSerial::print(const String & s, bool needCheck)
int BlinkerSerial::print(char * data, bool needCheck)
{
if (needCheck)
{
if (!checkPrintSpan())
{
respTime = millis();
return false;
}
}
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("Response: "), data);
if(connected())
{
BLINKER_LOG_ALL(BLINKER_F("Success..."));
stream->println(data);
return true;
}
else
{
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
int BlinkerSerial::checkPrintSpan()
{
if (millis() - respTime < BLINKER_PRINT_MSG_LIMIT)
{
if (respTimes > BLINKER_PRINT_MSG_LIMIT)
{
BLINKER_ERR_LOG(BLINKER_F("DEVICE NOT CONNECT OR MSG LIMIT"));
return false;
}
else
{
respTimes++;
return true;
}
}
else
{
respTimes = 0;
return true;
}
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,912 @@
#ifndef BLINKER_SERIAL_GPRS_H
#define BLINKER_SERIAL_GPRS_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
#include "../Functions/BlinkerHTTPAIR202.h"
#ifndef ARDUINOJSON_VERSION_MAJOR
#include "../modules/ArduinoJson/ArduinoJson.h"
#endif
#include "../Functions/BlinkerMQTTAIR202.h"
// #if defined(ESP32)
// #include <HardwareSerial.h>
// HardwareSerial *HSerial;
// #else
// #include <SoftwareSerial.h>
// SoftwareSerial *SSerial;
// #endif
char* MQTT_HOST_GPRS;
char* MQTT_ID_GPRS;
char* MQTT_NAME_GPRS;
char* MQTT_KEY_GPRS;
char* MQTT_PRODUCTINFO_GPRS;
char* UUID_GPRS;
char* AUTHKEY_GPRS;
char* MQTT_DEVICEID_GPRS;
char* DEVICE_NAME_GPRS;
char* BLINKER_PUB_TOPIC_GPRS;
char* BLINKER_SUB_TOPIC_GPRS;
uint16_t MQTT_PORT_GPRS;
BlinkerMQTTAIR202* mqtt_GPRS;
class BlinkerSerialGPRS : public BlinkerStream
{
public :
BlinkerSerialGPRS()
: stream(NULL), isConnect(false)
{}
int connect();
int connected();
int mConnected();
void disconnect();
void ping();
int available();
void subscribe();
int timedRead();
char * lastRead() { if (isFresh_GPRS) return msgBuf_GPRS; return ""; }
void flush();
// int print(const String & s, bool needCheck = true);
int print(char * data, bool needCheck = true);
int toServer(char * data);
// int bPrint(char * name, const String & data);
// int aliPrint(const String & s);
// int duerPrint(const String & s);
// int aliPrint(const String & data);
// int duerPrint(const String & data, bool report = false);
void begin(const char* _deviceType, String _imei);
void initStream(Stream& s, bool state, blinker_callback_t func);
char * deviceName();
char * authKey() { return AUTHKEY_GPRS; }
char * token() { if (!isMQTTinit) return ""; else return MQTT_KEY_GPRS; }
int init() { return isMQTTinit; }
int reRegister() { return connectServer(); }
int deviceRegister() { return connectServer(); }
// int authCheck();
void freshAlive() { kaTime = millis(); isAlive = true; }
// int needFreshShare();
private :
bool isMQTTinit = false;
int connectServer();
void checkKA();
int checkCanPrint();
int checkPrintSpan();
protected :
Stream* stream;
// char* streamData;
char* msgBuf_GPRS;
// bool isFresh = false;
bool isFresh_GPRS = false;
bool isConnect;
bool isHWS = false;
char* imei;
uint8_t respTimes = 0;
uint32_t respTime = 0;
bool isAvail_GPRS = false;
uint8_t dataFrom_GPRS = BLINKER_MSG_FROM_MQTT;
// uint8_t _sharerFrom = BLINKER_MQTT_FROM_AUTHER;
const char* _deviceType;
bool isAlive = false;
uint32_t kaTime = 0;
uint32_t latestTime;
uint32_t printTime = 0;
int isJson(const String & data);
uint8_t reconnect_time = 0;
blinker_callback_t listenFunc = NULL;
};
int BlinkerSerialGPRS::connect()
{
if (!isMQTTinit) return false;
if (mqtt_GPRS->connected()) return true;
disconnect();
if ((millis() - latestTime) < BLINKER_MQTT_CONNECT_TIMESLOT && latestTime > 0)
{
yield();
return false;
}
BLINKER_LOG(BLINKER_F("Connecting to MQTT... "));
BLINKER_LOG_FreeHeap_ALL();
if (!mqtt_GPRS->connect())
{
BLINKER_LOG(BLINKER_F("Retrying MQTT connection in "), \
BLINKER_MQTT_CONNECT_TIMESLOT/1000, \
BLINKER_F(" seconds..."));
this->latestTime = millis();
reconnect_time += 1;
if (reconnect_time >= 12)
{
reRegister();
reconnect_time = 0;
}
return false;
}
reconnect_time = 0;
BLINKER_LOG(BLINKER_F("MQTT Connected!"));
BLINKER_LOG_FreeHeap();
this->latestTime = millis();
return true;
}
int BlinkerSerialGPRS::connected()
{
if (!isMQTTinit) return false;
return mqtt_GPRS->connected();
}
int BlinkerSerialGPRS::mConnected()
{
if (!isMQTTinit) return false;
else return mqtt_GPRS->connected();
}
void BlinkerSerialGPRS::disconnect()
{
if (isMQTTinit) mqtt_GPRS->disconnect();
}
void BlinkerSerialGPRS::ping()
{
BLINKER_LOG_ALL(BLINKER_F("MQTT Ping!"));
BLINKER_LOG_FreeHeap_ALL();
if (!isMQTTinit) return;
if (!mqtt_GPRS->connected())
{
disconnect();
// delay(100);
// connect();
}
else
{
this->latestTime = millis();
}
}
int BlinkerSerialGPRS::available()
{
if (isMQTTinit) {
checkKA();
// if (!mqtt_PRO->connected() || \
// (millis() - this->latestTime) > BLINKER_MQTT_PING_TIMEOUT)
if ((millis() - this->latestTime) > 30000)
{
ping();
}
else
{
subscribe();
}
}
if (isAvail_GPRS)
{
isAvail_GPRS = false;
return true;
}
else {
return false;
}
}
void BlinkerSerialGPRS::subscribe()
{
if (!isMQTTinit) return;
if (mqtt_GPRS->readSubscription())
{
BLINKER_LOG_ALL(BLINKER_F("Got: "), mqtt_GPRS->lastRead);
// DynamicJsonBuffer jsonBuffer;
// JsonObject& root = jsonBuffer.parseObject(String(mqtt_GPRS->lastRead));
DynamicJsonDocument jsonBuffer(1024);
DeserializationError error = deserializeJson(jsonBuffer, String(mqtt_GPRS->lastRead));
JsonObject root = jsonBuffer.as<JsonObject>();
String _uuid = root["fromDevice"];
String dataGet = root["data"];
BLINKER_LOG_ALL(BLINKER_F("data: "), dataGet);
BLINKER_LOG_ALL(BLINKER_F("fromDevice: "), _uuid);
if (strcmp(_uuid.c_str(), UUID_GPRS) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("Authority uuid"));
kaTime = millis();
isAvail_GPRS = true;
isAlive = true;
// _sharerFrom = BLINKER_MQTT_FROM_AUTHER;
}
if (isFresh_GPRS) free(msgBuf_GPRS);
msgBuf_GPRS = (char*)malloc((dataGet.length()+1)*sizeof(char));
strcpy(msgBuf_GPRS, dataGet.c_str());
isFresh_GPRS = true;
this->latestTime = millis();
dataFrom_GPRS = BLINKER_MSG_FROM_MQTT;
}
}
void BlinkerSerialGPRS::flush()
{
if (isFresh_GPRS)
{
free(msgBuf_GPRS); isFresh_GPRS = false; isAvail_GPRS = false;
// isAliAvail = false; //isBavail = false;
}
}
int BlinkerSerialGPRS::timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
// void BlinkerSerialGPRS::flush()
// {
// if (isFresh)
// {
// free(streamData); isFresh_GPRS = false;
// }
// }
int BlinkerSerialGPRS::print(char * data, bool needCheck)
{
BLINKER_LOG_ALL(BLINKER_F("data: "), data);
uint16_t num = strlen(data);
data[num+8] = '\0';
for(uint16_t c_num = num; c_num > 0; c_num--)
{
data[c_num+7] = data[c_num-1];
}
// String data_add = BLINKER_F("{\"data\":");
char data_add[20] = "{\"data\":";
for(uint16_t c_num = 0; c_num < 8; c_num++)
{
data[c_num] = data_add[c_num];
}
// data_add = BLINKER_F(",\"fromDevice\":\"");
// strcat(data, data_add.c_str());
strcat(data, ",\"fromDevice\":\"");
strcat(data, MQTT_ID_GPRS);
// strcat(data, MQTT_DEVICEID_GPRS); //PRO
// data_add = BLINKER_F("\",\"toDevice\":\"");
// strcat(data, data_add.c_str());
strcat(data, "\",\"toDevice\":\"");
// if (_sharerFrom < BLINKER_MQTT_MAX_SHARERS_NUM)
// {
// strcat(data, _sharers[_sharerFrom]->uuid());
// }
// else
// {
strcat(data, UUID_GPRS);
// }
// data_add = BLINKER_F("\",\"deviceType\":\"OwnApp\"}");
// _sharerFrom = BLINKER_MQTT_FROM_AUTHER;
// strcat(data, data_add.c_str());
strcat(data, "\",\"deviceType\":\"OwnApp\"}");
// data_add = STRING_format(data);
if (!isJson(STRING_format(data))) return false;
// data_add.replace("\"", "\\22");
// strcpy(data, data_add.c_str());
uint16_t d_data_len;
for (uint16_t d_num = 0; d_num < 1024; d_num++)
{
if (data[d_num] == '\"')
{
data[d_num] = '\\';
d_data_len = strlen(data);
// BLINKER_LOG_ALL(BLINKER_F("d_num: "), d_num,
// BLINKER_F(", d_data_len: "), d_data_len);
for(uint16_t c_num = d_data_len; c_num > d_num; c_num--)
{
data[c_num + 2] = data[c_num];
}
data[d_num + 1] = '2';
data[d_num + 2] = '2';
}
}
// #if defined(ESP8266)
// data_add = "";
// #endif
// if (!isJson(STRING_format(data)) return false;
// strcpy(data, STRING_format(data).replace("\"", "\\22").c_str());
// String msg_data = STRING_format(data);
// msg_data.replace("\"", "\\22");
BLINKER_LOG_ALL(BLINKER_F("MQTT Publish..."));
BLINKER_LOG_FreeHeap_ALL();
bool _alive = isAlive;
if (needCheck)
{
if (!checkPrintSpan())
{
return false;
}
respTime = millis();
}
if (mqtt_GPRS->connected())
{
if (needCheck)
{
if (!checkCanPrint())
{
if (!_alive)
{
isAlive = false;
}
return false;
}
}
// if (! mqtt_GPRS->publish(BLINKER_PUB_TOPIC_GPRS, msg_data.c_str()))
if (! mqtt_GPRS->publish(BLINKER_PUB_TOPIC_GPRS, data))
{
BLINKER_LOG_ALL(data);
BLINKER_LOG_ALL(BLINKER_F("...Failed"));
BLINKER_LOG_FreeHeap_ALL();
if (!_alive)
{
isAlive = false;
}
return false;
}
else
{
BLINKER_LOG_ALL(data);
BLINKER_LOG_ALL(BLINKER_F("...OK!"));
BLINKER_LOG_FreeHeap_ALL();
if (needCheck) printTime = millis();
if (!_alive)
{
isAlive = false;
}
this->latestTime = millis();
return true;
}
}
else
{
BLINKER_ERR_LOG(BLINKER_F("MQTT Disconnected"));
isAlive = false;
return false;
}
}
int BlinkerSerialGPRS::toServer(char * data)
{
// if (!checkInit()) return false;
if (!isJson(STRING_format(data))) return false;
BLINKER_LOG_ALL(BLINKER_F("MQTT Publish to server..."));
BLINKER_LOG_FreeHeap_ALL();
bool _alive = isAlive;
if (mqtt_GPRS->connected())
{
if (! mqtt_GPRS->publish(BLINKER_PUB_TOPIC_GPRS, data))
{
BLINKER_LOG_ALL(data);
BLINKER_LOG_ALL(BLINKER_F("...Failed"));
BLINKER_LOG_FreeHeap_ALL();
return false;
}
else
{
BLINKER_LOG_ALL(data);
BLINKER_LOG_ALL(BLINKER_F("...OK!"));
BLINKER_LOG_FreeHeap_ALL();
return true;
}
}
else
{
BLINKER_ERR_LOG(BLINKER_F("MQTT Disconnected"));
isAlive = false;
return false;
}
}
// int BlinkerSerialGPRS::aliPrint(const String & s)
// {
// if (!checkPrintSpan()) {
// respTime = millis();
// return false;
// }
// String _s = s.substring(0, s.length() - 1);
// _s += BLINKER_F(",\"toDeviceAT\":\"AliGenie\"}");
// respTime = millis();
// BLINKER_LOG_ALL(BLINKER_F("AliGenie Response: "), _s);
// if(connected()) {
// BLINKER_LOG_ALL(BLINKER_F("Success..."));
// stream->println(_s);
// return true;
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
// return false;
// }
// }
// int BlinkerSerialGPRS::duerPrint(const String & s)
// {
// if (!checkPrintSpan()) {
// respTime = millis();
// return false;
// }
// String _s = s.substring(0, s.length() - 1);
// _s += BLINKER_F(",\"toDeviceAT\":\"DuerOS\"}");
// respTime = millis();
// BLINKER_LOG_ALL(BLINKER_F("DuerOS Response: "), _s);
// if(connected()) {
// BLINKER_LOG_ALL(BLINKER_F("Success..."));
// stream->println(_s);
// return true;
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
// return false;
// }
// }
// int BlinkerSerialGPRS::print(const String & s, bool needCheck)
void BlinkerSerialGPRS::begin(const char* _type, String _imei)
{
_deviceType = _type;
BLINKER_LOG_ALL(BLINKER_F("PRO deviceType: "), _type);
// stream = &s;
// stream->setTimeout(BLINKER_STREAM_TIMEOUT);
// isHWS = state;
imei = (char*)malloc((_imei.length() + 1)*sizeof(char));
strcpy(imei, _imei.c_str());
}
void BlinkerSerialGPRS::initStream(Stream& s, bool state, blinker_callback_t func)
{
// _deviceType = _type;
// BLINKER_LOG_ALL(BLINKER_F("PRO deviceType: "), _type);
stream = &s;
stream->setTimeout(BLINKER_STREAM_TIMEOUT);
isHWS = state;
listenFunc = func;
// streamPrint(BLINKER_CMD_CRESET_RESQ);
// _imei = (char*)malloc(imei.length()*sizeof(char));
// strcpy(_imei, imei.c_str());
}
char * BlinkerSerialGPRS::deviceName() { return MQTT_DEVICEID_GPRS;/*MQTT_ID_PRO;*/ }
void BlinkerSerialGPRS::checkKA()
{
if (millis() - kaTime >= BLINKER_MQTT_KEEPALIVE)
isAlive = false;
}
int BlinkerSerialGPRS::checkCanPrint() {
if ((millis() - printTime >= BLINKER_PRO_MSG_LIMIT && isAlive) || printTime == 0) {
return true;
}
else {
BLINKER_ERR_LOG(BLINKER_F("MQTT NOT ALIVE OR MSG LIMIT"));
checkKA();
return false;
}
}
int BlinkerSerialGPRS::checkPrintSpan()
{
if (millis() - respTime < BLINKER_PRINT_MSG_LIMIT)
{
if (respTimes > BLINKER_PRINT_MSG_LIMIT)
{
BLINKER_ERR_LOG(BLINKER_F("DEVICE NOT CONNECT OR MSG LIMIT"));
return false;
}
else
{
respTimes++;
return true;
}
}
else
{
respTimes = 0;
return true;
}
}
int BlinkerSerialGPRS::connectServer()
{
String host = BLINKER_F(BLINKER_SERVER_HTTPS);
String uri = "";
// uri += BLINKER_F("/api/v1/user/device/register?deviceType=");
// uri += _deviceType;
// uri += BLINKER_F("&deviceName=");
// uri += imei;
uri += BLINKER_F("/api/v1/user/device/diy/auth?authKey=");
uri += _deviceType;
BLINKER_LOG_ALL(BLINKER_F("HTTPS begin: "), host + uri);
BlinkerHTTPAIR202 http(*stream, isHWS, listenFunc);
http.begin(host, uri);
String payload;
if (http.GET())
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... success"));
payload = http.getString();
// return true;
}
else
{
BLINKER_LOG(BLINKER_F("[HTTP] GET... failed"));
return false;
}
BLINKER_LOG_ALL(BLINKER_F("reply was:"));
BLINKER_LOG_ALL(BLINKER_F("=============================="));
BLINKER_LOG_ALL(payload);
BLINKER_LOG_ALL(BLINKER_F("=============================="));
// DynamicJsonBuffer jsonBuffer;
// JsonObject& root = jsonBuffer.parseObject(payload);
DynamicJsonDocument jsonBuffer(1024);
DeserializationError error = deserializeJson(jsonBuffer, payload);
JsonObject root = jsonBuffer.as<JsonObject>();
if (STRING_contains_string(payload, BLINKER_CMD_NOTFOUND) || error ||
!STRING_contains_string(payload, BLINKER_CMD_IOTID)) {
// while(1) {
BLINKER_ERR_LOG(("Please make sure you have register this device!"));
// ::delay(60000);
return false;
// }
}
String _userID = root[BLINKER_CMD_DETAIL][BLINKER_CMD_DEVICENAME];
String _userName = root[BLINKER_CMD_DETAIL][BLINKER_CMD_IOTID];
String _key = root[BLINKER_CMD_DETAIL][BLINKER_CMD_IOTTOKEN];
String _productInfo = root[BLINKER_CMD_DETAIL][BLINKER_CMD_PRODUCTKEY];
String _broker = root[BLINKER_CMD_DETAIL][BLINKER_CMD_BROKER];
String _uuid = root[BLINKER_CMD_DETAIL][BLINKER_CMD_UUID];
if (isMQTTinit)
{
free(MQTT_HOST_GPRS);
free(MQTT_ID_GPRS);
free(MQTT_NAME_GPRS);
free(MQTT_KEY_GPRS);
free(MQTT_PRODUCTINFO_GPRS);
free(UUID_GPRS);
free(DEVICE_NAME_GPRS);
free(BLINKER_PUB_TOPIC_GPRS);
free(BLINKER_SUB_TOPIC_GPRS);
free(mqtt_GPRS);
// free(iotSub_GPRS);
isMQTTinit = false;
}
if (_broker == BLINKER_MQTT_BORKER_ALIYUN) {
// memcpy(DEVICE_NAME_MQTT, _userID.c_str(), 12);
DEVICE_NAME_GPRS = (char*)malloc((_userID.length()+1)*sizeof(char));
strcpy(DEVICE_NAME_GPRS, _userID.c_str());
MQTT_ID_GPRS = (char*)malloc((_userID.length()+1)*sizeof(char));
strcpy(MQTT_ID_GPRS, _userID.c_str());
MQTT_NAME_GPRS = (char*)malloc((_userName.length()+1)*sizeof(char));
strcpy(MQTT_NAME_GPRS, _userName.c_str());
MQTT_KEY_GPRS = (char*)malloc((_key.length()+1)*sizeof(char));
strcpy(MQTT_KEY_GPRS, _key.c_str());
MQTT_PRODUCTINFO_GPRS = (char*)malloc((_productInfo.length()+1)*sizeof(char));
strcpy(MQTT_PRODUCTINFO_GPRS, _productInfo.c_str());
MQTT_HOST_GPRS = (char*)malloc((strlen(BLINKER_MQTT_ALIYUN_HOST)+1)*sizeof(char));
strcpy(MQTT_HOST_GPRS, BLINKER_MQTT_ALIYUN_HOST);
MQTT_PORT_GPRS = BLINKER_MQTT_ALIYUN_PORT;
}
UUID_GPRS = (char*)malloc((_uuid.length()+1)*sizeof(char));
strcpy(UUID_GPRS, _uuid.c_str());
BLINKER_LOG_ALL(BLINKER_F("===================="));
BLINKER_LOG_ALL(BLINKER_F("DEVICE_NAME_GPRS: "), DEVICE_NAME_GPRS);
BLINKER_LOG_ALL(BLINKER_F("MQTT_PRODUCTINFO_GPRS: "), MQTT_PRODUCTINFO_GPRS);
BLINKER_LOG_ALL(BLINKER_F("MQTT_ID_GPRS: "), MQTT_ID_GPRS);
BLINKER_LOG_ALL(BLINKER_F("MQTT_NAME_GPRS: "), MQTT_NAME_GPRS);
BLINKER_LOG_ALL(BLINKER_F("MQTT_KEY_GPRS: "), MQTT_KEY_GPRS);
BLINKER_LOG_ALL(BLINKER_F("MQTT_BROKER: "), _broker);
BLINKER_LOG_ALL(BLINKER_F("HOST: "), MQTT_HOST_GPRS);
BLINKER_LOG_ALL(BLINKER_F("PORT: "), MQTT_PORT_GPRS);
BLINKER_LOG_ALL(BLINKER_F("UUID_GPRS: "), UUID_GPRS);
BLINKER_LOG_ALL(BLINKER_F("===================="));
if (_broker == BLINKER_MQTT_BORKER_ALIYUN) {
String PUB_TOPIC_STR = BLINKER_F("/");
PUB_TOPIC_STR += MQTT_PRODUCTINFO_GPRS;
PUB_TOPIC_STR += BLINKER_F("/");
PUB_TOPIC_STR += MQTT_ID_GPRS;
PUB_TOPIC_STR += BLINKER_F("/s");
BLINKER_PUB_TOPIC_GPRS = (char*)malloc((PUB_TOPIC_STR.length() + 1)*sizeof(char));
// memcpy(BLINKER_PUB_TOPIC_GPRS, PUB_TOPIC_STR.c_str(), str_len);
strcpy(BLINKER_PUB_TOPIC_GPRS, PUB_TOPIC_STR.c_str());
BLINKER_LOG_ALL(BLINKER_F("BLINKER_PUB_TOPIC_GPRS: "), BLINKER_PUB_TOPIC_GPRS);
String SUB_TOPIC_STR = BLINKER_F("/");
SUB_TOPIC_STR += MQTT_PRODUCTINFO_GPRS;
SUB_TOPIC_STR += BLINKER_F("/");
SUB_TOPIC_STR += MQTT_ID_GPRS;
SUB_TOPIC_STR += BLINKER_F("/r");
BLINKER_SUB_TOPIC_GPRS = (char*)malloc((SUB_TOPIC_STR.length() + 1)*sizeof(char));
// memcpy(BLINKER_SUB_TOPIC_GPRS, SUB_TOPIC_STR.c_str(), str_len);
strcpy(BLINKER_SUB_TOPIC_GPRS, SUB_TOPIC_STR.c_str());
BLINKER_LOG_ALL(BLINKER_F("BLINKER_SUB_TOPIC_GPRS: "), BLINKER_SUB_TOPIC_GPRS);
}
// String _userID = root[BLINKER_CMD_DETAIL][BLINKER_CMD_DEVICENAME];
// String _userName = root[BLINKER_CMD_DETAIL][BLINKER_CMD_IOTID];
// String _key = root[BLINKER_CMD_DETAIL][BLINKER_CMD_IOTTOKEN];
// String _productInfo = root[BLINKER_CMD_DETAIL][BLINKER_CMD_PRODUCTKEY];
// String _broker = root[BLINKER_CMD_DETAIL][BLINKER_CMD_BROKER];
// String _uuid = root[BLINKER_CMD_DETAIL][BLINKER_CMD_UUID];
// String _authKey = root[BLINKER_CMD_DETAIL][BLINKER_CMD_KEY];
// if (isMQTTinit)
// {
// free(MQTT_HOST_GPRS);
// free(MQTT_ID_GPRS);
// free(MQTT_NAME_GPRS);
// free(MQTT_KEY_GPRS);
// free(MQTT_PRODUCTINFO_GPRS);
// free(UUID_GPRS);
// free(AUTHKEY_GPRS);
// free(MQTT_DEVICEID_GPRS);
// free(BLINKER_PUB_TOPIC_GPRS);
// free(BLINKER_SUB_TOPIC_GPRS);
// free(mqtt_GPRS);
// isMQTTinit = false;
// }
// BLINKER_LOG_ALL(("===================="));
// // if (_broker == "BLINKER_MQTT_BORKER_ALIYUN") {
// // memcpy(DEVICE_NAME, _userID.c_str(), 12);
// String _deviceName = _userID.substring(12, 36);
// MQTT_DEVICEID_GPRS = (char*)malloc((_deviceName.length()+1)*sizeof(char));
// strcpy(MQTT_DEVICEID_GPRS, _deviceName.c_str());
// MQTT_ID_GPRS = (char*)malloc((_userID.length()+1)*sizeof(char));
// strcpy(MQTT_ID_GPRS, _userID.c_str());
// MQTT_NAME_GPRS = (char*)malloc((_userName.length()+1)*sizeof(char));
// strcpy(MQTT_NAME_GPRS, _userName.c_str());
// MQTT_KEY_GPRS = (char*)malloc((_key.length()+1)*sizeof(char));
// strcpy(MQTT_KEY_GPRS, _key.c_str());
// MQTT_PRODUCTINFO_GPRS = (char*)malloc((_productInfo.length()+1)*sizeof(char));
// strcpy(MQTT_PRODUCTINFO_GPRS, _productInfo.c_str());
// MQTT_HOST_GPRS = (char*)malloc((strlen(BLINKER_MQTT_ALIYUN_HOST)+1)*sizeof(char));
// strcpy(MQTT_HOST_GPRS, BLINKER_MQTT_ALIYUN_HOST);
// AUTHKEY_GPRS = (char*)malloc((_authKey.length()+1)*sizeof(char));
// strcpy(AUTHKEY_GPRS, _authKey.c_str());
// MQTT_PORT_GPRS = BLINKER_MQTT_ALIYUN_PORT;
// BLINKER_LOG_ALL(("===================="));
// // }
// UUID_GPRS = (char*)malloc((_uuid.length()+1)*sizeof(char));
// strcpy(UUID_GPRS, _uuid.c_str());
// char uuid_eeprom[BLINKER_AUUID_SIZE];
// BLINKER_LOG_ALL(("==========AUTH CHECK=========="));
// // if (!isFirst)
// // {
// // char _authCheck;
// // EEPROM.begin(BLINKER_EEP_SIZE);
// // EEPROM.get(BLINKER_EEP_ADDR_AUUID, uuid_eeprom);
// // if (strcmp(uuid_eeprom, _uuid.c_str()) != 0) {
// // // strcpy(UUID_PRO, _uuid.c_str());
// // strcpy(uuid_eeprom, _uuid.c_str());
// // EEPROM.put(BLINKER_EEP_ADDR_AUUID, uuid_eeprom);
// // EEPROM.get(BLINKER_EEP_ADDR_AUUID, uuid_eeprom);
// // BLINKER_LOG_ALL(BLINKER_F("===================="));
// // BLINKER_LOG_ALL(BLINKER_F("uuid_eeprom: "), uuid_eeprom);
// // BLINKER_LOG_ALL(BLINKER_F("_uuid: "), _uuid);
// // isNew = true;
// // }
// // EEPROM.get(BLINKER_EEP_ADDR_AUTH_CHECK, _authCheck);
// // if (_authCheck != BLINKER_AUTH_CHECK_DATA) {
// // EEPROM.put(BLINKER_EEP_ADDR_AUTH_CHECK, BLINKER_AUTH_CHECK_DATA);
// // isAuth = true;
// // }
// // EEPROM.commit();
// // EEPROM.end();
// // isFirst = true;
// // }
// BLINKER_LOG_ALL(BLINKER_F("===================="));
// BLINKER_LOG_ALL(BLINKER_F("DEVICE_NAME: "), imei);
// BLINKER_LOG_ALL(BLINKER_F("MQTT_PRODUCTINFO_GPRS: "), MQTT_PRODUCTINFO_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("MQTT_DEVICEID_GPRS: "), MQTT_DEVICEID_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("MQTT_ID_GPRS: "), MQTT_ID_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("MQTT_NAME_GPRS: "), MQTT_NAME_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("MQTT_KEY_GPRS: "), MQTT_KEY_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("MQTT_BROKER: "), _broker);
// BLINKER_LOG_ALL(BLINKER_F("HOST: "), MQTT_HOST_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("PORT: "), MQTT_PORT_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("UUID_GPRS: "), UUID_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("AUTHKEY_GPRS: "), AUTHKEY_GPRS);
// BLINKER_LOG_ALL(BLINKER_F("===================="));
// // if (_broker == BLINKER_MQTT_BORKER_ALIYUN) {
// String PUB_TOPIC_STR = BLINKER_F("/");
// PUB_TOPIC_STR += MQTT_PRODUCTINFO_GPRS;
// PUB_TOPIC_STR += BLINKER_F("/");
// PUB_TOPIC_STR += MQTT_DEVICEID_GPRS;
// PUB_TOPIC_STR += BLINKER_F("/s");
// BLINKER_PUB_TOPIC_GPRS = (char*)malloc((PUB_TOPIC_STR.length() + 1)*sizeof(char));
// strcpy(BLINKER_PUB_TOPIC_GPRS, PUB_TOPIC_STR.c_str());
// BLINKER_LOG_ALL(BLINKER_F("BLINKER_PUB_TOPIC_GPRS: "), BLINKER_PUB_TOPIC_GPRS);
// String SUB_TOPIC_STR = BLINKER_F("/");
// SUB_TOPIC_STR += MQTT_PRODUCTINFO_GPRS;
// SUB_TOPIC_STR += BLINKER_F("/");
// SUB_TOPIC_STR += MQTT_DEVICEID_GPRS;
// SUB_TOPIC_STR += BLINKER_F("/r");
// BLINKER_SUB_TOPIC_GPRS = (char*)malloc((SUB_TOPIC_STR.length() + 1)*sizeof(char));
// strcpy(BLINKER_SUB_TOPIC_GPRS, SUB_TOPIC_STR.c_str());
// BLINKER_LOG_ALL(BLINKER_F("BLINKER_SUB_TOPIC_GPRS: "), BLINKER_SUB_TOPIC_GPRS);
// // }
// if (_broker == BLINKER_MQTT_BORKER_ALIYUN) {
mqtt_GPRS = new BlinkerMQTTAIR202(*stream, isHWS, MQTT_HOST_GPRS, MQTT_PORT_GPRS,
MQTT_ID_GPRS, MQTT_NAME_GPRS, MQTT_KEY_GPRS, listenFunc);
// }
this->latestTime = millis();
isMQTTinit = true;
mqtt_GPRS->subscribe(BLINKER_SUB_TOPIC_GPRS);
return true;
}
// int BlinkerSerialGPRS::connect()
// {
// stream->println(STRING_format(BLINKER_CMD_MCONFIG_RESQ) +
// "=\"" + MQTT_ID_GPRS +
// "\",\"" + MQTT_NAME_GPRS +
// "\",\"" + MQTT_KEY_GPRS + "\"");
// }
int BlinkerSerialGPRS::isJson(const String & data)
{
BLINKER_LOG_ALL(BLINKER_F("isJson: "), data);
// DynamicJsonBuffer jsonBuffer;
// JsonObject& root = jsonBuffer.parseObject(data);
DynamicJsonDocument jsonBuffer(1024);
DeserializationError error = deserializeJson(jsonBuffer, data);
JsonObject root = jsonBuffer.as<JsonObject>();
// if (!root.success())
if (error)
{
BLINKER_ERR_LOG("Print data is not Json! ", data);
return false;
}
return true;
}
#endif

View File

@@ -0,0 +1,283 @@
#ifndef BLINKER_SERIAL_MQTT_H
#define BLINKER_SERIAL_MQTT_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
// #include "../Adapters/BlinkerSerialMQTT.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerialMQTT;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerialMQTT;
#endif
class BlinkerSerialMQTT : public BlinkerStream
{
public :
BlinkerSerialMQTT()
: stream(NULL), isConnect(false)
{}
int available();
int timedRead();
void begin(Stream& s, bool state);
char * lastRead() { return isFresh ? streamData : NULL; }
void flush();
int aliPrint(const String & s);
int duerPrint(const String & s, bool report = false);
int miPrint(const String & s);
// int print(const String & s, bool needCheck = true);
int print(char * data, bool needCheck = true);
int toServer(char * data) { return true; }
int connect() { isConnect = true; return connected(); }
int connected() { return isConnect; }
void disconnect() { isConnect = false; }
protected :
Stream* stream;
char* streamData;
bool isFresh = false;
bool isConnect;
bool isHWS = false;
uint8_t respTimes = 0;
uint32_t respTime = 0;
int checkPrintSpan();
};
int BlinkerSerialMQTT::available()
{
if (!isHWS)
{
#if defined(__AVR__) || defined(ESP8266)
if (!SSerialMQTT->isListening())
{
SSerialMQTT->listen();
::delay(100);
}
#endif
}
if (stream->available())
{
if (isFresh) free(streamData);
streamData = (char*)malloc(1*sizeof(char));
int16_t dNum = 0;
int c_d = timedRead();
while (dNum < BLINKER_MAX_READ_SIZE &&
c_d >=0 && c_d != '\n')
{
if (c_d != '\r')
{
streamData[dNum] = (char)c_d;
dNum++;
streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
}
c_d = timedRead();
}
dNum++;
streamData = (char*)realloc(streamData, dNum*sizeof(char));
streamData[dNum-1] = '\0';
stream->flush();
BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData);
BLINKER_LOG_FreeHeap_ALL();
if (strlen(streamData) < BLINKER_MAX_READ_SIZE)
{
if (streamData[strlen(streamData) - 1] == '\r')
streamData[strlen(streamData) - 1] = '\0';
isFresh = true;
return true;
}
else
{
free(streamData);
return false;
}
}
else
{
return false;
}
}
void BlinkerSerialMQTT::begin(Stream& s, bool state)
{
stream = &s;
stream->setTimeout(BLINKER_STREAM_TIMEOUT);
isHWS = state;
}
int BlinkerSerialMQTT::timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
void BlinkerSerialMQTT::flush()
{
if (isFresh)
{
free(streamData); isFresh = false;
}
}
int BlinkerSerialMQTT::aliPrint(const String & s)
{
if (!checkPrintSpan()) {
respTime = millis();
return false;
}
String _s = s.substring(0, s.length() - 1);
_s += BLINKER_F(",\"toDeviceAT\":\"AliGenie\"}");
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("AliGenie Response: "), _s);
if(connected()) {
BLINKER_LOG_ALL(BLINKER_F("Success..."));
stream->println(_s);
return true;
}
else {
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
int BlinkerSerialMQTT::duerPrint(const String & s, bool report)
{
if (!checkPrintSpan()) {
respTime = millis();
return false;
}
String _s = s.substring(0, s.length() - 1);
_s += BLINKER_F(",\"toDeviceAT\":\"DuerOS\"}");
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("DuerOS Response: "), _s);
if(connected()) {
BLINKER_LOG_ALL(BLINKER_F("Success..."));
stream->println(_s);
return true;
}
else {
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
int BlinkerSerialMQTT::miPrint(const String & s)
{
if (!checkPrintSpan()) {
respTime = millis();
return false;
}
String _s = s.substring(0, s.length() - 1);
_s += BLINKER_F(",\"toDeviceAT\":\"MIOT\"}");
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("MIOT Response: "), _s);
if(connected()) {
BLINKER_LOG_ALL(BLINKER_F("Success..."));
stream->println(_s);
return true;
}
else {
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
// int BlinkerSerialMQTT::print(const String & s, bool needCheck)
int BlinkerSerialMQTT::print(char * data, bool needCheck)
{
if (needCheck)
{
if (!checkPrintSpan())
{
respTime = millis();
return false;
}
}
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("Response: "), data);
if(connected())
{
BLINKER_LOG_ALL(BLINKER_F("Success..."));
stream->println(data);
return true;
}
else
{
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
int BlinkerSerialMQTT::checkPrintSpan()
{
if (millis() - respTime < BLINKER_PRINT_MSG_LIMIT)
{
if (respTimes > BLINKER_PRINT_MSG_LIMIT)
{
BLINKER_ERR_LOG(BLINKER_F("DEVICE NOT CONNECT OR MSG LIMIT"));
return false;
}
else
{
respTimes++;
return true;
}
}
else
{
respTimes = 0;
return true;
}
}
#endif

View File

@@ -0,0 +1,256 @@
#ifndef BLINKER_SERIAL_NBIOT_H
#define BLINKER_SERIAL_NBIOT_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
class BlinkerSerialNBIoT : public BlinkerStream
{
public :
BlinkerSerialNBIoT()
: stream(NULL), isConnect(false)
{}
int available();
int timedRead();
void begin(Stream& s, bool state);
char * lastRead() { return isFresh ? streamData : NULL; }
void flush();
int aliPrint(const String & s);
int duerPrint(const String & s);
// int print(const String & s, bool needCheck = true);
int print(char * data, bool needCheck = true);
// int toServer(char * data);
int connect() { isConnect = true; return connected(); }
int connected() { return isConnect; }
void disconnect() { isConnect = false; }
protected :
Stream* stream;
char* streamData;
bool isFresh = false;
bool isConnect;
bool isHWS = false;
uint8_t respTimes = 0;
uint16_t dataLen;
uint32_t respTime = 0;
String httpData;
int checkPrintSpan();
};
int BlinkerSerialNBIoT::available()
{
if (!isHWS)
{
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
if (stream->available())
{
if (isFresh) free(streamData);
streamData = (char*)malloc(1*sizeof(char));
int16_t dNum = 0;
int c_d = timedRead();
while (dNum < BLINKER_MAX_READ_SIZE &&
c_d >=0 && c_d != '\n')
{
if (c_d != '\r')
{
streamData[dNum] = (char)c_d;
dNum++;
streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
}
c_d = timedRead();
}
dNum++;
streamData = (char*)realloc(streamData, dNum*sizeof(char));
streamData[dNum-1] = '\0';
stream->flush();
BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData);
BLINKER_LOG_FreeHeap_ALL();
if (strlen(streamData) < BLINKER_MAX_READ_SIZE)
{
if (streamData[strlen(streamData) - 1] == '\r')
streamData[strlen(streamData) - 1] = '\0';
isFresh = true;
return true;
}
else
{
free(streamData);
return false;
}
}
else
{
return false;
}
}
void BlinkerSerialNBIoT::begin(Stream& s, bool state)
{
stream = &s;
stream->setTimeout(BLINKER_STREAM_TIMEOUT);
isHWS = state;
}
int BlinkerSerialNBIoT::timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
void BlinkerSerialNBIoT::flush()
{
if (isFresh)
{
free(streamData); isFresh = false;
}
}
int BlinkerSerialNBIoT::aliPrint(const String & s)
{
if (!checkPrintSpan()) {
respTime = millis();
return false;
}
String _s = s.substring(0, s.length() - 1);
_s += BLINKER_F(",\"toDeviceAT\":\"AliGenie\"}");
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("AliGenie Response: "), _s);
if(connected()) {
BLINKER_LOG_ALL(BLINKER_F("Success..."));
stream->println(_s);
return true;
}
else {
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
int BlinkerSerialNBIoT::duerPrint(const String & s)
{
if (!checkPrintSpan()) {
respTime = millis();
return false;
}
String _s = s.substring(0, s.length() - 1);
_s += BLINKER_F(",\"toDeviceAT\":\"DuerOS\"}");
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("DuerOS Response: "), _s);
if(connected()) {
BLINKER_LOG_ALL(BLINKER_F("Success..."));
stream->println(_s);
return true;
}
else {
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
// int BlinkerSerialNBIoT::print(const String & s, bool needCheck)
int BlinkerSerialNBIoT::print(char * data, bool needCheck)
{
if (needCheck)
{
if (!checkPrintSpan())
{
respTime = millis();
return false;
}
}
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("Response: "), data);
if(connected())
{
BLINKER_LOG_ALL(BLINKER_F("Success..."));
stream->println(data);
return true;
}
else
{
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
int BlinkerSerialNBIoT::checkPrintSpan()
{
if (millis() - respTime < BLINKER_PRINT_MSG_LIMIT)
{
if (respTimes > BLINKER_PRINT_MSG_LIMIT)
{
BLINKER_ERR_LOG(BLINKER_F("DEVICE NOT CONNECT OR MSG LIMIT"));
return false;
}
else
{
respTimes++;
return true;
}
}
else
{
respTimes = 0;
return true;
}
}
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,178 @@
#ifndef BLINKER_SUBSTREAM_H
#define BLINKER_SUBSTREAM_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerSubStream : public BlinkerStream
{
public :
BlinkerSubStream()
: isInit(false)
{}
int available()
{
if (_subAvail && _subRead)
{
if (_subAvail())
{
String data = _subRead();
if (isFresh) free(streamData);
streamData = (char*)malloc((data.length()+1)*sizeof(char));
strcpy(streamData, data.c_str());
isFresh = true;
return true;
}
}
return false;
}
void begin(const char* auth)
{
_authKey = (char*)malloc((strlen(auth)+1)*sizeof(char));
strcpy(_authKey, auth);
BLINKER_LOG_ALL(BLINKER_F("_authKey: "), auth);
if (_subBegin) _subBegin();
}
char * lastRead()
{
if (isFresh) return streamData;
else return "";
}
void flush()
{
if (isFresh)
{
free(streamData);
isFresh = false;
}
}
int print(char * data, bool needCheck = true)
{
if (needCheck)
{
if (!checkPrintSpan())
{
respTime = millis();
return false;
}
}
respTime = millis();
BLINKER_LOG_ALL(BLINKER_F("Response: "), data);
if(connected() && _subPrint)
{
BLINKER_LOG_ALL(BLINKER_F("Success..."));
_subPrint(data);
return true;
}
else
{
BLINKER_LOG_ALL(BLINKER_F("Faile... Disconnected"));
return false;
}
}
int connect()
{
if (_subConnect) return _subConnect();
else return true;
}
int connected()
{
if (_subConnected) return _subConnected();
else return true;
}
void disconnect()
{
if (_subDisconnect) _subDisconnect();
}
int checkPrintSpan()
{
if (millis() - respTime < BLINKER_PRINT_MSG_LIMIT)
{
if (respTimes > BLINKER_PRINT_MSG_LIMIT)
{
BLINKER_ERR_LOG(BLINKER_F("DEVICE NOT CONNECT OR MSG LIMIT"));
return false;
}
else
{
respTimes++;
return true;
}
}
else
{
respTimes = 0;
return true;
}
}
void attachAvailable(blinker_callback_return_int_t func)
{
_subAvail = func;
initStatus |= 0x01 << 0;
if (initStatus == 0x07) isInit = true;
}
void attachRead(blinker_callback_return_string_t func)
{
_subRead = func;
initStatus |= 0x01 << 1;
if (initStatus == 0x07) isInit = true;
}
void attachPrint(blinker_callback_with_string_arg_t func)
{
_subPrint = func;
initStatus |= 0x01 << 2;
if (initStatus == 0x07) isInit = true;
}
void attachBegin(blinker_callback_t func) { _subBegin = func; }
void attachConnect(blinker_callback_return_int_t func) { _subConnect = func; }
void attachConnected(blinker_callback_return_int_t func) { _subConnected = func; }
void attachDisconnect(blinker_callback_t func) { _subDisconnect = func; }
protected :
char* _authKey;
bool isInit;
int8_t initStatus = 0;
char* streamData;
bool isFresh;
bool isConnect;
uint8_t respTimes = 0;
uint32_t respTime = 0;
blinker_callback_return_int_t _subAvail = NULL;
blinker_callback_return_string_t _subRead = NULL;
blinker_callback_with_string_arg_t _subPrint = NULL;
blinker_callback_t _subBegin = NULL;
blinker_callback_return_int_t _subConnect = NULL;
blinker_callback_return_int_t _subConnected = NULL;
blinker_callback_t _subDisconnect = NULL;
};
#endif

View File

@@ -0,0 +1,29 @@
"-----BEGIN CERTIFICATE-----"
"MIIEgDCCA2igAwIBAgIQDKTfhr9lmWbWUT0hjX36oDANBgkqhkiG9w0BAQsFADBy"
"MQswCQYDVQQGEwJDTjElMCMGA1UEChMcVHJ1c3RBc2lhIFRlY2hub2xvZ2llcywg"
"SW5jLjEdMBsGA1UECxMURG9tYWluIFZhbGlkYXRlZCBTU0wxHTAbBgNVBAMTFFRy"
"dXN0QXNpYSBUTFMgUlNBIENBMB4XDTE4MDEwNDAwMDAwMFoXDTE5MDEwNDEyMDAw"
"MFowGDEWMBQGA1UEAxMNaW90ZGV2LmNsei5tZTCCASIwDQYJKoZIhvcNAQEBBQAD"
"ggEPADCCAQoCggEBALbOFn7cJ2I/FKMJqIaEr38n4kCuJCCeNf1bWdWvOizmU2A8"
"QeTAr5e6Q3GKeJRdPnc8xXhqkTm4LOhgdZB8KzuVZARtu23D4vj4sVzxgC/zwJlZ"
"MRMxN+cqI37kXE8gGKW46l2H9vcukylJX+cx/tjWDfS2YuyXdFuS/RjhCxLgXzbS"
"cve1W0oBZnBPRSMV0kgxTWj7hEGZNWKIzK95BSCiMN59b+XEu3NWGRb/VzSAiJEy"
"Hy9DcDPBC9TEg+p5itHtdMhy2gq1OwsPgl9HUT0xmDATSNEV2RB3vwviNfu9/Eif"
"ObhsV078zf30TqdiESqISEB68gJ0Otru67ePoTkCAwEAAaOCAWowggFmMB8GA1Ud"
"IwQYMBaAFH/TmfOgRw4xAFZWIo63zJ7dygGKMB0GA1UdDgQWBBR/KLqnke61779P"
"xc9htonQwLOxPDAYBgNVHREEETAPgg1pb3RkZXYuY2x6Lm1lMA4GA1UdDwEB/wQE"
"AwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwTAYDVR0gBEUwQzA3"
"BglghkgBhv1sAQIwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQu"
"Y29tL0NQUzAIBgZngQwBAgEwgYEGCCsGAQUFBwEBBHUwczAlBggrBgEFBQcwAYYZ"
"aHR0cDovL29jc3AyLmRpZ2ljZXJ0LmNvbTBKBggrBgEFBQcwAoY+aHR0cDovL2Nh"
"Y2VydHMuZGlnaXRhbGNlcnR2YWxpZGF0aW9uLmNvbS9UcnVzdEFzaWFUTFNSU0FD"
"QS5jcnQwCQYDVR0TBAIwADANBgkqhkiG9w0BAQsFAAOCAQEAhtM4eyrWB14ajJpQ"
"ibZ5FbzVuvv2Le0FOSoss7UFCDJUYiz2LiV8yOhL4KTY+oVVkqHaYtcFS1CYZNzj"
"6xWcqYZJ+pgsto3WBEgNEEe0uLSiTW6M10hm0LFW9Det3k8fqwSlljqMha3gkpZ6"
"8WB0f2clXOuC+f1SxAOymnGUsSqbU0eFSgevcOIBKR7Hr3YXBXH3jjED76Q52OMS"
"ucfOM9/HB3jN8o/ioQbkI7xyd/DUQtzK6hSArEoYRl3p5H2P4fr9XqmpoZV3i3gQ"
"oOdVycVtpLunyUoVAB2DcOElfDxxXCvDH3XsgoIU216VY03MCaUZf7kZ2GiNL+UX"
"9UBd0Q=="
"-----END CERTIFICATE-----"
A123456789B123456789C123456789D123456789E123456789F123456789G123456789H123456789I123456789J123456789K123456789L123456789M123456789N123456789O123456789P123456789Q123456789R123456789S123456789T123456789U123456789V123456789W123456789X123456789Y123456789Z123456789

View File

@@ -0,0 +1,386 @@
#ifndef BLINKER_H
#define BLINKER_H
#if defined(BLINKER_BLE)
#if defined(BLINKER_ALIGENIE_LIGHT) || defined(BLINKER_ALIGENIE_OUTLET) || \
defined(BLINKER_ALIGENIE_SWITCH)|| defined(BLINKER_ALIGENIE_SENSOR)
#error This code is intended to run on the BLINKER_MQTT mode! Please check your mode setting.
#endif
#if defined(BLINKER_DUEROS_LIGHT) || defined(BLINKER_DUEROS_OUTLET) || \
defined(BLINKER_DUEROS_SWITCH)|| defined(BLINKER_DUEROS_SENSOR)
#error This code is intended to run on the BLINKER_MQTT mode! Please check your mode setting.
#endif
#if defined(ESP32)
#include "BlinkerESP32BLE.h"
BlinkerESP32BLE Blinker;
#else
#include "BlinkerSerialBLE.h"
BlinkerSerialBLE Blinker;
#endif
#elif defined(BLINKER_WIFI) || defined(BLINKER_MQTT)
// #if defined(BLINKER_APCONFIG_V2)
// #define BLINKER_APCONFIG
// #define BLINKER_WITHOUT_WS_REG
// #endif
#if defined(BLINKER_WIFI)
#undef BLINKER_WIFI
#define BLINKER_MQTT
#endif
#define BLINKER_PROTOCOL_HTTP_SERVER
#include "BlinkerAssistant.h"
#if (defined(ESP8266) || defined(ESP32)) && !defined(BLINKER_MQTT_AT)
#include "BlinkerESPMQTT.h"
BlinkerESPMQTT Blinker;
#else
#define BLINKER_ESP_AT
#define BLINKER_MQTT_AT
#undef BLINKER_MQTT
#include "BlinkerSerialESPMQTT.h"
BlinkerSerialESPMQTT Blinker;
#endif
// #elif defined(BLINKER_WIFI_LOWPOWER)
// #if defined(ESP8266) || defined(ESP32)
// #include "BlinkerESPMQTTLP.h"
// BlinkerESPMQTTLP Blinker;
// #endif
#elif defined(BLINKER_PRO)
#if defined(BLINKER_ALIGENIE_LIGHT) || defined(BLINKER_ALIGENIE_OUTLET) || \
defined(BLINKER_ALIGENIE_SWITCH)|| defined(BLINKER_ALIGENIE_SENSOR)
#error This code is intended to run on the BLINKER_MQTT mode! Please check your mode setting.
#endif
#if defined(BLINKER_DUEROS_LIGHT) || defined(BLINKER_DUEROS_OUTLET) || \
defined(BLINKER_DUEROS_SWITCH)|| defined(BLINKER_DUEROS_SENSOR)
#error This code is intended to run on the BLINKER_MQTT mode! Please check your mode setting.
#endif
#define BLINKER_ALIGENIE
#define BLINKER_DUEROS
#ifndef BLINKER_ESP_SMARTCONFIG
#ifndef BLINKER_APCONFIG
#define BLINKER_ESP_SMARTCONFIG
#endif
#endif
#define BLINKER_PROTOCOL_HTTP_SERVER
#if defined(ESP8266) || defined(ESP32)
#include "BlinkerESPPRO.h"
BlinkerESPPRO Blinker;
#else
#error This code is intended to run on the ESP8266/ESP32 platform! Please check your Tools->Board setting.
#endif
#elif defined(BLINKER_PRO_ESP) || defined(BLINKER_WIFI_AUTO)
#include "BlinkerAssistant.h"
#if defined(BLINKER_APCONFIG_V2)
#define BLINKER_APCONFIG
#define BLINKER_WITHOUT_WS_REG
#endif
#define BLINKER_WITHOUT_WS_REG
#if defined(BLINKER_WIFI_AUTO)
#define BLINKER_PRO_ESP
#endif
// #ifndef BLINKER_ESP_SMARTCONFIG
// #ifndef BLINKER_APCONFIG
// #define BLINKER_ESP_SMARTCONFIG
// #endif
// #endif
#if defined(ESP8266) || defined(ESP32)
#include "BlinkerESPPROESP.h"
BlinkerESPPROESP Blinker;
#else
#error This code is intended to run on the ESP8266/ESP32 platform! Please check your Tools->Board setting.
#endif
#elif defined(BLINKER_AT_MQTT)
#define BLINKER_ESP_AT
#if defined(ESP8266) || defined(ESP32)
#include "BlinkerESPMQTTAT.h"
BlinkerESPMQTTAT Blinker;
#else
#error This code is intended to run on the ESP8266/ESP32 platform! Please check your Tools->Board setting.
#endif
#elif defined(BLINKER_WIFI_GATEWAY)
#include "BlinkerAssistant.h"
#if defined(ESP8266) || defined(ESP32)
#include "BlinkerESPGateway.h"
BlinkerESPGateway Blinker;
#else
#error This code is intended to run on the ESP8266/ESP32 platform! Please check your Tools->Board setting.
#endif
#elif defined(BLINKER_WIFI_SUBDEVICE)
#include "BlinkerAssistant.h"
#if defined(ESP8266) || defined(ESP32)
#include "BlinkerESPSubDevice.h"
BlinkerESPSubDevice Blinker;
#else
#error This code is intended to run on the ESP8266/ESP32 platform! Please check your Tools->Board setting.
#endif
#elif defined(BLINKER_NBIOT_WH)
#ifndef BLINKER_NB73_NBIOT
#define BLINKER_NB73_NBIOT
#endif
#include "BlinkerSerialWHNBIoT.h"
BlinkerSerialWHNBIoT Blinker;
#elif defined(BLINKER_NBIOT_SIM7020)
// #ifndef BLINKER_SIM7020C_NBIOT
// #define BLINKER_SIM7020C_NBIOT
// #endif
#include "BlinkerSerialSIMNBIoT.h"
BlinkerSerialSIMNBIoT Blinker;
#elif defined(BLINKER_NBIOT_SIM7000)
// #ifndef BLINKER_SIM7020C_NBIOT
// #define BLINKER_SIM7020C_NBIOT
// #endif
#ifndef BLINKER_WITHOUT_SSL
#define BLINKER_WITHOUT_SSL
#endif
#include "BlinkerSerialSIM7000NBIoT.h"
BlinkerSerialSIM7000NBIoT Blinker;
#elif defined(BLINKER_PRO_SIM7020)
#include "BlinkerSIMPRO.h"
BlinkerSIMPRO Blinker;
#elif defined(BLINKER_GPRS_AIR202)
// #ifndef BLINKER_AIR202_GPRS
// #define BLINKER_AIR202_GPRS
// #endif
#include "BlinkerSerialLUATGPRS.h"
BlinkerSerialLUATGPRS Blinker;
#elif defined(BLINKER_PRO_AIR202)
#include "BlinkerLUATPRO.h"
BlinkerLUATPRO Blinker;
// #elif defined(BLINKER_WIFI_AUTO)
// #define BLINKER_MQTT_AUTO
// #if !defined(BLINKER_ESP_SMARTCONFIG) && !defined(BLINKER_APCONFIG)
// #define BLINKER_ESP_SMARTCONFIG
// #endif
// #include "BlinkerESPMQTTAUTO.h"
// BlinkerESPMQTTAUTO Blinker;
#elif defined(BLINKER_LOWPOWER_AIR202)
#include "BlinkerLowPowerGPRS.h"
BlinkerLowPowerGPRS Blinker;
#elif defined(BLINKER_QRCODE_NBIOT_SIM7020)
#include "BlinkerQRCodeSIMNBIOT.h"
BlinkerQRCodeSerialSIMNBIoT Blinker;
#elif defined(BLINKER_QRCODE_NBIOT_SIM7000)
#ifndef BLINKER_WITHOUT_SSL
#define BLINKER_WITHOUT_SSL
#endif
#include "BlinkerQRCodeSIM7000NBIOT.h"
BlinkerQRCodeSerialSIM7000NBIoT Blinker;
#elif defined(BLINKER_HTTP)
#include "BlinkerESPHTTP.h"
BlinkerESPHTTP Blinker;
#else
#error Please set a mode BLINKER_BLE/BLINKER_WIFI/BLINKER_MQTT ! Please check your mode setting.
#endif
#include "BlinkerWidgets.h"
// #if defined(BLINKER_MQTT)
#if defined(BLINKER_ESP_TASK)
#if defined(ESP8266)
#error ESP8266 TASK NOT SUPPORT!
// #include "Schedule.h"
// extern "C" {
// #include "ets_sys.h"
// #include "user_interface.h"
// #include "cont.h"
// }
// #define blinker_procTaskPrio 1
// #define blinker_procTaskQueueLen 1
// os_event_t blinker_procTaskQueue[blinker_procTaskQueueLen];
// cont_t* blinker_g_pcont __attribute__((section(".noinit")));
// // uint32_t oldtime = 0;
// // static uint32_t s_micros_at_task_start;
// // static uint32_t oldtime = 0;
// void preloop_update_frequency() __attribute__((weak));
// void preloop_update_frequency() {
// #if defined(F_CPU) && (F_CPU == 160000000L)
// REG_SET_BIT(0x3ff00014, BIT(0));
// ets_update_cpu_frequency(160);
// #endif
// }
// static bool isInit = false;
// static void blinker_loop_wrapper() {
// preloop_update_frequency();
// if (!isInit)
// {
// Blinker.beginMQTT();
// isInit = true;
// }
// else
// {
// Blinker.run();
// }
// // Blinker.run();
// run_scheduled_functions();
// // esp_schedule();
// }
// static void blinker_run(os_event_t *events)
// {
// cont_run(blinker_g_pcont, &blinker_loop_wrapper);
// system_os_post(blinker_procTaskPrio, 0, (os_param_t)blinker_g_pcont);
// }
// void BLINKER_TAST_INIT()
// {
// // ets_task
// // #if defined(BLINKER_MQTT)
// // Blinker.beginMQTT();
// // #endif
// system_os_task(blinker_run, blinker_procTaskPrio, blinker_procTaskQueue, blinker_procTaskQueueLen);
// system_os_post(blinker_procTaskPrio, 0, (os_param_t)blinker_g_pcont);
// }
// #endif
// #endif
// #if defined(ESP32)
#elif defined(ESP32)
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <Arduino.h>
// #if CONFIG_AUTOSTART_ARDUINO
#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif
void blinkerLoopTask(void *pvParameters)
{
for(;;) {
Blinker.run();
vTaskDelay(1);
}
}
extern "C" void BLINKER_TAST_INIT()
{
// initArduino();
// #if defined(BLINKER_MQTT)
// Blinker.beginMQTT();
// #endif
xTaskCreatePinnedToCore(blinkerLoopTask,
"blinkerLoopTask",
8192,
NULL,
3,
NULL,
ARDUINO_RUNNING_CORE);
}
// #endif
#else
#error This code is intended to run on the ESP8266/ESP32 platform! Please check your Tools->Board setting.
#endif
#endif
#ifndef LED_BUILTIN
#if defined(ESP8266) || defined(ESP32)
#define LED_BUILTIN 2
#else
#define LED_BUILTIN 13
#endif
#endif
#endif

View File

@@ -0,0 +1,184 @@
#ifndef BLINKER_AT_MASTER_H
#define BLINKER_AT_MASTER_H
#include "BlinkerDebug.h"
#include "BlinkerConfig.h"
#include "BlinkerUtility.h"
enum blinker_at_m_state_t {
AT_M_NONE,
AT_M_RESP,
AT_M_OK,
AT_M_ERR
};
#define BLINKER_MAX_AT_MASTER_PARAM_NUM 6
#define BLINKER_MAX_AT_MASTER_DATA_SIZE 48
class BlinkerMasterAT
{
public :
BlinkerMasterAT() {}
// : _isReq(false)
// {}
void update(const String & data) {
// _data = data;
// BLINKER_LOG(BLINKER_F("update data: "), data);
serialize(data);
// return _isReq;
}
blinker_at_m_state_t getState() { return _isReq; }
String reqName() { return _reqName; }
uint8_t paramNum() { return _paramNum; }
String getParam(uint8_t num) {
if (num >= _paramNum) return "";
else return _param[num];
}
private :
blinker_at_m_state_t _isReq;
uint8_t _paramNum;
// String _data;
char _reqName[32];
char _param[BLINKER_MAX_AT_MASTER_PARAM_NUM][BLINKER_MAX_AT_MASTER_DATA_SIZE];
void serialize(const String & _data) {
BLINKER_LOG_ALL(BLINKER_F("serialize _data: "), _data);
// _reqName = "";
memset(_reqName, '\0', 32);
_isReq = AT_M_NONE;
int addr_start = _data.indexOf("+");
int addr_end = 0;
// BLINKER_LOG(BLINKER_F("serialize addr_start: "), addr_start);
// BLINKER_LOG(BLINKER_F("serialize addr_end: "), addr_end);
if ((addr_start != -1) && STRING_contains_string(_data, ":")) {
addr_start = 0;
addr_end = _data.indexOf(":");
if (addr_end == -1) {
_isReq = AT_M_NONE;
return;
}
else {
strcpy(_reqName, _data.substring(addr_start + 1, addr_end).c_str());
BLINKER_LOG_ALL(BLINKER_F("serialize _reqName: "), _reqName);
}
// _isReq = true;
// BLINKER_LOG(BLINKER_F("serialize _data: "), _data);
String serData;
uint16_t dataLen = _data.length();
addr_start = 0;
for (_paramNum = 0; _paramNum < BLINKER_MAX_AT_MASTER_PARAM_NUM; _paramNum++) {
addr_start += addr_end;
addr_start += 1;
serData = _data.substring(addr_start, dataLen);
addr_end = serData.indexOf(",");
// BLINKER_LOG(BLINKER_F("serialize serData: "), serData);
// BLINKER_LOG(BLINKER_F("serialize addr_start: "), addr_start);
// BLINKER_LOG(BLINKER_F("serialize addr_end: "), addr_end);
if (addr_end == -1) {
if (addr_start >= dataLen) {
_isReq = AT_M_NONE;
return;
}
addr_end = serData.indexOf(" ");
if (addr_end != -1) {
// _param[_paramNum] = serData.substring(0, addr_end);
if (addr_end != 0)
{
if (serData.substring(0, addr_end).length() < BLINKER_MAX_AT_MASTER_DATA_SIZE)
{
strcpy(_param[_paramNum], serData.substring(0, addr_end).c_str());
}
_isReq = AT_M_RESP;
BLINKER_LOG_ALL(BLINKER_F("_param0["), _paramNum, \
BLINKER_F("]: "), _param[_paramNum],
BLINKER_F(", addr_end: "), addr_end);
_paramNum++;
return;
}
else if (addr_end == 0 && serData.length() > 0)
{
serData = serData.substring(1, serData.length());
if (serData.substring(0, serData.length()).length() < BLINKER_MAX_AT_MASTER_DATA_SIZE)
{
strcpy(_param[_paramNum], serData.substring(0, serData.length()).c_str());
}
_isReq = AT_M_RESP;
BLINKER_LOG_ALL(BLINKER_F("_param0["), _paramNum, \
BLINKER_F("]: "), _param[_paramNum],
BLINKER_F(", addr_end: "), addr_end);
_paramNum++;
return;
}
}
// _param[_paramNum] = serData;
if (serData.length() < BLINKER_MAX_AT_MASTER_DATA_SIZE)
{
strcpy(_param[_paramNum], serData.c_str());
}
// BLINKER_LOG_ALL(BLINKER_F("serialize serData: "), serData);
BLINKER_LOG_ALL(BLINKER_F("_param1["), _paramNum, \
BLINKER_F("]: "), _param[_paramNum], \
" ", serData);
// BLINKER_LOG_ALL(BLINKER_F("serialize serData: "), serData);
_paramNum++;
_isReq = AT_M_RESP;
return;
}
else {
// _param[_paramNum] = serData.substring(0, addr_end);
if (serData.substring(0, addr_end).length() < BLINKER_MAX_AT_MASTER_DATA_SIZE)
{
strcpy(_param[_paramNum], serData.substring(0, addr_end).c_str());
}
}
BLINKER_LOG_ALL(BLINKER_F("_param["), _paramNum, \
BLINKER_F("]: "), _param[_paramNum]);
}
_isReq = AT_M_RESP;
return;
}
else if (_data == BLINKER_CMD_OK) {
_isReq = AT_M_OK;
return;
}
else if (_data == BLINKER_CMD_ERROR) {
_isReq = AT_M_ERR;
return;
}
else {
_isReq = AT_M_NONE;
return;
}
}
};
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,776 @@
#ifndef BLINKER_API_BASE_H
#define BLINKER_API_BASE_H
#include "BlinkerDebug.h"
#include "BlinkerConfig.h"
#include "BlinkerUtility.h"
template <class T>
int8_t checkNum(char * name, T * c, uint8_t count)
{
BLINKER_LOG_ALL(BLINKER_F("checkNum count: "), count);
for (uint8_t cNum = 0; cNum < count; cNum++)
{
// BLINKER_LOG_ALL(BLINKER_F("checkName: "), name, BLINKER_F(", name: "), c[cNum]->getName());
// BLINKER_LOG_ALL(BLINKER_F("is strcmp: "), strcmp(name, c[cNum]->getName()) == 0);
// BLINKER_LOG_ALL(BLINKER_F("is equal: "), name == c[cNum]->getName());
if (c[cNum]->checkName(name)) return cNum;
}
return BLINKER_OBJECT_NOT_AVAIL;
}
class BlinkerWidgets_num
{
public :
BlinkerWidgets_num(char * _name)
{
wName = (char*)malloc((strlen(_name)+1)*sizeof(char));
strcpy(wName, _name);
autoUpdate = true;
}
char * getName() { return wName; }
bool checkName(char * name) {
return strcmp(name, wName) == 0;
}
void setState(bool state) { autoUpdate = state; }
bool state() { return autoUpdate; }
private :
char *wName;
bool autoUpdate = false;
};
class BlinkerWidgets_string
{
public :
BlinkerWidgets_string(char * _name, blinker_callback_with_string_arg_t _func = NULL)
{
wName = (char*)malloc((strlen(_name)+1)*sizeof(char));
strcpy(wName, _name);
wfunc = _func;
}
char * getName() { return wName; }
void setFunc(blinker_callback_with_string_arg_t _func) { wfunc = _func; }
blinker_callback_with_string_arg_t getFunc() { return wfunc; }
bool checkName(char * name) {
return strcmp(name, wName) == 0;
}
private :
char *wName;
blinker_callback_with_string_arg_t wfunc;
};
class BlinkerWidgets_int32
{
public :
BlinkerWidgets_int32(char * _name, blinker_callback_with_int32_arg_t _func = NULL)
{
wName = (char*)malloc((strlen(_name)+1)*sizeof(char));
strcpy(wName, _name);
wfunc = _func;
}
char * getName() { return wName; }
void setFunc(blinker_callback_with_int32_arg_t _func) { wfunc = _func; }
blinker_callback_with_int32_arg_t getFunc() { return wfunc; }
bool checkName(char * name) {
return strcmp(name, wName) == 0;
}
private :
char *wName;
blinker_callback_with_int32_arg_t wfunc;
};
class BlinkerWidgets_rgb
{
public :
BlinkerWidgets_rgb(char * _name, blinker_callback_with_rgb_arg_t _func = NULL)
{
wName = (char*)malloc((strlen(_name)+1)*sizeof(char));
strcpy(wName, _name);
wfunc = _func;
}
char * getName() { return wName; }
void setFunc(blinker_callback_with_rgb_arg_t _func) { wfunc = _func; }
blinker_callback_with_rgb_arg_t getFunc() { return wfunc; }
bool checkName(char * name) {
return strcmp(name, wName) == 0;
}
private :
char *wName;
blinker_callback_with_rgb_arg_t wfunc;
};
class BlinkerWidgets_joy
{
public :
BlinkerWidgets_joy(char * _name, blinker_callback_with_joy_arg_t _func = NULL)
{
wName = (char*)malloc((strlen(_name)+1)*sizeof(char));
strcpy(wName, _name);
wfunc = _func;
}
char * getName() { return wName; }
void setFunc(blinker_callback_with_joy_arg_t _func) { wfunc = _func; }
blinker_callback_with_joy_arg_t getFunc() { return wfunc; }
bool checkName(char * name) {
return strcmp(name, wName) == 0;
}
private :
char *wName;
blinker_callback_with_joy_arg_t wfunc;
};
class BlinkerWidgets_table
{
public :
BlinkerWidgets_table(char * _name,
blinker_callback_with_table_arg_t _func = NULL,
blinker_callback_t _func2 = NULL)
{
wName = (char*)malloc((strlen(_name)+1)*sizeof(char));
strcpy(wName, _name);
wfunc = _func;
wfunc2 = _func2;
}
char * getName() { return wName; }
void setFunc(blinker_callback_with_table_arg_t _func, blinker_callback_t _func2)
{ wfunc = _func; wfunc2 = _func2; }
blinker_callback_with_table_arg_t getFunc() { return wfunc; }
blinker_callback_t getFunc2() { return wfunc2; }
bool checkName(char * name) {
return strcmp(name, wName) == 0;
}
private :
char *wName;
blinker_callback_with_table_arg_t wfunc;
blinker_callback_t wfunc2;
};
#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || \
defined(BLINKER_AT_MQTT) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_NBIOT_SIM7020) || defined(BLINKER_GPRS_AIR202) || \
defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202) || \
defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP) || \
defined(BLINKER_LOWPOWER_AIR202) || defined(BLINKER_WIFI_SUBDEVICE) || \
defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_NBIOT_SIM7000) || \
defined(BLINKER_QRCODE_NBIOT_SIM7000) || defined(BLINKE_HTTP)
class BlinkerBridge_key
{
public :
BlinkerBridge_key(char * _key, blinker_callback_with_string_arg_t _func = NULL)
{
bKey = (char*)malloc((strlen(_key)+1)*sizeof(char));
strcpy(bKey, _key);
wfunc = _func;
}
char * getKey() { return bKey; }
void setFunc(blinker_callback_with_string_arg_t _func) { wfunc = _func; }
blinker_callback_with_string_arg_t getFunc() { return wfunc; }
bool checkName(char * _key) { return strcmp(_key, bKey) == 0; }
char * getName()
{
if (_register) return bName;
else return "false";
}
void name(const String & name)
{
_register = true;
bName = (char*)malloc((name.length()+1)*sizeof(char));
strcpy(bName, name.c_str());
}
private :
char *bKey;
char *bName;
bool _register = false;
blinker_callback_with_string_arg_t wfunc;
// public :
// BlinkerBridge() {}
// void name(char name[])
// {
// _bName = (char*)malloc((strlen(name)+1)*sizeof(char));
// strcpy(_bName, name);
// }
// char * getName() { return _bName; }
// void freshBridge(const String & name)
// {
// bridgeName = (char*)malloc((name.length()+1)*sizeof(char));
// strcpy(bridgeName, name.c_str());
// }
// char * getBridge() { return bridgeName; }
// bool checkName(char name[]) { return strcmp(_bName, name) == 0; }
// private :
// char *_bName;
// char *bridgeName;
};
union BlinkerDataType
{
int32_t int_data;
uint32_t uint_data;
float float_data;
};
class BlinkerTimeSlotData
{
public :
BlinkerTimeSlotData()
: time_data(0)
, key_count(0)
{}
int8_t checkNum(char name[], uint8_t count)
{
BLINKER_LOG_ALL(BLINKER_F("checkNum count: "), count);
for (uint8_t cNum = 0; cNum < count; cNum++)
{
// BLINKER_LOG_ALL(BLINKER_F("checkName: "), name, BLINKER_F(", name: "), c[cNum]->getName());
// BLINKER_LOG_ALL(BLINKER_F("is strcmp: "), strcmp(name, c[cNum]->getName()) == 0);
// BLINKER_LOG_ALL(BLINKER_F("is equal: "), name == c[cNum]->getName());
if (strcmp(name, keys[cNum]) == 0) return cNum;
}
return BLINKER_OBJECT_NOT_AVAIL;
}
void saveData(char key[], int32_t data, time_t now_time)
{
if (key_count == 0) time_data = now_time;
int8_t num = checkNum(key, key_count);
// int32_t format_data;
if (num == BLINKER_OBJECT_NOT_AVAIL)
{
if (key_count < 6)
{
strcpy(keys[key_count], key);
data_type[key_count] = BLINKER_INT_DATA;
datas[key_count].int_data = data;
// for(uint8_t i=0; i<4; i++){
// *(&datas[key_count][0] + i) = *((uint8_t *)&data + i);
// }
// datas[key_count][4] = BLINKER_INT_DATA;
// for(uint8_t i=0; i<4; i++){
// *((uint8_t *)&format_data + i) = *(&datas[key_count][0] + i);
// }
BLINKER_LOG_ALL(BLINKER_F("new key: "), key, \
BLINKER_F(" key_count: "), key_count, \
BLINKER_F(" data: "), datas[key_count].int_data, \
BLINKER_F(" data_type: "), data_type[key_count]);
key_count++;
}
}
}
void saveData(char key[], uint32_t data, time_t now_time)
{
if (key_count == 0) time_data = now_time;
int8_t num = checkNum(key, key_count);
uint32_t format_data;
if (num == BLINKER_OBJECT_NOT_AVAIL)
{
if (key_count < 6)
{
strcpy(keys[key_count], key);
data_type[key_count] = BLINKER_UINT_DATA;
datas[key_count].uint_data = data;
// for(uint8_t i=0; i<4; i++){
// *(&datas[key_count][0] + i) = *((uint8_t *)&data + i);
// }
// datas[key_count][4] = BLINKER_INT_DATA;
// for(uint8_t i=0; i<4; i++){
// *((uint8_t *)&format_data + i) = *(&datas[key_count][0] + i);
// }
BLINKER_LOG_ALL(BLINKER_F("new key: "), key, \
BLINKER_F(" key_count: "), key_count, \
BLINKER_F(" data: "), datas[key_count].uint_data, \
BLINKER_F(" data_type: "), data_type[key_count]);
key_count++;
}
}
}
void saveData(char key[], float data, time_t now_time)
{
if (key_count == 0) time_data = now_time;
int8_t num = checkNum(key, key_count);
float format_data;
if (num == BLINKER_OBJECT_NOT_AVAIL)
{
if (key_count < 6)
{
strcpy(keys[key_count], key);
data_type[key_count] = BLINKER_FLOAT_DATA;
datas[key_count].float_data = data;
// for(uint8_t i=0; i<4; i++){
// *(&datas[key_count][0] + i) = *((uint8_t *)&data + i);
// }
// datas[key_count][4] = BLINKER_INT_DATA;
// for(uint8_t i=0; i<4; i++){
// *((uint8_t *)&format_data + i) = *(&datas[key_count][0] + i);
// }
BLINKER_LOG_ALL(BLINKER_F("new key: "), key, \
BLINKER_F(" key_count: "), key_count, \
BLINKER_F(" data: "), datas[key_count].float_data, \
BLINKER_F(" data_type: "), data_type[key_count]);
key_count++;
}
}
}
String getData()
{
int32_t int_data;
uint32_t uint_data;
float float_data;
BLINKER_LOG_FreeHeap_ALL();
String _data = BLINKER_F("{\"date\":");
_data += STRING_format(time_data);
for(uint8_t num = 0; num < key_count; num++)
{
_data += BLINKER_F(",\"");
_data += keys[num];
_data += BLINKER_F("\":");
// _data += keys[num];
BLINKER_LOG_ALL(BLINKER_F("datas["), num,
BLINKER_F("].data_type: "),
data_type[num]);
if (data_type[num] == BLINKER_INT_DATA)
{
// for(uint8_t i=0; i<4; i++){
// *((uint8_t *)&int_data + i) = *(&datas[key_count][0] + i);
// }
_data += STRING_format(datas[num].int_data);
BLINKER_LOG_ALL(BLINKER_F("datas[num].int_data: "), datas[num].int_data);
}
else if (data_type[num] == BLINKER_UINT_DATA)
{
// for(uint8_t i=0; i<4; i++){
// *((uint8_t *)&uint_data + i) = *(&datas[key_count][0] + i);
// }
_data += STRING_format(datas[num].uint_data);
BLINKER_LOG_ALL(BLINKER_F("datas[num].int_data: "), datas[num].uint_data);
}
else if (data_type[num] == BLINKER_FLOAT_DATA)
{
// for(uint8_t i=0; i<4; i++){
// *((uint8_t *)&float_data + i) = *(&datas[key_count][0] + i);
// }
_data += STRING_format(datas[num].float_data);
BLINKER_LOG_ALL(BLINKER_F("datas[num].int_data: "), datas[num].float_data);
}
}
_data += STRING_format("}");
return _data;
}
void flush()
{
key_count = 0;
time_data = 0;
}
// for(uint8_t i=0;i<4;i++){
// *(&test[0] + i) = *((uint8_t *)&testt + i); // 逐个字节单元进行复制
// Serial.println(*((uint8_t *)&testt + i));
// }
private :
time_t time_data;
uint8_t key_count;
uint8_t data_type[6];
char keys[6][15];
BlinkerDataType datas[6];
};
class BlinkerData
{
public :
BlinkerData()
// : _dname(NULL)
{
// data = (char*)malloc((256)*sizeof(char));
// memcpy(data,"\0",128);
for(uint8_t num = 0; num < BLINKER_MAX_DATA_COUNT; num++)
{
time_data[num] = 0;
memset(data[num], '\0', 10);
}
}
void name(const String & name) { _dname = name; }
String getName() { return _dname; }
bool saveData(const String & _data, time_t now_time, uint32_t _limit) {
if (dataCount > 0)
{
if (now_time - latest_time < _limit) return false;
}
latest_time = now_time;
if (dataCount >= BLINKER_MAX_DATA_COUNT)
{
dataCount = BLINKER_MAX_DATA_COUNT;
for (uint8_t num = 0; num < dataCount - 1; num++) {
time_data[num] = time_data[num + 1];
strcpy(data[num], data[num+1]);
}
time_data[dataCount - 1] = now_time;
strcpy(data[dataCount - 1], _data.c_str());
}
else
{
time_data[dataCount] = now_time;
strcpy(data[dataCount], _data.c_str());
dataCount++;
}
// String _data_;
// if (strlen(data)) _data_ = STRING_format(data);
// if (dataCount == 6) {
// _data_ += BLINKER_F(",[");
// _data_ += STRING_format(now_time);
// _data_ += BLINKER_F(",");
// _data_ += _data;
// _data_ += BLINKER_F("]");
// _data_ = "{\"data\":[" + STRING_format(_data_);
// _data_ += BLINKER_F("]}");
// DynamicJsonBuffer jsonDataBuffer;
// JsonObject& dataArray = jsonDataBuffer.parseObject(_data_);
// for (uint8_t num = 0; num < dataCount; num++) {
// dataArray["data"][num][0] = dataArray["data"][num+1][0];
// dataArray["data"][num][1] = dataArray["data"][num+1][1];
// }
// _data_ = BLINKER_F("");
// for (uint8_t num = 0; num < dataCount; num++)
// {
// String data_get = dataArray["data"][num];
// _data_ += data_get;
// if (num != dataCount - 1) _data_ += BLINKER_F(",");
// }
// if (_data_.length() < 128)
// {
// strcpy(data, _data_.c_str());
// }
// }
// else
// {
// if (_data_.length())
// {
// _data_ += BLINKER_F(",");
// }
// _data_ += BLINKER_F("[");
// _data_ += STRING_format(now_time);
// _data_ += BLINKER_F(",");
// _data_ += _data;
// _data_ += BLINKER_F("]");
// if (_data_.length() < 128) {
// strcpy(data, _data_.c_str());
// dataCount++;
// }
// }
BLINKER_LOG_ALL(BLINKER_F("saveData: "), _data);
BLINKER_LOG_ALL(BLINKER_F("saveData dataCount: "), dataCount);
return true;
}
String getData() {
// BLINKER_LOG_ALL(BLINKER_F("getData data: "), data);
String _data_ = BLINKER_F("[");//{\"data\":
// _data_ += STRING_format(data);
for (uint8_t num = 0; num < dataCount; num++) {
_data_ += "[";
_data_ += String(time_data[num]);
_data_ += ",";
_data_ += data[num];
_data_ += "]";
if (num + 1 < dataCount)
{
_data_ += ",";
}
// time_data[num] = latest_time;
// memcpy(data[num], "\0", 10);
}
_data_ += BLINKER_F("]");//}
// dataCount = 0;
// DynamicJsonBuffer jsonDataBuffer;
// JsonObject& dataArray = jsonDataBuffer.parseObject(_data_);
// uint32_t now_millis = millis();
// for (uint8_t num = dataCount; num > 0; num--) {
// uint8_t data_time = dataArray["data"][num][0];
// uint32_t real_time = now_time - (dataCount - data_time - 1)*60;
// dataArray["data"][num-1][0] = real_time;
// BLINKER_LOG_ALL(BLINKER_F("data_time: "), data_time,
// BLINKER_F(" now_time: "), now_time,
// BLINKER_F(" real_time: "), real_time);
// }
// String _data_decode = dataArray["data"];
BLINKER_LOG_ALL(BLINKER_F("getData _data_: "), _data_);
return _data_;
}
bool checkName(const String & name) { return ((_dname == name) ? true : false); }
void flush()
{
for (uint8_t num = 0; num < dataCount; num++) {
time_data[num] = latest_time;
memset(data[num], '\0', 10);
}
dataCount = 0;
}
private :
uint8_t dataCount = 0;
time_t latest_time = 0;
String _dname;
// char * data;
time_t time_data[BLINKER_MAX_DATA_COUNT];
char data[BLINKER_MAX_DATA_COUNT][10];
};
class BlinkerRTData
{
public :
BlinkerRTData()
// : _dname(NULL)
{
// data = (char*)malloc((256)*sizeof(char));
// memcpy(data,"\0",128);
for(uint8_t num = 0; num < 15; num++)
{
// time_data[num] = 0;
// data[num] = 0;
}
}
void name(const char* _name) { _dname = _name; }
const char* getName() { return _dname; }
// bool saveData(int _data, time_t now_time) {
// // if (dataCount > 0)
// // {
// // if (now_time - latest_time < _limit) return false;
// // }
// latest_time = now_time;
// if (dataCount >= BLINKER_MAX_RTDATA_DATA_SIZE)
// {
// full = true;
// dataCount = dataCount % BLINKER_MAX_RTDATA_DATA_SIZE;
// time_data[dataCount] = now_time;
// data[dataCount] = _data;
// dataCount++;
// // for (uint8_t num = 0; num < dataCount - 1; num++) {
// // time_data[num] = time_data[num + 1];
// // data[num] = data[num+1];
// // }
// // time_data[dataCount - 1] = now_time;
// // data[dataCount - 1] = _data;
// }
// else
// {
// time_data[dataCount] = now_time;
// data[dataCount] = _data;
// dataCount++;
// }
// BLINKER_LOG_ALL(BLINKER_F("saveData: "), _data);
// BLINKER_LOG_ALL(BLINKER_F("saveData dataCount: "), dataCount);
// return true;
// }
bool available() {
// if (full)
// {
// if (printCount >= BLINKER_MAX_RTDATA_DATA_SIZE)
// {
// return false;
// }
// } else {
// if (printCount >= dataCount)
// {
// return false;
// }
// }
if (is_fresh) return true;
return false;
}
// String getData() {
// // full = false;
// String _data_ = BLINKER_F("");
// // if (full)
// // {
// // if (printCount >= BLINKER_MAX_RTDATA_DATA_SIZE)
// // {
// // _data_ = "null";
// // }
// // } else {
// // if (printCount >= dataCount)
// // {
// // _data_ = "null";
// // }
// // }
// if (available())
// {
// _data_ += "\"";
// _data_ += _dname;
// _data_ += "\":{\"date\":";
// _data_ += String(time_data[printCount]);
// _data_ += ",\"value\":";
// _data_ += data[printCount];
// _data_ += "}";
// printCount++;
// }
// else
// {
// _data_ = "null";
// }
// // for (uint8_t num = 0; num < dataCount; num++) {
// // _data_ += "{\"date\":";
// // _data_ += String(time_data[printCount]);
// // _data_ += ",\"value\":";
// // _data_ += data[printCount];
// // _data_ += "}";
// // BLINKER_LOG_ALL(BLINKER_F("getData data: "), data);
// // printCount++;
// // if (num + 1 < dataCount)
// // {
// // _data_ += ",";
// // }
// // }
// // _data_ += BLINKER_F("]");
// BLINKER_LOG_ALL(BLINKER_F("getData _data_: "), _data_);
// return _data_;
// }
bool checkName(const char* name) { return strncmp(name, _dname, strlen(name)) == 0; }
// void flush()
// {
// for (uint8_t num = 0; num < dataCount; num++) {
// time_data[num] = latest_time;
// data[num] = 0;
// }
// dataCount = 0;
// printCount = 0;
// full = false;
// }
void state(bool fresh_state)
{
is_fresh = fresh_state;
}
private :
bool is_fresh = false;
// uint8_t dataCount = 0;
// uint8_t printCount = 0;
// time_t latest_time = 0;
// char * data;
// time_t time_data[15];
// int data[15];
// bool full = false;
const char* _dname;
};
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,65 @@
#ifndef BLINKER_AUTO_SUBDEVICE_H
#define BLINKER_AUTO_SUBDEVICE_H
#if defined(BLINKER_WIFI_SUBDEVICE)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <EEPROM.h>
// #include "Blinker/BlinkerAuto.h"
#include "Blinker/BlinkerConfig.h"
#include "Blinker/BlinkerDebug.h"
#include "Blinker/BlinkerUtility.h"
#ifndef ARDUINOJSON_VERSION_MAJOR
#include "modules/ArduinoJson/ArduinoJson.h"
#endif
class BlinkerAutoSubdevice
{
private :
uint8_t a_num;
// {
// "auto":{
// "ena":1,//_autoState
// "id":123456,//_autoId
// "logic":"numberic",//_logicType
// "data":
// [
// {
// "key":"humi",
// "value":40,
// "type":"<",//_targetState|_compareType
// "dur":10
// }
// ],
// "range":[540, 1260],
// }
// }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// | | | | | | _time1 0-1440min 11 | _time2 0-1440min 11
// | | | | | _duration 0-60min 6
// | | | | _targetState|_compareType on/off|less/equal/greater 2
// | | | _targetState|_compareType on/off|less/equal/greater
// |
// | logic_type state/numberic 2
// autoData
// | _linkNum
// - - - - - - - -
// | | |_logicType state/numberic/and/or 2
// | | _autoState true/false 1
// | _haveAuto
// |
// typestate
};
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,35 @@
// #ifndef BlinkerDataTicker_H
// #define BlinkerDataTicker_H
// #if defined(ESP8266) || defined(ESP32)
// #include "Blinker/BlinkerConfig.h"
// #include "Blinker/BlinkerDebug.h"
// #include "Blinker/BlinkerTimer.h"
// #include <Ticker.h>
// Ticker dataTicker;
// bool _dataTrigged = false;
// uint32_t _data_time = 60;
// void dataInit(uint8_t mins, uint8_t seconds, uint32_t tickers = 60)
// {
// _data_time = tickers;
// if (_data_time == 60)
// {
// uint32_t tTime = _data_time - seconds;
// dataTicker.
// }
// }
// void dataHandle()
// {
// _dataTrigged = true;
// }
// #endif
// #endif

View File

@@ -0,0 +1,129 @@
#include "Blinker/BlinkerDebug.h"
#include <stddef.h>
#ifdef ESP8266
extern "C" {
#include <ets_sys.h>
#include <os_type.h>
#include <mem.h>
}
#include <Esp.h>
#else
#include <inttypes.h>
#endif
#if defined(ARDUINO)
#if (defined(__AVR__))
#include <avr/pgmspace.h>
#elif defined(ESP8266) || defined(ESP32)
#include <pgmspace.h>
#endif
#endif
uint32_t BLINKER_FreeHeap()
{
#if defined(ARDUINO)
#if defined(ESP8266) || defined(ESP32)
return ESP.getFreeHeap();
// #elif defined(ARDUINO) && defined(ESP32)
// return ESP.getFreeHeap();
#elif defined(__AVR__)
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
#else
return 0;
#endif
#else
return 0;
#endif
}
BlinkerDebug BLINKER_DEBUG;
void BlinkerDebug::stream(Stream& s, blinker_debug_level_t level)
{
isInit = true;
debugger = &s;
debug_level = level;
}
void BlinkerDebug::time()
{
if (debug_level != _debug_none)
{
debugger->print(BLINKER_DEBUG_F("["));
debugger->print(millis());
debugger->print(BLINKER_DEBUG_F("] "));
}
return;
}
// uint32_t BlinkerDebug::BLINKER_FreeHeap()
// {
// #if defined(ARDUINO)
// #if defined(ESP8266) || defined(ESP32)
// return ESP.getFreeHeap();
// // #elif defined(ARDUINO) && defined(ESP32)
// // return ESP.getFreeHeap();
// #elif defined(__AVR__)
// extern int __heap_start, *__brkval;
// int v;
// return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
// #else
// return 0;
// #endif
// #else
// return 0;
// #endif
// }
void BlinkerDebug::freeheap()
{
if (debug_level != _debug_none)
{
// time();
// debugger->print(BLINKER_DEBUG_F("Freeheap: "));
debugger->println(BLINKER_FreeHeap());
}
return;
}
void BLINKER_LOG_TIME()
{
if (BLINKER_DEBUG.isDebug())
{
BLINKER_DEBUG.print(BLINKER_DEBUG_F("["));
BLINKER_DEBUG.print(millis());
BLINKER_DEBUG.print(BLINKER_DEBUG_F("] "));
}
return;
}
void BLINKER_LOG_FreeHeap()
{
if (BLINKER_DEBUG.isDebug())
{
BLINKER_LOG_TIME();
BLINKER_DEBUG.print(BLINKER_DEBUG_F("Freeheap: "));
BLINKER_DEBUG.freeheap();
}
return;
}
void BLINKER_LOG_FreeHeap_ALL()
{
if (BLINKER_DEBUG.isDebugAll())
{
BLINKER_LOG_TIME();
BLINKER_DEBUG.print(BLINKER_DEBUG_F("Freeheap: "));
BLINKER_DEBUG.freeheap();
}
return;
}
void BLINKER_LOG_T()
{
if (BLINKER_DEBUG.isDebug()) BLINKER_DEBUG.println();
return;
}

View File

@@ -0,0 +1,118 @@
#ifndef BLINKER_DEBUG_H
#define BLINKER_DEBUG_H
#if defined(ARDUINO)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#endif
#define BLINKER_DEBUG_F(s) F(s)
#define BLINKER_F(s) F(s)
uint32_t BLINKER_FreeHeap();
class BlinkerDebug
{
enum blinker_debug_level_t
{
_debug_none,
_debug_default,
_debug_all
};
public :
BlinkerDebug()
: isInit(false)
, debug_level(_debug_none)
{}
// BlinkerDebug(Stream& s, blinker_debug_level_t level = _debug_default)
// : debugger(&s)
// , debug_level(level)
// {}
void stream(Stream& s, blinker_debug_level_t level = _debug_default);
void debugAll() { debug_level = _debug_all; }
void time();
void freeheap();
bool isDebug() { return isInit ? debug_level != _debug_none : false; }
bool isDebugAll() { return isInit ? debug_level == _debug_all : false;}
template <typename T>
void print(T arg) { debugger->print(arg); }
template <typename T>
void println(T arg) { debugger->println(arg); }
void println() { debugger->println(); }
private :
bool isInit;
Stream* debugger;
blinker_debug_level_t debug_level;
// uint32_t BLINKER_FreeHeap();
};
extern BlinkerDebug BLINKER_DEBUG;
void BLINKER_LOG_TIME();
void BLINKER_LOG_FreeHeap();
void BLINKER_LOG_FreeHeap_ALL();
extern void BLINKER_LOG_T();
/* BLINKER_LOG_T递归模板 */
template <typename T,typename... Ts>
void BLINKER_LOG_T(T arg,Ts... args)
{
if (BLINKER_DEBUG.isDebug())
{
BLINKER_DEBUG.print(arg);
BLINKER_LOG_T(args...);
}
return;
}
/* BLINKER_LOG可变参数模板 */
template <typename... Ts>
void BLINKER_LOG(Ts... args)
{
BLINKER_LOG_TIME();
BLINKER_LOG_T(args...);
return;
}
/* BLINKER_ERR_LOG可变参数模板 */
template <typename... Ts>
void BLINKER_ERR_LOG(Ts... args)
{
if (BLINKER_DEBUG.isDebug())
{
BLINKER_LOG_TIME();
BLINKER_DEBUG.print(BLINKER_DEBUG_F("ERROR: "));
BLINKER_LOG_T(args...);
}
return;
}
template <typename... Ts>
void BLINKER_LOG_ALL(Ts... args)
{
if (BLINKER_DEBUG.isDebugAll())
{
BLINKER_LOG_TIME();
BLINKER_LOG_T(args...);
}
return;
}
template <typename... Ts>
void BLINKER_ERR_LOG_ALL(Ts... args)
{
if (BLINKER_DEBUG.isDebugAll()) { BLINKER_ERR_LOG(args...); }
return;
}
#endif

View File

@@ -0,0 +1,451 @@
#ifndef BLINKER_MQTT_AT_BASE_H
#define BLINKER_MQTT_AT_BASE_H
#if defined(ESP8266) || defined(ESP32)
#include "BlinkerDebug.h"
#include "BlinkerConfig.h"
#include "BlinkerUtility.h"
// extern uint32_t serialSet;
// #if defined(ESP8266)
// extern SerialConfig ss_cfg;
// #elif defined(ESP32)
// extern uint32_t ss_cfg;
// #endif
// #if defined(ESP8266)
// extern SerialConfig serConfig();
// #elif defined(ESP32)
// extern uint32_t serConfig();
// #endif
uint8_t parseMode(uint8_t _mode, uint8_t _pullState);
enum blinker_at_state_t {
AT_NONE,
AT_TEST,
AT_QUERY,
AT_SETTING,
AT_ACTION
};
enum blinker_at_status_t {
BL_BEGIN,
BL_INITED
// ,
// BL_SUC,
// MQTT_WIFI_PSWD
};
enum blinker_at_serial_t {
SER_BAUD,
SER_DBIT,
SER_SBIT,
SER_PRIT
};
enum blinker_at_mqtt_t {
MQTT_CONFIG_MODE,
MQTT_AUTH_KEY,
MQTT_WIFI_SSID,
MQTT_WIFI_PSWD
};
enum blinker_at_aligenie_t {
ALI_NONE,
ALI_LIGHT,
ALI_OUTLET,
ALI_SENSOR
};
enum blinker_at_dueros_t {
DUER_NONE,
DUER_LIGHT,
DUER_OUTLET,
DUER_SENSOR
};
uint32_t serialSet = BLINKER_SERIAL_DEFAULT;
#if defined(ESP8266)
SerialConfig ss_cfg;
#elif defined(ESP32)
uint32_t ss_cfg;
#endif
#if defined(ESP8266)
SerialConfig serConfig()
#elif defined(ESP32)
uint32_t serConfig()
#endif
{
uint8_t _bitSet = serialSet & 0xFF;
switch (_bitSet)
{
case BLINKER_SERIAL_5N1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_5N1"));
return SERIAL_5N1;
case BLINKER_SERIAL_6N1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_6N1"));
return SERIAL_6N1;
case BLINKER_SERIAL_7N1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_7N1"));
return SERIAL_7N1;
case BLINKER_SERIAL_8N1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_8N1"));
return SERIAL_8N1;
case BLINKER_SERIAL_5N2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_5N2"));
return SERIAL_5N2;
case BLINKER_SERIAL_6N2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_6N2"));
return SERIAL_6N2;
case BLINKER_SERIAL_7N2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_7N2"));
return SERIAL_7N2;
case BLINKER_SERIAL_8N2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_8N2"));
return SERIAL_8N2;
case BLINKER_SERIAL_5E1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_5E1"));
return SERIAL_5E1;
case BLINKER_SERIAL_6E1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_6E1"));
return SERIAL_6E1;
case BLINKER_SERIAL_7E1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_7E1"));
return SERIAL_7E1;
case BLINKER_SERIAL_8E1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_8E1"));
return SERIAL_8E1;
case BLINKER_SERIAL_5E2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_5E2"));
return SERIAL_5E2;
case BLINKER_SERIAL_6E2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_6E2"));
return SERIAL_6E2;
case BLINKER_SERIAL_7E2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_7E2"));
return SERIAL_7E2;
case BLINKER_SERIAL_8E2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_8E2"));
return SERIAL_8E2;
case BLINKER_SERIAL_5O1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_5O1"));
return SERIAL_5O1;
case BLINKER_SERIAL_6O1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_6O1"));
return SERIAL_6O1;
case BLINKER_SERIAL_7O1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_7O1"));
return SERIAL_7O1;
case BLINKER_SERIAL_8O1 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_8O1"));
return SERIAL_8O1;
case BLINKER_SERIAL_5O2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_5O2"));
return SERIAL_5O2;
case BLINKER_SERIAL_6O2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_6O2"));
return SERIAL_6O2;
case BLINKER_SERIAL_7O2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_7O2"));
return SERIAL_7O2;
case BLINKER_SERIAL_8O2 :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_8O2"));
return SERIAL_8O2;
default :
BLINKER_LOG_ALL(BLINKER_F("SerialConfig SERIAL_8N1"));
return SERIAL_8N1;
}
}
uint8_t parseMode(uint8_t _mode, uint8_t _pullState)
{
uint8_t _modes = _mode << 4 | _pullState;
switch(_modes)
{
case 0 << 4 | 0 :
return INPUT;
case 1 << 4 | 0 :
return OUTPUT;
case 0 << 4 | 1 :
return INPUT_PULLUP;
case 0 << 4 | 2 :
#if defined(ESP8266)
return INPUT_PULLDOWN_16;
#elif defined(ESP32)
return INPUT_PULLDOWN;
#endif
default :
return INPUT;
}
}
class BlinkerSlaverAT
{
public :
BlinkerSlaverAT() {}
void update(const String & data) {
// _data = data;
// BLINKER_LOG(BLINKER_F("update data: "), data);
// _isAT = serialize(data);
serialize(data);
BLINKER_LOG(BLINKER_F("serialize _set: "), _set);
// return _isAT;
}
blinker_at_state_t state() { return _set; }
String cmd() { return _atCmd; }
uint8_t paramNum() { return _paramNum; }
String getParam(uint8_t num) {
if (num >= _paramNum) return "";
else return _param[num];
}
private :
// bool _isAT;
blinker_at_state_t _set;
uint8_t _paramNum;
// String _data;
String _atCmd;
String _param[6];
void serialize(const String & _data) {
BLINKER_LOG_ALL(BLINKER_F("serialize _data: "), _data);
_paramNum = 0;
// _isAT = false;
_set = AT_NONE;
int addr_start = 0;//_data.indexOf("+");
int addr_end = 0;
String startCmd = _data.substring(0, 2);
BLINKER_LOG_ALL(BLINKER_F("startCmd: "), startCmd);
// startCmd = _data.substring(0, 3);
// BLINKER_LOG(BLINKER_F("startCmd: "), startCmd);
BLINKER_LOG_ALL(BLINKER_F("startCmd len: "), _data.length());
// BLINKER_LOG(BLINKER_F("serialize addr_start: "), addr_start);
// BLINKER_LOG(BLINKER_F("serialize addr_end: "), addr_end);
// if ((addr_start != -1) && STRING_contains_string(_data, ":"))
if (startCmd == BLINKER_CMD_AT)
{
// _isAT = true;
// if (_data.length() == 3) {
// _set = AT_ACTION;
// return true;
// }
addr_start = 3;
// int addr_end1 = _data.indexOf("=");
// int addr_end2 = _data.indexOf("?");
// int addr_end3 = _data.indexOf("\n");
// check "="
addr_end = _data.indexOf("=");
BLINKER_LOG_ALL(BLINKER_F("serialize addr_end: "), addr_end);
if (addr_end != -1)
{
_set = AT_SETTING;
_atCmd = _data.substring(addr_start, addr_end);
BLINKER_LOG_ALL(BLINKER_F("serialize _atCmd: "), _atCmd);
}
else
{
addr_end = _data.indexOf("?");
BLINKER_LOG_ALL(BLINKER_F("serialize addr_end: "), addr_end);
if (addr_end != -1)
{
_set = AT_QUERY;
_atCmd = _data.substring(addr_start, addr_end);
BLINKER_LOG_ALL(BLINKER_F("serialize _atCmd: "), _atCmd);
return;
}
else
{
addr_end = _data.indexOf("\r");
BLINKER_LOG_ALL(BLINKER_F("serialize addr_end: "), addr_end);
if (addr_end != -1)
{
_set = AT_ACTION;
if (addr_end == (addr_start-1)) {
_atCmd = startCmd;
BLINKER_LOG_ALL(BLINKER_F("serialize _atCmd: "), _atCmd);
return;
}
_atCmd = _data.substring(addr_start, addr_end);
BLINKER_LOG_ALL(BLINKER_F("serialize _atCmd: "), _atCmd);
return;
}
else if (_data.length() == startCmd.length())
{
_set = AT_ACTION;
// if (addr_end == -1) {
_atCmd = startCmd;
BLINKER_LOG_ALL(BLINKER_F("serialize _atCmd: "), _atCmd);
return;
// }
// _atCmd = _data.substring(addr_start, addr_end);
// BLINKER_LOG_ALL(BLINKER_F("serialize _atCmd: "), _atCmd);
// return;
}
else
{
_set = AT_ACTION;
_atCmd = _data.substring(3, _data.length());
BLINKER_LOG_ALL(BLINKER_F("serialize _atCmd: "), _atCmd);
return;
}
}
}
// BLINKER_LOG(BLINKER_F("serialize _data: "), _data);
String serData;
uint16_t dataLen;
if (_data.indexOf("\r") != -1) dataLen = _data.length() - 1;
else dataLen = _data.length();
addr_start = 0;
for (_paramNum = 0; _paramNum < 11; _paramNum++) {
// if (addr_end != -1)
// {
addr_start += addr_end;
addr_start += 1;
// }
// else
// {
// addr_start = 3;
// }
serData = _data.substring(addr_start, dataLen);
addr_end = serData.indexOf(",");
BLINKER_LOG_ALL(BLINKER_F("serialize serData: "), serData);
BLINKER_LOG_ALL(BLINKER_F("serialize addr_start: "), addr_start);
BLINKER_LOG_ALL(BLINKER_F("serialize addr_end: "), addr_end);
if (addr_end == -1) {
if (addr_start >= dataLen) return;
addr_end = serData.indexOf(" ");
if (addr_end != -1) {
_param[_paramNum] = serData.substring(0, addr_end);
if (_param[_paramNum] == "?" && _paramNum == 0) {
_set = AT_TEST;
}
BLINKER_LOG_ALL(BLINKER_F("_param0["), _paramNum, \
BLINKER_F("]: "), _param[_paramNum]);
// BLINKER_LOG_ALL(BLINKER_F("_set: "), _set);
_paramNum++;
return;
}
_param[_paramNum] = serData;
if (_param[_paramNum] == "?" && _paramNum == 0) {
_set = AT_TEST;
}
BLINKER_LOG_ALL(BLINKER_F("_param1["), _paramNum, \
BLINKER_F("]: "), _param[_paramNum]);
// BLINKER_LOG_ALL(BLINKER_F("_set: "), _set);
_paramNum++;
return;
}
else {
_param[_paramNum] = serData.substring(0, addr_end);
}
BLINKER_LOG_ALL(BLINKER_F("_param["), _paramNum, \
BLINKER_F("]: "), _param[_paramNum]);
}
// return;
}
// else {
// // _isAT = false;
// return;
// }
}
};
enum atPin_t {
PIN_SET,
PIN_MODE,
PIN_PULLSTATE
};
enum atIO_t {
IO_PIN,
IO_LVL
};
class PinData
{
public :
PinData(uint8_t _pin, uint8_t _mode, uint8_t _pullState)
{
// pin_num = _pin;
// pin_mode = _mode;
// pin_pull = _pullState;
_pinDatas = _pin << 8 | _mode << 4 | _pullState;
pinMode(_pin, parseMode(_mode, _pullState));
}
uint8_t getPin() { return (_pinDatas >> 8 & 0xFF); }
uint8_t getMode() { return (_pinDatas >> 4 & 0x0F); }
uint8_t getPull() { return (_pinDatas & 0x0F); }
String data() {
String _data = STRING_format(_pinDatas >> 8 & 0xFF) + "," + \
STRING_format(_pinDatas >> 4 & 0x0F) + "," + \
STRING_format(_pinDatas & 0x0F);
return _data;
}
bool checkPin(uint8_t _pin) { return (_pinDatas >> 8 & 0xFF) == _pin; }
void fresh(uint8_t _mode, uint8_t _pullState)
{
_pinDatas = (_pinDatas >> 8 & 0xFF) << 8 | _mode << 4 | _pullState;
pinMode((_pinDatas >> 8 & 0xFF) << 8, parseMode(_mode, _pullState));
}
private :
uint16_t _pinDatas;
// uint8_t pin_num;
// atPinMode_t pin_mode;
// atPull_t pin_pull;
};
// static class BlinkerSlaverAT * _slaverAT;
// static class PinData * _pinData[BLINKER_MAX_PIN_NUM];
#endif
#endif

View File

@@ -0,0 +1,637 @@
#ifndef BLINKER_PROTOCOL_H
#define BLINKER_PROTOCOL_H
#include "BlinkerConfig.h"
#include "BlinkerDebug.h"
#include "BlinkerStream.h"
#include "BlinkerUtility.h"
enum _blinker_state_t
{
CONNECTING,
CONNECTED,
DISCONNECTED
};
class BlinkerProtocol
{
public :
BlinkerProtocol()
: state(CONNECTING)
, isInit(false)
, isFresh(false)
, isAvail(false)
, availState(false)
, canParse(false)
{}
void transport(BlinkerStream & bStream) { conn = &bStream; isInit = true; }
// #if defined(BLINKER_LOWPOWER_AIR202)
// void print(const String & data);
// void print(const String & key, const String & data);
// char * deviceName() { if (isInit) return conn->deviceName(); else return ""; }
// char * authKey() { if (isInit) return conn->authKey(); else return ""; }
// int init() { return isInit ? conn->init() : false; }
// void begin(const char* _key, const char* _type, String _imei)
// { conn->begin(_key, _type, _imei); }
// int deviceRegister(){ return conn->deviceRegister(); }
// #else
int connect() { return isInit ? conn->connect() : false; }
int connected()
{
if (isInit)
{
// if (conn->connected())
// {
// state == CONNECTED;
// return true;
// }
// else
// {
// state = DISCONNECTED;
// return false;
// }
return conn->connected();
}
else
{
state = DISCONNECTED;
return false;
}
}
void disconnect() { if (isInit) { conn->disconnect(); state = DISCONNECTED; } }
// bool available() { if (availState) {availState = false; return true;} else return false; }
void flush();
void checkState(bool state = true) { isCheck = state; }
void print(const String & data);
void print(const String & key, const String & data);
#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || \
defined(BLINKER_AT_MQTT) || defined(BLINKER_MQTT_AT) || \
defined(BLINKER_WIFI_GATEWAY) || defined(BLINKER_NBIOT_SIM7020) || \
defined(BLINKER_GPRS_AIR202) || defined(BLINKER_PRO_SIM7020) || \
defined(BLINKER_PRO_AIR202) || defined(BLINKER_MQTT_AUTO) || \
defined(BLINKER_PRO_ESP) || defined(BLINKER_WIFI_SUBDEVICE) || \
defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_NBIOT_SIM7000) || \
defined(BLINKER_QRCODE_NBIOT_SIM7000) || defined(BLINKE_HTTP)
int aliPrint(const String & data) { return isInit ? conn->aliPrint(data) : false; }
int duerPrint(const String & data, bool report = false) { return isInit ? conn->duerPrint(data, report) : false; }
#if !defined(BLINKER_GPRS_AIR202) && !defined(BLINKER_NBIOT_SIM7020) && \
!defined(BLINKER_PRO_SIM7020) && !defined(BLINKER_PRO_AIR202) && \
!defined(BLINKER_QRCODE_NBIOT_SIM7020) && !defined(BLINKER_NBIOT_SIM7000) && \
!defined(BLINKER_QRCODE_NBIOT_SIM7000)
int miPrint(const String & data) { return isInit ? conn->miPrint(data) : false; }
#endif
// void ping() { if (isInit) conn->ping(); }
#if !defined(BLINKER_MQTT_AT)
int bPrint(char * name, const String & data) { return isInit ? conn->bPrint(name, data) : false; }
int autoPrint(unsigned long id) { return isInit ? conn->autoPrint(id) : false; }
void sharers(const String & data) { if (isInit) conn->sharers(data); }
int needFreshShare() { if (isInit) return conn->needFreshShare(); else return false; }
#endif
#endif
#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || \
defined(BLINKER_AT_MQTT) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_GPRS_AIR202) || defined(BLINKER_NBIOT_SIM7020) || \
defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202) || \
defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP) || \
defined(BLINKER_WIFI_SUBDEVICE) || defined(BLINKER_QRCODE_NBIOT_SIM7020) || \
defined(BLINKER_NBIOT_SIM7000) || defined(BLINKER_QRCODE_NBIOT_SIM7000) || \
defined(BLINKE_HTTP)
int toServer(char * data) { return isInit ? conn->toServer(data) : false; }
char * deviceName() { if (isInit) return conn->deviceName(); else return ""; }
char * authKey() { if (isInit) return conn->authKey(); else return ""; }
char * token() { if (isInit) return conn->token(); else return ""; }
int init() { return isInit ? conn->init() : false; }
int mConnected() { return isInit ? conn->mConnected() : false; }
void freshAlive() { if (isInit) conn->freshAlive(); }
#endif
#if defined(BLINKER_LOWPOWER_AIR202)
char * deviceName() { if (isInit) return conn->deviceName(); else return ""; }
char * authKey() { if (isInit) return conn->authKey(); else return ""; }
char * token() { if (isInit) return conn->token(); else return ""; }
int init() { return isInit ? conn->init() : false; }
void begin(const char* _key, const char* _type, String _imei) { conn->begin(_key, _type, _imei); }
int deviceRegister(){ return conn->deviceRegister(); }
#endif
#if defined(BLINKER_PRO) || defined(BLINKER_MQTT_AUTO) || \
defined(BLINKER_PRO_ESP) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_WIFI_SUBDEVICE)
int deviceRegister(){ return conn->deviceRegister(); }
int authCheck() { return conn->authCheck(); }
#if defined(BLINKER_PRO)
void begin(const char* _deviceType) { conn->begin(_deviceType); }
#elif defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP) || \
defined(BLINKER_WIFI_GATEWAY) || defined(BLINKER_WIFI_SUBDEVICE)
void begin(const char* _key, const char* _type) { conn->begin(_key, _type); }
#endif
#elif defined(BLINKER_GPRS_AIR202) || defined(BLINKER_NBIOT_SIM7020) || \
defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202) || \
defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_NBIOT_SIM7000) || \
defined(BLINKER_QRCODE_NBIOT_SIM7000)
int deviceRegister(){ return conn->deviceRegister(); }
#if defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_QRCODE_NBIOT_SIM7000)
void begin(const char* _authKey, const char* _deviceType, String _imei)
{ conn->begin(_authKey, _deviceType, _imei); }
#else
void begin(const char* _deviceType, String _imei)
{ conn->begin(_deviceType, _imei); }
#endif
#if defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202)
int authCheck() { return conn->authCheck(); }
#endif
#endif
// #if defined(BLINKER_WIFI_SUBDEVICE)
// void attachSubAvailable(blinker_callback_return_int_t func)
// { if (isInit) conn->attachAvailable(func); }
// void attachSubRead(blinker_callback_return_string_t func)
// { if (isInit) conn->attachRead(func); }
// void attachSubPrint(blinker_callback_with_string_arg_t func)
// { if (isInit) conn->attachPrint(func); }
// void attachSubBegin(blinker_callback_t func)
// { if (isInit) conn->attachBegin(func); }
// void attachSubConnect(blinker_callback_return_int_t func)
// { if (isInit) conn->attachConnect(func); }
// void attachSubConnected(blinker_callback_return_int_t func)
// { if (isInit) conn->attachConnected(func); }
// void attachSubDisconnect(blinker_callback_t func)
// { if (isInit) conn->attachDisconnect(func); }
// #endif
#if defined(BLINKER_WIFI_GATEWAY) || defined(BLINKER_WIFI_SUBDEVICE)
void meshCheck() { conn->meshCheck(); }
#if !defined(BLINKER_WIFI_SUBDEVICE)
void setTimezone(float tz) { conn->setTimezone(tz); }
#endif
#if defined(BLINKER_WIFI_SUBDEVICE)
int subPrint(const String & data) { return conn->subPrint(data); }
int meshAvail() { return conn->meshAvail(); }
char * meshLastRead() { return conn->meshLastRead(); }
void meshFlush() { conn->meshFlush(); }
#endif
#endif
#if defined(BLINKER_PRO_ESP) || defined(BLINKER_WIFI_GATEWAY)
void smartConfigType() { conn->setSmartConfig(); }
void apConfigType() { conn->setApConfig(); }
bool checkIsSmartConfig() { return conn->checkSmartConfig(); }
#endif
#if defined(BLINKER_HTTP)
void subscribe() { conn->subscribe(); }
#endif
// #endif
private :
protected :
BlinkerStream *conn;
_blinker_state_t state;
bool isInit;
bool isFresh;
bool isAvail;
bool availState;
bool canParse;
bool autoFormat = false;
bool isCheck = true;
uint32_t autoFormatFreshTime;
char* _sendBuf;
blinker_callback_with_string_arg_t _availableFunc = NULL;
// #if defined(BLINKER_LOWPOWER_AIR202)
// void checkFormat();
// void autoFormatData(const String & key, const String & jsonValue);
// #else
int checkAvail();
#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || \
defined(BLINKER_AT_MQTT) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202) || \
defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP) || \
defined(BLINKER_WIFI_SUBDEVICE) || defined(BLINKE_HTTP)
bool checkAliAvail() { return conn->aligenieAvail(); }
bool checkDuerAvail() { return conn->duerAvail(); }
#if !defined(BLINKER_GPRS_AIR202) && !defined(BLINKER_NBIOT_SIM7020) && \
!defined(BLINKER_PRO_SIM7020) && !defined(BLINKER_PRO_AIR202) && \
!defined(BLINKER_QRCODE_NBIOT_SIM7020) && !defined(BLINKER_NBIOT_SIM7000) && \
!defined(BLINKER_QRCODE_NBIOT_SIM7000)
bool checkMIOTAvail() { return conn->miAvail(); }
#endif
#endif
#if defined(BLINKER_AT_MQTT)
void begin(const char* auth) { return conn->begin(auth); }
int serialAvailable() { return conn->serialAvailable(); }
int serialPrint(const String & s1, const String & s2, bool needCheck = true)
{ return conn->serialPrint(s1, s2, needCheck); }
int serialPrint(const String & s, bool needCheck = true)
{ return conn->serialPrint(s, needCheck); }
int mqttPrint(const String & data)
{ return conn->mqttPrint(data); }
char * serialLastRead() { return conn->serialLastRead(); }
void aligenieType(blinker_at_aligenie_t _type)
{ conn->aligenieType(_type); }
void duerType(blinker_at_dueros_t _type)
{ conn->duerType(_type); }
char * deviceId() { return conn->deviceId(); }
char * uuid() { return conn->uuid(); }
void softAPinit() { conn->softAPinit(); }
void smartconfig() { conn->smartconfig(); }
int autoInit() { return conn->autoInit(); }
void connectWiFi(String _ssid, String _pswd) { return conn->connectWiFi(_ssid, _pswd); }
void connectWiFi(const char* _ssid, const char* _pswd) { return conn->connectWiFi(_ssid, _pswd); }
#endif
void checkFormat();
void checkAutoFormat();
char* dataParse() { if (canParse) return conn->lastRead(); else return ""; }
char* lastRead() { return conn->lastRead(); }
void isParsed() { BLINKER_LOG_ALL(BLINKER_F("isParsed")); flush(); }
int parseState() { return canParse; }
int printNow();
void _timerPrint(const String & n);
int _print(char * n, bool needCheckLength = true);
void autoFormatData(const String & key, const String & jsonValue);
// #endif
};
// #if !defined(BLINKER_LOWPOWER_AIR202)
void BlinkerProtocol::flush()
{
if (isInit && isAvail) conn->flush();
isAvail = false;
isFresh = false;
availState = false;
canParse = false;
}
int BlinkerProtocol::checkAvail()
{
if (!isInit) return false;
flush();
if (connected())
{
isAvail = conn->available();
if (isAvail)
{
BLINKER_LOG_ALL(BLINKER_F("checkAvail: "), isAvail);
isFresh = true;
canParse = true;
availState = true;
}
}
return isAvail;
}
void BlinkerProtocol::checkAutoFormat()
{
if (autoFormat)
{
if ((millis() - autoFormatFreshTime) >= BLINKER_MSG_AUTOFORMAT_TIMEOUT)
{
if (strlen(_sendBuf))
{
// #if !defined(BLINKER_LOWPOWER_AIR202)
#if defined(BLINKER_ARDUINOJSON)
_print(_sendBuf);
#else
strcat(_sendBuf, "}");
_print(_sendBuf);
#endif
// #endif
}
free(_sendBuf);
autoFormat = false;
BLINKER_LOG_FreeHeap_ALL();
}
}
}
int BlinkerProtocol::printNow()
{
if (strlen(_sendBuf) && autoFormat)
{
int8_t print_state = BLINKER_ERROR;
#if defined(BLINKER_ARDUINOJSON)
if (_print(_sendBuf)) print_state = BLINKER_SUCCESS;
#else
strcat(_sendBuf, "}");
if (_print(_sendBuf)) print_state = BLINKER_SUCCESS;
#endif
free(_sendBuf);
autoFormat = false;
BLINKER_LOG_FreeHeap_ALL();
return print_state;
}
return BLINKER_ERROR;
}
void BlinkerProtocol::_timerPrint(const String & n)
{
BLINKER_LOG_ALL(BLINKER_F("print: "), n);
if (n.length() <= BLINKER_MAX_SEND_SIZE)
{
checkFormat();
checkState(false);
strcpy(_sendBuf, n.c_str());
}
else
{
BLINKER_ERR_LOG(BLINKER_F("SEND DATA BYTES MAX THAN LIMIT!"));
}
}
int BlinkerProtocol::_print(char * n, bool needCheckLength)
{
BLINKER_LOG_ALL(BLINKER_F("print: "), n);
if (strlen(n) <= BLINKER_MAX_SEND_SIZE || \
!needCheckLength)
{
// BLINKER_LOG_FreeHeap_ALL();
BLINKER_LOG_ALL(BLINKER_F("Proto print..."));
BLINKER_LOG_FreeHeap_ALL();
conn->print(n, isCheck);
if (!isCheck) isCheck = true;
return true;
}
else {
BLINKER_ERR_LOG(BLINKER_F("SEND DATA BYTES MAX THAN LIMIT!"));
return false;
}
}
void BlinkerProtocol::print(const String & data)
{
#if !defined(BLINKER_LOWPOWER_AIR202)
checkFormat();
strcpy(_sendBuf, data.c_str());
_print(_sendBuf);
free(_sendBuf);
autoFormat = false;
BLINKER_LOG_FreeHeap_ALL();
#endif
}
void BlinkerProtocol::print(const String & key, const String & data)
{
checkFormat();
autoFormatData(key, data);
if ((millis() - autoFormatFreshTime) >= BLINKER_MSG_AUTOFORMAT_TIMEOUT)
{
autoFormatFreshTime = millis();
}
}
void BlinkerProtocol::checkFormat()
{
if (!autoFormat)
{
autoFormat = true;
_sendBuf = (char*)malloc(BLINKER_MAX_SEND_SIZE*sizeof(char));
memset(_sendBuf, '\0', BLINKER_MAX_SEND_SIZE);
}
}
void BlinkerProtocol::autoFormatData(const String & key, const String & jsonValue)
{
#if defined(BLINKER_ARDUINOJSON)
BLINKER_LOG_ALL(BLINKER_F("autoFormatData key: "), key, \
BLINKER_F(", json: "), jsonValue);
String _data;
if (STRING_contains_string(STRING_format(_sendBuf), key))
{
// DynamicJsonBuffer jsonSendBuffer;
DynamicJsonDocument jsonBuffer(1024);
if (strlen(_sendBuf)) {
BLINKER_LOG_ALL(BLINKER_F("add"));
// JsonObject& root = jsonSendBuffer.parseObject(STRING_format(_sendBuf));
deserializeJson(jsonBuffer, STRING_format(_sendBuf));
JsonObject root = jsonBuffer.as<JsonObject>();
if (root.containsKey(key)) {
root.remove(key);
}
// root.printTo(_data);
serializeJson(root, _data);
_data = _data.substring(0, _data.length() - 1);
if (_data.length() > 4 ) _data += BLINKER_F(",");
_data += jsonValue;
_data += BLINKER_F("}");
}
else {
BLINKER_LOG_ALL(BLINKER_F("new"));
_data = BLINKER_F("{");
_data += jsonValue;
_data += BLINKER_F("}");
}
}
else {
_data = STRING_format(_sendBuf);
if (_data.length())
{
BLINKER_LOG_ALL(BLINKER_F("add."));
_data = _data.substring(0, _data.length() - 1);
_data += BLINKER_F(",");
_data += jsonValue;
_data += BLINKER_F("}");
}
else {
BLINKER_LOG_ALL(BLINKER_F("new."));
_data = BLINKER_F("{");
_data += jsonValue;
_data += BLINKER_F("}");
}
}
if (_data.length() > BLINKER_MAX_SEND_BUFFER_SIZE)
{
BLINKER_ERR_LOG(BLINKER_F("FORMAT DATA SIZE IS MAX THAN LIMIT: "), BLINKER_MAX_SEND_BUFFER_SIZE);
return;
}
strcpy(_sendBuf, _data.c_str());
#else
String data;
BLINKER_LOG_ALL(BLINKER_F("autoFormatData data: "), jsonValue);
BLINKER_LOG_ALL(BLINKER_F("strlen(_sendBuf): "), strlen(_sendBuf));
BLINKER_LOG_ALL(BLINKER_F("data.length(): "), jsonValue.length());
if ((strlen(_sendBuf) + jsonValue.length()) >= BLINKER_MAX_SEND_BUFFER_SIZE)
{
BLINKER_ERR_LOG(BLINKER_F("FORMAT DATA SIZE IS MAX THAN LIMIT"));
return;
}
if (strlen(_sendBuf) > 0) {
data = "," + jsonValue;
strcat(_sendBuf, data.c_str());
}
else {
data = "{" + jsonValue;
strcpy(_sendBuf, data.c_str());
}
#endif
}
// #elif defined(BLINKER_LOWPOWER_AIR202)
// void BlinkerProtocol::print(const String & data)
// {
// #if !defined(BLINKER_LOWPOWER_AIR202)
// checkFormat();
// strcpy(_sendBuf, data.c_str());
// _print(_sendBuf);
// free(_sendBuf);
// autoFormat = false;
// BLINKER_LOG_FreeHeap_ALL();
// #endif
// }
// void BlinkerProtocol::print(const String & key, const String & data)
// {
// checkFormat();
// autoFormatData(key, data);
// if ((millis() - autoFormatFreshTime) >= BLINKER_MSG_AUTOFORMAT_TIMEOUT)
// {
// autoFormatFreshTime = millis();
// }
// }
// void BlinkerProtocol::checkFormat()
// {
// if (!autoFormat)
// {
// autoFormat = true;
// _sendBuf = (char*)malloc(BLINKER_MAX_SEND_SIZE*sizeof(char));
// memset(_sendBuf, '\0', BLINKER_MAX_SEND_SIZE);
// }
// }
// void BlinkerProtocol::autoFormatData(const String & key, const String & jsonValue)
// {
// #if defined(BLINKER_ARDUINOJSON)
// BLINKER_LOG_ALL(BLINKER_F("autoFormatData key: "), key,
// BLINKER_F(", json: "), jsonValue);
// String _data;
// if (STRING_contains_string(STRING_format(_sendBuf), key))
// {
// DynamicJsonBuffer jsonSendBuffer;
// if (strlen(_sendBuf)) {
// BLINKER_LOG_ALL(BLINKER_F("add"));
// JsonObject& root = jsonSendBuffer.parseObject(STRING_format(_sendBuf));
// if (root.containsKey(key)) {
// root.remove(key);
// }
// root.printTo(_data);
// _data = _data.substring(0, _data.length() - 1);
// if (_data.length() > 4 ) _data += BLINKER_F(",");
// _data += jsonValue;
// _data += BLINKER_F("}");
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("new"));
// _data = BLINKER_F("{");
// _data += jsonValue;
// _data += BLINKER_F("}");
// }
// }
// else {
// _data = STRING_format(_sendBuf);
// if (_data.length())
// {
// BLINKER_LOG_ALL(BLINKER_F("add."));
// _data = _data.substring(0, _data.length() - 1);
// _data += BLINKER_F(",");
// _data += jsonValue;
// _data += BLINKER_F("}");
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("new."));
// _data = BLINKER_F("{");
// _data += jsonValue;
// _data += BLINKER_F("}");
// }
// }
// if (_data.length() > BLINKER_MAX_SEND_BUFFER_SIZE)
// {
// BLINKER_ERR_LOG(BLINKER_F("FORMAT DATA SIZE IS MAX THAN LIMIT: "), BLINKER_MAX_SEND_BUFFER_SIZE);
// return;
// }
// strcpy(_sendBuf, _data.c_str());
// #else
// String data;
// BLINKER_LOG_ALL(BLINKER_F("autoFormatData data: "), jsonValue);
// BLINKER_LOG_ALL(BLINKER_F("strlen(_sendBuf): "), strlen(_sendBuf));
// BLINKER_LOG_ALL(BLINKER_F("data.length(): "), jsonValue.length());
// if ((strlen(_sendBuf) + jsonValue.length()) >= BLINKER_MAX_SEND_BUFFER_SIZE)
// {
// BLINKER_ERR_LOG(BLINKER_F("FORMAT DATA SIZE IS MAX THAN LIMIT"));
// return;
// }
// if (strlen(_sendBuf) > 0) {
// data = "," + jsonValue;
// strcat(_sendBuf, data.c_str());
// }
// else {
// data = "{" + jsonValue;
// strcpy(_sendBuf, data.c_str());
// }
// #endif
// }
// #endif
#endif

View File

@@ -0,0 +1,178 @@
#ifndef BLINKER_STREAM_H
#define BLINKER_STREAM_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
// #if defined(BLINKER_AT_MQTT)
// #include "Blinker/BlinkerMQTTATBase.h"
// #endif
#include "BlinkerUtility.h"
class BlinkerStream
{
public :
// #if defined(BLINKER_LOWPOWER_AIR202)
// virtual void begin(const char* _key, const char* _deviceType, String _imei) = 0;
// virtual char * deviceName() = 0;
// virtual char * authKey() = 0;
// virtual int init() = 0;
// virtual int deviceRegister() = 0;
// #else
virtual int available() = 0;
virtual char * lastRead() = 0;
virtual void flush() = 0;
// virtual int print(const String & s, bool needCheck = true) = 0;
virtual int print(char * data, bool needCheck = true) = 0;
virtual int connect() = 0;
virtual int connected() = 0;
virtual void disconnect() = 0;
#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || \
defined(BLINKER_AT_MQTT) || defined(BLINKER_MQTT_AT) || \
defined(BLINKER_WIFI_GATEWAY) || defined(BLINKER_NBIOT_SIM7020) || \
defined(BLINKER_GPRS_AIR202) || defined(BLINKER_PRO_SIM7020) || \
defined(BLINKER_PRO_AIR202) || defined(BLINKER_MQTT_AUTO) || \
defined(BLINKER_PRO_ESP) || defined(BLINKER_WIFI_SUBDEVICE) || \
defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_NBIOT_SIM7000) || \
defined(BLINKER_QRCODE_NBIOT_SIM7000) || defined(BLINKE_HTTP)
virtual int aliPrint(const String & data) = 0;
virtual int duerPrint(const String & data, bool report = false) = 0;
#if !defined(BLINKER_GPRS_AIR202) && !defined(BLINKER_NBIOT_SIM7020) && \
!defined(BLINKER_PRO_SIM7020) && !defined(BLINKER_PRO_AIR202) && \
!defined(BLINKER_QRCODE_NBIOT_SIM7020) && !defined(BLINKER_NBIOT_SIM7000) && \
!defined(BLINKER_QRCODE_NBIOT_SIM7000)
virtual int miPrint(const String & data) = 0;
#endif
// virtual void ping() = 0;
#if !defined(BLINKER_MQTT_AT)
virtual int bPrint(char * name, const String & data) = 0;
virtual int autoPrint(unsigned long id) = 0;
virtual void sharers(const String & data);
virtual int aligenieAvail() = 0;
virtual int duerAvail() = 0;
#if !defined(BLINKER_GPRS_AIR202) && !defined(BLINKER_NBIOT_SIM7020) && \
!defined(BLINKER_PRO_SIM7020) && !defined(BLINKER_PRO_AIR202) && \
!defined(BLINKER_QRCODE_NBIOT_SIM7020) && !defined(BLINKER_NBIOT_SIM7000) && \
!defined(BLINKER_QRCODE_NBIOT_SIM7000)
virtual int miAvail() = 0;
#endif
virtual int needFreshShare() = 0;
#endif
#endif
#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || \
defined(BLINKER_AT_MQTT) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_GPRS_AIR202) || defined(BLINKER_NBIOT_SIM7020) || \
defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202) || \
defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP) || \
defined(BLINKER_WIFI_SUBDEVICE) || defined(BLINKER_QRCODE_NBIOT_SIM7020) || \
defined(BLINKER_NBIOT_SIM7000) || defined(BLINKER_QRCODE_NBIOT_SIM7000) || \
defined(BLINKE_HTTP)
virtual int toServer(char * data) = 0;
virtual char * deviceName() = 0;
virtual char * authKey() = 0;
virtual char * token() = 0;
virtual int init() = 0;
virtual int mConnected() = 0;
virtual void freshAlive() = 0;
#endif
#if defined(BLINKER_LOWPOWER_AIR202)
virtual char * deviceName() = 0;
virtual char * authKey() = 0;
virtual int init() = 0;
virtual void begin(const char* _key, const char* _type, String _imei) = 0;
virtual int deviceRegister() = 0;
#endif
// #if defined(BLINKER_MQTT) || defined(BLINKER_PRO)
// virtual int aligenieAvail() = 0;
// virtual int duerAvail() = 0;
#if defined(BLINKER_AT_MQTT)
virtual void begin(const char* auth) = 0;
// virtual void begin() = 0;
virtual int serialAvailable() = 0;
virtual int serialPrint(const String & s1, const String & s2, bool needCheck = true) = 0;
virtual int serialPrint(const String & s, bool needCheck = true) = 0;
virtual int mqttPrint(const String & data);
virtual char * serialLastRead() = 0;
virtual void aligenieType(int _type) = 0;
virtual void duerType(int _type) = 0;
virtual char * deviceId() = 0;
virtual char * uuid() = 0;
virtual void softAPinit() = 0;
virtual void smartconfig() = 0;
virtual int autoInit() = 0;
virtual void connectWiFi(String _ssid, String _pswd) = 0;
virtual void connectWiFi(const char* _ssid, const char* _pswd) = 0;
#endif
#if defined(BLINKER_PRO) || defined(BLINKER_MQTT_AUTO) || \
defined(BLINKER_PRO_ESP) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_WIFI_SUBDEVICE)
virtual int deviceRegister() = 0;
virtual int authCheck() = 0;
#if defined(BLINKER_PRO)
virtual void begin(const char* _deviceType) = 0;
#elif defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP) || \
defined(BLINKER_WIFI_GATEWAY) || defined(BLINKER_WIFI_SUBDEVICE)
virtual void begin(const char* auth, const char* type) = 0;
#endif
#elif defined(BLINKER_GPRS_AIR202) || defined(BLINKER_NBIOT_SIM7020) || \
defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202) || \
defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_NBIOT_SIM7000) || \
defined(BLINKER_QRCODE_NBIOT_SIM7000)
virtual int deviceRegister() = 0;
#if defined(BLINKER_QRCODE_NBIOT_SIM7020) || defined(BLINKER_QRCODE_NBIOT_SIM7000)
virtual void begin(const char* _auth, const char* _type, String _imei) = 0;
#else
virtual void begin(const char* _type, String _imei) = 0;
#endif
#if defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202)
virtual int authCheck() = 0;
#endif
#endif
#if defined(BLINKER_WIFI_GATEWAY) || defined(BLINKER_WIFI_SUBDEVICE)
virtual void meshCheck() = 0;
#if !defined(BLINKER_WIFI_SUBDEVICE)
virtual void setTimezone(float tz) = 0;
#endif
#if defined(BLINKER_WIFI_SUBDEVICE)
virtual int subPrint(const String & data) = 0;
virtual int meshAvail() = 0;
virtual char * meshLastRead() = 0;
virtual void meshFlush() = 0;
#endif
#endif
#if defined(BLINKER_PRO_ESP) || defined(BLINKER_WIFI_GATEWAY)
virtual void setSmartConfig() = 0;
virtual void setApConfig() = 0;
virtual bool checkSmartConfig() = 0;
#endif
#if defined(BLINKER_HTTP)
virtual void subscribe() = 0;
#endif
// #if defined(BLINKER_WIFI_SUBDEVICE)
// virtual void attachAvailable(blinker_callback_return_int_t func) = 0;
// virtual void attachRead(blinker_callback_return_string_t func) = 0;
// virtual void attachPrint(blinker_callback_with_string_arg_t func) = 0;
// virtual void attachBegin(blinker_callback_t func) = 0;
// virtual void attachConnect(blinker_callback_return_int_t func) = 0;
// virtual void attachConnected(blinker_callback_return_int_t func) = 0;
// virtual void attachDisconnect(blinker_callback_t func) = 0;
// #endif
// #endif
};
#endif

View File

@@ -0,0 +1,143 @@
#if defined(ESP8266) || defined(ESP32)
#include "BlinkerConfig.h"
#include "BlinkerDebug.h"
#include "BlinkerTimer.h"
Ticker cdTicker;
Ticker lpTicker;
Ticker tmTicker;
bool _cdRunState = false;
bool _lpRunState = false;
bool _tmRunState = false;
bool _cdState = false;
bool _lpState = false;
bool _tmState = false;
bool _lpRun1 = true;
bool _tmRun1 = true;
bool _tmDay = false;
bool _cdTrigged = false;
bool _lpTrigged = false;
bool _tmTrigged = false;
bool _isTimingLoop = false;
uint8_t _lpTimes;
uint8_t _lpTrigged_times;
uint32_t _cdTime1;
uint32_t _cdTime2;
uint32_t _cdStart;
uint32_t _cdData;
// bool _cdStop = true;
uint32_t _lpTime1;
uint32_t _lpTime1_start;
uint32_t _lpTime2;
uint32_t _lpTime2_start;
uint32_t _lpData;
bool _lpStop = true;
uint32_t _tmTime1;
uint32_t _tmTime2;
uint32_t _tmTime;
uint8_t _timingDay = 0;
uint8_t taskCount = 0;
uint8_t triggedTask = 0;
void disableTimer() {
_cdRunState = false;
cdTicker.detach();
_lpRunState = false;
lpTicker.detach();
_tmRunState = false;
tmTicker.detach();
}
void _cd_callback()
{
uint32_t cd_during = millis() -_cdStart;
uint32_t cd_remain = _cdTime1 * 60 - cd_during / 1000;
if (_cdTime1 * 60 > BLINKER_ONE_HOUR_TIME) {
if (cd_remain > 0) {
if (cd_remain > BLINKER_ONE_HOUR_TIME) cd_remain = BLINKER_ONE_HOUR_TIME;
cdTicker.once(cd_remain, _cd_callback);
return;
}
}
// _cdState = false;
_cdTrigged = true;
BLINKER_LOG_ALL(BLINKER_F("countdown trigged!"));
}
void _lp_callback()
{
if (_lpRun1) {
uint32_t lp_1_during = millis() -_lpTime1_start;
uint32_t lp_1_remain = _lpTime1 * 60 - lp_1_during / 1000;
if (_lpTime1 * 60 > BLINKER_ONE_HOUR_TIME) {
if (lp_1_remain > 0) {
if (lp_1_remain > BLINKER_ONE_HOUR_TIME) lp_1_remain = BLINKER_ONE_HOUR_TIME;
lpTicker.once(lp_1_remain, _lp_callback);
return;
}
}
}
else {
uint32_t lp_2_during = millis() -_lpTime2_start;
uint32_t lp_2_remain = _lpTime2 * 60 - lp_2_during / 1000;
if (_lpTime2 * 60 > BLINKER_ONE_HOUR_TIME) {
if (lp_2_remain > 0) {
if (lp_2_remain > BLINKER_ONE_HOUR_TIME) lp_2_remain = BLINKER_ONE_HOUR_TIME;
lpTicker.once(lp_2_remain, _lp_callback);
return;
}
}
}
_lpRun1 = !_lpRun1;
if (_lpRun1) {
_lpTrigged_times++;
if (_lpTimes) {
if (_lpTimes == _lpTrigged_times && _lpTimes != 0) {
lpTicker.detach();
_lpStop = true;
}
else {
lpTicker.once(_lpTime1 * 60, _lp_callback);
}
}
else {
lpTicker.once(_lpTime1 * 60, _lp_callback);
}
}
else {
lpTicker.once(_lpTime2 * 60, _lp_callback);
}
_lpTrigged = true;
BLINKER_LOG_ALL(BLINKER_F("loop trigged!"));
}
void timingHandle(uint8_t cbackData)
{
uint8_t task = cbackData;
_tmTrigged = true;
triggedTask = task;
}
#endif

View File

@@ -0,0 +1,57 @@
#ifndef BLINKER_TIMER_H
#define BLINKER_TIMER_H
#if defined(ESP8266) || defined(ESP32)
#include <Ticker.h>
#include <EEPROM.h>
extern Ticker cdTicker;
extern Ticker lpTicker;
extern Ticker tmTicker;
extern bool _cdRunState;
extern bool _lpRunState;
extern bool _tmRunState;
extern bool _cdState;
extern bool _lpState;
extern bool _tmState;
extern bool _lpRun1;
extern bool _tmRun1;
extern bool _tmDay;
extern bool _cdTrigged;
extern bool _lpTrigged;
extern bool _tmTrigged;
extern bool _isTimingLoop;
extern uint8_t _lpTimes;
extern uint8_t _lpTrigged_times;
extern uint32_t _cdTime1;
extern uint32_t _cdTime2;
extern uint32_t _cdStart;
extern uint32_t _cdData;
// bool _cdStop = true;
extern uint32_t _lpTime1;
extern uint32_t _lpTime1_start;
extern uint32_t _lpTime2;
extern uint32_t _lpTime2_start;
extern uint32_t _lpData;
extern bool _lpStop;
extern uint32_t _tmTime1;
extern uint32_t _tmTime2;
extern uint32_t _tmTime;
extern uint8_t _timingDay;
extern uint8_t taskCount;
extern uint8_t triggedTask;
void disableTimer();
void _cd_callback();
void _lp_callback();
void timingHandle(uint8_t cbackData);
#endif
#endif

View File

@@ -0,0 +1,279 @@
#include "BlinkerUtility.h"
#if defined(ESP8266)
extern "C" {
#include <user_interface.h>
}
String macDeviceName()
{
uint8_t mac[6];
char macStr[13] = { 0 };
#define STATION_IF 0x00
wifi_get_macaddr(STATION_IF, mac);
sprintf(macStr, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
String macStr_l = STRING_format(macStr);
//macStr_l.toLowerCase();
//BLINKER_LOG("MACADDR: ", macStr_l);
return macStr_l;
}
#elif defined(ESP32)
#include "esp_wifi.h"
String macDeviceName()
{
uint8_t mac[6];
char macStr[13] = { 0 };
// #define WIFI_IF_STA 0x00
esp_wifi_get_mac(WIFI_IF_STA, mac);
sprintf(macStr, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
String macStr_l = STRING_format(macStr);
//macStr_l.toLowerCase();
//BLINKER_LOG("MACADDR: ", macStr_l);
return macStr_l;
}
#endif
// String deviceLocalIP()
// {
// uint8_t mac[4];
// char macStr[13] = { 0 };
// ips = WiFi.localIP();
// sprintf(macStr, "%d.%d.%d.%d", ips[0], ips[1], ips[2], ips[3]);
// String macStr_l = STRING_format(macStr);
// //macStr_l.toLowerCase();
// //BLINKER_LOG("MACADDR: ", macStr_l);
// return macStr_l;
// }
String STRING_find_string(const String & src, const String & targetStart, const String & targetEnd, uint8_t skipNum) {
int addr_start = src.indexOf(targetStart);
int addr_end;
if (targetEnd.length()) {
addr_end = src.indexOf(targetEnd, addr_start + targetStart.length() + skipNum);
}
else {
addr_end = src.length();
}
if (addr_start == -1 || addr_end == -1) {
return "";
}
else {
return src.substring(addr_start + targetStart.length() + skipNum, addr_end);
}
}
bool STRING_contains_string(const String & src, const String & key)
{
int addr_start = src.indexOf(key);
uint8_t keyLen = key.length();
return key == src.substring(addr_start, addr_start + keyLen);
}
bool STRING_find_string_value(const String & src, String & dst, const String & key)
{
int addr_start = src.indexOf(key);
uint8_t keyLen = key.length();
// BLINKER_LOG("addr_start: ", addr_start);
// BLINKER_LOG("keyLen: ", keyLen);
int addr_end = src.indexOf(STRING_VALUE_END, addr_start + keyLen + STRING_VALUE_SKIP);
// BLINKER_LOG("addr_end: ", addr_end);
if (addr_start == -1 || addr_end == -1) {
return false;
}
else {
dst = src.substring(addr_start + keyLen + STRING_VALUE_SKIP, addr_end);
// BLINKER_LOG("dst: ", dst);
return true;
}
}
int32_t STRING_find_numberic_value(const String & src, const String & key)
{
int addr_start = src.indexOf(key);
uint8_t keyLen = key.length();
if ( key != src.substring(addr_start, addr_start + keyLen)) {
return FIND_KEY_VALUE_FAILED;
}
int addr_end1 = src.indexOf(NUMBERIC_VALUE_END_1, addr_start + keyLen + NUMBERIC_VALUE_SKIP);
int addr_end2 = src.indexOf(NUMBERIC_VALUE_END_2, addr_start + keyLen + NUMBERIC_VALUE_SKIP);
int addr_end = BlinkerMin(addr_end1, addr_end2);
if (addr_end == -1) {
addr_end = BlinkerMax(addr_end1, addr_end2);
}
if (addr_start == -1 || addr_end == -1) {
return FIND_KEY_VALUE_FAILED;
}
else {
String value = src.substring(addr_start + keyLen + NUMBERIC_VALUE_SKIP, addr_end);
return value.toInt();
}
}
float STRING_find_float_value(const String & src, const String & key)
{
int addr_start = src.indexOf(key);
uint8_t keyLen = key.length();
if ( key != src.substring(addr_start, addr_start + keyLen)) {
return FIND_KEY_VALUE_FAILED;
}
int addr_end1 = src.indexOf(NUMBERIC_VALUE_END_1, addr_start + keyLen + NUMBERIC_VALUE_SKIP);
int addr_end2 = src.indexOf(NUMBERIC_VALUE_END_2, addr_start + keyLen + NUMBERIC_VALUE_SKIP);
int addr_end = BlinkerMin(addr_end1, addr_end2);
if (addr_end == -1) {
addr_end = BlinkerMax(addr_end1, addr_end2);
}
if (addr_start == -1 || addr_end == -1) {
return (float)FIND_KEY_VALUE_FAILED;
}
else {
String value = src.substring(addr_start + keyLen + NUMBERIC_VALUE_SKIP, addr_end);
return value.toFloat();
}
}
int32_t STRING_find_array_numberic_value(const String & src, const String & key, uint8_t num)
{
int addr_start = src.indexOf(key);
uint8_t keyLen = key.length();
if ( key != src.substring(addr_start, addr_start + keyLen) ) {
return FIND_KEY_VALUE_FAILED;
}
addr_start = addr_start + keyLen + ARRAY_NUMBERIC_VALUE_SKIP_START;
if (num > 0) {
int value_start1, value_start2, temp;
for (uint8_t times = 0; times < num; ++times) {
value_start1 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_1, addr_start);
value_start2 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_2, addr_start);
temp = BlinkerMin(value_start1, value_start2);
if(temp == -1) {
temp = BlinkerMax(value_start1, value_start2);
}
addr_start = temp + ARRAY_NUMBERIC_VALUE_SKIP_IN;
}
}
int addr_end1 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_1, addr_start);
int addr_end2 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_2, addr_start);
int addr_end = BlinkerMin(addr_end1, addr_end2);
if (addr_end == -1) {
addr_end = BlinkerMax(addr_end1, addr_end2);
}
if (addr_start == -1 || addr_end == -1) {
return FIND_KEY_VALUE_FAILED;
}
else {
String value = src.substring(addr_start, addr_end);
return value.toInt();
}
}
float STRING_find_array_float_value(const String & src, const String & key, uint8_t num)
{
int addr_start = src.indexOf(key);
uint8_t keyLen = key.length();
if ( key != src.substring(addr_start, addr_start + keyLen)) {
return FIND_KEY_VALUE_FAILED;
}
addr_start = addr_start + keyLen + ARRAY_NUMBERIC_VALUE_SKIP_START;
if (num > 0) {
int value_start1, value_start2, temp;
for (uint8_t times = 0; times < num; ++times) {
value_start1 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_1, addr_start);
value_start2 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_2, addr_start);
temp = BlinkerMin(value_start1, value_start2);
if(temp == -1) {
temp = BlinkerMax(value_start1, value_start2);
}
addr_start = temp + ARRAY_NUMBERIC_VALUE_SKIP_IN;
}
}
int addr_end1 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_1, addr_start);
int addr_end2 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_2, addr_start);
int addr_end = BlinkerMin(addr_end1, addr_end2);
if (addr_end == -1) {
addr_end = BlinkerMax(addr_end1, addr_end2);
}
if (addr_start == -1 || addr_end == -1) {
return (float)FIND_KEY_VALUE_FAILED;
}
else {
String value = src.substring(addr_start, addr_end);
return value.toFloat();
}
}
String STRING_find_array_string_value(const String & src, const String & key, uint8_t num)
{
int addr_start = src.indexOf(key);
uint8_t keyLen = key.length();
if ( key != src.substring(addr_start, addr_start + keyLen) ) {
return "";
}
addr_start = addr_start + keyLen + ARRAY_NUMBERIC_VALUE_SKIP_START;
if (num > 0) {
int value_start1, value_start2, temp;
for (uint8_t times = 0; times < num; ++times) {
value_start1 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_1, addr_start);
value_start2 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_2, addr_start);
temp = BlinkerMin(value_start1, value_start2);
if(temp == -1) {
temp = BlinkerMax(value_start1, value_start2);
}
addr_start = temp + ARRAY_NUMBERIC_VALUE_SKIP_IN;
}
}
int addr_end1 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_1, addr_start);
int addr_end2 = src.indexOf(ARRAY_NUMBERIC_VALUE_END_2, addr_start);
int addr_end = BlinkerMin(addr_end1, addr_end2);
if (addr_end == -1) {
addr_end = BlinkerMax(addr_end1, addr_end2);
}
if (addr_start == -1 || addr_end == -1) {
return "";
}
else {
String value = src.substring(addr_start, addr_end);
return value;
}
}

View File

@@ -0,0 +1,194 @@
#ifndef BlinkerUtility_H
#define BlinkerUtility_H
#if defined(ARDUINO)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#endif
#define NUMBERIC_VALUE_SKIP 2
#define ARRAY_NUMBERIC_VALUE_SKIP_START 3
#define ARRAY_NUMBERIC_VALUE_SKIP_IN 1
#define STRING_VALUE_SKIP 3
#define NUMBERIC_VALUE_END_1 ","
#define NUMBERIC_VALUE_END_2 "}"
#define ARRAY_NUMBERIC_VALUE_END_1 ","
#define ARRAY_NUMBERIC_VALUE_END_2 "]"
#define STRING_VALUE_END "\""
#define FIND_KEY_VALUE_FAILED -1000
#if defined(BLINKER_ARDUINOJSON) || defined(BLINKER_PRO) || \
defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP)
#ifndef ARDUINOJSON_VERSION_MAJOR
#include "modules/ArduinoJson/ArduinoJson.h"
#endif
#endif
extern "C" {
// typedef void (*callbackFunction)(void);
typedef void (*blinker_callback_t)(void);
typedef void (*blinker_callback_with_arg_t)(void*);
// typedef void (*blinker_callback_with_char_arg_t)(char*);
typedef void (*blinker_callback_with_string_arg_t)(const String & data);
typedef void (*blinker_callback_with_string_uint8_arg_t)(const String & data, uint8_t num);
typedef void (*blinker_callback_with_string_string_arg_t)(const String & data, const String & state);
typedef void (*blinker_callback_with_uint8_arg_t)(uint8_t data);
typedef void (*blinker_callback_with_int32_arg_t)(int32_t data);
typedef void (*blinker_callback_with_int32_uint8_arg_t)(int32_t data, uint8_t num);
typedef void (*blinker_callback_with_rgb_arg_t)(uint8_t r_data, uint8_t g_data, uint8_t b_data, uint8_t bright_data);
typedef void (*blinker_callback_with_joy_arg_t)(uint8_t x_data, uint8_t y_data);
typedef void (*blinker_callback_with_table_arg_t)(uint8_t data);
typedef String (*blinker_callback_return_string_t)(void);
#if defined(BLINKER_PRO) || defined(BLINKER_PRO_SIM7020) || \
defined(BLINKER_PRO_AIR202) || defined(BLINKER_MQTT_AUTO) || \
defined(BLINKER_PRO_ESP) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_WIFI_SUBDEVICE)
typedef bool (*blinker_callback_with_json_arg_t)(const JsonObject & data);
#endif
typedef int (*blinker_callback_return_int_t)(void);
// typedef void (*callback_with_bool_arg_t)(bool state);
}
// enum blinker_at_aligenie_t
// {
// ALI_NONE,
// ALI_LIGHT,
// ALI_OUTLET,
// ALI_SENSOR
// };
#if defined(BLINKER_MQTT) || defined(BLINKER_PRO) || \
defined(BLINKER_AT_MQTT) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_NBIOT_SIM7020) || defined(BLINKER_GPRS_AIR202) || \
defined(BLINKER_PRO_SIM7020) || defined(BLINKER_PRO_AIR202) || \
defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP) || \
defined(BLINKER_WIFI_SUBDEVICE) || defined(BLINKER_QRCODE_NBIOT_SIM7020) || \
defined(BLINKER_NBIOT_SIM7000) || defined(BLINKER_QRCODE_NBIOT_SIM7000)
class BlinkerSharer
{
public :
BlinkerSharer(const String & _uuid)
{
name = (char*)malloc((_uuid.length()+1)*sizeof(char));
strcpy(name, _uuid.c_str());
}
char * uuid() { return name; }
private :
char * name;
};
#if defined(BLINKER_WIFI_GATEWAY)
class BlinkerMeshSub
{
public :
BlinkerMeshSub(uint32_t nodeId)
{
_id = nodeId;
_authState = false;
_new = true;
}
bool isNew() { return _new; }
void state(bool st) { _new = st; }
void auth(const String & name, const String & key,
const String & type, uint16_t vas)
{
_new = false;
_name = (char*)malloc((name.length()+1)*sizeof(char));
strcpy(_name, name.c_str());
_key = (char*)malloc((key.length()+1)*sizeof(char));
strcpy(_key, key.c_str());
_type = (char*)malloc((type.length()+1)*sizeof(char));
strcpy(_type, type.c_str());
_vas = vas;
BLINKER_LOG_ALL("auth msg, name: ", _name,
", key: ", _key,
", type: ", _type,
", vas: ", _vas);
}
void authData(const String & key, const String & name)
{
_auth = (char*)malloc((key.length()+1)*sizeof(char));
strcpy(_auth, key.c_str());
_dId = (char*)malloc((name.length()+1)*sizeof(char));
strcpy(_dId, name.c_str());
BLINKER_LOG_ALL("register msg, authKey: ", _auth,
", deviceId: ", _dId);
_authState = true;
}
void freshAuth(bool authState) { _authState = authState; }
bool isAuth() { return _authState; }
char *name() { return _name; }
char *key() { return _key; }
char *type() { return _type; }
char *authKey() { return _auth; }
String deviceName() { return _dId; }
uint32_t id() { return _id; }
private :
uint32_t _id;
bool _authState;
bool _new;
char *_name;
char *_key;
char *_type;
uint16_t _vas = 0;
char *_auth;
char *_dId;
};
#endif
#endif
template<class T>
String STRING_format(T p) { return String(p); }
#if defined(ESP8266) || defined(ESP32)
String macDeviceName();
// #elif defined(ESP32)
// String macDeviceName();
#endif
template<class T>
const T& BlinkerMin(const T& a, const T& b) { return (b < a) ? b : a; }
template<class T>
const T& BlinkerMax(const T& a, const T& b) { return (b < a) ? a : b; }
String STRING_find_string(const String & src, const String & targetStart, const String & targetEnd, uint8_t skipNum);
bool STRING_contains_string(const String & src, const String & key);
bool STRING_find_string_value(const String & src, String & dst, const String & key);
int32_t STRING_find_numberic_value(const String & src, const String & key);
float STRING_find_float_value(const String & src, const String & key);
int32_t STRING_find_array_numberic_value(const String & src, const String & key, uint8_t num);
float STRING_find_array_float_value(const String & src, const String & key, uint8_t num);
String STRING_find_array_string_value(const String & src, const String & key, uint8_t num);
#endif

View File

@@ -0,0 +1,530 @@
#ifndef BLINKER_ASSISTANT_H
#define BLINKER_ASSISTANT_H
#if defined(BLINKER_ALIGENIE_LIGHT)
#if defined(BLINKER_ALIGENIE_OUTLET)
#undef BLINKER_ALIGENIE_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_MULTI_OUTLET)
#undef BLINKER_ALIGENIE_MULTI_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_SWITCH)
#undef BLINKER_ALIGENIE_SWITCH
#endif
#if defined(BLINKER_ALIGENIE_SENSOR)
#undef BLINKER_ALIGENIE_SENSOR
#endif
#if defined(BLINKER_ALIGENIE_FAN)
#undef BLINKER_ALIGENIE_FAN
#endif
#if defined(BLINKER_ALIGENIE_AIRCONDITION)
#undef BLINKER_ALIGENIE_AIRCONDITION
#endif
#define BLINKER_ALIGENIE
#elif defined(BLINKER_ALIGENIE_OUTLET)
#if defined(BLINKER_ALIGENIE_MULTI_OUTLET)
#undef BLINKER_ALIGENIE_MULTI_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_LIGHT)
#undef BLINKER_ALIGENIE_LIGHT
#endif
#if defined(BLINKER_ALIGENIE_SWITCH)
#undef BLINKER_ALIGENIE_SWITCH
#endif
#if defined(BLINKER_ALIGENIE_SENSOR)
#undef BLINKER_ALIGENIE_SENSOR
#endif
#if defined(BLINKER_ALIGENIE_FAN)
#undef BLINKER_ALIGENIE_FAN
#endif
#if defined(BLINKER_ALIGENIE_AIRCONDITION)
#undef BLINKER_ALIGENIE_AIRCONDITION
#endif
#define BLINKER_ALIGENIE
#elif defined(BLINKER_ALIGENIE_MULTI_OUTLET)
#if defined(BLINKER_ALIGENIE_OUTLET)
#undef BLINKER_ALIGENIE_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_LIGHT)
#undef BLINKER_ALIGENIE_LIGHT
#endif
#if defined(BLINKER_ALIGENIE_SWITCH)
#undef BLINKER_ALIGENIE_SWITCH
#endif
#if defined(BLINKER_ALIGENIE_SENSOR)
#undef BLINKER_ALIGENIE_SENSOR
#endif
#if defined(BLINKER_ALIGENIE_FAN)
#undef BLINKER_ALIGENIE_FAN
#endif
#if defined(BLINKER_ALIGENIE_AIRCONDITION)
#undef BLINKER_ALIGENIE_AIRCONDITION
#endif
#define BLINKER_ALIGENIE
#elif defined(BLINKER_ALIGENIE_SWITCH)
#if defined(BLINKER_ALIGENIE_LIGHT)
#undef BLINKER_ALIGENIE_LIGHT
#endif
#if defined(BLINKER_ALIGENIE_OUTLET)
#undef BLINKER_ALIGENIE_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_MULTI_OUTLET)
#undef BLINKER_ALIGENIE_MULTI_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_SENSOR)
#undef BLINKER_ALIGENIE_SENSOR
#endif
#if defined(BLINKER_ALIGENIE_FAN)
#undef BLINKER_ALIGENIE_FAN
#endif
#if defined(BLINKER_ALIGENIE_AIRCONDITION)
#undef BLINKER_ALIGENIE_AIRCONDITION
#endif
#define BLINKER_ALIGENIE
#elif defined(BLINKER_ALIGENIE_SENSOR)
#if defined(BLINKER_ALIGENIE_LIGHT)
#undef BLINKER_ALIGENIE_LIGHT
#endif
#if defined(BLINKER_ALIGENIE_OUTLET)
#undef BLINKER_ALIGENIE_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_MULTI_OUTLET)
#undef BLINKER_ALIGENIE_MULTI_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_SWITCH)
#undef BLINKER_ALIGENIE_SWITCH
#endif
#if defined(BLINKER_ALIGENIE_FAN)
#undef BLINKER_ALIGENIE_FAN
#endif
#if defined(BLINKER_ALIGENIE_AIRCONDITION)
#undef BLINKER_ALIGENIE_AIRCONDITION
#endif
#define BLINKER_ALIGENIE
#elif defined(BLINKER_ALIGENIE_FAN)
#if defined(BLINKER_ALIGENIE_LIGHT)
#undef BLINKER_ALIGENIE_LIGHT
#endif
#if defined(BLINKER_ALIGENIE_OUTLET)
#undef BLINKER_ALIGENIE_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_MULTI_OUTLET)
#undef BLINKER_ALIGENIE_MULTI_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_SWITCH)
#undef BLINKER_ALIGENIE_SWITCH
#endif
#if defined(BLINKER_ALIGENIE_SENSOR)
#undef BLINKER_ALIGENIE_SENSOR
#endif
#if defined(BLINKER_ALIGENIE_AIRCONDITION)
#undef BLINKER_ALIGENIE_AIRCONDITION
#endif
#define BLINKER_ALIGENIE
#elif defined(BLINKER_ALIGENIE_AIRCONDITION)
#if defined(BLINKER_ALIGENIE_LIGHT)
#undef BLINKER_ALIGENIE_LIGHT
#endif
#if defined(BLINKER_ALIGENIE_OUTLET)
#undef BLINKER_ALIGENIE_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_MULTI_OUTLET)
#undef BLINKER_ALIGENIE_MULTI_OUTLET
#endif
#if defined(BLINKER_ALIGENIE_SWITCH)
#undef BLINKER_ALIGENIE_SWITCH
#endif
#if defined(BLINKER_ALIGENIE_SENSOR)
#undef BLINKER_ALIGENIE_SENSOR
#endif
#if defined(BLINKER_ALIGENIE_FAN)
#undef BLINKER_ALIGENIE_FAN
#endif
#define BLINKER_ALIGENIE
#endif
#if defined(BLINKER_DUEROS_LIGHT)
#if defined(BLINKER_DUEROS_OUTLET)
#undef BLINKER_DUEROS_OUTLET
#endif
#if defined(BLINKER_DUEROS_MULTI_OUTLET)
#undef BLINKER_DUEROS_MULTI_OUTLET
#endif
#if defined(BLINKER_DUEROS_SWITCH)
#undef BLINKER_DUEROS_SWITCH
#endif
#if defined(BLINKER_DUEROS_SENSOR)
#undef BLINKER_DUEROS_SENSOR
#endif
#if defined(BLINKER_DUEROS_FAN)
#undef BLINKER_DUEROS_FAN
#endif
#if defined(BLINKER_DUEROS_AIRCONDITION)
#undef BLINKER_DUEROS_AIRCONDITION
#endif
#define BLINKER_DUEROS
#elif defined(BLINKER_DUEROS_OUTLET)
#if defined(BLINKER_DUEROS_MULTI_OUTLET)
#undef BLINKER_DUEROS_MULTI_OUTLET
#endif
#if defined(BLINKER_DUEROS_LIGHT)
#undef BLINKER_DUEROS_LIGHT
#endif
#if defined(BLINKER_DUEROS_SWITCH)
#undef BLINKER_DUEROS_SWITCH
#endif
#if defined(BLINKER_DUEROS_SENSOR)
#undef BLINKER_DUEROS_SENSOR
#endif
#if defined(BLINKER_DUEROS_FAN)
#undef BLINKER_DUEROS_FAN
#endif
#if defined(BLINKER_DUEROS_AIRCONDITION)
#undef BLINKER_DUEROS_AIRCONDITION
#endif
#define BLINKER_DUEROS
#elif defined(BLINKER_DUEROS_MULTI_OUTLET)
#if defined(BLINKER_DUEROS_OUTLET)
#undef BLINKER_DUEROS_OUTLET
#endif
#if defined(BLINKER_DUEROS_LIGHT)
#undef BLINKER_DUEROS_LIGHT
#endif
#if defined(BLINKER_DUEROS_SWITCH)
#undef BLINKER_DUEROS_SWITCH
#endif
#if defined(BLINKER_DUEROS_SENSOR)
#undef BLINKER_DUEROS_SENSOR
#endif
#if defined(BLINKER_DUEROS_FAN)
#undef BLINKER_DUEROS_FAN
#endif
#if defined(BLINKER_DUEROS_AIRCONDITION)
#undef BLINKER_DUEROS_AIRCONDITION
#endif
#define BLINKER_DUEROS
#elif defined(BLINKER_DUEROS_SENSOR)
#if defined(BLINKER_DUEROS_LIGHT)
#undef BLINKER_DUEROS_LIGHT
#endif
#if defined(BLINKER_DUEROS_SWITCH)
#undef BLINKER_DUEROS_SWITCH
#endif
#if defined(BLINKER_DUEROS_OUTLET)
#undef BLINKER_DUEROS_OUTLET
#endif
#if defined(BLINKER_DUEROS_MULTI_OUTLET)
#undef BLINKER_DUEROS_MULTI_OUTLET
#endif
#if defined(BLINKER_DUEROS_FAN)
#undef BLINKER_DUEROS_FAN
#endif
#if defined(BLINKER_DUEROS_AIRCONDITION)
#undef BLINKER_DUEROS_AIRCONDITION
#endif
#define BLINKER_DUEROS
#elif defined(BLINKER_DUEROS_FAN)
#if defined(BLINKER_DUEROS_LIGHT)
#undef BLINKER_DUEROS_LIGHT
#endif
#if defined(BLINKER_DUEROS_SWITCH)
#undef BLINKER_DUEROS_SWITCH
#endif
#if defined(BLINKER_DUEROS_OUTLET)
#undef BLINKER_DUEROS_OUTLET
#endif
#if defined(BLINKER_DUEROS_MULTI_OUTLET)
#undef BLINKER_DUEROS_MULTI_OUTLET
#endif
#if defined(BLINKER_DUEROS_SENSOR)
#undef BLINKER_DUEROS_SENSOR
#endif
#if defined(BLINKER_DUEROS_AIRCONDITION)
#undef BLINKER_DUEROS_AIRCONDITION
#endif
#define BLINKER_DUEROS
#elif defined(BLINKER_DUEROS_AIRCONDITION)
#if defined(BLINKER_DUEROS_LIGHT)
#undef BLINKER_DUEROS_LIGHT
#endif
#if defined(BLINKER_DUEROS_SWITCH)
#undef BLINKER_DUEROS_SWITCH
#endif
#if defined(BLINKER_DUEROS_OUTLET)
#undef BLINKER_DUEROS_OUTLET
#endif
#if defined(BLINKER_DUEROS_MULTI_OUTLET)
#undef BLINKER_DUEROS_MULTI_OUTLET
#endif
#if defined(BLINKER_DUEROS_SENSOR)
#undef BLINKER_DUEROS_SENSOR
#endif
#if defined(BLINKER_DUEROS_FAN)
#undef BLINKER_DUEROS_FAN
#endif
#define BLINKER_DUEROS
#endif
#if defined(BLINKER_MIOT_LIGHT)
#if defined(BLINKER_MIOT_OUTLET)
#undef BLINKER_MIOT_OUTLET
#endif
#if defined(BLINKER_MIOT_MULTI_OUTLET)
#undef BLINKER_MIOT_MULTI_OUTLET
#endif
#if defined(BLINKER_MIOT_SWITCH)
#undef BLINKER_MIOT_SWITCH
#endif
#if defined(BLINKER_MIOT_SENSOR)
#undef BLINKER_MIOT_SENSOR
#endif
#if defined(BLINKER_MIOT_FAN)
#undef BLINKER_MIOT_FAN
#endif
#if defined(BLINKER_MIOT_AIRCONDITION)
#undef BLINKER_MIOT_AIRCONDITION
#endif
#define BLINKER_MIOT
#elif defined(BLINKER_MIOT_OUTLET)
#if defined(BLINKER_MIOT_MULTI_OUTLET)
#undef BLINKER_MIOT_MULTI_OUTLET
#endif
#if defined(BLINKER_MIOT_LIGHT)
#undef BLINKER_MIOT_LIGHT
#endif
#if defined(BLINKER_MIOT_SWITCH)
#undef BLINKER_MIOT_SWITCH
#endif
#if defined(BLINKER_MIOT_SENSOR)
#undef BLINKER_MIOT_SENSOR
#endif
#if defined(BLINKER_MIOT_FAN)
#undef BLINKER_MIOT_FAN
#endif
#if defined(BLINKER_MIOT_AIRCONDITION)
#undef BLINKER_MIOT_AIRCONDITION
#endif
#define BLINKER_MIOT
#elif defined(BLINKER_MIOT_MULTI_OUTLET)
#if defined(BLINKER_MIOT_OUTLET)
#undef BLINKER_MIOT_OUTLET
#endif
#if defined(BLINKER_MIOT_LIGHT)
#undef BLINKER_MIOT_LIGHT
#endif
#if defined(BLINKER_MIOT_SWITCH)
#undef BLINKER_MIOT_SWITCH
#endif
#if defined(BLINKER_MIOT_SENSOR)
#undef BLINKER_MIOT_SENSOR
#endif
#if defined(BLINKER_MIOT_FAN)
#undef BLINKER_MIOT_FAN
#endif
#if defined(BLINKER_MIOT_AIRCONDITION)
#undef BLINKER_MIOT_AIRCONDITION
#endif
#define BLINKER_MIOT
#elif defined(BLINKER_MIOT_SWITCH)
#if defined(BLINKER_MIOT_LIGHT)
#undef BLINKER_MIOT_LIGHT
#endif
#if defined(BLINKER_MIOT_OUTLET)
#undef BLINKER_MIOT_OUTLET
#endif
#if defined(BLINKER_MIOT_MULTI_OUTLET)
#undef BLINKER_MIOT_MULTI_OUTLET
#endif
#if defined(BLINKER_MIOT_SENSOR)
#undef BLINKER_MIOT_SENSOR
#endif
#if defined(BLINKER_MIOT_FAN)
#undef BLINKER_MIOT_FAN
#endif
#if defined(BLINKER_MIOT_AIRCONDITION)
#undef BLINKER_MIOT_AIRCONDITION
#endif
#define BLINKER_MIOT
#elif defined(BLINKER_MIOT_SENSOR)
#if defined(BLINKER_MIOT_LIGHT)
#undef BLINKER_MIOT_LIGHT
#endif
#if defined(BLINKER_MIOT_OUTLET)
#undef BLINKER_MIOT_OUTLET
#endif
#if defined(BLINKER_MIOT_MULTI_OUTLET)
#undef BLINKER_MIOT_MULTI_OUTLET
#endif
#if defined(BLINKER_MIOT_SWITCH)
#undef BLINKER_MIOT_SWITCH
#endif
#if defined(BLINKER_MIOT_FAN)
#undef BLINKER_MIOT_FAN
#endif
#if defined(BLINKER_MIOT_AIRCONDITION)
#undef BLINKER_MIOT_AIRCONDITION
#endif
#define BLINKER_MIOT
#elif defined(BLINKER_MIOT_FAN)
#if defined(BLINKER_MIOT_LIGHT)
#undef BLINKER_MIOT_LIGHT
#endif
#if defined(BLINKER_MIOT_OUTLET)
#undef BLINKER_MIOT_OUTLET
#endif
#if defined(BLINKER_MIOT_MULTI_OUTLET)
#undef BLINKER_MIOT_MULTI_OUTLET
#endif
#if defined(BLINKER_MIOT_SWITCH)
#undef BLINKER_MIOT_SWITCH
#endif
#if defined(BLINKER_MIOT_SENSOR)
#undef BLINKER_MIOT_SENSOR
#endif
#if defined(BLINKER_MIOT_AIRCONDITION)
#undef BLINKER_MIOT_AIRCONDITION
#endif
#define BLINKER_MIOT
#elif defined(BLINKER_MIOT_AIRCONDITION)
#if defined(BLINKER_MIOT_LIGHT)
#undef BLINKER_MIOT_LIGHT
#endif
#if defined(BLINKER_MIOT_OUTLET)
#undef BLINKER_MIOT_OUTLET
#endif
#if defined(BLINKER_MIOT_MULTI_OUTLET)
#undef BLINKER_MIOT_MULTI_OUTLET
#endif
#if defined(BLINKER_MIOT_SWITCH)
#undef BLINKER_MIOT_SWITCH
#endif
#if defined(BLINKER_MIOT_SENSOR)
#undef BLINKER_MIOT_SENSOR
#endif
#if defined(BLINKER_MIOT_FAN)
#undef BLINKER_MIOT_FAN
#endif
#define BLINKER_MIOT
#endif
#endif

View File

@@ -0,0 +1,32 @@
#ifndef BLINKER_ESP32_BLE_H
#define BLINKER_ESP32_BLE_H
#if defined(ESP32)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerBLE.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerESP32BLE : public BlinkerApi
{
public :
void begin()
{
BApi::begin();
Transp.begin();
transport(Transp);
BLINKER_LOG("ESP32_BLE initialized...");
}
private :
BlinkerBLE Transp;
};
#endif
#endif

View File

@@ -0,0 +1,32 @@
#ifndef BLINKER_ESP_GATEWAY_H
#define BLINKER_ESP_GATEWAY_H
#if defined(ESP8266) || defined(ESP32)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerGateway.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerESPGateway : public BlinkerApi
{
public :
void begin(const char* _key, const char* _type)
{
transport(Transp);
BApi::begin(_key, _type);
BApi::loadTimer();
BLINKER_LOG(BLINKER_F("ESP_Gateway initialized..."));
}
private :
BlinkerGateway Transp;
};
#endif
#endif

View File

@@ -0,0 +1,65 @@
#ifndef BLINKER_ESP_HTTP_H
#define BLINKER_ESP_HTTP_H
#if defined(ESP8266) || defined(ESP32)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerHTTP.h"
#include "Blinker/BlinkerApi.h"
#ifndef ARDUINOJSON_VERSION_MAJOR
#include "modules/ArduinoJson/ArduinoJson.h"
#endif
#if defined(ESP8266)
#include <ESP8266mDNS.h>
#include <ESP8266WiFi.h>
// #include <ESP8266WebServer.h>
#elif defined(ESP32)
#include <ESPmDNS.h>
#include <WiFi.h>
// #include <WebServer.h>
#endif
typedef BlinkerApi BApi;
class BlinkerESPHTTP : public BlinkerApi
{
public :
#if defined(BLINKER_ESP_SMARTCONFIG) || defined(BLINKER_APCONFIG)
void begin(const char* _auth)
{
BApi::begin();
Transp.begin(_auth);
transport(Transp);
#if defined(BLINKER_ESP_SMARTCONFIG)
Transp.smartconfigBegin();
#elif defined(BLINKER_APCONFIG)
Transp.apconfigBegin();
#endif
}
#else
void begin( const char* _auth,
const char* _ssid,
const char* _pswd )
{
BApi::begin();
Transp.begin(_auth);
transport(Transp);
Transp.commonBegin(_ssid, _pswd);
}
#endif
private :
BlinkerHTTP Transp;
};
#endif
#endif

View File

@@ -0,0 +1,683 @@
#ifndef BLINKER_ESP_MQTT_H
#define BLINKER_ESP_MQTT_H
#if defined(ESP8266) || defined(ESP32)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#if defined(ESP8266)
#ifndef BLINKER_WITH_SSL
#define BLINKER_WITHOUT_SSL
#endif
#endif
#include "Adapters/BlinkerMQTT.h"
#include "Blinker/BlinkerApi.h"
#ifndef ARDUINOJSON_VERSION_MAJOR
#include "modules/ArduinoJson/ArduinoJson.h"
#endif
#if defined(ESP8266)
#include <ESP8266mDNS.h>
#include <ESP8266WiFi.h>
// #include <ESP8266WebServer.h>
#elif defined(ESP32)
#include <ESPmDNS.h>
#include <WiFi.h>
// #include <WebServer.h>
#endif
typedef BlinkerApi BApi;
class BlinkerESPMQTT : public BlinkerApi
{
public :
#if defined(BLINKER_ESP_SMARTCONFIG) || defined(BLINKER_APCONFIG)
void begin(const char* _auth)
{
#if defined(BLINKER_ALIGENIE_LIGHT)
String _aliType = BLINKER_F("&aliType=light");
#elif defined(BLINKER_ALIGENIE_OUTLET)
String _aliType = BLINKER_F("&aliType=outlet");
#elif defined(BLINKER_ALIGENIE_MULTI_OUTLET)
String _aliType = BLINKER_F("&aliType=multi_outlet");
#elif defined(BLINKER_ALIGENIE_SENSOR)
String _aliType = BLINKER_F("&aliType=sensor");
#elif defined(BLINKER_ALIGENIE_FAN)
String _aliType = BLINKER_F("&aliType=fan");
#elif defined(BLINKER_ALIGENIE_AIRCONDITION)
String _aliType = BLINKER_F("&aliType=aircondition");
#else
String _aliType = BLINKER_F("");
#endif
#if defined(BLINKER_DUEROS_LIGHT)
String _duerType = BLINKER_F("&duerType=LIGHT");
#elif defined(BLINKER_DUEROS_OUTLET)
String _duerType = BLINKER_F("&duerType=SOCKET");
#elif defined(BLINKER_DUEROS_MULTI_OUTLET)
String _duerType = BLINKER_F("&duerType=MULTI_SOCKET");
#elif defined(BLINKER_DUEROS_SENSOR)
String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
#elif defined(BLINKER_DUEROS_FAN)
String _duerType = BLINKER_F("&duerType=FAN");
#elif defined(BLINKER_DUEROS_AIRCONDITION)
String _duerType = BLINKER_F("&duerType=AIR_CONDITION");
#else
String _duerType = BLINKER_F("");
#endif
#if defined(BLINKER_MIOT_LIGHT)
String _miType = BLINKER_F("&miType=light");
#elif defined(BLINKER_MIOT_OUTLET)
String _miType = BLINKER_F("&miType=outlet");
#elif defined(BLINKER_MIOT_MULTI_OUTLET)
String _miType = BLINKER_F("&miType=multi_outlet");
#elif defined(BLINKER_MIOT_SENSOR)
String _miType = BLINKER_F("&miType=sensor");
#elif defined(BLINKER_MIOT_FAN)
String _miType = BLINKER_F("&miType=fan");
#elif defined(BLINKER_MIOT_AIRCONDITION)
String _miType = BLINKER_F("&miType=aircondition");
#else
String _miType = BLINKER_F("");
#endif
BApi::begin();
Transp.aliType(_aliType);
Transp.duerType(_duerType);
Transp.miType(_miType);
Transp.begin(_auth);
transport(Transp);
#if defined(BLINKER_ESP_SMARTCONFIG)
Transp.smartconfigBegin();
#elif defined(BLINKER_APCONFIG)
Transp.apconfigBegin();
#endif
BApi::loadTimer();
// #if defined(BLINKER_ESP_SMARTCONFIG)
// smartconfigBegin(_auth, _aliType, _duerType);
// #elif defined(BLINKER_APCONFIG)
// apconfigBegin(_auth, _aliType, _duerType);
// #endif
// __auth = _auth;
// #ifndef BLINKER_ESP_TASK
// beginMQTT();
// #endif
}
#elif defined(BLINKER_APCONFIG_V2)
void begin()
{
#if defined(BLINKER_ALIGENIE_LIGHT)
String _aliType = BLINKER_F("&aliType=light");
#elif defined(BLINKER_ALIGENIE_OUTLET)
String _aliType = BLINKER_F("&aliType=outlet");
#elif defined(BLINKER_ALIGENIE_MULTI_OUTLET)
String _aliType = BLINKER_F("&aliType=multi_outlet");
#elif defined(BLINKER_ALIGENIE_SENSOR)
String _aliType = BLINKER_F("&aliType=sensor");
#elif defined(BLINKER_ALIGENIE_FAN)
String _aliType = BLINKER_F("&aliType=fan");
#elif defined(BLINKER_ALIGENIE_AIRCONDITION)
String _aliType = BLINKER_F("&aliType=aircondition");
#else
String _aliType = BLINKER_F("");
#endif
#if defined(BLINKER_DUEROS_LIGHT)
String _duerType = BLINKER_F("&duerType=LIGHT");
#elif defined(BLINKER_DUEROS_OUTLET)
String _duerType = BLINKER_F("&duerType=SOCKET");
#elif defined(BLINKER_DUEROS_MULTI_OUTLET)
String _duerType = BLINKER_F("&duerType=MULTI_SOCKET");
#elif defined(BLINKER_DUEROS_SENSOR)
String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
#elif defined(BLINKER_DUEROS_FAN)
String _duerType = BLINKER_F("&duerType=FAN");
#elif defined(BLINKER_DUEROS_AIRCONDITION)
String _duerType = BLINKER_F("&duerType=AIR_CONDITION");
#else
String _duerType = BLINKER_F("");
#endif
#if defined(BLINKER_MIOT_LIGHT)
String _miType = BLINKER_F("&miType=light");
#elif defined(BLINKER_MIOT_OUTLET)
String _miType = BLINKER_F("&miType=outlet");
#elif defined(BLINKER_MIOT_MULTI_OUTLET)
String _miType = BLINKER_F("&miType=multi_outlet");
#elif defined(BLINKER_MIOT_SENSOR)
String _miType = BLINKER_F("&miType=sensor");
#elif defined(BLINKER_MIOT_FAN)
String _miType = BLINKER_F("&miType=fan");
#elif defined(BLINKER_MIOT_AIRCONDITION)
String _miType = BLINKER_F("&miType=aircondition");
#else
String _miType = BLINKER_F("");
#endif
BApi::begin();
Transp.aliType(_aliType);
Transp.duerType(_duerType);
Transp.miType(_miType);
// Transp.begin(_auth);
transport(Transp);
// #if defined(BLINKER_ESP_SMARTCONFIG)
// Transp.smartconfigBegin();
// #elif defined(BLINKER_APCONFIG)
Transp.apconfigBegin();
// #endif
BApi::loadTimer();
// #if defined(BLINKER_ESP_SMARTCONFIG)
// smartconfigBegin(_auth, _aliType, _duerType);
// #elif defined(BLINKER_APCONFIG)
// apconfigBegin(_auth, _aliType, _duerType);
// #endif
// __auth = _auth;
// #ifndef BLINKER_ESP_TASK
// beginMQTT();
// #endif
}
#else
void begin( const char* _auth,
const char* _ssid,
const char* _pswd )
{
#if defined(BLINKER_ALIGENIE_LIGHT)
String _aliType = BLINKER_F("&aliType=light");
#elif defined(BLINKER_ALIGENIE_OUTLET)
String _aliType = BLINKER_F("&aliType=outlet");
#elif defined(BLINKER_ALIGENIE_MULTI_OUTLET)
String _aliType = BLINKER_F("&aliType=multi_outlet");
#elif defined(BLINKER_ALIGENIE_SENSOR)
String _aliType = BLINKER_F("&aliType=sensor");
#elif defined(BLINKER_ALIGENIE_FAN)
String _aliType = BLINKER_F("&aliType=fan");
#elif defined(BLINKER_ALIGENIE_AIRCONDITION)
String _aliType = BLINKER_F("&aliType=aircondition");
#else
String _aliType = BLINKER_F("");
#endif
#if defined(BLINKER_DUEROS_LIGHT)
String _duerType = BLINKER_F("&duerType=LIGHT");
#elif defined(BLINKER_DUEROS_OUTLET)
String _duerType = BLINKER_F("&duerType=SOCKET");
#elif defined(BLINKER_DUEROS_MULTI_OUTLET)
String _duerType = BLINKER_F("&duerType=MULTI_SOCKET");
#elif defined(BLINKER_DUEROS_SENSOR)
String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
#elif defined(BLINKER_DUEROS_FAN)
String _duerType = BLINKER_F("&duerType=FAN");
#elif defined(BLINKER_DUEROS_AIRCONDITION)
String _duerType = BLINKER_F("&duerType=AIR_CONDITION");
#else
String _duerType = BLINKER_F("");
#endif
#if defined(BLINKER_MIOT_LIGHT)
String _miType = BLINKER_F("&miType=light");
#elif defined(BLINKER_MIOT_OUTLET)
String _miType = BLINKER_F("&miType=outlet");
#elif defined(BLINKER_MIOT_MULTI_OUTLET)
String _miType = BLINKER_F("&miType=multi_outlet");
#elif defined(BLINKER_MIOT_SENSOR)
String _miType = BLINKER_F("&miType=sensor");
#elif defined(BLINKER_MIOT_FAN)
String _miType = BLINKER_F("&miType=fan");
#elif defined(BLINKER_MIOT_AIRCONDITION)
String _miType = BLINKER_F("&miType=aircondition");
#else
String _miType = BLINKER_F("");
#endif
BApi::begin();
Transp.aliType(_aliType);
Transp.duerType(_duerType);
Transp.miType(_miType);
Transp.begin(_auth);
transport(Transp);
#if defined(BLINKER_WIFI_MULTI)
Transp.multiBegin(_ssid, _pswd);
#else
Transp.commonBegin(_ssid, _pswd);
#endif
BApi::loadTimer();
// __auth = _auth;
// __ssid = _ssid;
// __pswd = _pswd;
// #ifndef BLINKER_ESP_TASK
// beginMQTT();
// #endif
}
#endif
#if defined(BLINKER_WIFI_MULTI)
void addAP( const char* _ssid,
const char* _pswd)
{
BLINKER_LOG(BLINKER_F("wifiMulti add "), _ssid);
wifiMulti.addAP(_ssid, _pswd);
}
void existAP( const char* _ssid,
const char* _pswd)
{
BLINKER_LOG(BLINKER_F("wifiMulti existAP "), _ssid);
wifiMulti.existsAP(_ssid, _pswd);
}
void cleanAPlist()
{
wifiMulti.cleanAPlist();
}
#endif
// void beginMQTT()
// {
// #if defined(BLINKER_ALIGENIE_LIGHT)
// String _aliType = BLINKER_F("&aliType=light");
// #elif defined(BLINKER_ALIGENIE_OUTLET)
// String _aliType = BLINKER_F("&aliType=outlet");
// #elif defined(BLINKER_ALIGENIE_SENSOR)
// String _aliType = BLINKER_F("&aliType=sensor");
// #else
// String _aliType = BLINKER_F("");
// #endif
// #if defined(BLINKER_DUEROS_LIGHT)
// String _duerType = BLINKER_F("&duerType=LIGHT");
// #elif defined(BLINKER_DUEROS_OUTLET)
// String _duerType = BLINKER_F("&duerType=SOCKET");
// #elif defined(BLINKER_DUEROS_SENSOR)
// String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
// #else
// String _duerType = BLINKER_F("");
// #endif
// #if defined(BLINKER_ESP_SMARTCONFIG)
// smartconfigBegin(__auth, _aliType, _duerType);
// #elif defined(BLINKER_APCONFIG)
// apconfigBegin(__auth, _aliType, _duerType);
// #else
// commonBegin(__auth, __ssid, __pswd, _aliType, _duerType);
// #endif
// }
private :
// void commonBegin(const char* _auth,
// const char* _ssid,
// const char* _pswd,
// String & _alitype,
// String & _duertype);
// void smartconfigBegin(const char* _auth, String & _alitype, String & _duertype);
// void apconfigBegin(const char* _auth, String & _alitype, String & _duertype);
// bool autoInit();
// void smartconfig();
// void softAPinit();
// // void serverClient();
// bool parseUrl(String data);
// void connectWiFi(String _ssid, String _pswd);
// void connectWiFi(const char* _ssid, const char* _pswd);
// const char* __auth;
// const char* __ssid;
// const char* __pswd;
BlinkerMQTT Transp;
};
// void BlinkerESPMQTT::commonBegin(const char* _auth,
// const char* _ssid,
// const char* _pswd,
// String & _alitype,
// String & _duertype)
// {
// BApi::begin();
// connectWiFi(_ssid, _pswd);
// // BApi::loadOTA();
// Transp.aliType(_alitype);
// Transp.duerType(_duertype);
// Transp.begin(_auth);
// transport(Transp);
// BApi::loadTimer();
// #if defined(ESP8266)
// BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
// #elif defined(ESP32)
// BLINKER_LOG(BLINKER_F("ESP32_MQTT initialized..."));
// #endif
// }
// void BlinkerESPMQTT::smartconfigBegin(const char* _auth, String & _alitype,
// String & _duertype)
// {
// BApi::begin();
// if (!autoInit()) smartconfig();
// // BApi::loadOTA();
// Transp.aliType(_alitype);
// Transp.duerType(_duertype);
// Transp.begin(_auth);
// transport(Transp);
// BApi::loadTimer();
// #if defined(ESP8266)
// BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
// #elif defined(ESP32)
// BLINKER_LOG(BLINKER_F("ESP32_MQTT initialized..."));
// #endif
// }
// void BlinkerESPMQTT::apconfigBegin(const char* _auth, String & _alitype,
// String & _duertype)
// {
// BApi::begin();
// if (!autoInit())
// {
// softAPinit();
// // while(WiFi.status() != WL_CONNECTED)
// // {
// // serverClient();
// // ::delay(10);
// // }
// }
// // BApi::loadOTA();
// Transp.aliType(_alitype);
// Transp.duerType(_duertype);
// Transp.begin(_auth);
// transport(Transp);
// BApi::loadTimer();
// #if defined(ESP8266)
// BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
// #elif defined(ESP32)
// BLINKER_LOG(BLINKER_F("ESP32_MQTT initialized..."));
// #endif
// }
// bool BlinkerESPMQTT::autoInit()
// {
// WiFi.mode(WIFI_STA);
// String _hostname = BLINKER_F("DiyArduino_");
// _hostname += macDeviceName();
// #if defined(ESP8266)
// WiFi.hostname(_hostname.c_str());
// #elif defined(ESP32)
// WiFi.setHostname(_hostname.c_str());
// #endif
// WiFi.begin();
// ::delay(500);
// BLINKER_LOG(BLINKER_F("Waiting for WiFi "),
// BLINKER_WIFI_AUTO_INIT_TIMEOUT / 1000,
// BLINKER_F("s, will enter SMARTCONFIG or "),
// BLINKER_F("APCONFIG while WiFi not connect!"));
// uint8_t _times = 0;
// while (WiFi.status() != WL_CONNECTED) {
// ::delay(500);
// if (_times > BLINKER_WIFI_AUTO_INIT_TIMEOUT / 500) break;
// _times++;
// }
// if (WiFi.status() != WL_CONNECTED) return false;
// else {
// BLINKER_LOG(BLINKER_F("WiFi Connected."));
// BLINKER_LOG(BLINKER_F("IP Address: "));
// BLINKER_LOG(WiFi.localIP());
// return true;
// }
// }
// void BlinkerESPMQTT::smartconfig()
// {
// WiFi.mode(WIFI_STA);
// String _hostname = BLINKER_F("DiyArduino_");
// _hostname += macDeviceName();
// #if defined(ESP8266)
// WiFi.hostname(_hostname.c_str());
// #elif defined(ESP32)
// WiFi.setHostname(_hostname.c_str());
// #endif
// WiFi.beginSmartConfig();
// BLINKER_LOG(BLINKER_F("Waiting for SmartConfig."));
// while (!WiFi.smartConfigDone()) {
// ::delay(500);
// }
// BLINKER_LOG(BLINKER_F("SmartConfig received."));
// BLINKER_LOG(BLINKER_F("Waiting for WiFi"));
// while (WiFi.status() != WL_CONNECTED) {
// ::delay(500);
// }
// BLINKER_LOG(BLINKER_F("WiFi Connected."));
// BLINKER_LOG(BLINKER_F("IP Address: "));
// BLINKER_LOG(WiFi.localIP());
// }
// void BlinkerESPMQTT::softAPinit()
// {
// WiFiServer _server(80);
// WiFiClient _client;
// IPAddress apIP(192, 168, 4, 1);
// #if defined(ESP8266)
// IPAddress netMsk(255, 255, 255, 0);
// #endif
// // _server = new WiFiServer(80);
// WiFi.mode(WIFI_AP);
// String softAP_ssid = BLINKER_F("DiyArduino_");
// softAP_ssid += macDeviceName();
// #if defined(ESP8266)
// WiFi.hostname(softAP_ssid.c_str());
// WiFi.softAPConfig(apIP, apIP, netMsk);
// #elif defined(ESP32)
// WiFi.setHostname(softAP_ssid.c_str());
// WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
// #endif
// WiFi.softAP(softAP_ssid.c_str(), ("12345678"));
// delay(100);
// _server.begin();
// BLINKER_LOG(BLINKER_F("AP IP address: "), WiFi.softAPIP());
// BLINKER_LOG(BLINKER_F("HTTP _server started"));
// BLINKER_LOG(BLINKER_F("URL: http://"), WiFi.softAPIP());
// while(WiFi.status() != WL_CONNECTED)
// {
// // serverClient();
// _client = _server.available();
// // if (_client.status() == CLOSED)
// if (!_client.connected())
// {
// _client.stop();
// BLINKER_LOG(BLINKER_F("Connection closed on _client"));
// }
// else
// {
// if (_client.available())
// {
// String data = _client.readStringUntil('\r');
// // data = data.substring(4, data.length() - 9);
// _client.flush();
// BLINKER_LOG(BLINKER_F("clientData: "), data);
// if (STRING_contains_string(data, "ssid") &&
// STRING_contains_string(data, "pswd"))
// {
// String msg = BLINKER_F("{\"hello\":\"world\"}");
// String s= BLINKER_F("HTTP/1.1 200 OK\r\n");
// s += BLINKER_F("Content-Type: application/json;");
// s += BLINKER_F("charset=utf-8\r\n");
// s += BLINKER_F("Content-Length: ");
// s += String(msg.length());
// s += BLINKER_F("\r\nConnection: Keep Alive\r\n\r\n");
// s += msg;
// s += BLINKER_F("\r\n");
// _client.print(s);
// _client.stop();
// parseUrl(data);
// }
// }
// }
// ::delay(10);
// }
// }
// // void BlinkerESPMQTT::serverClient()
// // {
// // if (!_client)
// // {
// // _client = _server->available();
// // }
// // else
// // {
// // // if (_client.status() == CLOSED)
// // if (!_client.connected())
// // {
// // _client.stop();
// // BLINKER_LOG(BLINKER_F("Connection closed on _client"));
// // }
// // else
// // {
// // if (_client.available())
// // {
// // String data = _client.readStringUntil('\r');
// // // data = data.substring(4, data.length() - 9);
// // _client.flush();
// // BLINKER_LOG(BLINKER_F("clientData: "), data);
// // if (STRING_contains_string(data, "ssid") &&
// // STRING_contains_string(data, "pswd"))
// // {
// // String msg = BLINKER_F("{\"hello\":\"world\"}");
// // String s= BLINKER_F("HTTP/1.1 200 OK\r\n");
// // s += BLINKER_F("Content-Type: application/json;");
// // s += BLINKER_F("charset=utf-8\r\n");
// // s += BLINKER_F("Content-Length: ");
// // s += String(msg.length());
// // s += BLINKER_F("\r\nConnection: Keep Alive\r\n\r\n");
// // s += msg;
// // s += BLINKER_F("\r\n");
// // _client.print(s);
// // _client.stop();
// // parseUrl(data);
// // }
// // }
// // }
// // }
// // }
// bool BlinkerESPMQTT::parseUrl(String data)
// {
// BLINKER_LOG(BLINKER_F("APCONFIG data: "), data);
// DynamicJsonBuffer jsonBuffer;
// JsonObject& wifi_data = jsonBuffer.parseObject(data);
// if (!wifi_data.success()) {
// return false;
// }
// String _ssid = wifi_data["ssid"];
// String _pswd = wifi_data["pswd"];
// BLINKER_LOG(BLINKER_F("ssid: "), _ssid);
// BLINKER_LOG(BLINKER_F("pswd: "), _pswd);
// // free(_server);
// connectWiFi(_ssid, _pswd);
// return true;
// }
// void BlinkerESPMQTT::connectWiFi(String _ssid, String _pswd)
// {
// connectWiFi(_ssid.c_str(), _pswd.c_str());
// }
// void BlinkerESPMQTT::connectWiFi(const char* _ssid, const char* _pswd)
// {
// uint32_t connectTime = millis();
// BLINKER_LOG(BLINKER_F("Connecting to "), _ssid);
// WiFi.mode(WIFI_STA);
// String _hostname = BLINKER_F("DiyArduinoMQTT_");
// _hostname += macDeviceName();
// #if defined(ESP8266)
// WiFi.hostname(_hostname.c_str());
// #elif defined(ESP32)
// WiFi.setHostname(_hostname.c_str());
// #endif
// if (_pswd && strlen(_pswd)) {
// WiFi.begin(_ssid, _pswd);
// }
// else {
// WiFi.begin(_ssid);
// }
// while (WiFi.status() != WL_CONNECTED) {
// ::delay(50);
// if (millis() - connectTime > BLINKER_CONNECT_TIMEOUT_MS && WiFi.status() != WL_CONNECTED) {
// connectTime = millis();
// BLINKER_LOG(BLINKER_F("WiFi connect timeout, please check ssid and pswd!"));
// BLINKER_LOG(BLINKER_F("Retring WiFi connect again!"));
// }
// }
// BLINKER_LOG(BLINKER_F("Connected"));
// IPAddress myip = WiFi.localIP();
// BLINKER_LOG(BLINKER_F("Your IP is: "), myip);
// }
#endif
#endif

View File

@@ -0,0 +1,62 @@
#ifndef BLINKER_ESP_MQTT_AT_H
#define BLINKER_ESP_MQTT_AT_H
#if defined(ESP8266) || defined(ESP32)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerMQTTAT.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerESPMQTTAT : public BlinkerApi
{
public :
void begin()
{
BApi::begin();
::delay(100);
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.get(BLINKER_EEP_ADDR_SERIALCFG, serialSet);
uint32_t ss_baud = serialSet >> 8 & 0x00FFFFFF;
ss_cfg = serConfig();
if (ss_baud != 300 || ss_baud != 1200 || ss_baud != 2400 ||
ss_baud != 4800 || ss_baud != 9600 || ss_baud != 19200 ||
ss_baud != 38400 || ss_baud != 57600 || ss_baud != 74880 ||
ss_baud != 115200 || ss_baud != 230400 || ss_baud != 250000 ||
ss_baud != 500000 || ss_baud != 1000000 || ss_baud != 2000000)
{
serialSet = BLINKER_SERIAL_DEFAULT;
ss_baud = 9600;
ss_cfg = SERIAL_8N1;
EEPROM.put(BLINKER_EEP_ADDR_SERIALCFG, serialSet);
}
EEPROM.commit();
EEPROM.end();
Serial.begin(ss_baud, ss_cfg);
Transp.serialBegin(Serial, true);
transport(Transp);
BApi::atBegin();
// strcpy(BApi::_authKey, this->conn.authKey().c_str());
// strcpy(BApi::_deviceName, this->conn.deviceName().c_str());
BApi::loadTimer();
BLINKER_LOG(BLINKER_F("BLINKER_MQTT_AT initialized..."));
}
private :
BlinkerMQTTAT Transp;
};
#endif
#endif

View File

@@ -0,0 +1,32 @@
#ifndef BLINKER_ESP_MQTT_AUTO_H
#define BLINKER_ESP_MQTT_AUTO_H
#if defined(ESP8266) || defined(ESP32)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerMQTTAUTO.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerESPMQTTAUTO : public BlinkerApi
{
public :
void begin(const char* _key, const char* _type)
{
transport(Transp);
BApi::begin(_key, _type);
BApi::loadTimer();
BLINKER_LOG(BLINKER_F("ESP8266_MQTT_AUTO initialized..."));
}
private :
BlinkerMQTTAUTO Transp;
};
#endif
#endif

View File

@@ -0,0 +1,520 @@
// #ifndef BLINKER_ESP_MQTT_LP_H
// #define BLINKER_ESP_MQTT_LP_H
// #if defined(ESP8266) || defined(ESP32)
// #ifndef BLINKER_ARDUINOJSON
// #define BLINKER_ARDUINOJSON
// #endif
// #include "Adapters/BlinkerMQTTLP.h"
// #include "Blinker/BlinkerApi.h"
// #include "modules/ArduinoJson/ArduinoJson.h"
// #if defined(ESP8266)
// #include <ESP8266mDNS.h>
// #include <ESP8266WiFi.h>
// #include <ESP8266WebServer.h>
// #elif defined(ESP32)
// #include <ESPmDNS.h>
// #include <WiFi.h>
// #include <WebServer.h>
// #endif
// typedef BlinkerApi BApi;
// class BlinkerESPMQTTLP : public BlinkerApi
// {
// public :
// #if defined(BLINKER_ESP_SMARTCONFIG) || defined(BLINKER_APCONFIG)
// void begin(const char* _auth)
// {
// #if defined(BLINKER_ALIGENIE_LIGHT)
// String _aliType = BLINKER_F("&aliType=light");
// #elif defined(BLINKER_ALIGENIE_OUTLET)
// String _aliType = BLINKER_F("&aliType=outlet");
// #elif defined(BLINKER_ALIGENIE_MULTI_OUTLET)
// String _aliType = BLINKER_F("&aliType=multi_outlet");
// #elif defined(BLINKER_ALIGENIE_SENSOR)
// String _aliType = BLINKER_F("&aliType=sensor");
// #else
// String _aliType = BLINKER_F("");
// #endif
// #if defined(BLINKER_DUEROS_LIGHT)
// String _duerType = BLINKER_F("&duerType=LIGHT");
// #elif defined(BLINKER_DUEROS_OUTLET)
// String _duerType = BLINKER_F("&duerType=SOCKET");
// #elif defined(BLINKER_DUEROS_MULTI_OUTLET)
// String _duerType = BLINKER_F("&duerType=MULTI_SOCKET");
// #elif defined(BLINKER_DUEROS_SENSOR)
// String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
// #else
// String _duerType = BLINKER_F("");
// #endif
// BApi::begin();
// Transp.aliType(_aliType);
// Transp.duerType(_duerType);
// Transp.begin(_auth);
// transport(Transp);
// #if defined(BLINKER_ESP_SMARTCONFIG)
// Transp.smartconfigBegin();
// #elif defined(BLINKER_APCONFIG)
// Transp.apconfigBegin();
// #endif
// BApi::loadTimer();
// // #if defined(BLINKER_ESP_SMARTCONFIG)
// // smartconfigBegin(_auth, _aliType, _duerType);
// // #elif defined(BLINKER_APCONFIG)
// // apconfigBegin(_auth, _aliType, _duerType);
// // #endif
// // __auth = _auth;
// // #ifndef BLINKER_ESP_TASK
// // beginMQTT();
// // #endif
// }
// #else
// void begin( const char* _auth,
// const char* _ssid,
// const char* _pswd )
// {
// #if defined(BLINKER_ALIGENIE_LIGHT)
// String _aliType = BLINKER_F("&aliType=light");
// #elif defined(BLINKER_ALIGENIE_OUTLET)
// String _aliType = BLINKER_F("&aliType=outlet");
// #elif defined(BLINKER_ALIGENIE_MULTI_OUTLET)
// String _aliType = BLINKER_F("&aliType=multi_outlet");
// #elif defined(BLINKER_ALIGENIE_SENSOR)
// String _aliType = BLINKER_F("&aliType=sensor");
// #else
// String _aliType = BLINKER_F("");
// #endif
// #if defined(BLINKER_DUEROS_LIGHT)
// String _duerType = BLINKER_F("&duerType=LIGHT");
// #elif defined(BLINKER_DUEROS_OUTLET)
// String _duerType = BLINKER_F("&duerType=SOCKET");
// #elif defined(BLINKER_DUEROS_MULTI_OUTLET)
// String _duerType = BLINKER_F("&duerType=MULTI_SOCKET");
// #elif defined(BLINKER_DUEROS_SENSOR)
// String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
// #else
// String _duerType = BLINKER_F("");
// #endif
// BApi::begin();
// Transp.aliType(_aliType);
// Transp.duerType(_duerType);
// Transp.begin(_auth);
// transport(Transp);
// Transp.commonBegin(_ssid, _pswd);
// BApi::loadTimer();
// // __auth = _auth;
// // __ssid = _ssid;
// // __pswd = _pswd;
// // #ifndef BLINKER_ESP_TASK
// // beginMQTT();
// // #endif
// }
// #endif
// // void beginMQTT()
// // {
// // #if defined(BLINKER_ALIGENIE_LIGHT)
// // String _aliType = BLINKER_F("&aliType=light");
// // #elif defined(BLINKER_ALIGENIE_OUTLET)
// // String _aliType = BLINKER_F("&aliType=outlet");
// // #elif defined(BLINKER_ALIGENIE_SENSOR)
// // String _aliType = BLINKER_F("&aliType=sensor");
// // #else
// // String _aliType = BLINKER_F("");
// // #endif
// // #if defined(BLINKER_DUEROS_LIGHT)
// // String _duerType = BLINKER_F("&duerType=LIGHT");
// // #elif defined(BLINKER_DUEROS_OUTLET)
// // String _duerType = BLINKER_F("&duerType=SOCKET");
// // #elif defined(BLINKER_DUEROS_SENSOR)
// // String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
// // #else
// // String _duerType = BLINKER_F("");
// // #endif
// // #if defined(BLINKER_ESP_SMARTCONFIG)
// // smartconfigBegin(__auth, _aliType, _duerType);
// // #elif defined(BLINKER_APCONFIG)
// // apconfigBegin(__auth, _aliType, _duerType);
// // #else
// // commonBegin(__auth, __ssid, __pswd, _aliType, _duerType);
// // #endif
// // }
// private :
// // void commonBegin(const char* _auth,
// // const char* _ssid,
// // const char* _pswd,
// // String & _alitype,
// // String & _duertype);
// // void smartconfigBegin(const char* _auth, String & _alitype, String & _duertype);
// // void apconfigBegin(const char* _auth, String & _alitype, String & _duertype);
// // bool autoInit();
// // void smartconfig();
// // void softAPinit();
// // // void serverClient();
// // bool parseUrl(String data);
// // void connectWiFi(String _ssid, String _pswd);
// // void connectWiFi(const char* _ssid, const char* _pswd);
// // const char* __auth;
// // const char* __ssid;
// // const char* __pswd;
// BlinkerMQTTLP Transp;
// };
// // void BlinkerESPMQTTLP::commonBegin(const char* _auth,
// // const char* _ssid,
// // const char* _pswd,
// // String & _alitype,
// // String & _duertype)
// // {
// // BApi::begin();
// // connectWiFi(_ssid, _pswd);
// // // BApi::loadOTA();
// // Transp.aliType(_alitype);
// // Transp.duerType(_duertype);
// // Transp.begin(_auth);
// // transport(Transp);
// // BApi::loadTimer();
// // #if defined(ESP8266)
// // BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
// // #elif defined(ESP32)
// // BLINKER_LOG(BLINKER_F("ESP32_MQTT initialized..."));
// // #endif
// // }
// // void BlinkerESPMQTTLP::smartconfigBegin(const char* _auth, String & _alitype,
// // String & _duertype)
// // {
// // BApi::begin();
// // if (!autoInit()) smartconfig();
// // // BApi::loadOTA();
// // Transp.aliType(_alitype);
// // Transp.duerType(_duertype);
// // Transp.begin(_auth);
// // transport(Transp);
// // BApi::loadTimer();
// // #if defined(ESP8266)
// // BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
// // #elif defined(ESP32)
// // BLINKER_LOG(BLINKER_F("ESP32_MQTT initialized..."));
// // #endif
// // }
// // void BlinkerESPMQTTLP::apconfigBegin(const char* _auth, String & _alitype,
// // String & _duertype)
// // {
// // BApi::begin();
// // if (!autoInit())
// // {
// // softAPinit();
// // // while(WiFi.status() != WL_CONNECTED)
// // // {
// // // serverClient();
// // // ::delay(10);
// // // }
// // }
// // // BApi::loadOTA();
// // Transp.aliType(_alitype);
// // Transp.duerType(_duertype);
// // Transp.begin(_auth);
// // transport(Transp);
// // BApi::loadTimer();
// // #if defined(ESP8266)
// // BLINKER_LOG(BLINKER_F("ESP8266_MQTT initialized..."));
// // #elif defined(ESP32)
// // BLINKER_LOG(BLINKER_F("ESP32_MQTT initialized..."));
// // #endif
// // }
// // bool BlinkerESPMQTTLP::autoInit()
// // {
// // WiFi.mode(WIFI_STA);
// // String _hostname = BLINKER_F("DiyArduino_");
// // _hostname += macDeviceName();
// // #if defined(ESP8266)
// // WiFi.hostname(_hostname.c_str());
// // #elif defined(ESP32)
// // WiFi.setHostname(_hostname.c_str());
// // #endif
// // WiFi.begin();
// // ::delay(500);
// // BLINKER_LOG(BLINKER_F("Waiting for WiFi "),
// // BLINKER_WIFI_AUTO_INIT_TIMEOUT / 1000,
// // BLINKER_F("s, will enter SMARTCONFIG or "),
// // BLINKER_F("APCONFIG while WiFi not connect!"));
// // uint8_t _times = 0;
// // while (WiFi.status() != WL_CONNECTED) {
// // ::delay(500);
// // if (_times > BLINKER_WIFI_AUTO_INIT_TIMEOUT / 500) break;
// // _times++;
// // }
// // if (WiFi.status() != WL_CONNECTED) return false;
// // else {
// // BLINKER_LOG(BLINKER_F("WiFi Connected."));
// // BLINKER_LOG(BLINKER_F("IP Address: "));
// // BLINKER_LOG(WiFi.localIP());
// // return true;
// // }
// // }
// // void BlinkerESPMQTTLP::smartconfig()
// // {
// // WiFi.mode(WIFI_STA);
// // String _hostname = BLINKER_F("DiyArduino_");
// // _hostname += macDeviceName();
// // #if defined(ESP8266)
// // WiFi.hostname(_hostname.c_str());
// // #elif defined(ESP32)
// // WiFi.setHostname(_hostname.c_str());
// // #endif
// // WiFi.beginSmartConfig();
// // BLINKER_LOG(BLINKER_F("Waiting for SmartConfig."));
// // while (!WiFi.smartConfigDone()) {
// // ::delay(500);
// // }
// // BLINKER_LOG(BLINKER_F("SmartConfig received."));
// // BLINKER_LOG(BLINKER_F("Waiting for WiFi"));
// // while (WiFi.status() != WL_CONNECTED) {
// // ::delay(500);
// // }
// // BLINKER_LOG(BLINKER_F("WiFi Connected."));
// // BLINKER_LOG(BLINKER_F("IP Address: "));
// // BLINKER_LOG(WiFi.localIP());
// // }
// // void BlinkerESPMQTTLP::softAPinit()
// // {
// // WiFiServer _server(80);
// // WiFiClient _client;
// // IPAddress apIP(192, 168, 4, 1);
// // #if defined(ESP8266)
// // IPAddress netMsk(255, 255, 255, 0);
// // #endif
// // // _server = new WiFiServer(80);
// // WiFi.mode(WIFI_AP);
// // String softAP_ssid = BLINKER_F("DiyArduino_");
// // softAP_ssid += macDeviceName();
// // #if defined(ESP8266)
// // WiFi.hostname(softAP_ssid.c_str());
// // WiFi.softAPConfig(apIP, apIP, netMsk);
// // #elif defined(ESP32)
// // WiFi.setHostname(softAP_ssid.c_str());
// // WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
// // #endif
// // WiFi.softAP(softAP_ssid.c_str(), ("12345678"));
// // delay(100);
// // _server.begin();
// // BLINKER_LOG(BLINKER_F("AP IP address: "), WiFi.softAPIP());
// // BLINKER_LOG(BLINKER_F("HTTP _server started"));
// // BLINKER_LOG(BLINKER_F("URL: http://"), WiFi.softAPIP());
// // while(WiFi.status() != WL_CONNECTED)
// // {
// // // serverClient();
// // _client = _server.available();
// // // if (_client.status() == CLOSED)
// // if (!_client.connected())
// // {
// // _client.stop();
// // BLINKER_LOG(BLINKER_F("Connection closed on _client"));
// // }
// // else
// // {
// // if (_client.available())
// // {
// // String data = _client.readStringUntil('\r');
// // // data = data.substring(4, data.length() - 9);
// // _client.flush();
// // BLINKER_LOG(BLINKER_F("clientData: "), data);
// // if (STRING_contains_string(data, "ssid") &&
// // STRING_contains_string(data, "pswd"))
// // {
// // String msg = BLINKER_F("{\"hello\":\"world\"}");
// // String s= BLINKER_F("HTTP/1.1 200 OK\r\n");
// // s += BLINKER_F("Content-Type: application/json;");
// // s += BLINKER_F("charset=utf-8\r\n");
// // s += BLINKER_F("Content-Length: ");
// // s += String(msg.length());
// // s += BLINKER_F("\r\nConnection: Keep Alive\r\n\r\n");
// // s += msg;
// // s += BLINKER_F("\r\n");
// // _client.print(s);
// // _client.stop();
// // parseUrl(data);
// // }
// // }
// // }
// // ::delay(10);
// // }
// // }
// // // void BlinkerESPMQTTLP::serverClient()
// // // {
// // // if (!_client)
// // // {
// // // _client = _server->available();
// // // }
// // // else
// // // {
// // // // if (_client.status() == CLOSED)
// // // if (!_client.connected())
// // // {
// // // _client.stop();
// // // BLINKER_LOG(BLINKER_F("Connection closed on _client"));
// // // }
// // // else
// // // {
// // // if (_client.available())
// // // {
// // // String data = _client.readStringUntil('\r');
// // // // data = data.substring(4, data.length() - 9);
// // // _client.flush();
// // // BLINKER_LOG(BLINKER_F("clientData: "), data);
// // // if (STRING_contains_string(data, "ssid") &&
// // // STRING_contains_string(data, "pswd"))
// // // {
// // // String msg = BLINKER_F("{\"hello\":\"world\"}");
// // // String s= BLINKER_F("HTTP/1.1 200 OK\r\n");
// // // s += BLINKER_F("Content-Type: application/json;");
// // // s += BLINKER_F("charset=utf-8\r\n");
// // // s += BLINKER_F("Content-Length: ");
// // // s += String(msg.length());
// // // s += BLINKER_F("\r\nConnection: Keep Alive\r\n\r\n");
// // // s += msg;
// // // s += BLINKER_F("\r\n");
// // // _client.print(s);
// // // _client.stop();
// // // parseUrl(data);
// // // }
// // // }
// // // }
// // // }
// // // }
// // bool BlinkerESPMQTTLP::parseUrl(String data)
// // {
// // BLINKER_LOG(BLINKER_F("APCONFIG data: "), data);
// // DynamicJsonBuffer jsonBuffer;
// // JsonObject& wifi_data = jsonBuffer.parseObject(data);
// // if (!wifi_data.success()) {
// // return false;
// // }
// // String _ssid = wifi_data["ssid"];
// // String _pswd = wifi_data["pswd"];
// // BLINKER_LOG(BLINKER_F("ssid: "), _ssid);
// // BLINKER_LOG(BLINKER_F("pswd: "), _pswd);
// // // free(_server);
// // connectWiFi(_ssid, _pswd);
// // return true;
// // }
// // void BlinkerESPMQTTLP::connectWiFi(String _ssid, String _pswd)
// // {
// // connectWiFi(_ssid.c_str(), _pswd.c_str());
// // }
// // void BlinkerESPMQTTLP::connectWiFi(const char* _ssid, const char* _pswd)
// // {
// // uint32_t connectTime = millis();
// // BLINKER_LOG(BLINKER_F("Connecting to "), _ssid);
// // WiFi.mode(WIFI_STA);
// // String _hostname = BLINKER_F("DiyArduinoMQTT_");
// // _hostname += macDeviceName();
// // #if defined(ESP8266)
// // WiFi.hostname(_hostname.c_str());
// // #elif defined(ESP32)
// // WiFi.setHostname(_hostname.c_str());
// // #endif
// // if (_pswd && strlen(_pswd)) {
// // WiFi.begin(_ssid, _pswd);
// // }
// // else {
// // WiFi.begin(_ssid);
// // }
// // while (WiFi.status() != WL_CONNECTED) {
// // ::delay(50);
// // if (millis() - connectTime > BLINKER_CONNECT_TIMEOUT_MS && WiFi.status() != WL_CONNECTED) {
// // connectTime = millis();
// // BLINKER_LOG(BLINKER_F("WiFi connect timeout, please check ssid and pswd!"));
// // BLINKER_LOG(BLINKER_F("Retring WiFi connect again!"));
// // }
// // }
// // BLINKER_LOG(BLINKER_F("Connected"));
// // IPAddress myip = WiFi.localIP();
// // BLINKER_LOG(BLINKER_F("Your IP is: "), myip);
// // }
// #endif
// #endif

View File

@@ -0,0 +1,32 @@
#ifndef BLINKER_ESP_PRO_H
#define BLINKER_ESP_PRO_H
#if defined(ESP8266) || defined(ESP32)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerPRO.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerESPPRO : public BlinkerApi
{
public :
void begin(const char* _type = BLINKER_AIR_DETECTOR)
{
transport(Transp);
BApi::begin(_type);
BApi::loadTimer();
BLINKER_LOG(BLINKER_F("ESP8266_PRO initialized..."));
}
private :
BlinkerPRO Transp;
};
#endif
#endif

View File

@@ -0,0 +1,32 @@
#ifndef BLINKER_ESP_PRO_ESP_H
#define BLINKER_ESP_PRO_ESP_H
#if defined(ESP8266) || defined(ESP32)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerPROESP.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerESPPROESP : public BlinkerApi
{
public :
void begin(const char* _key, const char* _type)
{
transport(Transp);
BApi::begin(_key, _type);
BApi::loadTimer();
BLINKER_LOG(BLINKER_F("ESP_PRO initialized..."));
}
private :
BlinkerPROESP Transp;
};
#endif
#endif

View File

@@ -0,0 +1,27 @@
#ifndef BLINKER_ESP_SUBDEVICE_H
#define BLINKER_ESP_SUBDEVICE_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerSubDevice.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerESPSubDevice : public BlinkerApi
{
public :
void begin(const char* _key, const char* _type)
{
transport(Transp);
BApi::begin(_key, _type);
BLINKER_LOG(BLINKER_F("Blinker SubDevice initialized..."));
}
private :
BlinkerSubDevice Transp;
};
#endif

View File

@@ -0,0 +1,165 @@
#ifndef BLINKER_LUAT_PRO_H
#define BLINKER_LUAT_PRO_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerPROAIR202.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
typedef BlinkerApi BApi;
void listening()
{
yield();
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
class BlinkerLUATPRO : public BlinkerApi
{
public :
void begin( const char* _type = BLINKER_AIR_DETECTOR,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
serialBegin(_type, _rx_pin, _tx_pin, _baud);
// BApi::begin(_type);
::delay(100);
// BApi::atInit();
BLINKER_LOG(BLINKER_F("Blinker NBIoT initialized..."));
}
private :
void serialBegin(const char* _type,
uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BApi::begin(_type, Serial1, true, listening);
#else
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
BApi::begin(_type, Serial, true, listening);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BLINKER_LOG_ALL(BLINKER_F("Serial1 initialized..."));
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial1, true, listening);
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.initStream(Serial2, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial2, true, listening);
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.initStream(Serial3, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial3, true, listening);
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial, true, listening);
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.initStream(*HSerial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *HSerial, true, listening);
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
#endif
}
private :
BlinkerProAIR202 Transp;
};
#endif

View File

@@ -0,0 +1,161 @@
#ifndef BLINKER_LOW_POWER_GPRS_H
#define BLINKER_LOW_POWER_GPRS_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerAIR202LP.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
typedef BlinkerApi BApi;
void listening()
{
yield();
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
class BlinkerLowPowerGPRS : public BlinkerApi
{
public :
void begin(const char* _key, const char* _type,
uint8_t _rx_pin = 2, uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
serialBegin(_key, _type, _rx_pin, _tx_pin, _baud);
::delay(100);
BLINKER_LOG(BLINKER_F("Blinker GPRS initialized..."));
}
private :
void serialBegin(const char* _key,
const char* _type,
uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BApi::begin(_key, _type, Serial1, true, listening);
#else
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
BApi::begin(_key, _type, Serial, true, listening);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_key, _type, Serial1, true, listening);
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.initStream(Serial2, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_key, _type, Serial2, true, listening);
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.initStream(Serial3, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_key, _type, Serial3, true, listening);
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_key, _type, *SSerial, false, listening);
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_key, _type, Serial, true, listening);
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_key, _type, *SSerial, false, listening);
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.initStream(*HSerial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_key, _type, *HSerial, true, listening);
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_key, _type, *SSerial, false, listening);
#endif
}
BlinkerAIR202LP Transp;
};
#endif

View File

@@ -0,0 +1,194 @@
#ifndef BLINKER_QRCODE_SERIAL_SIM7000_NBIOT_H
#define BLINKER_QRCODE_SERIAL_SIM7000_NBIOT_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerQRCodeSIM7000.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
typedef BlinkerApi BApi;
void listening()
{
yield();
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
class BlinkerQRCodeSerialSIM7000NBIoT : public BlinkerApi
{
public :
// void begin( const char* _type = BLINKER_AIR_DETECTOR,
// uint8_t _rx_pin = 2,
// uint8_t _tx_pin = 3,
// uint32_t _baud = 9600)
void begin( const char* _type = BLINKER_AIR_DETECTOR,
const char* _auth = BLINKER_AIR_DETECTOR,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
#if defined(BLINKER_ALIGENIE_LIGHT)
String _aliType = BLINKER_F("&aliType=light");
#elif defined(BLINKER_ALIGENIE_OUTLET)
String _aliType = BLINKER_F("&aliType=outlet");
#elif defined(BLINKER_ALIGENIE_SENSOR)
String _aliType = BLINKER_F("&aliType=sensor");
#else
String _aliType = BLINKER_F("");
#endif
#if defined(BLINKER_DUEROS_LIGHT)
String _duerType = BLINKER_F("&duerType=LIGHT");
#elif defined(BLINKER_DUEROS_OUTLET)
String _duerType = BLINKER_F("&duerType=SOCKET");
#elif defined(BLINKER_DUEROS_SENSOR)
String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
#else
String _duerType = BLINKER_F("");
#endif
Transp.aliType(_aliType);
Transp.duerType(_duerType);
serialBegin(_type, _auth, _rx_pin, _tx_pin, _baud);
// BApi::begin(_type);
::delay(100);
// BApi::atInit();
BLINKER_LOG(BLINKER_F("Blinker NBIoT initialized..."));
}
private :
void serialBegin(const char* _type,
const char* _auth,
uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BApi::begin(Serial1, true, listening);
#else
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
BApi::begin(_type, _auth, Serial, true, listening);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Serial1.println("test!!!");
Transp.initStream(Serial1, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, Serial1, true, listening);
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.initStream(Serial2, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, Serial2, true, listening);
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.initStream(Serial3, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, Serial3, true, listening);
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, *SSerial, false, listening);
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, Serial, true, listening);
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, *SSerial, false, listening);
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.initStream(*HSerial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, *HSerial, true, listening);
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, *SSerial, false, listening);
#endif
}
private :
BlinkerQRCodeSIM7000 Transp;
};
#endif

View File

@@ -0,0 +1,194 @@
#ifndef BLINKER_QRCODE_SERIAL_SIM_NBIOT_H
#define BLINKER_QRCODE_SERIAL_SIM_NBIOT_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerQRCodeSIM7020.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
typedef BlinkerApi BApi;
void listening()
{
yield();
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
class BlinkerQRCodeSerialSIMNBIoT : public BlinkerApi
{
public :
// void begin( const char* _type = BLINKER_AIR_DETECTOR,
// uint8_t _rx_pin = 2,
// uint8_t _tx_pin = 3,
// uint32_t _baud = 9600)
void begin( const char* _type = BLINKER_AIR_DETECTOR,
const char* _auth = BLINKER_AIR_DETECTOR,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
#if defined(BLINKER_ALIGENIE_LIGHT)
String _aliType = BLINKER_F("&aliType=light");
#elif defined(BLINKER_ALIGENIE_OUTLET)
String _aliType = BLINKER_F("&aliType=outlet");
#elif defined(BLINKER_ALIGENIE_SENSOR)
String _aliType = BLINKER_F("&aliType=sensor");
#else
String _aliType = BLINKER_F("");
#endif
#if defined(BLINKER_DUEROS_LIGHT)
String _duerType = BLINKER_F("&duerType=LIGHT");
#elif defined(BLINKER_DUEROS_OUTLET)
String _duerType = BLINKER_F("&duerType=SOCKET");
#elif defined(BLINKER_DUEROS_SENSOR)
String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
#else
String _duerType = BLINKER_F("");
#endif
Transp.aliType(_aliType);
Transp.duerType(_duerType);
serialBegin(_type, _auth, _rx_pin, _tx_pin, _baud);
// BApi::begin(_type);
::delay(100);
// BApi::atInit();
BLINKER_LOG(BLINKER_F("Blinker NBIoT initialized..."));
}
private :
void serialBegin(const char* _type,
const char* _auth,
uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BApi::begin(Serial1, true, listening);
#else
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
BApi::begin(_type, _auth, Serial, true, listening);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Serial1.println("test!!!");
Transp.initStream(Serial1, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, Serial1, true, listening);
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.initStream(Serial2, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, Serial2, true, listening);
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.initStream(Serial3, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, Serial3, true, listening);
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, *SSerial, false, listening);
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, Serial, true, listening);
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, *SSerial, false, listening);
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.initStream(*HSerial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, *HSerial, true, listening);
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, _auth, *SSerial, false, listening);
#endif
}
private :
BlinkerQRCodeSIM7020 Transp;
};
#endif

View File

@@ -0,0 +1,188 @@
#ifndef BLINKER_SIM_PRO_H
#define BLINKER_SIM_PRO_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerPROSIM7020.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
typedef BlinkerApi BApi;
void listening()
{
yield();
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
class BlinkerSIMPRO : public BlinkerApi
{
public :
void begin( const char* _type = BLINKER_AIR_DETECTOR,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
// #if defined(BLINKER_ALIGENIE_LIGHT)
// String _aliType = BLINKER_F("&aliType=light");
// #elif defined(BLINKER_ALIGENIE_OUTLET)
// String _aliType = BLINKER_F("&aliType=outlet");
// #elif defined(BLINKER_ALIGENIE_SENSOR)
// String _aliType = BLINKER_F("&aliType=sensor");
// #else
// String _aliType = BLINKER_F("");
// #endif
// #if defined(BLINKER_DUEROS_LIGHT)
// String _duerType = BLINKER_F("&duerType=LIGHT");
// #elif defined(BLINKER_DUEROS_OUTLET)
// String _duerType = BLINKER_F("&duerType=SOCKET");
// #elif defined(BLINKER_DUEROS_SENSOR)
// String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
// #else
// String _duerType = BLINKER_F("");
// #endif
// Transp.aliType(_aliType);
// Transp.duerType(_duerType);
serialBegin(_type, _rx_pin, _tx_pin, _baud);
// BApi::begin(_type);
::delay(100);
// BApi::atInit();
BLINKER_LOG(BLINKER_F("Blinker NBIoT initialized..."));
}
private :
void serialBegin(const char* _type,
uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BApi::begin(_type, Serial1, true, listening);
#else
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
BApi::begin(_type, Serial, true, listening);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BLINKER_LOG_ALL(BLINKER_F("Serial1 initialized..."));
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial1, true, listening);
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.initStream(Serial2, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial2, true, listening);
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.initStream(Serial3, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial3, true, listening);
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial, true, listening);
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.initStream(*HSerial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *HSerial, true, listening);
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
#endif
}
private :
BlinkerProSIM7020 Transp;
};
#endif

View File

@@ -0,0 +1,173 @@
#ifndef BLINKER_SERIAL_BLE_H
#define BLINKER_SERIAL_BLE_H
#if !defined (__AVR__)
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#ifndef ARDUINOJSON_VERSION_MAJOR
#include "modules/ArduinoJson/ArduinoJson.h"
#endif
#if defined(ESP32)
#include <HardwareSerial.h>
#elif defined (__AVR__) || defined(ESP8266)
#include <SoftwareSerial.h>
#endif
#else
#if defined (BLINKER_ARDUINOJSON)
#ifndef ARDUINOJSON_VERSION_MAJOR
#include "modules/ArduinoJson/ArduinoJson.h"
#endif
#endif
#endif
#include "Adapters/BlinkerSerial.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerSerialBLE : public BlinkerApi
{
public :
void begin( uint8_t ss_rx_pin = 2,
uint8_t ss_tx_pin = 3,
uint32_t ss_baud = 9600)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.begin(Serial1, true);
transport(Transp);
// this->conn.begin(Serial1, true);
#else
Serial.begin(ss_baud);
Transp.begin(Serial, true);
transport(Transp);
// this->conn.begin(Serial, true);
#endif
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
BApi::begin();
Serial1.begin(ss_baud);
Transp.begin(Serial1, true);
transport(Transp);
// this->conn.begin(Serial1, true);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
BApi::begin();
Serial2.begin(ss_baud);
Transp.begin(Serial2, true);
transport(Transp);
// this->conn.begin(Serial2, true);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
BApi::begin();
Serial3.begin(ss_baud);
Transp.begin(Serial3, true);
transport(Transp);
// this->conn.begin(Serial3, true);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
#endif
else {
BApi::begin();
SSerialBLE = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerialBLE->begin(ss_baud);
Transp.begin(*SSerialBLE, false);
transport(Transp);
// this->conn.begin(*SSerialBLE, false);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
BApi::begin();
Serial.begin(ss_baud);
Transp.begin(Serial, true);
transport(Transp);
// this->conn.begin(Serial, true);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
else {
BApi::begin();
SSerialBLE = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerialBLE->begin(ss_baud);
Transp.begin(*SSerialBLE, false);
transport(Transp);
// this->conn.begin(*SSerialBLE, false);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
#elif defined(ESP32)
BApi::begin();
HSerialBLE = new HardwareSerial(1);
HSerialBLE->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.begin(*HSerialBLE, true);
transport(Transp);
// this->conn.begin(*HSerialBLE, true);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
#elif defined(ARDUINO_SAM_DUE)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
BApi::begin();
Serial.begin(ss_baud);
Transp.begin(Serial, true);
transport(Transp);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
BApi::begin();
Serial1.begin(ss_baud);
Transp.begin(Serial1, true);
transport(Transp);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
BApi::begin();
Serial2.begin(ss_baud);
Transp.begin(Serial2, true);
transport(Transp);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
BApi::begin();
Serial3.begin(ss_baud);
Transp.begin(Serial3, true);
transport(Transp);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
return;
}
else
{
BLINKER_ERR_LOG(BLINKER_F("Arduino DUE, Please set right Hardware Serial!"));
return;
}
#else
BApi::begin();
SSerialBLE = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerialBLE->begin(ss_baud);
Transp.begin(*SSerialBLE, false);
transport(Transp);
// this->conn.begin(*SSerialBLE, false);
BLINKER_LOG(BLINKER_F("SerialBLE initialized..."));
#endif
}
private :
BlinkerSerial Transp;
};
#endif

View File

@@ -0,0 +1,150 @@
#ifndef BLINKER_SERIAL_ESP_MQTT_H
#define BLINKER_SERIAL_ESP_MQTT_H
#include "Adapters/BlinkerSerialMQTT.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
#else
#include <SoftwareSerial.h>
#endif
typedef BlinkerApi BApi;
class BlinkerSerialESPMQTT : public BlinkerApi
{
public :
#if defined(BLINKER_ESP_SMARTCONFIG)
void begin( const char* _auth,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
BApi::begin();
::delay(100);
serialBegin(_rx_pin, _tx_pin, _baud);
BApi::atInit(_auth);
BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
}
#elif defined(BLINKER_APCONFIG)
void begin( const char* _auth,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
BApi::begin();
::delay(100);
serialBegin(_rx_pin, _tx_pin, _baud);
BApi::atInit(_auth);
BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
}
#endif
void begin( const char* _auth,
const char* _ssid,
const char* _pswd,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
BApi::begin();
::delay(100);
serialBegin(_rx_pin, _tx_pin, _baud);
BApi::atInit(_auth, _ssid, _pswd);
BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
}
private :
void serialBegin(uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.begin(Serial1, true);
transport(Transp);
#else
Serial.begin(ss_baud);
Transp.begin(Serial, true);
transport(Transp);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Transp.begin(Serial1, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.begin(Serial2, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.begin(Serial3, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#endif
else {
// BApi::begin();
SSerialMQTT = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerialMQTT->begin(ss_baud);
Transp.begin(*SSerialMQTT, false);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.begin(Serial, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
else {
// BApi::begin();
SSerialMQTT = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerialMQTT->begin(ss_baud);
Transp.begin(*SSerialMQTT, false);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
}
#elif defined(ESP32)
// BApi::begin();
HSerialMQTT = new HardwareSerial(1);
HSerialMQTT->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.begin(*HSerialMQTT, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
#else
// BApi::begin();
SSerialMQTT = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerialMQTT->begin(ss_baud);
Transp.begin(*SSerialMQTT, false);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
#endif
}
private :
BlinkerSerialMQTT Transp;
};
#endif

View File

@@ -0,0 +1,164 @@
#ifndef BLINKER_SERIAL_LUAT_GPRS_H
#define BLINKER_SERIAL_LUAT_GPRS_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerSerialAIR202.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
typedef BlinkerApi BApi;
void listening()
{
yield();
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
class BlinkerSerialLUATGPRS : public BlinkerApi
{
public :
void begin( const char* _type = BLINKER_AIR_DETECTOR,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
serialBegin(_type, _rx_pin, _tx_pin, _baud);
// BApi::begin(_type);
::delay(100);
// BApi::atInit();
BLINKER_LOG(BLINKER_F("Blinker GPRS initialized..."));
}
private :
void serialBegin(const char* _type,
uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BApi::begin(_type, Serial1, true, listening);
#else
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
BApi::begin(_type, Serial, true, listening);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial1, true, listening);
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.initStream(Serial2, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial2, true, listening);
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.initStream(Serial3, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial3, true, listening);
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial, true, listening);
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.initStream(*HSerial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *HSerial, true, listening);
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
#endif
}
private :
BlinkerSerialAIR202 Transp;
};
#endif

View File

@@ -0,0 +1,188 @@
#ifndef BLINKER_SERIAL_SIM7000_NBIOT_H
#define BLINKER_SERIAL_SIM7000_NBIOT_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerSerialSIM7000.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
typedef BlinkerApi BApi;
void listening()
{
yield();
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
class BlinkerSerialSIM7000NBIoT : public BlinkerApi
{
public :
void begin( const char* _type = BLINKER_AIR_DETECTOR,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
#if defined(BLINKER_ALIGENIE_LIGHT)
String _aliType = BLINKER_F("&aliType=light");
#elif defined(BLINKER_ALIGENIE_OUTLET)
String _aliType = BLINKER_F("&aliType=outlet");
#elif defined(BLINKER_ALIGENIE_SENSOR)
String _aliType = BLINKER_F("&aliType=sensor");
#else
String _aliType = BLINKER_F("");
#endif
#if defined(BLINKER_DUEROS_LIGHT)
String _duerType = BLINKER_F("&duerType=LIGHT");
#elif defined(BLINKER_DUEROS_OUTLET)
String _duerType = BLINKER_F("&duerType=SOCKET");
#elif defined(BLINKER_DUEROS_SENSOR)
String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
#else
String _duerType = BLINKER_F("");
#endif
Transp.aliType(_aliType);
Transp.duerType(_duerType);
serialBegin(_type, _rx_pin, _tx_pin, _baud);
// BApi::begin(_type);
::delay(100);
// BApi::atInit();
BLINKER_LOG(BLINKER_F("Blinker NBIoT initialized..."));
}
private :
void serialBegin(const char* _type,
uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BApi::begin(_type, Serial1, true, listening);
#else
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
BApi::begin(_type, Serial, true, listening);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Serial1.println("test!!!");
Transp.initStream(Serial1, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial1, true, listening);
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.initStream(Serial2, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial2, true, listening);
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.initStream(Serial3, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial3, true, listening);
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial, true, listening);
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.initStream(*HSerial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *HSerial, true, listening);
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
#endif
}
private :
BlinkerSerialSIM7000 Transp;
};
#endif

View File

@@ -0,0 +1,188 @@
#ifndef BLINKER_SERIAL_SIM_NBIOT_H
#define BLINKER_SERIAL_SIM_NBIOT_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerSerialSIM7020.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
HardwareSerial *HSerial;
#else
#include <SoftwareSerial.h>
SoftwareSerial *SSerial;
#endif
typedef BlinkerApi BApi;
void listening()
{
yield();
#if defined(__AVR__) || defined(ESP8266)
if (!SSerial->isListening())
{
SSerial->listen();
::delay(100);
}
#endif
}
class BlinkerSerialSIMNBIoT : public BlinkerApi
{
public :
void begin( const char* _type = BLINKER_AIR_DETECTOR,
uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
#if defined(BLINKER_ALIGENIE_LIGHT)
String _aliType = BLINKER_F("&aliType=light");
#elif defined(BLINKER_ALIGENIE_OUTLET)
String _aliType = BLINKER_F("&aliType=outlet");
#elif defined(BLINKER_ALIGENIE_SENSOR)
String _aliType = BLINKER_F("&aliType=sensor");
#else
String _aliType = BLINKER_F("");
#endif
#if defined(BLINKER_DUEROS_LIGHT)
String _duerType = BLINKER_F("&duerType=LIGHT");
#elif defined(BLINKER_DUEROS_OUTLET)
String _duerType = BLINKER_F("&duerType=SOCKET");
#elif defined(BLINKER_DUEROS_SENSOR)
String _duerType = BLINKER_F("&duerType=AIR_MONITOR");
#else
String _duerType = BLINKER_F("");
#endif
Transp.aliType(_aliType);
Transp.duerType(_duerType);
serialBegin(_type, _rx_pin, _tx_pin, _baud);
// BApi::begin(_type);
::delay(100);
// BApi::atInit();
BLINKER_LOG(BLINKER_F("Blinker NBIoT initialized..."));
}
private :
void serialBegin(const char* _type,
uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.initStream(Serial1, true, listening);
transport(Transp);
BApi::begin(_type, Serial1, true, listening);
#else
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
BApi::begin(_type, Serial, true, listening);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Serial1.println("test!!!");
Transp.initStream(Serial1, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial1, true, listening);
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.initStream(Serial2, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial2, true, listening);
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.initStream(Serial3, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial3, true, listening);
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.initStream(Serial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, Serial, true, listening);
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.initStream(*HSerial, true, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *HSerial, true, listening);
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.initStream(*SSerial, false, listening);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
BApi::begin(_type, *SSerial, false, listening);
#endif
}
private :
BlinkerSerialSIM7020 Transp;
};
#endif

View File

@@ -0,0 +1,121 @@
#ifndef BLINKER_SERIAL_WH_NBIOT_H
#define BLINKER_SERIAL_WH_NBIOT_H
#include "Adapters/BlinkerSerialNBIoT.h"
#include "Blinker/BlinkerApi.h"
#if defined(ESP32)
#include <HardwareSerial.h>
#else
#include <SoftwareSerial.h>
#endif
typedef BlinkerApi BApi;
class BlinkerSerialWHNBIoT : public BlinkerApi
{
public :
void begin( uint8_t _rx_pin = 2,
uint8_t _tx_pin = 3,
uint32_t _baud = 9600)
{
BApi::begin();
::delay(100);
serialBegin(_rx_pin, _tx_pin, _baud);
BApi::atInit();
BLINKER_LOG(BLINKER_F("Blinker NBIoT initialized..."));
}
private :
void serialBegin(uint8_t ss_rx_pin,
uint8_t ss_tx_pin,
uint32_t ss_baud)
{
#if defined (__AVR__)
if (ss_rx_pin == 0 && ss_tx_pin == 1){
// BApi::begin();
#if defined (__AVR_ATmega32U4__)
Serial1.begin(ss_baud);
Transp.begin(Serial1, true);
transport(Transp);
#else
Serial.begin(ss_baud);
Transp.begin(Serial, true);
transport(Transp);
#endif
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
// BApi::begin();
Serial1.begin(ss_baud);
Transp.begin(Serial1, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
// BApi::begin();
Serial2.begin(ss_baud);
Transp.begin(Serial2, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
// BApi::begin();
Serial3.begin(ss_baud);
Transp.begin(Serial3, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
#endif
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.begin(*SSerial, false);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
}
#elif defined(ESP8266)
if (ss_rx_pin == RX && ss_tx_pin == TX) {
// BApi::begin();
Serial.begin(ss_baud);
Transp.begin(Serial, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
return;
}
else {
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.begin(*SSerial, false);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
}
#elif defined(ESP32)
// BApi::begin();
HSerial = new HardwareSerial(1);
HSerial->begin(ss_baud, SERIAL_8N1, ss_rx_pin, ss_tx_pin);
Transp.begin(*HSerial, true);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
#else
// BApi::begin();
SSerial = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
SSerial->begin(ss_baud);
Transp.begin(*SSerial, false);
transport(Transp);
// BLINKER_LOG(BLINKER_F("SerialMQTT initialized..."));
#endif
}
private :
BlinkerSerialNBIoT Transp;
};
#endif

View File

@@ -0,0 +1,28 @@
#ifndef BLINKER_SUBDEVICE_H
#define BLINKER_SUBDEVICE_H
#ifndef BLINKER_ARDUINOJSON
#define BLINKER_ARDUINOJSON
#endif
#include "Adapters/BlinkerSubStream.h"
#include "Blinker/BlinkerApi.h"
typedef BlinkerApi BApi;
class BlinkerSubDevice : public BlinkerApi
{
public :
void begin(const char* _auth)
{
BApi::begin();
Transp.begin(_auth);
transport(Transp);
BLINKER_LOG(BLINKER_F("Blinker SubDevice initialized..."));
}
private :
BlinkerSubStream Transp;
};
#endif

View File

@@ -0,0 +1,54 @@
#ifndef BLINKER_WIDGETS_H
#define BLINKER_WIDGETS_H
#if defined(BLINKER_MQTT) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_MQTT_AUTO)
#include "Functions/BlinkerBridge.h"
#endif
#include "Functions/BlinkerButton.h"
#include "Functions/BlinkerNumber.h"
#include "Functions/BlinkerImage.h"
#include "Functions/BlinkerRGB.h"
// #if defined(BLINKER_BLE)
#include "Functions/BlinkerJoystick.h"
// #endif
#include "Functions/BlinkerSlider.h"
#include "Functions/BlinkerSwitch.h"
#include "Functions/BlinkerTab.h"
#include "Functions/BlinkerText.h"
#if defined(BLINKER_ALIGENIE)
#include "Functions/BlinkerAliGenie.h"
BLINKERALIGENIE BlinkerAliGenie;
#endif
#if defined(BLINKER_DUEROS)
#include "Functions/BlinkerDuerOS.h"
BLINKERDUEROS BlinkerDuerOS;
#endif
#if defined(BLINKER_MIOT)
#include "Functions/BlinkerMIOT.h"
BLINKERMIOT BlinkerMIOT;
#endif
#if defined(BLINKER_BLE) || defined(BLINKER_WIFI) || \
defined(BLINKER_MQTT) || defined(BLINKER_PRO) || \
defined(BLINKER_NBIOT_WH) || defined(BLINKER_WIFI_GATEWAY) || \
defined(BLINKER_MQTT_AUTO) || defined(BLINKER_PRO_ESP) || \
defined(BLINKE_HTTP)
BlinkerSwitch BUILTIN_SWITCH;
#endif
#if defined(BLINKER_PRO_ESP)
#include "Functions/BlinkerEvent.h"
BLINKEREVENT BlinkerEvent;
#endif
#endif

View File

@@ -0,0 +1,814 @@
#ifndef BLINKER_AIR202_H
#define BLINKER_AIR202_H
#if defined(ARDUINO)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#endif
#include "../Blinker/BlinkerATMaster.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
// #if defined(ESP32)
// #include <HardwareSerial.h>
// HardwareSerial *HSerial_API;
// #else
// #include <SoftwareSerial.h>
// SoftwareSerial *SSerial_API;
// #endif
#include <time.h>
#define BLINKER_AIR202_DATA_BUFFER_SIZE 1024
enum air202_status_t
{
air202_cgtt_state_ver_check,
air202_cgtt_state_ver_check_success,
air202_cgtt_state,
air202_cgtt_state_req,
air202_cgtt_state_success,
air202_sapbar_pdp_req,
air202_sapbar_pdp_success,
air202_sapbar_pdp_failed,
air202_sapbar_apn_req,
air202_sapbar_apn_success,
air202_sapbar_apn_failed,
air202_sapbar_save_req,
air202_sapbar_save_success,
air202_sapbar_save_failed,
air202_sapbar_fresh_req,
air202_sapbar_fresh_success,
air202_sapbar_fresh_failed,
};
class BlinkerAIR202
{
public :
BlinkerAIR202() :
isHWS(false)
{}
~BlinkerAIR202() { flush(); }
time_t _ntpTime = 0;
void setStream(Stream& s, bool isHardware, blinker_callback_t _func)
{ stream = &s; isHWS = isHardware; listenFunc = _func; }
void setTimezone(float tz) { _timezone = tz; }
String lang() { return _LANG; }
String lat() { return _LAT; }
// int16_t year()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_year + 1900;
// }
// return -1;
// }
// int8_t month()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_mon + 1;
// }
// return -1;
// }
// int8_t mday()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_mday;
// }
// return -1;
// }
// int8_t wday()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_wday;
// }
// return -1;
// }
// int8_t hour()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_hour;
// }
// return -1;
// }
// int8_t minute()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_min;
// }
// return -1;
// }
// int8_t second()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_sec;
// }
// return -1;
// }
// time_t time()
// {
// if (_ntpTime)
// {
// return _ntpTime - _timezone * 3600;
// }
// return millis();
// }
// int32_t dtime()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_hour * 60 * 60 + timeinfo.tm_min * 60 + timeinfo.tm_sec;
// }
// return -1;
// }
bool getNTP(float tz = 8.0)
{
streamPrint(BLINKER_CMD_CNTP_REQ);
uint32_t os_time = millis();
while(millis() - os_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
break;
}
}
}
streamPrint(BLINKER_CMD_CCLK_REQ);
os_time = millis();
while(millis() - os_time < _airTimeout * 10)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CCLK)
{
struct tm timeinfo;
timeinfo.tm_year = _masterAT->getParam(0).substring(2, 4).toInt() + 130;
timeinfo.tm_mon = _masterAT->getParam(0).substring(5, 7).toInt() - 1;
timeinfo.tm_mday = _masterAT->getParam(0).substring(8, 10).toInt() - 1;
timeinfo.tm_hour = _masterAT->getParam(1).substring(0, 2).toInt();
timeinfo.tm_min = _masterAT->getParam(1).substring(3, 5).toInt();
timeinfo.tm_sec = _masterAT->getParam(1).substring(6, 8).toInt();
// BLINKER_LOG_ALL(BLINKER_F("year: "), timeinfo.tm_year);
// BLINKER_LOG_ALL(BLINKER_F("mon: "), timeinfo.tm_mon);
// BLINKER_LOG_ALL(BLINKER_F("mday: "), timeinfo.tm_mday);
// BLINKER_LOG_ALL(BLINKER_F("hour: "), timeinfo.tm_hour);
// BLINKER_LOG_ALL(BLINKER_F("mins: "), timeinfo.tm_min);
// BLINKER_LOG_ALL(BLINKER_F("secs: "), timeinfo.tm_sec);
#if defined(ESP8266) || defined(ESP32)
_ntpTime = mktime(&timeinfo);
#else
_ntpTime = mk_gmtime(&timeinfo);
#endif
BLINKER_LOG_ALL(BLINKER_F("year: "), timeinfo.tm_year);
BLINKER_LOG_ALL(BLINKER_F("mon: "), timeinfo.tm_mon);
BLINKER_LOG_ALL(BLINKER_F("mday: "), timeinfo.tm_mday);
BLINKER_LOG_ALL(BLINKER_F("hour: "), timeinfo.tm_hour);
BLINKER_LOG_ALL(BLINKER_F("mins: "), timeinfo.tm_min);
BLINKER_LOG_ALL(BLINKER_F("secs: "), timeinfo.tm_sec);
// ::delay(200);
// _ntpTime = mktime(&timeinfo);
// _ntpTime -= _timezone * 3600;
BLINKER_LOG_ALL(BLINKER_F("==_ntpTime: "), _ntpTime);
BLINKER_LOG_ALL(BLINKER_F("==_ntpTime: "), timeinfo.tm_hour);
BLINKER_LOG_ALL(BLINKER_F("==Current time: "), asctime(&timeinfo));
free(_masterAT);
return true;
}
free(_masterAT);
}
}
return false;
}
bool getAMGSMLOC(float tz = 8.0)
{
uint32_t os_time = millis();
streamPrint(BLINKER_CMD_AMGSMLOC_REQ);
while(millis() - os_time < _airTimeout * 10)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_AMGSMLOC &&
_masterAT->getParam(0) == " 0")
{
strcpy(_LANG, _masterAT->getParam(1).c_str());
strcpy(_LAT, _masterAT->getParam(2).c_str());
BLINKER_LOG_ALL(BLINKER_F("LANG.: "), _LANG);
BLINKER_LOG_ALL(BLINKER_F("LAT.: "), _LAT);
struct tm timeinfo;
timeinfo.tm_year = _masterAT->getParam(3).substring(0, 4).toInt() - 1870;
timeinfo.tm_mon = _masterAT->getParam(3).substring(5, 7).toInt() - 1;
timeinfo.tm_mday = _masterAT->getParam(3).substring(8, 10).toInt() - 1;
BLINKER_LOG_ALL(BLINKER_F("year: "), timeinfo.tm_year);
BLINKER_LOG_ALL(BLINKER_F("mon: "), timeinfo.tm_mon);
BLINKER_LOG_ALL(BLINKER_F("mday: "), timeinfo.tm_mday);
timeinfo.tm_hour = _masterAT->getParam(4).substring(0, 2).toInt();
timeinfo.tm_min = _masterAT->getParam(4).substring(3, 5).toInt();
timeinfo.tm_sec = _masterAT->getParam(4).substring(6, 8).toInt();
BLINKER_LOG_ALL(BLINKER_F("hour: "), timeinfo.tm_hour);
BLINKER_LOG_ALL(BLINKER_F("mins: "), timeinfo.tm_min);
BLINKER_LOG_ALL(BLINKER_F("secs: "), timeinfo.tm_sec);
#if defined(ESP8266) || defined(ESP32)
_ntpTime = mktime(&timeinfo);
#else
_ntpTime = mk_gmtime(&timeinfo);
#endif
// _ntpTime -= _timezone * 3600;
BLINKER_LOG_ALL(BLINKER_F("_ntpTime: "), _ntpTime);
free(_masterAT);
return true;
}
free(_masterAT);
}
}
return false;
}
int checkCGTT()
{
uint32_t http_time = millis();
air202_status_t cgtt_status = air202_cgtt_state_ver_check;
streamPrint(BLINKER_CMD_CGMMR_REQ);
cgtt_status = air202_cgtt_state_ver_check;
while(millis() - http_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_CGMMR_RESP) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_cgtt_state_ver_check_success"));
cgtt_status = air202_cgtt_state_ver_check_success;
break;
}
}
}
if (cgtt_status != air202_cgtt_state_ver_check_success) return false;
streamPrint(BLINKER_CMD_CGQTT_REQ);
cgtt_status = air202_cgtt_state;
http_time = millis();
while(millis() - http_time < _airTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CGATT &&
_masterAT->getParam(0).toInt() == 1)
{
BLINKER_LOG_ALL(BLINKER_F("air202_cgtt_state_req"));
cgtt_status = air202_cgtt_state_req;
free(_masterAT);
break;
}
free(_masterAT);
}
}
if (cgtt_status != air202_cgtt_state_req) return false;
// http_time = millis();
// while(millis() - http_time < _airTimeout)
// {
// if (available())
// {
// if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("air202_cgtt_state_success"));
// cgtt_status = air202_cgtt_state_success;
// break;
// }
// }
// }
// if (cgtt_status != air202_cgtt_state_success) return false;
BLINKER_LOG_ALL(BLINKER_F("check CGTT success"));
return true;
// stream->println(BLINKER_CMD_CGQTT_REQ);
// cgtt_status = air202_cgtt_state;
}
bool powerCheck()
{
streamPrint(BLINKER_CMD_AT);
streamPrint("ATE0");
if (!checkCGTT()) return false;
BLINKER_LOG_ALL(BLINKER_F("power check"));
streamPrint(BLINKER_CMD_AT);
uint32_t dev_time = millis();
while(millis() - dev_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("power on"));
// SAPBR();
return SAPBR();
}
}
}
return false;
}
bool SAPBR()
{
streamPrint(STRING_format(BLINEKR_CMD_SAPBR_REQ) + \
"=3,1,\"CONTYPE\",\"GPRS\"");
air202_status_t sapbar_status = air202_sapbar_pdp_req;
uint32_t dev_time = millis();
while(millis() - dev_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_sapbar_pdp_success"));
sapbar_status = air202_sapbar_pdp_success;
break;
}
}
}
if (sapbar_status != air202_sapbar_pdp_success) return false;
streamPrint(STRING_format(BLINEKR_CMD_SAPBR_REQ) + \
"=3,1,\"APN\",\"CMNET\"");
sapbar_status = air202_sapbar_apn_req;
dev_time = millis();
while(millis() - dev_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_sapbar_apn_success"));
sapbar_status = air202_sapbar_apn_success;
break;
}
}
}
if (sapbar_status != air202_sapbar_apn_success) return false;
streamPrint(STRING_format(BLINEKR_CMD_SAPBR_REQ) + "=5,1");
sapbar_status = air202_sapbar_save_req;
dev_time = millis();
while(millis() - dev_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_sapbar_save_success"));
sapbar_status = air202_sapbar_save_success;
break;
}
}
}
if (sapbar_status != air202_sapbar_save_success) return false;
streamPrint(STRING_format(BLINEKR_CMD_SAPBR_REQ) + "=1,1");
sapbar_status = air202_sapbar_fresh_req;
dev_time = millis();
while(millis() - dev_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_sapbar_fresh_success"));
sapbar_status = air202_sapbar_fresh_success;
break;
}
}
}
if (sapbar_status != air202_sapbar_fresh_success) return false;
return true;
}
String getIMEI()
{
uint32_t dev_time = millis();
streamPrint(BLINEKR_CMD_CGSN_REQ);
char _imei[16];
while(millis() - dev_time < _airTimeout)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), streamData,
BLINKER_F(", length: "), strlen(streamData));
if (strlen(streamData) == 15)
{
strcpy(_imei, streamData);
}
}
}
dev_time = millis();
while(millis() - dev_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), _imei,
BLINKER_F(", length: "), strlen(streamData));
return _imei;
}
}
}
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), _imei,
BLINKER_F(", length: "), strlen(streamData));
return _imei;
}
String getICCID()
{
uint32_t dev_time = millis();
streamPrint(BLINKER_CMD_ICCID_REQ);
char _iccid[21];
while(millis() - dev_time < _airTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_ICCID)
{
String get_iccid = _masterAT->getParam(0);
if (_masterAT->getParam(0).length() == 20)
{
strcpy(_iccid, _masterAT->getParam(0).c_str());
free(_masterAT);
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), _iccid,
BLINKER_F(", length: "), strlen(_iccid));
break;
}
}
free(_masterAT);
}
}
dev_time = millis();
while(millis() - dev_time < _airTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), _iccid,
BLINKER_F(", length: "), strlen(streamData));
return _iccid;
}
}
}
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), _iccid,
BLINKER_F(", length: "), strlen(streamData));
return _iccid;
}
bool isReboot()
{
streamPrint(BLINKER_CMD_AT);
uint32_t dev_time = millis();
while(millis() - dev_time < _airTimeout)
{
if (available())
{
if (strncmp(streamData, BLINKER_CMD_AT, 2) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("device reboot"));
// SAPBR();
streamPrint("ATE0");
return true;
}
// else if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("device not reboot"));
// return false;
// }
}
}
streamPrint("ATE0");
return false;
}
void flush()
{
if (isFresh) free(streamData);
}
protected :
class BlinkerMasterAT * _masterAT;
blinker_callback_t listenFunc = NULL;
Stream* stream;
// char streamData[128];
char* streamData;
bool isFresh = false;
bool isHWS = false;
uint16_t _airTimeout = 1000;
// time_t _ntpTime = 0;
float _timezone = 8.0;
char _LANG[14];
char _LAT[14];
// int16_t _year = -1;
// int8_t _mon = -1;
// int8_t _mday = -1;
// int16_t _hour = -1;
// int8_t _mins = -1;
// int8_t _secs = -1;
void streamPrint(const String & s)
{
stream->println(s);
BLINKER_LOG_ALL(s);
}
int timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
bool available()
{
yield();
if (!isHWS)
{
// #if defined(__AVR__) || defined(ESP8266)
// if (!SSerial_API->isListening())
// {
// SSerial_API->listen();
// ::delay(100);
// }
// #endif
if (listenFunc) listenFunc();
}
// char _data[BLINKER_AIR202_DATA_BUFFER_SIZE];// = { '\0' };
// memset(_data, '\0', BLINKER_AIR202_DATA_BUFFER_SIZE);
if (stream->available())
{
// strcpy(_data, stream->readStringUntil('\n').c_str());
String _data = stream->readStringUntil('\n');
BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), _data);
// _data[strlen(_data) - 1] = '\0';
if (isFresh)
{
free(streamData);
isFresh = false;
}
if (_data.length() <= 1) return false;
streamData = (char*)malloc((_data.length() + 1)*sizeof(char));
strcpy(streamData, _data.c_str());
if (_data.length() > 0) streamData[_data.length() - 1] = '\0';
isFresh = true;
return true;
// // streamData = "";
// // memset(streamData, '\0', 128);
// if (isFresh) free(streamData);
// streamData = (char*)malloc(1*sizeof(char));
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // int16_t dNum = strlen(streamData);
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), streamData,
// // BLINKER_F(", dNum: "), dNum);
// // // stream->readString();
// int16_t dNum = 0;
// int c_d = timedRead();
// while (dNum < BLINKER_MAX_READ_SIZE &&
// c_d >=0 && c_d != '\n')
// {
// // if (c_d != '\r')
// {
// streamData[dNum] = (char)c_d;
// dNum++;
// streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
// }
// c_d = timedRead();
// }
// // dNum++;
// // // // streamData = (char*)realloc(streamData, dNum*sizeof(char));
// // streamData[dNum-1] = '\0';
// // stream->flush();
// // 7b2264657461696c223a207b2262726f6b6572223a2022616c6979756e222c20226465766963654e616d65223a20223237383636394232304d3235423634323230354e33435850222c2022696f744964223a20225346455467613255784b784e386a69716e4e516730303130356435343030222c2022696f74546f6b656e223a20226331353530643464346334623432666338376431343639373333363166353539222c202270726f647563744b6579223a20224a67434762486c6e64677a222c202275756964223a20223733633762356134623266323231633061373264376234313238653430323337227d2c20226d657373616765223a20313030307d
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// // BLINKER_F(" , dNum: "), dNum);
// // BLINKER_LOG_FreeHeap_ALL();
// if (dNum < BLINKER_MAX_READ_SIZE && dNum > 0)
// {
// // if (streamData[dNum - 1] == '\r')
// streamData[dNum - 1] = '\0';
// BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// BLINKER_F(" , dNum: "), dNum);
// isFresh = true;
// return true;
// }
// else
// {
// return false;
// }
}
else
{
return false;
}
}
};
// BlinkerAIR202 BLINKER_AIR202;
#endif

View File

@@ -0,0 +1,619 @@
#ifndef BLINKER_ALIGENIE_H
#define BLINKER_ALIGENIE_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BLINKERALIGENIE
{
public :
BLINKERALIGENIE() {}
void attachPowerState(blinker_callback_with_string_uint8_arg_t newFunction)
{
Blinker.attachAliGenieSetPowerState(newFunction);
}
void attachPowerState(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachAliGenieSetPowerState(newFunction);
}
void attachHSwing(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachAliGenieSetHSwingState(newFunction);
}
void attachVSwing(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachAliGenieSetVSwingState(newFunction);
}
void attachColor(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachAliGenieSetColor(newFunction);
}
void attachMode(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachAliGenieSetMode(newFunction);
}
void attachCancelMode(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachAliGenieSetcMode(newFunction);
}
void attachLevel(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachAliGenieSetLevel(newFunction);
}
void attachLevel(blinker_callback_with_uint8_arg_t newFunction)
{
Blinker.attachAliGenieSetLevel(newFunction);
}
void attachRelativeLevel(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachAliGenieRelativeLevel(newFunction);
}
void attachTemp(blinker_callback_with_uint8_arg_t newFunction)
{
Blinker.attachAliGenieSetTemp(newFunction);
}
void attachRelativeTemp(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachAliGenieRelativeTemp(newFunction);
}
void attachBrightness(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachAliGenieSetBrightness(newFunction);
}
void attachRelativeBrightness(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachAliGenieRelativeBrightness(newFunction);
}
void attachColorTemperature(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachAliGenieSetColorTemperature(newFunction);
}
void attachRelativeColorTemperature(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachAliGenieRelativeColorTemperature(newFunction);
}
void attachQuery(blinker_callback_with_int32_uint8_arg_t newFunction)
{
Blinker.attachAliGenieQuery(newFunction);
}
void attachQuery(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachAliGenieQuery(newFunction);
}
void powerState(const String & state, uint8_t num)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_POWERSTATE);
payload += BLINKER_F("\":\"");
payload += state;
if (num != 0)
{
payload += BLINKER_F("\",\"num\":");
payload += STRING_format(num);
}
else
{
payload += BLINKER_F("\"");
}
// Blinker.aligeniePrint(payload);
if (_fresh >> 0 & 0x01) {
free(aState);
}
aState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aState, payload.c_str());
_fresh |= 0x01 << 0;
}
void powerState(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_POWERSTATE);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 0 & 0x01) {
free(aState);
}
aState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aState, payload.c_str());
_fresh |= 0x01 << 0;
}
void color(const String & clr)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLOR_);
payload += BLINKER_F("\":\"");
payload += clr;
payload += BLINKER_F("\",");
payload += BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLOR);
payload += BLINKER_F("\":\"");
payload += clr;
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 1 & 0x01) {
free(aColor);
}
aColor = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aColor, payload.c_str());
_fresh |= 0x01 << 1;
}
void mode(const String & md)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_MODE);
payload += BLINKER_F("\":\"");
payload += md;
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 2 & 0x01) {
free(aMode);
}
aMode = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aMode, payload.c_str());
_fresh |= 0x01 << 2;
}
void colorTemp(int clrTemp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLORTEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(clrTemp);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 3 & 0x01) {
free(aCtemp);
}
aCtemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCtemp, payload.c_str());
_fresh |= 0x01 << 3;
}
void brightness(int bright)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_BRIGHTNESS);
payload += BLINKER_F("\":\"");
payload += STRING_format(bright);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 4 & 0x01) {
free(aBright);
}
aBright = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aBright, payload.c_str());
_fresh |= 0x01 << 4;
}
void temp(double _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void temp(float _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void temp(int _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void humi(double _humi)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HUMI);
payload += BLINKER_F("\":\"");
payload += STRING_format(_humi);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 6 & 0x01) {
free(aHumi);
}
aHumi = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aHumi, payload.c_str());
_fresh |= 0x01 << 6;
}
void humi(float _humi)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HUMI);
payload += BLINKER_F("\":\"");
payload += STRING_format(_humi);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 6 & 0x01) {
free(aHumi);
}
aHumi = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aHumi, payload.c_str());
_fresh |= 0x01 << 6;
}
void humi(int _humi)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HUMI);
payload += BLINKER_F("\":\"");
payload += STRING_format(_humi);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 6 & 0x01) {
free(aHumi);
}
aHumi = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aHumi, payload.c_str());
_fresh |= 0x01 << 6;
}
void pm25(double _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void pm25(float _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void pm25(int _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void level(uint8_t _level)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_LEVEL);
payload += BLINKER_F("\":\"");
payload += STRING_format(_level);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 8 & 0x01) {
free(aLevel);
}
aLevel = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aLevel, payload.c_str());
_fresh |= 0x01 << 8;
}
void level(const String & _level)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_LEVEL);
payload += BLINKER_F("\":\"");
payload += _level;
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 8 & 0x01) {
free(aLevel);
}
aLevel = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aLevel, payload.c_str());
_fresh |= 0x01 << 8;
}
void hswing(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HSTATE);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
if (_fresh >> 9 & 0x01) {
free(ahState);
}
ahState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(ahState, payload.c_str());
_fresh |= 0x01 << 9;
}
void vswing(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_VSTATE);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
if (_fresh >> 10 & 0x01) {
free(avState);
}
avState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(avState, payload.c_str());
_fresh |= 0x01 << 10;
}
void print()
{
if (_fresh == 0) return;
String aliData;
if (_fresh >> 0 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aState;
free(aState);
}
if (_fresh >> 1 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aColor;
free(aColor);
}
if (_fresh >> 2 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aMode;
free(aMode);
}
if (_fresh >> 3 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aCtemp;
free(aCtemp);
}
if (_fresh >> 4 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aBright;
free(aBright);
}
if (_fresh >> 5 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aTemp;
free(aTemp);
}
if (_fresh >> 6 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aHumi;
free(aHumi);
}
if (_fresh >> 7 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aPm25;
free(aPm25);
}
if (_fresh >> 8 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += aLevel;
free(aLevel);
}
if (_fresh >> 9 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += ahState;
free(ahState);
}
if (_fresh >> 10 & 0x01) {
if (aliData.length()) aliData += BLINKER_F(",");
else aliData += BLINKER_F("{");
aliData += avState;
free(avState);
}
aliData += BLINKER_F("}");
_fresh = 0;
Blinker.aligeniePrint(aliData);
}
private :
char * aState;
char * aColor;
char * aMode;
char * aCtemp;
char * aBright;
char * aTemp;
char * aHumi;
char * aPm25;
char * aLevel;
char * ahState;
char * avState;
uint16_t _fresh = 0;
};
#endif

View File

@@ -0,0 +1,65 @@
#ifndef BLINKER_BRIDGE_H
#define BLINKER_BRIDGE_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerBridge
{
public :
BlinkerBridge(char _key[], blinker_callback_with_string_arg_t _func = NULL)
{
bNum = Blinker.attachBridge(_key, _func);
}
void attach(blinker_callback_with_string_arg_t _func)
{
if (bNum == 0) return;
Blinker.freshAttachBridge(Blinker.bridgeKey(bNum), _func);
}
template <typename T1>
void print(T1 n1)
{
BLINKER_LOG_ALL(BLINKER_F("Bridge to: "), bNum, BLINKER_F(", data: "), n1);
if (strcmp(Blinker.bridgeName(bNum), BLINKER_CMD_FALSE) != 0)
{
Blinker.bridgePrint(Blinker.bridgeName(bNum), STRING_format(n1));
}
}
template <typename T1, typename T2>
void print(T1 n1, T2 n2)
{
if (strcmp(Blinker.bridgeName(bNum), BLINKER_CMD_FALSE) != 0)
{
String msg = BLINKER_F("{\"");
msg += STRING_format(n1);
msg += BLINKER_F("\":\"");
msg += STRING_format(n2);
msg += BLINKER_F("\"}");
Blinker.bridgePrint(Blinker.bridgeName(bNum), msg);
}
}
// void name(char name[])
// {
// _bName = (char*)malloc((strlen(name)+1)*sizeof(char));
// strcpy(_bName, name);
// }
// char * getName() { return _bName; }
// void freshBridge(const String & name)
// {
// bridgeName = (char*)malloc((name.length()+1)*sizeof(char));
// strcpy(bridgeName, name.c_str());
// }
// char * getBridge() { return bridgeName; }
// bool checkName(char name[]) { return strcmp(_bName, name) == 0; }
private :
uint8_t bNum;
};
#endif

View File

@@ -0,0 +1,220 @@
#ifndef BlinkerButton_H
#define BlinkerButton_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerButton
{
public :
BlinkerButton(char _name[], blinker_callback_with_string_arg_t _func = NULL)
{
wNum = Blinker.attachWidget(_name, _func);
}
void attach(blinker_callback_with_string_arg_t _func)
{
if (wNum == 0) return;
Blinker.freshAttachWidget(Blinker.widgetName_str(wNum), _func);
}
void icon(const String & _icon)
{
if (_fresh >> 0 & 0x01) free(bicon);
bicon = (char*)malloc((_icon.length()+1)*sizeof(char));
strcpy(bicon, _icon.c_str());
_fresh |= 0x01 << 0;
}
void color(const String & _clr)
{
if (_fresh >> 1 & 0x01) free(iconClr);
iconClr = (char*)malloc((_clr.length()+1)*sizeof(char));
strcpy(iconClr, _clr.c_str());
_fresh |= 0x01 << 1;
}
template <typename T>
void content(T _con)
{
if (_fresh >> 2 & 0x01) free(bcon);
String _bcon = STRING_format(_con);
bcon = (char*)malloc((_bcon.length()+1)*sizeof(char));
strcpy(bcon, _bcon.c_str());
_fresh |= 0x01 << 2;
}
template <typename T>
void text(T _text)
{
if (_fresh >> 3 & 0x01) free(btext);
String _btext = STRING_format(_text);
btext = (char*)malloc((_btext.length()+1)*sizeof(char));
strcpy(btext, _btext.c_str());
_fresh |= 0x01 << 3;
}
template <typename T1, typename T2>
void text(T1 _text1, T2 _text2)
{
if (_fresh >> 3 & 0x01) free(btext);
String _btext = STRING_format(_text1);
btext = (char*)malloc((_btext.length()+1)*sizeof(char));
strcpy(btext, _btext.c_str());
_fresh |= 0x01 << 3;
if (_fresh >> 4 & 0x01) free(btext1);
_btext = STRING_format(_text2);
btext1 = (char*)malloc((_btext.length()+1)*sizeof(char));
strcpy(btext1, _btext.c_str());
_fresh |= 0x01 << 4;
}
void textColor(const String & _clr)
{
if (_fresh >> 5 & 0x01) free(textClr);
textClr = (char*)malloc((_clr.length()+1)*sizeof(char));
strcpy(textClr, _clr.c_str());
_fresh |= 0x01 << 5;
}
void print() { print(""); }
void print(const String & _state)
{
if ((_fresh == 0 && _state.length() == 0) || \
wNum == 0)
{
return;
}
String buttonData;
if (_state.length())
{
buttonData += BLINKER_F("{\"");
buttonData += BLINKER_F(BLINKER_CMD_SWITCH);
buttonData += BLINKER_F("\":\"");
buttonData += (_state);
buttonData += BLINKER_F("\"");
}
if (_fresh >> 0 & 0x01)
{
if (buttonData.length()) buttonData += BLINKER_F(",");
else buttonData += BLINKER_F("{");
buttonData += BLINKER_F("\"");
buttonData += BLINKER_F(BLINKER_CMD_ICON);
buttonData += BLINKER_F("\":\"");
buttonData += (bicon);
buttonData += BLINKER_F("\"");
free(bicon);
}
if (_fresh >> 1 & 0x01)
{
if (buttonData.length()) buttonData += BLINKER_F(",");
else buttonData += STRING_format(BLINKER_F("{"));
buttonData += BLINKER_F("\"");
buttonData += BLINKER_F(BLINKER_CMD_COLOR);
buttonData += BLINKER_F("\":\"");
buttonData += (iconClr);
buttonData += BLINKER_F("\"");
free(iconClr);
}
if (_fresh >> 2 & 0x01)
{
if (buttonData.length()) buttonData += BLINKER_F(",");
else buttonData += BLINKER_F("{");
buttonData += BLINKER_F("\"");
buttonData += BLINKER_F(BLINKER_CMD_CONTENT);
buttonData += BLINKER_F("\":\"");
buttonData += (bcon);
buttonData += BLINKER_F("\"");
free(bcon);
}
if (_fresh >> 3 & 0x01)
{
if (buttonData.length()) buttonData += BLINKER_F(",");
else buttonData += BLINKER_F("{");
buttonData += BLINKER_F("\"");
buttonData += BLINKER_F(BLINKER_CMD_TEXT);
buttonData += BLINKER_F("\":\"");
buttonData += (btext);
buttonData += BLINKER_F("\"");
free(btext);
}
if (_fresh >> 4 & 0x01)
{
if (buttonData.length()) buttonData += BLINKER_F(",");
else buttonData += BLINKER_F("{");
buttonData += BLINKER_F("\"");
buttonData += BLINKER_F(BLINKER_CMD_TEXT1);
buttonData += BLINKER_F("\":\"");
buttonData += (btext1);
buttonData += BLINKER_F("\"");
free(btext1);
}
if (_fresh >> 5 & 0x01)
{
if (buttonData.length()) buttonData += BLINKER_F(",");
else buttonData += BLINKER_F("{");
buttonData += BLINKER_F("\"");
buttonData += BLINKER_F(BLINKER_CMD_TEXTCOLOR);
buttonData += BLINKER_F("\":\"");
buttonData += (textClr);
buttonData += BLINKER_F("\"");
free(textClr);
}
buttonData += BLINKER_F("}");
_fresh = 0;
Blinker.printArray(Blinker.widgetName_str(wNum), buttonData);
}
private :
uint8_t wNum;
char * bicon;
char * iconClr;
char * bcon;
char * btext;
char * btext1;
char * textClr;
uint8_t _fresh = 0;
};
#endif

View File

@@ -0,0 +1,69 @@
#ifndef BLINKER_REAL_TIME_DATA_H
#define BLINKER_REAL_TIME_DATA_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerRealTimeData
{
public :
BlinkerRealTimeData(char _name[], blinker_callback_t _func = NULL)
{
Blinker.attachRTData(_name, _func);
}
void saveData(char key[], int32_t data)
{
if (_count = 0)
{
strcpy(_keys, key);
}
_datas[_count] = data;
_times[_count] = Blinker.time();
_count++;
}
void print()
{
String rtData;
if (_count > 0)
{
rtData = BLINKER_F("{\"rt\":\"{\"");
rtData += STRING_format(_keys);
rtData += BLINKER_F("\":[");
for (uint8_t i = 0; i < _count; i++)
{
rtData += BLINKER_F("{\"value\":");
rtData += STRING_format(_datas[i]);
rtData += BLINKER_F(",\"date\":");
rtData += STRING_format(_times[i]);
rtData += BLINKER_F("}");
if (i < _count - 1)
{
rtData += BLINKER_F(",");
}
}
rtData += BLINKER_F("]}}");
Blinker.printObject(Blinker.widgetName_str(wNum), buttonData);
_count = 0;
}
}
private :
uint8_t wNum;
uint8_t _count = 0;
int32_t _datas[12];
time_t _times[12];
char _keys[12];
};
#endif

View File

@@ -0,0 +1,935 @@
#ifndef BLINKER_DUEROS_H
#define BLINKER_DUEROS_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BLINKERDUEROS
{
public :
BLINKERDUEROS() {}
void attachPowerState(blinker_callback_with_string_uint8_arg_t newFunction)
{
Blinker.attachDuerOSSetPowerState(newFunction);
}
void attachPowerState(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachDuerOSSetPowerState(newFunction);
}
void attachColor(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachDuerOSSetColor(newFunction);
}
void attachMode(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachDuerOSSetMode(newFunction);
}
void attachCancelMode(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachDuerOSSetcMode(newFunction);
}
void attachLevel(blinker_callback_with_uint8_arg_t newFunction)
{
Blinker.attachDuerOSSetLevel(newFunction);
}
void attachRelativeLevel(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachDuerOSRelativeLevel(newFunction);
}
void attachTemp(blinker_callback_with_uint8_arg_t newFunction)
{
Blinker.attachDuerOSSetTemp(newFunction);
}
void attachRelativeTemp(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachDuerOSRelativeTemp(newFunction);
}
void attachBrightness(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachDuerOSSetBrightness(newFunction);
}
void attachRelativeBrightness(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachDuerOSRelativeBrightness(newFunction);
}
void attachColorTemperature(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachDuerOSSetColorTemperature(newFunction);
}
void attachRelativeColorTemperature(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachDuerOSRelativeColorTemperature(newFunction);
}
void attachQuery(blinker_callback_with_int32_uint8_arg_t newFunction)
{
Blinker.attachDuerOSQuery(newFunction);
}
void attachQuery(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachDuerOSQuery(newFunction);
}
void powerState(const String & state, uint8_t num)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_POWERSTATE);
payload += BLINKER_F("\":\"");
payload += state;
if (num != 0)
{
payload += BLINKER_F("\",\"num\":");
payload += STRING_format(num);
}
else
{
payload += BLINKER_F("\"");
}
// Blinker.DuerOSPrint(payload);
if (_fresh >> 0 & 0x01) {
free(aState);
}
aState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aState, payload.c_str());
_fresh |= 0x01 << 0;
}
void powerState(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_POWERSTATE);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 0 & 0x01) {
free(aState);
}
aState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aState, payload.c_str());
_fresh |= 0x01 << 0;
}
void color(int32_t clr)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLOR_);
payload += BLINKER_F("\":\"");
payload += clr;
payload += BLINKER_F("\",");
payload += BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLOR);
payload += BLINKER_F("\":\"");
payload += clr;
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 1 & 0x01) {
free(aColor);
}
aColor = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aColor, payload.c_str());
_fresh |= 0x01 << 1;
}
void mode(const String & now_md)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_MODE);
payload += BLINKER_F("\":[\"\",\"");
payload += now_md;
payload += BLINKER_F("\"]");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 2 & 0x01) {
free(aMode);
}
aMode = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aMode, payload.c_str());
_fresh |= 0x01 << 2;
}
void mode(const String & pre_md, const String & now_md)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_MODE);
payload += BLINKER_F("\":[\"");
payload += pre_md;
payload += BLINKER_F("\",\"");
payload += now_md;
payload += BLINKER_F("\"]");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 2 & 0x01) {
free(aMode);
}
aMode = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aMode, payload.c_str());
_fresh |= 0x01 << 2;
}
void colorTemp(int clrTemp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLORTEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(clrTemp);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 3 & 0x01) {
free(aCtemp);
}
aCtemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCtemp, payload.c_str());
_fresh |= 0x01 << 3;
}
void brightness(int now_bright)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_BRIGHTNESS);
payload += BLINKER_F("\":[\"\",");
payload += STRING_format(now_bright);
payload += BLINKER_F("]");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 4 & 0x01) {
free(aBright);
}
aBright = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aBright, payload.c_str());
_fresh |= 0x01 << 4;
}
void brightness(int pre_bright, int now_bright)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_BRIGHTNESS);
payload += BLINKER_F("\":[");
payload += STRING_format(pre_bright);
payload += BLINKER_F(",");
payload += STRING_format(now_bright);
payload += BLINKER_F("]");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 4 & 0x01) {
free(aBright);
}
aBright = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aBright, payload.c_str());
_fresh |= 0x01 << 4;
}
void temp(double _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void temp(float _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void temp(int _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void humi(double _humi)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HUMI);
payload += BLINKER_F("\":\"");
payload += STRING_format(_humi);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 6 & 0x01) {
free(aHumi);
}
aHumi = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aHumi, payload.c_str());
_fresh |= 0x01 << 6;
}
void humi(float _humi)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HUMI);
payload += BLINKER_F("\":\"");
payload += STRING_format(_humi);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 6 & 0x01) {
free(aHumi);
}
aHumi = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aHumi, payload.c_str());
_fresh |= 0x01 << 6;
}
void humi(int _humi)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HUMI);
payload += BLINKER_F("\":\"");
payload += STRING_format(_humi);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 6 & 0x01) {
free(aHumi);
}
aHumi = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aHumi, payload.c_str());
_fresh |= 0x01 << 6;
}
void pm25(double _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void pm25(float _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void pm25(int _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void pm10(double _pm10)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM10);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm10);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 8 & 0x01) {
free(aPm10);
}
aPm10 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm10, payload.c_str());
_fresh |= 0x01 << 8;
}
void pm10(float _pm10)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM10);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm10);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 8 & 0x01) {
free(aPm10);
}
aPm10 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm10, payload.c_str());
_fresh |= 0x01 << 8;
}
void pm10(int _pm10)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM10);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm10);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 8 & 0x01) {
free(aPm10);
}
aPm10 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm10, payload.c_str());
_fresh |= 0x01 << 8;
}
void co2(double _co2)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_CO2);
payload += BLINKER_F("\":\"");
payload += STRING_format(_co2);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 9 & 0x01) {
free(aCO2);
}
aCO2 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCO2, payload.c_str());
_fresh |= 0x01 << 9;
}
void co2(float _co2)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_CO2);
payload += BLINKER_F("\":\"");
payload += STRING_format(_co2);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 9 & 0x01) {
free(aCO2);
}
aCO2 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCO2, payload.c_str());
_fresh |= 0x01 << 9;
}
void co2(int _co2)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_CO2);
payload += BLINKER_F("\":\"");
payload += STRING_format(_co2);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 9 & 0x01) {
free(aCO2);
}
aCO2 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCO2, payload.c_str());
_fresh |= 0x01 << 9;
}
void aqi(int _aqi)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_AQI);
payload += BLINKER_F("\":\"");
payload += STRING_format(_aqi);
payload += BLINKER_F("\"");
// Blinker.DuerOSPrint(payload);
if (_fresh >> 10 & 0x01) {
free(aAQI);
}
aAQI = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aAQI, payload.c_str());
_fresh |= 0x01 << 10;
}
void time(uint32_t _time)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TIME_ALL);
payload += BLINKER_F("\":");
payload += STRING_format(_time/1000);
// Blinker.DuerOSPrint(payload);
if (_fresh >> 11 & 0x01) {
free(aTIME);
}
aTIME = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTIME, payload.c_str());
_fresh |= 0x01 << 11;
}
void level(uint8_t _level)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_LEVEL);
payload += BLINKER_F("\":");
payload += STRING_format(_level);
// Blinker.DuerOSPrint(payload);
if (_fresh >> 12 & 0x01) {
free(aLevel);
}
aLevel = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aLevel, payload.c_str());
_fresh |= 0x01 << 12;
}
void print()
{
if (_fresh == 0) return;
String duerData;
if (_fresh >> 0 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aState;
free(aState);
}
if (_fresh >> 1 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aColor;
free(aColor);
}
if (_fresh >> 2 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aMode;
free(aMode);
}
if (_fresh >> 3 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aCtemp;
free(aCtemp);
}
if (_fresh >> 4 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aBright;
free(aBright);
}
if (_fresh >> 5 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aTemp;
free(aTemp);
}
if (_fresh >> 6 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aHumi;
free(aHumi);
}
if (_fresh >> 7 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aPm25;
free(aPm25);
}
if (_fresh >> 8 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aPm10;
free(aPm10);
}
if (_fresh >> 9 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aCO2;
free(aCO2);
}
if (_fresh >> 10 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aAQI;
free(aAQI);
}
if (_fresh >> 11 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aTIME;
free(aTIME);
}
if (_fresh >> 12 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aLevel;
free(aLevel);
}
duerData += BLINKER_F("}");
_fresh = 0;
Blinker.duerPrint(duerData);
}
void report()
{
if (_fresh == 0) return;
String duerData;
bool isCheck = false;
if (_fresh >> 0 & 0x01) {
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aState;
isCheck = true;
free(aState);
}
if (_fresh >> 1 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aColor;
isCheck = true;
}
free(aColor);
}
if (_fresh >> 2 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aMode;
isCheck = true;
}
free(aMode);
}
if (_fresh >> 3 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aBright;
isCheck = true;
}
free(aBright);
}
if (_fresh >> 4 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aTemp;
isCheck = true;
}
free(aTemp);
}
if (_fresh >> 5 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aHumi;
isCheck = true;
}
free(aHumi);
}
if (_fresh >> 6 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aPm25;
isCheck = true;
}
free(aPm25);
}
if (_fresh >> 7 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aPm10;
isCheck = true;
}
free(aPm10);
}
if (_fresh >> 8 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aCO2;
isCheck = true;
}
free(aCO2);
}
if (_fresh >> 9 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aAQI;
isCheck = true;
}
free(aAQI);
}
if (_fresh >> 10 & 0x01) {
if (!isCheck)
{
if (duerData.length()) duerData += BLINKER_F(",");
else duerData += BLINKER_F("{");
duerData += aTIME;
isCheck = true;
}
free(aTIME);
}
duerData += BLINKER_F("}");
_fresh = 0;
Blinker.duerPrint(duerData, true);
}
private :
char * aState;
char * aColor;
char * aMode;
char * aCtemp;
char * aBright;
char * aTemp;
char * aHumi;
char * aPm25;
char * aPm10;
char * aCO2;
char * aAQI;
char * aTIME;
char * aLevel;
uint16_t _fresh = 0;
};
#endif

View File

@@ -0,0 +1,32 @@
#ifndef BLINKER_EVENT_H
#define BLINKER_EVENT_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BLINKEREVENT
{
public :
BLINKEREVENT()
{}
void warning(const String & msg)
{
Blinker.eventWarn(msg);
}
void error(const String & msg)
{
Blinker.eventError(msg);
}
void message(const String & msg)
{
Blinker.eventMsg(msg);
}
private :
};
#endif

View File

@@ -0,0 +1,881 @@
#ifndef BLINKER_HTTP_AIR202_H
#define BLINKER_HTTP_AIR202_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
// #include "Adapters/BlinkerSerialM QTT.h"
#include "../Blinker/BlinkerATMaster.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
// #if defined(ESP32)
// #include <HardwareSerial.h>
// HardwareSerial *HSerial_HTTP;
// #else
// #include <SoftwareSerial.h>
// SoftwareSerial *SSerial_HTTP;
// #endif
#define BLINKER_HTTP_AIR202_DEFAULT_TIMEOUT 5000UL
#define BLINKER_HTTP_AIR202_DATA_BUFFER_SIZE 1024
enum air202_http_status_t
{
air202_init,
air202_init_success,
air202_ver_check,
air202_ver_check_success,
air202_cgtt,
air202_cgtt_resp,
air202_cgtt_success,
air202_spbar_1,
air202_spbar_1_success,
air202_spbar_2,
air202_spbar_2_success,
air202_http_init,
air202_http_init_success,
air202_http_init_failed,
air202_http_para_set,
air202_http_para_set_success,
air202_http_para_set_failed,
air202_http_start,
air202_http_start_success,
air202_http_start_failed,
air202_http_data_set,
air202_http_data_set_success,
air202_http_data_set_failed,
air202_http_data_post,
air202_http_data_post_success,
air202_http_data_post_failed,
air202_http_upload_success,
air202_http_upload_failed,
air202_http_read_response,
air202_http_read_success,
air202_http_read_failed,
air202_http_read_paylaod,
air202_http_end,
air202_http_end_failed,
air202_http_end_success
};
enum air202_status_sap_t
{
air202_sap_cgtt_state_ver_check,
air202_sap_cgtt_state_ver_check_success,
air202_sap_cgtt_state,
air202_sap_cgtt_state_resp,
air202_sap_cgtt_state_success,
air202_sap_sapbar_pdp_REQ,
air202_sap_sapbar_pdp_success,
air202_sap_sapbar_pdp_failed,
air202_sap_sapbar_apn_REQ,
air202_sap_sapbar_apn_success,
air202_sap_sapbar_apn_failed,
air202_sap_sapbar_save_REQ,
air202_sap_sapbar_save_success,
air202_sap_sapbar_save_failed,
air202_sap_sapbar_fresh_REQ,
air202_sap_sapbar_fresh_success,
air202_sap_sapbar_fresh_failed,
};
class BlinkerHTTPAIR202
{
public :
BlinkerHTTPAIR202(Stream& s, bool isHardware, blinker_callback_t func)
{ stream = &s; isHWS = isHardware; listenFunc = func; }
~BlinkerHTTPAIR202() { flush(); }
void streamPrint(const String & s)
{
// stream->flush();
BLINKER_LOG_ALL(s);
stream->println(s);
}
int checkCGTT()
{
uint32_t http_time = millis();
air202_http_status_t http_status = air202_init;
streamPrint(BLINKER_CMD_AT);
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_init_success"));
http_status = air202_init_success;
break;
}
}
}
if (http_status != air202_init_success) return false;
streamPrint(BLINKER_CMD_CGMMR_REQ);
http_status = air202_ver_check;
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_CGMMR_RESP) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_ver_check_success"));
http_status = air202_ver_check_success;
break;
}
}
}
if (http_status != air202_ver_check_success) return false;
streamPrint(BLINKER_CMD_CGQTT_REQ);
http_status = air202_cgtt;
while(millis() - http_time < _httpTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CGATT &&
_masterAT->getParam(0).toInt() == 1)
{
BLINKER_LOG_ALL(BLINKER_F("air202_cgtt_resp"));
http_status = air202_cgtt_resp;
}
free(_masterAT);
break;
}
}
if (http_status != air202_cgtt_resp) return false;
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_cgtt_success"));
http_status = air202_cgtt_success;
break;
}
}
}
if (http_status != air202_cgtt_success) return false;
// stream->println(BLINKER_CMD_CGQTT_REQ);
// http_status = air202_cgtt;
}
bool begin(String host, String uri) { _host = host; _uri = uri; }
void setTimeout(uint16_t timeout) { _httpTimeout = timeout; }
bool GET()
{
// streamPrint(STRING_format(BLINKER_CMD_MDISCONNECT_REQ));
// uint32_t mqtt_time = millis();
// uint8_t status_get = 0;
// while(millis() - mqtt_time < _httpTimeout)
// {
// if (available())
// {
// BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
// if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
// // return true;
// break;
// }
// }
// }
// streamPrint(STRING_format(BLINKER_CMD_MIPCLOSE_REQ));
// mqtt_time = millis();
// while(millis() - mqtt_time < _httpTimeout)
// {
// if (available())
// {
// BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
// if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
// // return true;
// break;
// }
// }
// }
// streamPrint(STRING_format(BLINKER_CMD_CIPSHUT_REQ));
// mqtt_time = millis();
// while(millis() - mqtt_time < _httpTimeout)
// {
// if (available())
// {
// BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
// if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
// return true;
// // break;
// }
// }
// }
streamPrint(BLINKER_CMD_HTTPINIT_REQ);
uint32_t http_time = millis();
air202_http_status_t http_status = air202_http_init;
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_init_success"));
http_status = air202_http_init_success;
break;
}
}
}
if (http_status != air202_http_init_success)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_init_failed"));
return false;
}
streamPrint(STRING_format(BLINKER_CMD_HTTPPARA_REQ) + "=\"CID\",1");
http_status = air202_http_para_set;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_para_set_success 1"));
http_status = air202_http_para_set_success;
break;
}
}
}
if (http_status != air202_http_para_set_success) return false;
streamPrint(STRING_format(BLINKER_CMD_HTTPPARA_REQ) + \
"=\"URL\",\"" + _host + _uri + "\"");
http_status = air202_http_para_set;
http_time = millis();
while(millis() - http_time < _httpTimeout*2)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_para_set_success 2"));
http_status = air202_http_para_set_success;
break;
}
else if (strcmp(streamData, BLINKER_CMD_ERROR) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_para_set_failed 2"));
// streamPrint("AT+CPOWD");
break;
}
}
}
// http_status = air202_http_para_set_success;// TBD
if (http_status != air202_http_para_set_success)
{
// streamPrint(STRING_format(BLINKER_CMD_HTTPERM_REQ));
// streamPrint("AT+CPOWD");
return false;
}
streamPrint(STRING_format(BLINKER_CMD_HTTPACTION_REQ) + "=0");
http_status = air202_http_start;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_start_success"));
http_status = air202_http_start_success;
break;
}
else if (strcmp(streamData, BLINKER_CMD_ERROR) == 0)
{
streamPrint(STRING_format(BLINKER_CMD_HTTPREAD_REQ));
::delay(500);
streamPrint(STRING_format(BLINKER_CMD_HTTPERM_REQ));
::delay(500);
break;
}
}
}
if (http_status != air202_http_start_success)
{
streamPrint(STRING_format(BLINKER_CMD_HTTPERM_REQ));
return false;
}
http_time = millis();
while(millis() - http_time < _httpTimeout*10)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_HTTPACTION)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_upload_success"));
http_status = air202_http_upload_success;
free(_masterAT);
break;
}
free(_masterAT);
// break;
}
}
// if (http_status != air202_http_upload_success) return false;
streamPrint(STRING_format(BLINKER_CMD_HTTPREAD_REQ));
http_status = air202_http_read_response;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_HTTPREAD)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_read_success"));
http_status = air202_http_read_success;
free(_masterAT);
break;
}
free(_masterAT);
// break;
}
}
if (http_status != air202_http_read_success) return false;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) != 0)
{
// payload = streamData;
if (isFreshPayload) free(payload);
isFreshPayload = true;
// char data_buff[1024] = { '\0' };
payload = (char*)malloc((strlen(streamData) + 1)*sizeof(char));
strcpy(payload, streamData);
BLINKER_LOG_ALL(BLINKER_F("payload: "), payload);
}
}
}
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_HTTPERM_REQ));
http_status = air202_http_end;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_end_success"));
http_status = air202_http_end_success;
return true;
}
}
}
if (http_status != air202_http_end_success) return false;
}
bool POST(String _msg, String _type, String _application)
{
uint32_t http_time = millis();
air202_http_status_t http_status = air202_http_init;
streamPrint(BLINKER_CMD_HTTPINIT_REQ);
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_init_success"));
http_status = air202_http_init_success;
break;
}
}
}
if (http_status != air202_http_init_success) return false;
streamPrint(STRING_format(BLINKER_CMD_HTTPPARA_REQ) + "=\"CID\",1");
http_status = air202_http_para_set;
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_para_set_success 1"));
http_status = air202_http_para_set_success;
break;
}
}
}
if (http_status != air202_http_para_set_success) return false;
streamPrint(STRING_format(BLINKER_CMD_HTTPPARA_REQ) + \
"=\"URL\",\"" + _host + _uri + "\"");
http_status = air202_http_para_set;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_para_set_success 2"));
http_status = air202_http_para_set_success;
break;
}
}
}
if (http_status != air202_http_para_set_success) return false;
// streamPrint(STRING_format(BLINKER_CMD_HTTPPARA_REQ) + \
// "=\"" + _type + "\",\"" + _application + "\"");
// http_status = air202_http_para_set;
// http_time = millis();
// while(millis() - http_time < _httpTimeout)
// {
// if (available())
// {
// if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("air202_http_para_set_success 3"));
// http_status = air202_http_para_set_success;
// break;
// }
// }
// }
// if (http_status != air202_http_para_set_success) return false;
// _msg.replace("\"", "\\22");
// _msg = "\"" + _msg + "\"";
streamPrint(STRING_format(BLINKER_CMD_HTTPDATA_REQ) + \
"=" + _msg.length() + ",10000");
http_status = air202_http_data_set;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_DOWNLOAD) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_data_set_success"));
http_status = air202_http_data_set_success;
break;
}
}
}
if (http_status != air202_http_data_set_success) return false;
streamPrint(_msg);
http_status = air202_http_data_post;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_data_post_success"));
http_status = air202_http_data_post_success;
break;
}
}
}
if (http_status != air202_http_data_post_success) return false;
streamPrint(STRING_format(BLINKER_CMD_HTTPACTION_REQ) + "=1");
http_status = air202_http_start;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_start_success"));
http_status = air202_http_start_success;
break;
}
}
}
if (http_status != air202_http_start_success)
{
streamPrint(STRING_format(BLINKER_CMD_HTTPERM_REQ));
return false;
}
http_time = millis();
while(millis() - http_time < _httpTimeout*2)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_HTTPACTION)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_upload_success"));
http_status = air202_http_upload_success;
free(_masterAT);
break;
}
free(_masterAT);
// break;
}
}
if (http_status != air202_http_upload_success)
{
streamPrint(STRING_format(BLINKER_CMD_HTTPERM_REQ));
return false;
}
streamPrint(STRING_format(BLINKER_CMD_HTTPREAD_REQ));
http_status = air202_http_read_response;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_HTTPREAD)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_read_success, data len: "), _masterAT->getParam(0));
http_status = air202_http_read_success;
free(_masterAT);
break;
}
free(_masterAT);
// break;
}
}
if (http_status != air202_http_read_success) return false;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) != 0)
{
// payload = streamData;
if (isFreshPayload) free(payload);
isFreshPayload = true;
char data_buff[1024] = { '\0' };
payload = (char*)malloc((strlen(streamData) + 1)*sizeof(char));
strcpy(payload, streamData);
BLINKER_LOG_ALL(BLINKER_F("payload: "), payload);
}
}
}
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_HTTPERM_REQ));
http_status = air202_http_end;
http_time = millis();
while(millis() - http_time < _httpTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("air202_http_end_success"));
http_status = air202_http_end_success;
break;
}
}
}
if (http_status != air202_http_end_success) return false;
return true;
}
String getString()
{
return payload;//TBD
}
void flush()
{
if (isFreshPayload) free(payload);
if (isFresh) free(streamData);
}
protected :
class BlinkerMasterAT * _masterAT;
Stream* stream;
// String streamData;
// char streamData[1024];
char* streamData;
// String payload = "";
char* payload;
bool isFreshPayload = false;
bool isFresh = false;
bool isHWS = false;
String _host;
String _uri;
blinker_callback_t listenFunc = NULL;
uint16_t _httpTimeout = BLINKER_HTTP_AIR202_DEFAULT_TIMEOUT;
int timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
bool available()
{
yield();
if (!isHWS)
{
// #if defined(__AVR__) || defined(ESP8266)
// if (!SSerial_HTTP->isListening())
// {
// SSerial_HTTP->listen();
// ::delay(100);
// }
// #endif
if (listenFunc) listenFunc();
}
// char _data[BLINKER_HTTP_AIR202_DATA_BUFFER_SIZE];// = { '\0' };
// memset(_data, '\0', BLINKER_HTTP_AIR202_DATA_BUFFER_SIZE);
// if (!isFresh) streamData = (char*)malloc(BLINKER_HTTP_AIR202_DATA_BUFFER_SIZE*sizeof(char));
// else memset(streamData, '\0', BLINKER_HTTP_AIR202_DATA_BUFFER_SIZE);
if (stream->available())
{
// strcpy(_data, stream->readStringUntil('\n').c_str());
String _data = stream->readStringUntil('\n');
BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), _data, BLINKER_F(", len: "), _data.length());
// _data[strlen(_data) - 1] = '\0';
if (isFresh)
{
free(streamData);
isFresh = false;
}
if (_data.length() <= 1) return false;
streamData = (char*)malloc((_data.length() + 1)*sizeof(char));
strcpy(streamData, _data.c_str());
if (_data.length() > 0 && streamData[_data.length() - 1] == '\r') streamData[_data.length() - 1] = '\0';
isFresh = true;
return true;
// if (isFresh) free(streamData);
// streamData = (char*)malloc(1*sizeof(char));
// // streamData = "";
// // memset(streamData, '\0', 1024);
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), stream->readString());
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // int16_t dNum = strlen(streamData);
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), streamData,
// // BLINKER_F(", dNum: "), dNum);
// int16_t dNum = 0;
// int c_d = timedRead();
// while (dNum < BLINKER_MAX_READ_SIZE &&
// c_d >=0 && c_d != '\n')
// {
// // if (c_d != '\r')
// {
// streamData[dNum] = (char)c_d;
// dNum++;
// streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
// }
// c_d = timedRead();
// }
// // dNum++;
// // // streamData = (char*)realloc(streamData, dNum*sizeof(char));
// // streamData[dNum-1] = '\0';
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // int16_t dNum = strlen(streamData);
// // streamData[dNum-1] = '\0';
// // stream->flush();
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// // BLINKER_F(" , dNum: "), dNum);
// // BLINKER_LOG_FreeHeap_ALL();
// if (dNum < BLINKER_MAX_READ_SIZE && dNum > 0)
// {
// // if (streamData[dNum - 1] == '\r')
// streamData[dNum - 1] = '\0';
// BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// BLINKER_F(" , dNum: "), dNum);
// isFresh = true;
// return true;
// }
// else
// {
// return false;
// }
}
else
{
return false;
}
}
};
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,43 @@
#ifndef BlinkerImage_H
#define BlinkerImage_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerImage
{
public :
BlinkerImage(char _name[], blinker_callback_with_int32_arg_t _func = NULL)
{
wNum = Blinker.attachWidget(_name, _func);
// numName = (char*)malloc((strlen(_name)+1)*sizeof(char));
// strcpy(numName, _name);
}
void attach(blinker_callback_with_int32_arg_t _func)
{
if (wNum == 0) return;
Blinker.freshAttachWidget(Blinker.widgetName_int(wNum), _func);
}
uint8_t getNum() { return num; }
void print(uint8_t _num)
{
num = _num;
String ImageData = BLINKER_F("{\"img\":");
ImageData += STRING_format(num);
ImageData += BLINKER_F("}");
// Blinker.printArray(numName, ImageData);
Blinker.printArray(Blinker.widgetName_int(wNum), ImageData);
}
private :
uint8_t wNum;
uint8_t num = 0;
};
#endif

View File

@@ -0,0 +1,31 @@
#ifndef BLINKER_JOYSTICK_H
#define BLINKER_JOYSTICK_H
// #if defined(BLINKER_BLE)
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerJoystick
{
public :
BlinkerJoystick(char _name[], blinker_callback_with_joy_arg_t _func = NULL)
// : jName(_name)
{
wNum = Blinker.attachWidget(_name, _func);
}
void attach(blinker_callback_with_joy_arg_t _func)
{
if (wNum == 0) {
return;
}
Blinker.freshAttachWidget(Blinker.widgetName_joy(wNum), _func);
}
private :
uint8_t wNum;
};
// #endif
#endif

View File

@@ -0,0 +1,977 @@
#ifndef BLINKER_MIOT_H
#define BLINKER_MIOT_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BLINKERMIOT
{
public :
BLINKERMIOT() {}
void attachPowerState(blinker_callback_with_string_uint8_arg_t newFunction)
{
Blinker.attachMIOTSetPowerState(newFunction);
}
void attachPowerState(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetPowerState(newFunction);
}
void attachHSwing(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetHSwingState(newFunction);
}
void attachVSwing(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetVSwingState(newFunction);
}
void attachECO(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetECO(newFunction);
}
void attachAnion(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetAnion(newFunction);
}
void attachHeater(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetHeater(newFunction);
}
void attachDryer(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetDryer(newFunction);
}
void attachSleep(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetSleep(newFunction);
}
void attachSoft(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetSoft(newFunction);
}
void attachUV(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetUV(newFunction);
}
void attachUnStraightBlow(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetUNSB(newFunction);
}
void attachColor(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachMIOTSetColor(newFunction);
}
void attachMode(blinker_callback_with_string_string_arg_t newFunction)
{
Blinker.attachMIOTSetMode(newFunction);
}
void attachMode(blinker_callback_with_uint8_arg_t newFunction)
{
Blinker.attachMIOTSetMode(newFunction);
}
void attachLevel(blinker_callback_with_uint8_arg_t newFunction)
{
Blinker.attachMIOTSetLevel(newFunction);
}
void attachTemp(blinker_callback_with_uint8_arg_t newFunction)
{
Blinker.attachMIOTSetTemp(newFunction);
}
void attachHumi(blinker_callback_with_uint8_arg_t newFunction)
{
Blinker.attachMIOTSetHumi(newFunction);
}
// void attachCancelMode(blinker_callback_with_string_arg_t newFunction)
// {
// Blinker.attachMIOTSetcMode(newFunction);
// }
void attachBrightness(blinker_callback_with_string_arg_t newFunction)
{
Blinker.attachMIOTSetBrightness(newFunction);
}
// void attachRelativeBrightness(blinker_callback_with_int32_arg_t newFunction)
// {
// Blinker.attachMIOTRelativeBrightness(newFunction);
// }
void attachColorTemperature(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachMIOTSetColorTemperature(newFunction);
}
// void attachRelativeColorTemperature(blinker_callback_with_int32_arg_t newFunction)
// {
// Blinker.attachMIOTRelativeColorTemperature(newFunction);
// }
void attachQuery(blinker_callback_with_int32_uint8_arg_t newFunction)
{
Blinker.attachMIOTQuery(newFunction);
}
void attachQuery(blinker_callback_with_int32_arg_t newFunction)
{
Blinker.attachMIOTQuery(newFunction);
}
void powerState(const String & state, uint8_t num)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_POWERSTATE);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
if (num != 0)
{
payload += BLINKER_F(",\"num\":");
payload += STRING_format(num);
}
else
{
payload += BLINKER_F("");
}
// Blinker.aligeniePrint(payload);
if (_fresh >> 0 & 0x01) {
free(aState);
}
aState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aState, payload.c_str());
_fresh |= 0x01 << 0;
}
void powerState(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_POWERSTATE);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// if (state == "on")
// {
// payload += "true";
// }
// else
// {
// payload += "false";
// }
// payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 0 & 0x01) {
free(aState);
}
aState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aState, payload.c_str());
_fresh |= 0x01 << 0;
}
void color(uint32_t clr)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLOR_);
payload += BLINKER_F("\":");
payload += STRING_format(clr);
payload += BLINKER_F(",");
payload += BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLOR);
payload += BLINKER_F("\":");
payload += STRING_format(clr);
// Blinker.aligeniePrint(payload);
if (_fresh >> 1 & 0x01) {
free(aColor);
}
aColor = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aColor, payload.c_str());
_fresh |= 0x01 << 1;
}
void mode(uint8_t md)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_MODE);
payload += BLINKER_F("\":\"");
payload += STRING_format(md);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 2 & 0x01) {
free(aMode);
}
aMode = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aMode, payload.c_str());
_fresh |= 0x01 << 2;
}
void mode(const String & mode, const String & state)
{
String payload = BLINKER_F("\"");
payload += mode;
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 2 & 0x01) {
free(aMode);
}
aMode = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aMode, payload.c_str());
_fresh |= 0x01 << 2;
}
void colorTemp(int clrTemp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_COLORTEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(clrTemp);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 3 & 0x01) {
free(aCtemp);
}
aCtemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCtemp, payload.c_str());
_fresh |= 0x01 << 3;
}
void brightness(int bright)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_BRIGHTNESS);
payload += BLINKER_F("\":\"");
payload += STRING_format(bright);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 4 & 0x01) {
free(aBright);
}
aBright = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aBright, payload.c_str());
_fresh |= 0x01 << 4;
}
void temp(double _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void temp(float _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void temp(int _temp)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_TEMP);
payload += BLINKER_F("\":\"");
payload += STRING_format(_temp);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 5 & 0x01) {
free(aTemp);
}
aTemp = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aTemp, payload.c_str());
_fresh |= 0x01 << 5;
}
void humi(int _humi)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HUMI);
payload += BLINKER_F("\":\"");
payload += STRING_format(_humi);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 6 & 0x01) {
free(aHumi);
}
aHumi = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aHumi, payload.c_str());
_fresh |= 0x01 << 6;
}
void pm25(double _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void pm25(float _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void pm25(int _pm25)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_PM25);
payload += BLINKER_F("\":\"");
payload += STRING_format(_pm25);
payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 7 & 0x01) {
free(aPm25);
}
aPm25 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aPm25, payload.c_str());
_fresh |= 0x01 << 7;
}
void co2(double _co2)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_CO2);
payload += BLINKER_F("\":\"");
payload += STRING_format(_co2);
payload += BLINKER_F("\"");
if (_fresh >> 8 & 0x01) {
free(aCO2);
}
aCO2 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCO2, payload.c_str());
_fresh |= 0x01 << 8;
}
void co2(float _co2)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_CO2);
payload += BLINKER_F("\":\"");
payload += STRING_format(_co2);
payload += BLINKER_F("\"");
if (_fresh >> 8 & 0x01) {
free(aCO2);
}
aCO2 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCO2, payload.c_str());
_fresh |= 0x01 << 8;
}
void co2(int _co2)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_CO2);
payload += BLINKER_F("\":\"");
payload += STRING_format(_co2);
payload += BLINKER_F("\"");
if (_fresh >> 8 & 0x01) {
free(aCO2);
}
aCO2 = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aCO2, payload.c_str());
_fresh |= 0x01 << 8;
}
void level(int _lvl)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_LEVEL);
payload += BLINKER_F("\":\"");
payload += STRING_format(_lvl);
payload += BLINKER_F("\"");
if (_fresh >> 9 & 0x01) {
free(aLevel);
}
aLevel = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(aLevel, payload.c_str());
_fresh |= 0x01 << 9;
}
void hswing(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HSTATE);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// if (state == "on")
// {
// payload += "true";
// }
// else
// {
// payload += "false";
// }
// payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 10 & 0x01) {
free(ahState);
}
ahState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(ahState, payload.c_str());
_fresh |= 0x01 << 10;
}
void vswing(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_VSTATE);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// if (state == "on")
// {
// payload += "true";
// }
// else
// {
// payload += "false";
// }
// payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 11 & 0x01) {
free(avState);
}
avState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(avState, payload.c_str());
_fresh |= 0x01 << 11;
}
void eco(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_ECO);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// if (state == "on")
// {
// payload += "true";
// }
// else
// {
// payload += "false";
// }
// payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 12 & 0x01) {
free(ecoState);
}
ecoState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(ecoState, payload.c_str());
_fresh |= 0x01 << 12;
}
void anion(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_ANION);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// if (state == "on")
// {
// payload += "true";
// }
// else
// {
// payload += "false";
// }
// payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 13 & 0x01) {
free(anionState);
}
anionState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(anionState, payload.c_str());
_fresh |= 0x01 << 13;
}
void heater(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_HEATER);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
// if (state == "on")
// {
// payload += "true";
// }
// else
// {
// payload += "false";
// }
// payload += BLINKER_F("\"");
// Blinker.aligeniePrint(payload);
if (_fresh >> 14 & 0x01) {
free(heaterState);
}
heaterState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(heaterState, payload.c_str());
_fresh |= 0x01 << 14;
}
void dryer(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_DRYER);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
if (_fresh >> 15 & 0x01) {
free(dryerState);
}
dryerState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(dryerState, payload.c_str());
_fresh |= 0x01 << 15;
}
void sleep(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_SLEEP);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
if (_fresh >> 16 & 0x01) {
free(sleepState);
}
sleepState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(sleepState, payload.c_str());
_fresh |= 0x01 << 16;
}
void soft(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_SOFT);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
if (_fresh >> 17 & 0x01) {
free(softState);
}
softState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(softState, payload.c_str());
_fresh |= 0x01 << 17;
}
void uv(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_UV);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
if (_fresh >> 18 & 0x01) {
free(uvState);
}
uvState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(uvState, payload.c_str());
_fresh |= 0x01 << 18;
}
void unStraightBlow(const String & state)
{
String payload = BLINKER_F("\"");
payload += STRING_format(BLINKER_CMD_UNSB);
payload += BLINKER_F("\":\"");
payload += state;
payload += BLINKER_F("\"");
if (_fresh >> 19 & 0x01) {
free(unsbState);
}
unsbState = (char*)malloc((payload.length()+1)*sizeof(char));
strcpy(unsbState, payload.c_str());
_fresh |= 0x01 << 19;
}
void print()
{
if (_fresh == 0) return;
String miData;
if (_fresh >> 0 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aState;
free(aState);
}
if (_fresh >> 1 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aColor;
free(aColor);
}
if (_fresh >> 2 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aMode;
free(aMode);
}
if (_fresh >> 3 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aCtemp;
free(aCtemp);
}
if (_fresh >> 4 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aBright;
free(aBright);
}
if (_fresh >> 5 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aTemp;
free(aTemp);
}
if (_fresh >> 6 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aHumi;
free(aHumi);
}
if (_fresh >> 7 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aPm25;
free(aPm25);
}
if (_fresh >> 8 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aCO2;
free(aCO2);
}
if (_fresh >> 9 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += aLevel;
free(aLevel);
}
if (_fresh >> 10 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += ahState;
free(ahState);
}
if (_fresh >> 11 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += avState;
free(avState);
}
if (_fresh >> 12 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += ecoState;
free(ecoState);
}
if (_fresh >> 13 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += anionState;
free(anionState);
}
if (_fresh >> 14 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += heaterState;
free(heaterState);
}
if (_fresh >> 15 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += dryerState;
free(dryerState);
}
if (_fresh >> 16 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += sleepState;
free(sleepState);
}
if (_fresh >> 17 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += softState;
free(softState);
}
if (_fresh >> 18 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += uvState;
free(uvState);
}
if (_fresh >> 19 & 0x01) {
if (miData.length()) miData += BLINKER_F(",");
else miData += BLINKER_F("{");
miData += unsbState;
free(unsbState);
}
miData += BLINKER_F("}");
_fresh = 0;
Blinker.miotPrint(miData);
}
private :
char * aState;
char * aColor;
char * aMode;
char * aCtemp;
char * aBright;
char * aTemp;
char * aHumi;
char * aPm25;
char * aCO2;
char * aLevel;
char * ahState;
char * avState;
char * ecoState;
char * anionState;
char * heaterState;
char * dryerState;
char * sleepState;
char * softState;
char * uvState;
char * unsbState;
uint32_t _fresh = 0;
};
#endif

View File

@@ -0,0 +1,764 @@
#ifndef BLINKER_MQTT_AIR202_H
#define BLINKER_MQTT_AIR202_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "../Blinker/BlinkerATMaster.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
// #if defined(ESP32)
// #include <HardwareSerial.h>
// HardwareSerial *HSerial_MQTT;
// #else
// #include <SoftwareSerial.h>
// SoftwareSerial *SSerial_MQTT;
// #endif
#define BLINKER_MQTT_AIR202_DEFAULT_TIMEOUT 5000UL
#define BLINKER_MQTT_AIR202_DATA_BUFFER_SIZE 1024
enum air202_mqtt_status_t
{
mqtt_init,
mqtt_init_success,
mqtt_set,
mqtt_set_ok,
mqtt_set_connect_ok,
mqtt_connect,
mqtt_connect_ok,
mqtt_connect_success,
mqtt_set_sub_topic,
mqtt_set_sub_ok,
mqtt_set_sub_success,
mqtt_set_pub,
mqtt_set_pub_ok,
mqtt_set_pub_success
};
class BlinkerMQTTAIR202
{
public :
BlinkerMQTTAIR202(Stream& s, bool isHardware,
const char * server, uint16_t port,
const char * cid, const char * user,
const char * pass, blinker_callback_t func)
{
stream = &s; isHWS = isHardware;
servername = server; portnum = port;
clientid = cid; username = user;
password = pass; listenFunc = func;
}
~BlinkerMQTTAIR202() { flush(); }
int connect();
int connected();
int disconnect();
void subscribe(const char * topic);
int publish(const char * topic, const char * msg);
int readSubscription(uint16_t time_out = 1000);
char* lastRead;
const char* subTopic;
// char streamData[1024];
char* streamData;
void flush()
{
if(isFresh) free(streamData);
if(isRead) free(lastRead);
isFresh = false;
isRead = false;
}
protected :
class BlinkerMasterAT * _masterAT;
blinker_callback_t listenFunc = NULL;
Stream* stream;
// char* streamData;
bool isFresh = false;
bool isHWS = false;
bool isConnected = false;
bool isFreshSub = false;
bool isRead = false;
const char * servername;
uint16_t portnum;
const char * clientid;
const char * username;
const char * password;
uint32_t mqtt_time;
uint32_t connect_time;
uint16_t _mqttTimeout = 5000;
uint32_t _debug_time;
air202_mqtt_status_t mqtt_status;
bool streamAvailable();
void streamPrint(const String & s)
{
BLINKER_LOG_ALL(s);
stream->println(s);
}
int timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
};
int BlinkerMQTTAIR202::connect()
{
streamPrint(STRING_format(BLINKER_CMD_CSTT_REQ) +
"=\"" + BLINKER_CMD_CMNET + "\"");
// BLINKER_LOG_ALL(STRING_format(BLINKER_CMD_MCONFIG_REQ) +
// "=\"" + clientid + "\",\"" + username +
// "\",\"" + password + "\"");
mqtt_status = mqtt_init;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt cstt success"));
// mqtt_status = mqtt_init_success;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_CIICR_REQ));
// BLINKER_LOG_ALL(STRING_format(BLINKER_CMD_MCONFIG_REQ) +
// "=\"" + clientid + "\",\"" + username +
// "\",\"" + password + "\"");
mqtt_status = mqtt_init;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt ciicr success"));
// mqtt_status = mqtt_init_success;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_MCONFIG_REQ) +
"=\"" + clientid + "\",\"" + username +
"\",\"" + password + "\"");
// BLINKER_LOG_ALL(STRING_format(BLINKER_CMD_MCONFIG_REQ) +
// "=\"" + clientid + "\",\"" + username +
// "\",\"" + password + "\"");
mqtt_status = mqtt_init;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt init success"));
mqtt_status = mqtt_init_success;
break;
}
}
}
if (mqtt_status != mqtt_init_success) return false;
streamPrint(STRING_format(BLINKER_CMD_SSLMIPSTART) +
"=\"" + servername + "\"," + STRING_format(portnum));
// BLINKER_LOG_ALL(STRING_format(BLINKER_CMD_SSLMIPSTART) +
// "=\"" + servername + "\"," + STRING_format(portnum));
mqtt_status = mqtt_set;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set ok"));
mqtt_status = mqtt_set_ok;
break;
}
}
}
if (mqtt_status != mqtt_set_ok) return false;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_CONNECT_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set connect ok, can connect now"));
mqtt_status = mqtt_set_connect_ok;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_MCONNECT_REQ) +
"=1,300");
mqtt_status = mqtt_connect;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set connect, get ok, wait connact"));
mqtt_status = mqtt_connect_ok;
break;
}
}
}
if (mqtt_status != mqtt_connect_ok) return false;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt connacted"));
mqtt_status = mqtt_connect_success;
break;
}
}
}
if (mqtt_status != mqtt_connect_success) return false;
isConnected = true;
// return true;
streamPrint(STRING_format(BLINKER_CMD_MSUB_REQ) +
"=\"" + subTopic + "\",0");
mqtt_status = mqtt_set_sub_topic;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set sub ok"));
mqtt_status = mqtt_set_sub_ok;
break;
}
}
}
if (mqtt_status != mqtt_set_sub_ok) return false;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_SUBACK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set sub success"));
mqtt_status = mqtt_set_sub_success;
break;
}
}
}
if (mqtt_status != mqtt_set_sub_success) return false;
return true;
}
int BlinkerMQTTAIR202::connected()
{
// if (isConnected && millis() - connect_time <= 30000)
// {
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (millis() - _debug_time >= 2000)
// {
// _debug_time = millis();
// BLINKER_LOG_ALL(BLINKER_F("== connected, readSubscription =="));
// }
// if (streamAvailable())
// {
// BLINKER_LOG_ALL(BLINKER_F("== connected available readSubscription =="));
// _masterAT = new BlinkerMasterAT();
// _masterAT->update(STRING_format(streamData));
// if (_masterAT->getState() != AT_M_NONE &&
// _masterAT->reqName() == BLINKER_CMD_MQTTSTATUS)
// {
// if(_masterAT->getParam(0).toInt() == 1)
// {
// free(_masterAT);
// connect_time = millis();
// isConnected = true;
// return true;
// }
// else
// {
// BLINKER_LOG_ALL("mqtt not connected!");
// isConnected = false;
// free(_masterAT);
// return false;
// }
// }
// else if (_masterAT->getState() != AT_M_NONE &&
// _masterAT->reqName() == BLINKER_CMD_MSUB)
// {
// String subData = String(streamData).substring(
// _masterAT->getParam(0).length() +
// _masterAT->getParam(1).length() +
// 10);
// BLINKER_LOG_ALL("sub data: ", subData);
// if (isFreshSub) free(lastRead);
// lastRead = (char*)malloc((subData.length()+1)*sizeof(char));
// strcpy(lastRead, subData.c_str());
// isFreshSub = true;
// connect_time = millis();
// free(_masterAT);
// return true;
// }
// free(_masterAT);
// BLINKER_LOG_ALL("== free connected at master ==");
// }
// }
// return true;
// }
// else
{
if (isFreshSub)
{
connect_time = millis();
return true;
}
if (!isConnected || millis() - connect_time >= 15000)
{
streamPrint(STRING_format(BLINKER_CMD_MQTTSTATU_REQ));
// connect_time = millis();
// }
mqtt_time = millis();
uint8_t status_get = 0;
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("== connected available =="));
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_MQTTSTATUS)
{
if(_masterAT->getParam(0).toInt() == 1)
{
free(_masterAT);
connect_time = millis();
isConnected = true;
return true;
}
else
{
BLINKER_LOG_ALL("mqtt not connected!");
isConnected = false;
free(_masterAT);
return false;
}
}
else if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_MSUB)
{
String subData = String(streamData).substring(
_masterAT->getParam(0).length() +
_masterAT->getParam(1).length() +
10);
BLINKER_LOG_ALL("sub data: ", subData);
if (isRead) free(lastRead);
lastRead = (char*)malloc((subData.length()+1)*sizeof(char));
strcpy(lastRead, subData.c_str());
isFreshSub = true;
isConnected = true;
isRead = true;
connect_time = millis();
free(_masterAT);
BLINKER_LOG_ALL("== free at master, return ==");
return true;
}
free(_masterAT);
BLINKER_LOG_ALL("== free connected at master ==");
}
}
}
}
return isConnected;
// return status_get;
}
int BlinkerMQTTAIR202::disconnect()
{
streamPrint(STRING_format(BLINKER_CMD_MDISCONNECT_REQ));
mqtt_time = millis();
uint8_t status_get = 0;
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
// return true;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_MIPCLOSE_REQ));
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
// return true;
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_CIPSHUT_REQ));
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("== disconnect available =="));
if (strcmp(streamData, BLINKER_CMD_CONNACK_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
return true;
// break;
}
}
}
return false;
}
void BlinkerMQTTAIR202::subscribe(const char * topic)
{
subTopic = topic;
// streamPrint(STRING_format(BLINKER_CMD_MPUB_REQ) +
// "=\"" + topic + "\",0");
// mqtt_status = mqtt_set_sub_topic;
// mqtt_time = millis();
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (streamAvailable())
// {
// if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt set sub ok"));
// mqtt_status = mqtt_set_sub_ok;
// break;
// }
// }
// }
// if (mqtt_status != mqtt_set_sub_ok) return false;
// mqtt_time = millis();
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (streamAvailable())
// {
// if (strcmp(streamData, BLINKER_CMD_SUBACK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt set sub success"));
// mqtt_status = mqtt_set_sub_success;
// break;
// }
// }
// }
// if (mqtt_status != mqtt_set_sub_success) return false;
// return true;
}
int BlinkerMQTTAIR202::publish(const char * topic, const char * msg)
{
streamPrint(STRING_format(BLINKER_CMD_MPUB_REQ) +
"=\"" + topic + "\",0,0,\"" + msg + "\"");
mqtt_status = mqtt_set_pub;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set pub ok"));
mqtt_status = mqtt_set_pub_ok;
break;
}
}
}
if (mqtt_status != mqtt_set_pub_ok) return false;
// mqtt_time = millis();
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (streamAvailable())
// {
// if (strcmp(streamData, BLINKER_CMD_PUBACK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("mqtt set pub success"));
// mqtt_status = mqtt_set_pub_success;
// break;
// }
// }
// }
// if (mqtt_status != mqtt_set_pub_success) return false;
return true;
}
int BlinkerMQTTAIR202::readSubscription(uint16_t time_out)
{
// if (millis() - _debug_time >= 2000)
// {
// _debug_time = millis();
// BLINKER_LOG_ALL(BLINKER_F("== readSubscription =="));
// }
if (isFreshSub)
{
isFreshSub = false;
return true;
}
else
{
// return false;
// }
mqtt_time = millis();
while(millis() - mqtt_time < time_out)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("readSubscription"));
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_MSUB)
{
String subData = String(streamData).substring(
_masterAT->getParam(0).length() +
_masterAT->getParam(1).length() +
8);
BLINKER_LOG_ALL(BLINKER_F("mqtt sub data: "), subData);
if (isRead) free(lastRead);
lastRead = (char*)malloc((subData.length()+1)*sizeof(char));
strcpy(lastRead, subData.c_str());
isFreshSub = false;
isConnected = true;
isRead = true;
connect_time = millis();
free(_masterAT);
return true;
}
free(_masterAT);
}
}
return false;
}
}
bool BlinkerMQTTAIR202::streamAvailable()
{
yield();
if (!isHWS)
{
// #if defined(__AVR__) || defined(ESP8266)
// if (!SSerial_MQTT->isListening())
// {
// SSerial_MQTT->listen();
// ::delay(100);
// }
// #endif
if (listenFunc) listenFunc();
}
// char _data[BLINKER_MQTT_AIR202_DATA_BUFFER_SIZE];// = { '\0' };
// memset(_data, '\0', BLINKER_MQTT_AIR202_DATA_BUFFER_SIZE);
// if (!isFresh) streamData = (char*)malloc(BLINKER_MQTT_AIR202_DATA_BUFFER_SIZE*sizeof(char));
if (stream->available())
{
// strcpy(_data, stream->readStringUntil('\n').c_str());
String _data = stream->readStringUntil('\n');
BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), _data);
// _data[strlen(_data) - 1] = '\0';
if (isFresh)
{
free(streamData);
isFresh = false;
}
if (_data.length() <= 1) return false;
streamData = (char*)malloc((_data.length() + 1)*sizeof(char));
strcpy(streamData, _data.c_str());
if (_data.length() > 0) streamData[_data.length() - 1] = '\0';
isFresh = true;
return true;
// if (isFresh) free(streamData);
// streamData = (char*)malloc(1*sizeof(char));
// // streamData = "";
// // memset(streamData, '\0', 1024);
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), streamData);
// // int16_t dNum = strlen(streamData);
// int16_t dNum = 0;
// int c_d = timedRead();
// while (dNum < BLINKER_MAX_READ_SIZE &&
// c_d >=0 && c_d != '\n')
// {
// // if (c_d != '\r')
// {
// streamData[dNum] = (char)c_d;
// dNum++;
// streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
// }
// c_d = timedRead();
// }
// // dNum++;
// // streamData = (char*)realloc(streamData, dNum*sizeof(char));
// // streamData[dNum-1] = '\0';
// // streamData = stream->readStringUntil('\n');
// // streamData[streamData.length()-1] = '\0';
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial TEST: "), stream->readStringUntil('\n'));
// // stream->flush();
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// // BLINKER_F(" , dNum: "), dNum);
// // BLINKER_LOG_FreeHeap_ALL();
// if (dNum < BLINKER_MAX_READ_SIZE && dNum > 0)
// {
// // if (streamData[strlen(streamData) - 1] == '\r')
// streamData[dNum - 1] = '\0';
// BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// BLINKER_F(" , dNum: "), dNum);
// isFresh = true;
// return true;
// }
// else
// {
// // free(streamData);
// return false;
// }
}
else
{
return false;
}
}
#endif

View File

@@ -0,0 +1,663 @@
#ifndef BLINKER_MQTT_SIM7000_H
#define BLINKER_MQTT_SIM7000_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "../Blinker/BlinkerATMaster.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
// #if defined(ESP32)
// #include <HardwareSerial.h>
// HardwareSerial *HSerial_MQTT;
// #else
// #include <SoftwareSerial.h>
// SoftwareSerial *SSerial_MQTT;
// #endif
#define BLINKER_MQTT_SIM7000_DEFAULT_TIMEOUT 5000UL
#define BLINKER_MQTT_SIM7000_DATA_BUFFER_SIZE 1024
enum sim7000_mqtt_status_t
{
sim7000_mqtt_url_set,
sim7000_mqtt_url_success,
sim7000_mqtt_user_set,
sim7000_mqtt_user_success,
sim7000_mqtt_pswd_set,
sim7000_mqtt_pswd_success,
sim7000_mqtt_cid_set,
sim7000_mqtt_cid_success,
sim7000_mqtt_kat_set,
sim7000_mqtt_kat_success,
sim7000_mqtt_connect,
sim7000_mqtt_connect_success,
sim7000_mqtt_set_sub,
sim7000_mqtt_set_sub_success
};
class BlinkerMQTTSIM7000
{
public :
BlinkerMQTTSIM7000(Stream& s, bool isHardware,
const char * server, uint16_t port,
const char * cid, const char * user,
const char * pass, blinker_callback_t func)
{
stream = &s; isHWS = isHardware;
servername = server; portnum = port;
clientid = cid; username = user;
password = pass; listenFunc = func;
// streamData = (char*)malloc(BLINKER_HTTP_SIM7000_DATA_BUFFER_SIZE*sizeof(char));
}
~BlinkerMQTTSIM7000() { flush(); }
int connect();
int connected();
int disconnect();
void subscribe(const char * topic);
int publish(const char * topic, const char * msg);
int readSubscription(uint16_t time_out = 1000);
char* lastRead;
const char* subTopic;
// char streamData[1024];
char* streamData;
void flush()
{
if(isFresh) free(streamData);
if(isRead) free(lastRead);
isFresh = false;
isRead = false;
}
protected :
class BlinkerMasterAT * _masterAT;
blinker_callback_t listenFunc = NULL;
Stream* stream;
// char* streamData;
bool isFresh = false;
bool isHWS = false;
bool isConnected = false;
bool isFreshSub = false;
bool isRead = false;
const char * servername;
uint16_t portnum;
const char * clientid;
const char * username;
const char * password;
uint32_t mqtt_time;
uint32_t ping_time;
uint32_t connect_time;
uint16_t _mqttTimeout = 5000;
uint32_t _debug_time;
sim7000_mqtt_status_t mqtt_status;
char c_hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int8_t dcode_data(char d_test)
{
for (uint8_t num = 0; num < 16; num++)
{
if (d_test == c_hex[num]) return num;
}
return -1;
}
char ecode_data(uint8_t d_test)
{
return c_hex[d_test];
}
String encode(char data[])
{
char _d[1024] = {'\0'};
for(uint16_t num = 0; num < strlen(data); num++)
{
_d[num*2] = (ecode_data(data[num] >> 4));
_d[num*2+1] = (ecode_data(data[num] & 0x0F));
}
return _d;
}
String encode(String data)
{
char _d[1024] = {'\0'};
for(uint16_t num = 0; num < data.length(); num++)
{
_d[num*2] = (ecode_data((uint8_t)data[num] >> 4));
_d[num*2+1] = (ecode_data((uint8_t)data[num] & 0x0F));
}
return _d;
}
bool streamAvailable();
void streamPrint(const String & s)
{
BLINKER_LOG_ALL(BLINKER_F("SIM MQTT: "), s);
stream->println(s);
}
int timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
};
int BlinkerMQTTSIM7000::connect()
{
streamPrint(STRING_format(BLINKER_CMD_SMCONF_REQ) + \
"=url," + STRING_format(servername) + \
"," + STRING_format(portnum));
mqtt_status = sim7000_mqtt_url_set;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
mqtt_status = sim7000_mqtt_url_success;
break;
}
}
}
if (mqtt_status != sim7000_mqtt_url_success)
{
return false;
}
// streamPrint(STRING_format(BLINKER_CMD_SMCONF_REQ) + \
// "=\"KEEPTIME\",60");
// mqtt_time = millis();
// mqtt_status = sim7000_mqtt_url_set;
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (streamAvailable())
// {
// if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// mqtt_status = sim7000_mqtt_user_success;
// break;
// }
// }
// }
// if (mqtt_status != sim7000_mqtt_user_success)
// {
// return false;
// }
streamPrint(STRING_format(BLINKER_CMD_SMCONF_REQ) + \
"=username," + username);
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
mqtt_status = sim7000_mqtt_user_success;
break;
}
}
}
if (mqtt_status != sim7000_mqtt_user_success)
{
return false;
}
streamPrint(STRING_format(BLINKER_CMD_SMCONF_REQ) + \
"=password," + password);
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
mqtt_status = sim7000_mqtt_pswd_success;
break;
}
}
}
if (mqtt_status != sim7000_mqtt_pswd_success)
{
return false;
}
streamPrint(STRING_format(BLINKER_CMD_SMCONF_REQ) + \
"=clientid,\"" + clientid + "\"");
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
mqtt_status = sim7000_mqtt_cid_success;
break;
}
}
}
if (mqtt_status != sim7000_mqtt_cid_success)
{
return false;
}
streamPrint(STRING_format(BLINKER_CMD_SMCONN_REQ));
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
mqtt_status = sim7000_mqtt_connect_success;
break;
}
}
}
if (mqtt_status != sim7000_mqtt_connect_success)
{
return false;
}
streamPrint(STRING_format(BLINKER_CMD_SMUNSUB_REQ) + \
"=\"" + subTopic + "\"");
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_SMSUB_REQ) + \
"=\"" + subTopic + "\",0");
mqtt_time = millis();
mqtt_status = sim7000_mqtt_set_sub;
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
mqtt_status = sim7000_mqtt_set_sub_success;
break;
}
}
}
if (mqtt_status != sim7000_mqtt_set_sub_success)
{
return false;
}
return true;
}
int BlinkerMQTTSIM7000::connected()
{
streamPrint(STRING_format(BLINKER_CMD_SMSTATE_REQ) + \
"?");
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("connected query"));
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_SMSTATE &&
_masterAT->getParam(0).toInt() == 1)
{
isConnected = true;
}
else
{
isConnected = false;
}
free(_masterAT);
BLINKER_LOG_ALL(BLINKER_F("isConnected: "), isConnected);
break;
}
}
// streamPrint(STRING_format(BLINKER_CMD_SMSUB_REQ) + \
// "=\"" + subTopic + "\",0");
// while(millis() - mqtt_time < _mqttTimeout)
// {
// if (streamAvailable())
// {
// if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// break;
// }
// }
// }
return isConnected;
}
int BlinkerMQTTSIM7000::disconnect()
{
streamPrint(STRING_format(BLINKER_CMD_SMDISC_REQ));
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt disconnect"));
return true;
}
}
}
return false;
}
void BlinkerMQTTSIM7000::subscribe(const char * topic)
{
subTopic = topic;
}
int BlinkerMQTTSIM7000::publish(const char * topic, const char * msg)
{
streamPrint(STRING_format(BLINKER_CMD_SMPUB_REQ) +
"=\"" + topic + "\"," +
STRING_format(strlen(msg)) + ",0,0");
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strncmp(streamData, ">", 1) == 0)
{
// return true;
break;
}
}
}
streamPrint(msg);
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
return true;
}
}
}
return false;
}
int BlinkerMQTTSIM7000::readSubscription(uint16_t time_out)
{
if (isFreshSub)
{
isFreshSub = false;
return true;
}
else
{
// BLINKER_LOG_ALL(BLINKER_F("readSubscription in"));
mqtt_time = millis();
while(millis() - mqtt_time < time_out)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("readSubscription"));
BLINKER_LOG_FreeHeap_ALL();
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_SMSUB)
{
String _Data = STRING_format(streamData);
time_t now_time = millis();
while (millis() - now_time < 1500)
{
if (stream->available())
{
_Data += stream->readStringUntil('\n').c_str();
}
// delay(1000);
}
BLINKER_LOG_ALL(BLINKER_F("_Data: "), _Data);
// String subData = _Data.substring(
// _masterAT->getParam(0).length() +
// _masterAT->getParam(1).length() +
// _masterAT->getParam(2).length() +
// _masterAT->getParam(3).length() +
// _masterAT->getParam(4).length() +
// _masterAT->getParam(5).length() +
// 15, _Data.length() - 1);
String subData = _Data.substring(_Data.indexOf(",") + 2, _Data.length() - 1);//_masterAT->getParam(1);
// BLINKER_LOG_ALL(BLINKER_F("leng 0: "), _masterAT->getParam(0).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 1: "), _masterAT->getParam(1).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 2: "), _masterAT->getParam(2).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 3: "), _masterAT->getParam(3).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 4: "), _masterAT->getParam(4).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 5: "), _masterAT->getParam(5).length());
BLINKER_LOG_ALL(BLINKER_F("mqtt sub data: "), subData);
if (isRead) free(lastRead);
lastRead = (char*)malloc((subData.length()+1)*sizeof(char));
// strcpy(lastRead, subData.c_str());
memset(lastRead, '\0', subData.length()+1);
strcpy(lastRead, subData.c_str());
isFreshSub = false;
isConnected = true;
isRead = true;
connect_time = millis();
free(_masterAT);
return true;
}
else if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CMQDISCON)
{
free(_masterAT);
isConnected = false;
return false;
}
free(_masterAT);
}
}
return false;
}
}
bool BlinkerMQTTSIM7000::streamAvailable()
{
yield();
if (!isHWS)
{
// #if defined(__AVR__) || defined(ESP8266)
// if (!SSerial_MQTT->isListening())
// {
// SSerial_MQTT->listen();
// ::delay(100);
// }
// #endif
if (listenFunc) listenFunc();
}
// char _data[BLINKER_MQTT_SIM7000_DATA_BUFFER_SIZE];// = { '\0' };
// memset(_data, '\0', BLINKER_MQTT_SIM7000_DATA_BUFFER_SIZE);
// if (!isFresh) streamData = (char*)malloc(BLINKER_MQTT_SIM7000_DATA_BUFFER_SIZE*sizeof(char));
if (stream->available())
{
// strcpy(_data, stream->readStringUntil('\n').c_str());
String _data = stream->readStringUntil('\n');
// time_t now_time = millis();
// while (millis() - now_time < 1500)
// {
// if (stream->available())
// {
// _data += stream->readStringUntil('\n').c_str();
// }
// // delay(1000);
// }
BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), _data);
BLINKER_LOG_ALL("len: ", _data.length());
// _data[strlen(_data) - 1] = '\0';
// stream->flush();
if (isFresh)
{
free(streamData);
isFresh = false;
}
if (_data.length() <= 1) return false;
streamData = (char*)malloc((_data.length() + 1)*sizeof(char));
strcpy(streamData, _data.c_str());
if (streamData[_data.length() - 1] == '\r') streamData[_data.length() - 1] = '\0';
isFresh = true;
return true;
// if (isFresh) free(streamData);
// streamData = (char*)malloc(1*sizeof(char));
// // streamData = "";
// // memset(streamData, '\0', 1024);
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), streamData);
// // int16_t dNum = strlen(streamData);
// int16_t dNum = 0;
// int c_d = timedRead();
// while (dNum < BLINKER_MAX_READ_SIZE &&
// c_d >=0 && c_d != '\n')
// {
// // if (c_d != '\r')
// {
// streamData[dNum] = (char)c_d;
// dNum++;
// streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
// }
// c_d = timedRead();
// }
// // dNum++;
// // streamData = (char*)realloc(streamData, dNum*sizeof(char));
// // streamData[dNum-1] = '\0';
// // streamData = stream->readStringUntil('\n');
// // streamData[streamData.length()-1] = '\0';
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial TEST: "), stream->readStringUntil('\n'));
// // stream->flush();
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// // BLINKER_F(" , dNum: "), dNum);
// // BLINKER_LOG_FreeHeap_ALL();
// if (dNum < BLINKER_MAX_READ_SIZE && dNum > 0)
// {
// // if (streamData[strlen(streamData) - 1] == '\r')
// streamData[dNum - 1] = '\0';
// BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// BLINKER_F(" , dNum: "), dNum);
// isFresh = true;
// return true;
// }
// else
// {
// // free(streamData);
// return false;
// }
}
else
{
return false;
}
}
#endif

View File

@@ -0,0 +1,570 @@
#ifndef BLINKER_MQTT_SIM7020_H
#define BLINKER_MQTT_SIM7020_H
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "../Blinker/BlinkerATMaster.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
// #if defined(ESP32)
// #include <HardwareSerial.h>
// HardwareSerial *HSerial_MQTT;
// #else
// #include <SoftwareSerial.h>
// SoftwareSerial *SSerial_MQTT;
// #endif
#define BLINKER_MQTT_SIM7020_DEFAULT_TIMEOUT 5000UL
#define BLINKER_MQTT_SIM7020_DATA_BUFFER_SIZE 1024
enum sim7020_mqtt_status_t
{
sim7020_mqtt_init,
sim7020_mqtt_init_success,
sim7020_mqtt_connect,
sim7020_mqtt_connect_success,
sim7020_mqtt_set_sub,
sim7020_mqtt_set_sub_success
};
class BlinkerMQTTSIM7020
{
public :
BlinkerMQTTSIM7020(Stream& s, bool isHardware,
const char * server, uint16_t port,
const char * cid, const char * user,
const char * pass, blinker_callback_t func)
{
stream = &s; isHWS = isHardware;
servername = server; portnum = port;
clientid = cid; username = user;
password = pass; listenFunc = func;
// streamData = (char*)malloc(BLINKER_HTTP_SIM7020_DATA_BUFFER_SIZE*sizeof(char));
}
~BlinkerMQTTSIM7020() { flush(); }
int connect();
int connected();
int disconnect();
void subscribe(const char * topic);
int publish(const char * topic, const char * msg);
int readSubscription(uint16_t time_out = 1000);
char* lastRead;
const char* subTopic;
// char streamData[1024];
char* streamData;
void flush()
{
if(isFresh) free(streamData);
if(isRead) free(lastRead);
isFresh = false;
isRead = false;
}
protected :
class BlinkerMasterAT * _masterAT;
blinker_callback_t listenFunc = NULL;
Stream* stream;
// char* streamData;
bool isFresh = false;
bool isHWS = false;
bool isConnected = false;
bool isFreshSub = false;
bool isRead = false;
const char * servername;
uint16_t portnum;
const char * clientid;
const char * username;
const char * password;
uint32_t mqtt_time;
uint32_t ping_time;
uint32_t connect_time;
uint16_t _mqttTimeout = 5000;
uint32_t _debug_time;
sim7020_mqtt_status_t mqtt_status;
char c_hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int8_t dcode_data(char d_test)
{
for (uint8_t num = 0; num < 16; num++)
{
if (d_test == c_hex[num]) return num;
}
return -1;
}
char ecode_data(uint8_t d_test)
{
return c_hex[d_test];
}
String encode(char data[])
{
char _d[1024] = {'\0'};
for(uint16_t num = 0; num < strlen(data); num++)
{
_d[num*2] = (ecode_data(data[num] >> 4));
_d[num*2+1] = (ecode_data(data[num] & 0x0F));
}
return _d;
}
String encode(String data)
{
char _d[1024] = {'\0'};
for(uint16_t num = 0; num < data.length(); num++)
{
_d[num*2] = (ecode_data((uint8_t)data[num] >> 4));
_d[num*2+1] = (ecode_data((uint8_t)data[num] & 0x0F));
}
return _d;
}
bool streamAvailable();
void streamPrint(const String & s)
{
BLINKER_LOG_ALL(BLINKER_F("SIM MQTT: "), s);
stream->println(s);
}
int timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
};
int BlinkerMQTTSIM7020::connect()
{
streamPrint(STRING_format(BLINKER_CMD_CMQNEW_REQ) + \
"=\"" + servername + "\",\"" + STRING_format(portnum) + \
"\",12000,1024");
mqtt_status = sim7020_mqtt_init;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CMQNEW &&
_masterAT->getParam(0).toInt() == 0)
{
free(_masterAT);
mqtt_status = sim7020_mqtt_init_success;
break;
}
free(_masterAT);
}
}
if (mqtt_status != sim7020_mqtt_init_success) return false;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
break;
}
}
}
streamPrint(STRING_format(BLINKER_CMD_CMQCON_REQ) +
"=0,3,\"" + clientid + "\",600,0,0,\"" +
username + "\",\"" + password + "\"");
mqtt_status = sim7020_mqtt_connect;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
mqtt_status = sim7020_mqtt_connect_success;
// return true;
break;
}
}
}
if (mqtt_status != sim7020_mqtt_connect_success) return false;
// isConnected = true;
streamPrint(STRING_format(BLINKER_CMD_CMQSUB_REQ) +
"=0,\"" + subTopic + "\",0");
mqtt_status = sim7020_mqtt_set_sub;
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("mqtt set sub ok"));
mqtt_status = sim7020_mqtt_set_sub_success;
isConnected = true;
ping_time = millis();
return true;
}
}
}
if (mqtt_status != sim7020_mqtt_set_sub_success) return false;
}
int BlinkerMQTTSIM7020::connected()
{
// yield();
// BLINKER_LOG_ALL(BLINKER_F(">>>>>> mqtt connected check <<<<<<"));
if (isConnected && (millis() - ping_time) <= 10000)
{
yield();
return isConnected;
}
if ((millis() - ping_time) <= 60000) return false;
BLINKER_LOG_ALL(BLINKER_F(">>>>>> mqtt connected check ping_time<<<<<<"));
streamPrint(STRING_format(BLINKER_CMD_CMQCON_REQ) + "?");
mqtt_time = millis();
ping_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("connected query"));
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CMQCON &&
_masterAT->getParam(0).toInt() == 0)
{
if (_masterAT->getParam(1) == "1")
{
isConnected = true;
}
else
{
isConnected = false;
}
free(_masterAT);
BLINKER_LOG_ALL(BLINKER_F("isConnected: "), isConnected);
break;
}
free(_masterAT);
}
}
while(streamAvailable()) { delay(10); }
return isConnected;
}
int BlinkerMQTTSIM7020::disconnect()
{
streamPrint(STRING_format(BLINKER_CMD_CMQDISCON_RESQ) + "=0");
mqtt_time = millis();
isConnected = false;
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
// isConnected = false;
return true;
}
}
}
return false;
}
void BlinkerMQTTSIM7020::subscribe(const char * topic)
{
subTopic = topic;
}
int BlinkerMQTTSIM7020::publish(const char * topic, const char * msg)
{
streamPrint(STRING_format(BLINKER_CMD_CMQPUB_REQ) +
"=0,\"" + topic + "\",0,0,0," +
STRING_format(strlen(msg)*2) + ",\"" +
encode(msg) + "\"");
mqtt_time = millis();
while(millis() - mqtt_time < _mqttTimeout)
{
if (streamAvailable())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
return true;
}
}
}
return false;
}
int BlinkerMQTTSIM7020::readSubscription(uint16_t time_out)
{
if (isFreshSub)
{
isFreshSub = false;
return true;
}
else
{
mqtt_time = millis();
while(millis() - mqtt_time < time_out)
{
if (streamAvailable())
{
BLINKER_LOG_ALL(BLINKER_F("readSubscription"));
BLINKER_LOG_FreeHeap_ALL();
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CMQPUB)
{
String _Data = STRING_format(streamData);
time_t now_time = millis();
while (millis() - now_time < 1500)
{
if (stream->available())
{
_Data += stream->readStringUntil('\n').c_str();
}
// delay(1000);
}
BLINKER_LOG_ALL(BLINKER_F("_Data: "), _Data);
String subData = _Data.substring(
_masterAT->getParam(0).length() +
_masterAT->getParam(1).length() +
_masterAT->getParam(2).length() +
_masterAT->getParam(3).length() +
_masterAT->getParam(4).length() +
_masterAT->getParam(5).length() +
15, _Data.length() - 1);
// BLINKER_LOG_ALL(BLINKER_F("leng 0: "), _masterAT->getParam(0).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 1: "), _masterAT->getParam(1).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 2: "), _masterAT->getParam(2).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 3: "), _masterAT->getParam(3).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 4: "), _masterAT->getParam(4).length());
// BLINKER_LOG_ALL(BLINKER_F("leng 5: "), _masterAT->getParam(5).length());
BLINKER_LOG_ALL(BLINKER_F("mqtt sub data: "), subData);
if (isRead) free(lastRead);
lastRead = (char*)malloc((subData.length()/2+1)*sizeof(char));
// strcpy(lastRead, subData.c_str());
memset(lastRead, '\0', subData.length()/2+1);
for(uint16_t num = 0; num < subData.length()/2; num++)
{
lastRead[num] = (char)(dcode_data(subData[num*2])<< 4 | dcode_data(subData[num*2+1]));
}
isFreshSub = false;
isConnected = true;
isRead = true;
connect_time = millis();
free(_masterAT);
return true;
}
else if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CMQDISCON)
{
free(_masterAT);
isConnected = false;
return false;
}
free(_masterAT);
}
}
return false;
}
}
bool BlinkerMQTTSIM7020::streamAvailable()
{
yield();
if (!isHWS)
{
// #if defined(__AVR__) || defined(ESP8266)
// if (!SSerial_MQTT->isListening())
// {
// SSerial_MQTT->listen();
// ::delay(100);
// }
// #endif
if (listenFunc) listenFunc();
}
// char _data[BLINKER_MQTT_SIM7020_DATA_BUFFER_SIZE];// = { '\0' };
// memset(_data, '\0', BLINKER_MQTT_SIM7020_DATA_BUFFER_SIZE);
// if (!isFresh) streamData = (char*)malloc(BLINKER_MQTT_SIM7020_DATA_BUFFER_SIZE*sizeof(char));
if (stream->available())
{
// strcpy(_data, stream->readStringUntil('\n').c_str());
String _data = stream->readStringUntil('\n');
// time_t now_time = millis();
// while (millis() - now_time < 1500)
// {
// if (stream->available())
// {
// _data += stream->readStringUntil('\n').c_str();
// }
// // delay(1000);
// }
BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), _data);
BLINKER_LOG_ALL("len: ", _data.length());
// _data[strlen(_data) - 1] = '\0';
// stream->flush();
if (isFresh)
{
free(streamData);
isFresh = false;
}
if (_data.length() <= 1) return false;
streamData = (char*)malloc((_data.length() + 1)*sizeof(char));
strcpy(streamData, _data.c_str());
if (streamData[_data.length() - 1] == '\r') streamData[_data.length() - 1] = '\0';
isFresh = true;
return true;
// if (isFresh) free(streamData);
// streamData = (char*)malloc(1*sizeof(char));
// // streamData = "";
// // memset(streamData, '\0', 1024);
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), streamData);
// // int16_t dNum = strlen(streamData);
// int16_t dNum = 0;
// int c_d = timedRead();
// while (dNum < BLINKER_MAX_READ_SIZE &&
// c_d >=0 && c_d != '\n')
// {
// // if (c_d != '\r')
// {
// streamData[dNum] = (char)c_d;
// dNum++;
// streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
// }
// c_d = timedRead();
// }
// // dNum++;
// // streamData = (char*)realloc(streamData, dNum*sizeof(char));
// // streamData[dNum-1] = '\0';
// // streamData = stream->readStringUntil('\n');
// // streamData[streamData.length()-1] = '\0';
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial TEST: "), stream->readStringUntil('\n'));
// // stream->flush();
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// // BLINKER_F(" , dNum: "), dNum);
// // BLINKER_LOG_FreeHeap_ALL();
// if (dNum < BLINKER_MAX_READ_SIZE && dNum > 0)
// {
// // if (streamData[strlen(streamData) - 1] == '\r')
// streamData[dNum - 1] = '\0';
// BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// BLINKER_F(" , dNum: "), dNum);
// isFresh = true;
// return true;
// }
// else
// {
// // free(streamData);
// return false;
// }
}
else
{
return false;
}
}
#endif

View File

@@ -0,0 +1,157 @@
#ifndef BlinkerNUM_H
#define BlinkerNUM_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerNumber
{
public :
BlinkerNumber(char _name[])
{
numName = (char*)malloc((strlen(_name)+1)*sizeof(char));
strcpy(numName, _name);
}
void icon(const String & _icon)
{
if (_fresh >> 0 & 0x01) free(nicon);
nicon = (char*)malloc((_icon.length()+1)*sizeof(char));
strcpy(nicon, _icon.c_str());
_fresh |= 0x01 << 0;
}
void color(const String & _clr)
{
if (_fresh >> 1 & 0x01) free(ncolor);
ncolor = (char*)malloc((_clr.length()+1)*sizeof(char));
strcpy(ncolor, _clr.c_str());
_fresh |= 0x01 << 1;
}
void unit(const String & _unit)
{
if (_fresh >> 2 & 0x01) free(nunit);
nunit = (char*)malloc((_unit.length()+1)*sizeof(char));
strcpy(nunit, _unit.c_str());
_fresh |= 0x01 << 2;
}
template <typename T>
void text(T _text)
{
// if (isnan(_text)) return;
if (_fresh >> 3 & 0x01) free(ntext);
String _ntext = STRING_format(_text);
ntext = (char*)malloc((_ntext.length()+1)*sizeof(char));
strcpy(ntext, _ntext.c_str());
_fresh |= 0x01 << 3;
}
void print(char value) { _print(STRING_format(value)); }
void print(unsigned char value) { _print(STRING_format(value)); }
void print(int value) { _print(STRING_format(value)); }
void print(unsigned int value) { _print(STRING_format(value)); }
void print(long value) { _print(STRING_format(value)); }
void print(unsigned long value) { _print(STRING_format(value)); }
void print(double value) { _print(STRING_format(value)); }
void print() { _print(""); }
private :
char * numName;
char * nicon;// = "";
char * ncolor;// = "";
char * nunit;// = "";
char * ntext;
uint8_t _fresh = 0;
void _print(const String & value)
{
if (_fresh == 0 && value.length() == 0) return;
String numberData = "";
if (value.length())
{
numberData += BLINKER_F("{\"");
numberData += BLINKER_F(BLINKER_CMD_VALUE);
numberData += BLINKER_F("\":");
numberData += value;
Blinker.printNumArray(numName, value);
}
if (_fresh >> 0 & 0x01)
{
if (numberData.length()) numberData += BLINKER_F(",");
else numberData += BLINKER_F("{");
numberData += BLINKER_F("\"");
numberData += BLINKER_F(BLINKER_CMD_ICON);
numberData += BLINKER_F("\":\"");
numberData += nicon;
numberData += BLINKER_F("\"");
free(nicon);
}
if (_fresh >> 1 & 0x01)
{
if (numberData.length()) numberData += BLINKER_F(",");
else numberData += BLINKER_F("{");
numberData += BLINKER_F("\"");
numberData += BLINKER_F(BLINKER_CMD_COLOR);
numberData += BLINKER_F("\":\"");
numberData += ncolor;
numberData += BLINKER_F("\"");
free(ncolor);
}
if (_fresh >> 2 & 0x01)
{
if (numberData.length()) numberData += BLINKER_F(",");
else numberData += BLINKER_F("{");
numberData += BLINKER_F("\"");
numberData += BLINKER_F(BLINKER_CMD_UNIT);
numberData += BLINKER_F("\":\"");
numberData += nunit;
numberData += BLINKER_F("\"");
free(nunit);
}
if (_fresh >> 3 & 0x01)
{
if (numberData.length()) numberData += BLINKER_F(",");
else numberData += BLINKER_F("{");
numberData += BLINKER_F("\"");
numberData += BLINKER_F(BLINKER_CMD_TEXT);
numberData += BLINKER_F("\":\"");
numberData += (ntext);
numberData += BLINKER_F("\"");
free(ntext);
}
numberData += BLINKER_F("}");
_fresh = 0;
Blinker.printArray(numName, numberData);
}
};
#endif

View File

@@ -0,0 +1,583 @@
#ifndef BLINKER_OTA_H
#define BLINKER_OTA_H
#if (defined(ESP8266) || defined(ESP32))
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include <EEPROM.h>
#if defined(ESP8266)
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#ifndef BLINKER_WITHOUT_SSL
extern BearSSL::WiFiClientSecure client_mqtt;
#endif
#elif defined(ESP32)
#include <WiFi.h>
#include <Update.h>
// extern WiFiClient client_s;
extern WiFiClientSecure client_s;
#endif
#include "BlinkerUpdater.h"
// extern WiFiClientSecure client_s;
enum bota_status_t{
BLINKER_UPGRADE_DISABLE,
BLINKER_UPGRADE_BEGIN,
BLINKER_UPGRADE_START,
BLINKER_UPGRADE_FAIL,
BLINKER_UPGRADE_LOAD_FAIL,
BLINKER_UPGRADE_VERI_FAIL,
BLINKER_UPGRADE_SUCCESS
};
// #define VERSIONPARAM "1.0.1"
// if (loadOTACheck()) {
// if (!loadVersion()) {
// saveVersion();
// UPGRADE_STATUS = UPGRADE_SUCCESS;
// }
// else {
// UPGRADE_STATUS = UPGRADE_FAIL;
// }
// }
class BlinkerOTA
{
public :
BlinkerOTA() {}
// void parseData(char *OTAdata);
void host(String _host) { ota_host = _host; }
void url(String _url) { ota_url = _url; }
void setURL(String urlstr);
void config(String _host, String _url, String _fingerPrint, String _md5)
{
// #if defined(ESP8266)
BLINKER_LOG_ALL(_host.indexOf("https"));
if (_host.indexOf("https") != -1)
{
ota_host = _host.substring(8);
}
else
{
ota_host = _host;
}
// #else
// ota_host = _host;
// #endif
ota_url = _url;
ota_fingerPrint = _fingerPrint;
ota_md5 = _md5;
}
bool update();
// bool run();
bota_status_t status() {
return _status;
}
uint8_t loadOTACheck();
void saveOTARun();
void saveOTACheck();
void clearOTACheck();
bool loadVersion();
void saveVersion();
private :
String getHeaderValue(String header, String headerName) {
return header.substring(strlen(headerName.c_str()));
}
protected :
// #if defined(ESP32)
// uint8_t OTACheck;
// #endif
String ota_host;
String ota_url;
String ota_fingerPrint;
String ota_md5;
uint16_t ota_port = 443;
char *otaUrl;
bota_status_t _status;
// int
size_t contentLength = 0;
bool isValidContentType = false;
};
void BlinkerOTA::setURL(String url) {
otaUrl = (char*)malloc(url.length()*sizeof(char));
strcpy(otaUrl,url.c_str());
BLINKER_LOG_ALL(BLINKER_F("ota url: "), otaUrl);
_status = BLINKER_UPGRADE_START;
update();
}
bool BlinkerOTA::update() {
saveOTACheck();
#if defined(ESP8266)
// #ifndef BLINKER_WITHOUT_SSL
BearSSL::WiFiClientSecure client_s;
BLINKER_LOG_FreeHeap();
#ifndef BLINKER_WITHOUT_SSL
client_mqtt.stop();
#endif
::delay(100);
bool mfln = client_s.probeMaxFragmentLength(ota_host, ota_port, 1024);
if (mfln) {
client_s.setBufferSizes(1024, 1024);
}
// client_s.setFingerprint(ota_fingerPrint.c_str());
BLINKER_LOG_FreeHeap();
client_s.setInsecure();
// BLINKER_LOG_ALL(BLINKER_F("Connecting to: "), ota_host);
// t_httpUpdate_return ret = BlinkerhttpUpdate.update(client_s, ota_host + ota_url, ota_md5, "");
// switch (ret) {
// case HTTP_UPDATE_FAILED:
// // USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
// // break;
// BLINKER_LOG_ALL(BLINKER_F("HTTP_UPDATE_FAILD Error : "), BlinkerhttpUpdate.getLastError());
// return false;
// case HTTP_UPDATE_NO_UPDATES:
// // USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES");
// // break;
// BLINKER_LOG_ALL(BLINKER_F("HTTP_UPDATE_NO_UPDATES"));
// return false;
// case HTTP_UPDATE_OK:
// // USE_SERIAL.println("HTTP_UPDATE_OK");
// // break;
// BLINKER_LOG_ALL(BLINKER_F("HTTP_UPDATE_OK"));
// return true;
// }
// #else
// WiFiClient client_s;
// #endif
#elif defined(ESP32)
client_s.stop();
#endif
BlinkerUpdater.setMD5(ota_md5.c_str());
while (1) {
if (!client_s.connect(ota_host.c_str(), ota_port)) {
BLINKER_ERR_LOG(BLINKER_F("server connection failed"));
BLINKER_LOG_FreeHeap();
::delay(1000);
}
else {
BLINKER_LOG_ALL(BLINKER_F("connection succeed"));
BLINKER_LOG_FreeHeap();
BLINKER_LOG_ALL(BLINKER_F("Fetching Bin: "), ota_url);
String _ota_url = BLINKER_F("GET ");
_ota_url += ota_url;
_ota_url += BLINKER_F(" HTTP/1.1\r\nHost: ");
_ota_url += ota_host;
_ota_url += BLINKER_F("\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n");
// client_s.print(String("GET ") + ota_url + " HTTP/1.1\r\n" +
// "Host: " + ota_host + "\r\n" +
// "Cache-Control: no-cache\r\n" +
// "Connection: close\r\n\r\n");
client_s.print(_ota_url);
BLINKER_LOG_ALL(BLINKER_F("_ota_url: "), _ota_url);
unsigned long timeout = millis();
while (client_s.available() == 0) {
if (millis() - timeout > 5000) {
BLINKER_LOG_ALL(BLINKER_F("Client Timeout !"));
client_s.stop();
_status = BLINKER_UPGRADE_LOAD_FAIL;
return false;
}
}
// Once the response is available,
// check stuff
while (client_s.available()) {
// read line till /n
String line = client_s.readStringUntil('\n');
// remove space, to check if the line is end of headers
line.trim();
// if the the line is empty,
// this is end of headers
// break the while and feed the
// remaining `client_s` to the
// Update.writeStream();
if (!line.length()) {
//headers ended
break; // and get the OTA started
}
BLINKER_LOG_ALL(BLINKER_F("line: "), line);
// Check if the HTTP Response is 200
// else break and Exit Update
if (line.startsWith("HTTP/1.1")) {
if (line.indexOf("200") < 0) {
BLINKER_LOG_ALL(BLINKER_F("Got a non 200 status code from server. Exiting OTA Update."));
break;
}
}
// extract headers here
// Start with content length
if (line.startsWith("Content-Length: ")) {
contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str());
BLINKER_LOG_ALL(BLINKER_F("Got "), contentLength, BLINKER_F(" bytes from server"));
}
// Next, the content type
if (line.startsWith("Content-Type: ")) {
String contentType = getHeaderValue(line, "Content-Type: ");
BLINKER_LOG_ALL(BLINKER_F("Got "), contentType, BLINKER_F(" payload."));
if (contentType == "application/octet-stream") {
isValidContentType = true;
}
}
}
// Check what is the contentLength and if content type is `application/octet-stream`
BLINKER_LOG_ALL(BLINKER_F("contentLength : "),
contentLength,
BLINKER_F(", isValidContentType : "),
isValidContentType);
// check contentLength and content type
if (contentLength && isValidContentType)
{
// Check if there is enough to OTA Update
bool canBegin = BlinkerUpdater.begin(contentLength);
// If yes, begin
if (canBegin) {
BLINKER_LOG(BLINKER_F("Begin OTA. This may take 2 - 5 mins to complete. Things might be quite for a while.. Patience!"));
// No activity would appear on the Serial monitor
// So be patient. This may take 2 - 5mins to complete
size_t written = BlinkerUpdater.writeStream(client_s);
if (written == contentLength) {
BLINKER_LOG(BLINKER_F("Written : "), written, BLINKER_F(" successfully"));
}
else {
BLINKER_LOG(BLINKER_F("Written only : "), written,
BLINKER_F("/"), contentLength, BLINKER_F(". Retry?"));
}
if (BlinkerUpdater.end()) {
BLINKER_LOG(BLINKER_F("OTA done!"));
if (BlinkerUpdater.isFinished()) {
BLINKER_LOG(BLINKER_F("Update successfully completed. Rebooting."));
_status = BLINKER_UPGRADE_SUCCESS;
// ESP.restart();
return true;
} else {
BLINKER_LOG(BLINKER_F("Update not finished? Something went wrong!"));
_status = BLINKER_UPGRADE_FAIL;
return false;
}
}
else {
BLINKER_LOG(BLINKER_F("Error Occurred. Error #: "), BlinkerUpdater.getError());
_status = BLINKER_UPGRADE_FAIL;
// return false;
}
}
else {
// not enough space to begin OTA
// Understand the partitions and
// space availability
BLINKER_LOG(BLINKER_F("Not enough space to begin OTA"));
client_s.flush();
_status = BLINKER_UPGRADE_FAIL;
return false;
}
}
else {
BLINKER_LOG(BLINKER_F("There was no content in the response"));
client_s.flush();
_status = BLINKER_UPGRADE_FAIL;
return false;
}
}
}
// #endif
}
// bool BlinkerOTA::run()
// {
// while (1) {
// if (!client_s.connect(ota_host.c_str(), ota_port)) {
// BLINKER_ERR_LOG(BLINKER_F("server connection failed"));
// BLINKER_LOG_FreeHeap();
// ::delay(1000);
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("connection succeed"));
// BLINKER_LOG_FreeHeap();
// BLINKER_LOG_ALL(BLINKER_F("Fetching Bin: "), ota_url);
// client_s.print(String("GET ") + ota_url + " HTTP/1.1\r\n" +
// "Host: " + ota_host + "\r\n" +
// "Cache-Control: no-cache\r\n" +
// "Connection: close\r\n\r\n");
// unsigned long timeout = millis();
// while (client_s.available() == 0) {
// if (millis() - timeout > 5000) {
// BLINKER_LOG_ALL(BLINKER_F("Client Timeout !"));
// client_s.stop();
// _status = BLINKER_UPGRADE_LOAD_FAIL;
// return false;
// }
// }
// // Once the response is available,
// // check stuff
// while (client_s.available()) {
// // read line till /n
// String line = client_s.readStringUntil('\n');
// // remove space, to check if the line is end of headers
// line.trim();
// // if the the line is empty,
// // this is end of headers
// // break the while and feed the
// // remaining `client_s` to the
// // Update.writeStream();
// if (!line.length()) {
// //headers ended
// break; // and get the OTA started
// }
// // Check if the HTTP Response is 200
// // else break and Exit Update
// if (line.startsWith("HTTP/1.1")) {
// if (line.indexOf("200") < 0) {
// BLINKER_LOG_ALL(BLINKER_F("Got a non 200 status code from server. Exiting OTA Update."));
// break;
// }
// }
// // extract headers here
// // Start with content length
// if (line.startsWith("Content-Length: ")) {
// contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str());
// BLINKER_LOG_ALL(BLINKER_F("Got "), contentLength, BLINKER_F(" bytes from server"));
// }
// // Next, the content type
// if (line.startsWith("Content-Type: ")) {
// String contentType = getHeaderValue(line, "Content-Type: ");
// BLINKER_LOG_ALL(BLINKER_F("Got "), contentType, BLINKER_F(" payload."));
// if (contentType == "application/octet-stream") {
// isValidContentType = true;
// }
// }
// }
// // Check what is the contentLength and if content type is `application/octet-stream`
// BLINKER_LOG_ALL(BLINKER_F("contentLength : "),
// contentLength,
// BLINKER_F(", isValidContentType : "),
// isValidContentType);
// // check contentLength and content type
// if (contentLength && isValidContentType)
// {
// // Check if there is enough to OTA Update
// bool canBegin = BlinkerUpdater.begin(contentLength);
// // If yes, begin
// if (canBegin) {
// BLINKER_LOG(BLINKER_F("Begin OTA. This may take 2 - 5 mins to complete. Things might be quite for a while.. Patience!"));
// // No activity would appear on the Serial monitor
// // So be patient. This may take 2 - 5mins to complete
// size_t written = BlinkerUpdater.writeStream(client_s);
// if (written == contentLength) {
// BLINKER_LOG(BLINKER_F("Written : "), written, BLINKER_F(" successfully"));
// }
// else {
// BLINKER_LOG(BLINKER_F("Written only : "), written,
// BLINKER_F("/"), contentLength, BLINKER_F(". Retry?"));
// }
// if (BlinkerUpdater.end()) {
// BLINKER_LOG(BLINKER_F("OTA done!"));
// if (BlinkerUpdater.isFinished()) {
// BLINKER_LOG(BLINKER_F("Update successfully completed. Rebooting."));
// _status = BLINKER_UPGRADE_SUCCESS;
// // ESP.restart();
// return true;
// } else {
// BLINKER_LOG(BLINKER_F("Update not finished? Something went wrong!"));
// _status = BLINKER_UPGRADE_FAIL;
// return false;
// }
// }
// else {
// BLINKER_LOG(BLINKER_F("Error Occurred. Error #: "), BlinkerUpdater.getError());
// _status = BLINKER_UPGRADE_FAIL;
// // return false;
// }
// }
// else {
// // not enough space to begin OTA
// // Understand the partitions and
// // space availability
// BLINKER_LOG(BLINKER_F("Not enough space to begin OTA"));
// client_s.flush();
// _status = BLINKER_UPGRADE_FAIL;
// return false;
// }
// }
// else {
// BLINKER_LOG(BLINKER_F("There was no content in the response"));
// client_s.flush();
// _status = BLINKER_UPGRADE_FAIL;
// return false;
// }
// }
// }
// }
uint8_t BlinkerOTA::loadOTACheck() {
// #if defined(ESP8266)
static uint8_t OTACheck;
// #endif
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.get(BLINKER_EEP_ADDR_OTA_CHECK, OTACheck);
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("OTA Check: "), OTACheck);
// BLINKER_LOG_ALL(BLINKER_F("BLINKER_EEP_ADDR_OTA_CHECK: "), BLINKER_EEP_ADDR_OTA_CHECK);
return OTACheck;
// if (OTACheck != BLINKER_OTA_START) {
// BLINKER_LOG_ALL(BLINKER_F("OTA NOT START"));
// return false;
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("OTA START"));
// return true;
// }
}
void BlinkerOTA::saveOTARun() {
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_OTA_CHECK, BLINKER_OTA_RUN);
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("OTA RUN: "), BLINKER_OTA_RUN);
}
void BlinkerOTA::saveOTACheck() {
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_OTA_CHECK, BLINKER_OTA_START);
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("OTA START: "), BLINKER_OTA_START);
}
void BlinkerOTA::clearOTACheck() {
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_OTA_CHECK, BLINKER_OTA_CLEAR);
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("OTA CLEAR: "), BLINKER_OTA_CLEAR);
_status = BLINKER_UPGRADE_DISABLE;
}
bool BlinkerOTA::loadVersion() {
// #if defined(BLINKER_PRO)
// uint32_t versionCheck;
// EEPROM.begin(BLINKER_EEP_SIZE);
// EEPROM.get(BLINKER_EEP_ADDR_OTA_INFO, versionCheck);//+BUNDLINGSIZE+isBundling
// EEPROM.commit();
// EEPROM.end();
// BLINKER_LOG_ALL(BLINKER_F("loadVersion: "), versionCheck);
// if (versionCheck != BLINKER_OTA_VERSION_CODE) {
// BLINKER_LOG_ALL(BLINKER_F("OTA SUCCESS BLINKER_OTA_VERSION_CODE NOT UPGRADE"));
// _status = BLINKER_UPGRADE_SUCCESS;
// return false;
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("OTA FAIL OR NOT START OTA"));
// _status = BLINKER_UPGRADE_FAIL;
// return true;
// }
// #else
char versionCheck[11];
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.get(BLINKER_EEP_ADDR_OTA_INFO, versionCheck);//+BUNDLINGSIZE+isBundling
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("loadVersion: "), versionCheck);
if (strcmp(versionCheck, BLINKER_OTA_VERSION_CODE)) {
BLINKER_LOG_ALL(BLINKER_F("OTA SUCCESS BLINKER_OTA_VERSION_CODE NOT UPGRADE"));
_status = BLINKER_UPGRADE_SUCCESS;
return false;
}
else {
BLINKER_LOG_ALL(BLINKER_F("OTA FAIL OR NOT START OTA"));
_status = BLINKER_UPGRADE_FAIL;
return true;
}
// #endif
}
void BlinkerOTA::saveVersion() {
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_OTA_INFO, BLINKER_OTA_VERSION_CODE);//+BUNDLINGSIZE+isBundling
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("SAVE BLINKER_OTA_VERSION_CODE"));
}
#endif
#endif

View File

@@ -0,0 +1,66 @@
#ifndef BlinkerRGB_H
#define BlinkerRGB_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerRGB
{
public :
BlinkerRGB(char _name[], blinker_callback_with_rgb_arg_t _func = NULL)
// : rgbName(_name)
{
wNum = Blinker.attachWidget(_name, _func);
}
void attach(blinker_callback_with_rgb_arg_t _func)
{
if (wNum == 0) return;
Blinker.freshAttachWidget(Blinker.widgetName_rgb(wNum), _func);
}
void brightness(uint8_t _bright) { rgbrightness = _bright; }
void print(uint8_t _r, uint8_t _g, uint8_t _b)
{
if (wNum == 0) return;
String rgbData = BLINKER_F("[");
rgbData += STRING_format(_r);
rgbData += BLINKER_F(",");
rgbData += STRING_format(_g);
rgbData += BLINKER_F(",");
rgbData += STRING_format(_b);
rgbData += BLINKER_F(",");
rgbData += STRING_format(rgbrightness);
rgbData += BLINKER_F("]");
Blinker.printArray(Blinker.widgetName_rgb(wNum), rgbData);
}
void print(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _bright)
{
if (wNum == 0) {
return;
}
String rgbData = BLINKER_F("[");
rgbData += STRING_format(_r);
rgbData += BLINKER_F(",");
rgbData += STRING_format(_g);
rgbData += BLINKER_F(",");
rgbData += STRING_format(_b);
rgbData += BLINKER_F(",");
rgbData += STRING_format(_bright);
rgbData += BLINKER_F("]");
Blinker.printArray(Blinker.widgetName_rgb(wNum), rgbData);
}
private :
uint8_t wNum;
uint8_t rgbrightness = 0;
};
#endif

View File

@@ -0,0 +1,542 @@
#ifndef BLINKER_SIM7000_H
#define BLINKER_SIM7000_H
#if defined(ARDUINO)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#endif
#include "../Blinker/BlinkerATMaster.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
#include <time.h>
#define BLINKER_SIM7000_DATA_BUFFER_SIZE 1024
enum sim7000_status_t
{
sim7000_cpin_REQ,
sim7000_cpin_success,
sim7000_csq_REQ,
sim7000_csq_success,
sim7000_cgreg_REQ,
sim7000_cgreg_success,
sim7000_cgact_REQ,
sim7000_cgact_success,
sim7000_cops_REQ,
sim7000_cops_success,
sim7000_cnact_REQ,
sim7000_cnact_success,
sim7000_cgcontrdp_REQ,
sim7000_cgcontrdp_success,
};
class BlinkerSIM7000
{
public :
BlinkerSIM7000() :
isHWS(false)
{}
~BlinkerSIM7000() { flush(); }
time_t _ntpTime = 0;
void setStream(Stream& s, bool isHardware, blinker_callback_t _func)
{ stream = &s; isHWS = isHardware; listenFunc = _func; }
bool getSNTP(float _tz = 8.0, char _url[] = "120.25.108.11")
{
// uint32_t os_time = millis();
// _timezone = _tz;
// streamPrint(STRING_format(BLINKER_CMD_CNTP_REQ) + \
// "=" + STRING_format(_url) + \
// "," + STRING_format((int8_t)(_tz * 4)));
// while(millis() - os_time < _simTimeout * 10)
// {
// if (available())
// {
// _masterAT = new BlinkerMasterAT();
// _masterAT->update(STRING_format(streamData));
// if (_masterAT->getState() != AT_M_NONE &&
// _masterAT->reqName() == BLINKER_CMD_CNTP)
// {
// free(_masterAT);
// return true;
// }
// free(_masterAT);
// }
// }
// return false;
return true;
}
int checkPDN()
{
uint32_t sim_time = millis();
sim7000_status_t pdn_status = sim7000_cpin_REQ;
streamPrint(BLINKER_CMD_CPIN_REQ);
while(millis() - sim_time < _simTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CPIN &&
_masterAT->getParam(0) == BLINKER_CMD_READY)
{
BLINKER_LOG_ALL(BLINKER_F("sim7000_cpin_success"));
pdn_status = sim7000_cpin_success;
free(_masterAT);
break;
}
free(_masterAT);
}
}
if (pdn_status != sim7000_cpin_success) return false;
// streamPrint(BLINKER_CMD_CSQ_REQ);
// sim_time = millis();
// while(millis() - sim_time < _simTimeout)
// {
// if (available())
// {
// _masterAT = new BlinkerMasterAT();
// _masterAT->update(STRING_format(streamData));
// if (_masterAT->getState() != AT_M_NONE &&
// _masterAT->reqName() == BLINKER_CMD_CSQ &&
// _masterAT->getParam(0).toInt() != 99)
// {
// BLINKER_LOG_ALL(BLINKER_F("sim7000_csq_success"));
// pdn_status = sim7000_csq_success;
// free(_masterAT);
// break;
// }
// free(_masterAT);
// }
// }
// if (pdn_status != sim7000_csq_success) return false;
// streamPrint(BLINKER_CMD_COPS_REQ);
// sim_time = millis();
// while(millis() - sim_time < _simTimeout)
// {
// if (available())
// {
// _masterAT = new BlinkerMasterAT();
// _masterAT->update(STRING_format(streamData));
// if (_masterAT->getState() != AT_M_NONE &&
// _masterAT->reqName() == BLINKER_CMD_COPS)
// // &&
// // _masterAT->getParam(3).toInt() == 9)
// {
// BLINKER_LOG_ALL(BLINKER_F("sim7000_cops_success"));
// pdn_status = sim7000_cops_success;
// free(_masterAT);
// break;
// }
// free(_masterAT);
// }
// }
// if (pdn_status != sim7000_cops_success) return false;
streamPrint(STRING_format(BLINKER_CMD_CNACT_REQ) + "=?");
sim_time = millis();
while(millis() - sim_time < _simTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == "CNACT" &&
_masterAT->getParam(0).toInt() == 0)
{
BLINKER_LOG_ALL(BLINKER_F("sim7000_cops_success"));
pdn_status = sim7000_cops_success;
free(_masterAT);
break;
}
free(_masterAT);
// return true;
}
}
streamPrint(STRING_format(BLINKER_CMD_CNACT_REQ) + "=1,\"cmnet\"");
sim_time = millis();
while(millis() - sim_time < _simTimeout)
{
if (available())
{
if (strncmp(streamData, BLINKER_CMD_OK, 2) == 0)
{
//return true;
pdn_status = sim7000_cnact_success;
}
}
}
delay(2000);
while(millis() - sim_time < _simTimeout)
{
if (available())
{
if (strstr(streamData, "ACTIVE") && strstr(streamData, "DEACTIVED") == 0)
{
return true;
// pdn_status = sim7000_cnact_success;
}
}
}
return true;
}
String getIMEI()
{
uint32_t dev_time = millis();
streamPrint(BLINEKR_CMD_GSN_REQ);
char _imei[16];
while(millis() - dev_time < _simTimeout)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), streamData,
BLINKER_F(", length: "), strlen(streamData));
if (strlen(streamData) == 15)
{
strcpy(_imei, streamData);
}
}
}
dev_time = millis();
while(millis() - dev_time < _simTimeout)
{
if (available())
{
if (strncmp(streamData, BLINKER_CMD_OK, 2) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), _imei,
BLINKER_F(", length: "), strlen(streamData));
return _imei;
}
}
}
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), _imei,
BLINKER_F(", length: "), strlen(streamData));
return _imei;
}
String getICCID()
{
uint32_t dev_time = millis();
streamPrint(BLINKER_CMD_CCID_REQ);
char _iccid[21];
while(millis() - dev_time < _simTimeout)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), streamData,
BLINKER_F(", length: "), strlen(streamData));
if (strlen(streamData) == 20)
{
strcpy(_iccid, streamData);
}
}
}
dev_time = millis();
while(millis() - dev_time < _simTimeout)
{
if (available())
{
if (strncmp(streamData, BLINKER_CMD_OK, 2) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), _iccid,
BLINKER_F(", length: "), strlen(streamData));
return _iccid;
}
}
}
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), _iccid,
BLINKER_F(", length: "), strlen(streamData));
return _iccid;
}
bool powerCheck()
{
// streamPrint(BLINKER_CMD_AT);
streamPrint("ATE0");
uint32_t dev_time = millis();
while(millis() - dev_time < _simTimeout)
{
if (available())
{
if (strncmp(streamData, BLINKER_CMD_OK, 2) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("ATE0"));
}
}
}
if (!checkPDN()) return false;
// BLINKER_LOG_ALL(BLINKER_F("power check"));
// streamPrint(BLINKER_CMD_AT);
// uint32_t dev_time = millis();
// while(millis() - dev_time < _simTimeout)
// {
// if (available())
// {
// if (strncmp(streamData, BLINKER_CMD_OK, 2) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("power on"));
// // SAPBR();
return true;
// }
// }
// }
// return false;
}
bool isReboot()
{
streamPrint(BLINKER_CMD_AT);
uint32_t dev_time = millis();
while(millis() - dev_time < _simTimeout)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("data len: "), strlen(streamData));
if (strncmp(streamData, BLINKER_CMD_AT, 2) == 0 || \
(strlen(streamData) != 0 && strncmp(streamData, BLINKER_CMD_OK, 2) != 0 && \
strncmp(streamData, BLINKER_CMD_ERROR, 5) != 0))
{
BLINKER_LOG_ALL(BLINKER_F("device reboot"));
// SAPBR();
// streamPrint("ATE0");
return true;
}
if (strncmp(streamData, BLINKER_CMD_OK, 2) == 0) return false;
// else if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("device not reboot"));
// return false;
// }
}
}
return false;
}
bool isAlive()
{
streamPrint(BLINKER_CMD_AT, "isAlive");
uint32_t dev_time = millis();
while(millis() - dev_time < _simTimeout * 2)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("alive"));
return true;
}
}
BLINKER_LOG_ALL(BLINKER_F("not alive"));
return false;
}
void flush()
{
if (isFresh) free(streamData);
BLINKER_LOG_ALL(BLINKER_F("flush sim7000"));
}
bool checkStream(char * data)
{
bool _check_ = strcmp(data, streamData);
BLINKER_LOG_ALL(BLINKER_F("checkStream: "), _check_);
return _check_ == 0;
}
bool available()
{
yield();
if (!isHWS)
{
// #if defined(__AVR__) || defined(ESP8266)
// if (!SSerial_API->isListening())
// {
// SSerial_API->listen();
// ::delay(100);
// }
// #endif
if (listenFunc) listenFunc();
}
// char _data[BLINKER_SIM7000_DATA_BUFFER_SIZE];// = { '\0' };
// memset(_data, '\0', BLINKER_SIM7000_DATA_BUFFER_SIZE);
if (stream->available())
{
// strcpy(_data, stream->readStringUntil('\n').c_str());
String _data = stream->readStringUntil('\n');
BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), _data);
// _data[strlen(_data) - 1] = '\0';
if (isFresh)
{
free(streamData);
isFresh = false;
}
if (_data.length() <= 1) return false;
streamData = (char*)malloc((_data.length() + 1)*sizeof(char));
strcpy(streamData, _data.c_str());
if (_data.length() > 0) streamData[_data.length() - 1] = '\0';
isFresh = true;
return true;
// // streamData = "";
// // memset(streamData, '\0', 128);
// if (isFresh) free(streamData);
// streamData = (char*)malloc(1*sizeof(char));
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // int16_t dNum = strlen(streamData);
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), streamData,
// // BLINKER_F(", dNum: "), dNum);
// // // stream->readString();
// int16_t dNum = 0;
// int c_d = timedRead();
// while (dNum < BLINKER_MAX_READ_SIZE &&
// c_d >=0 && c_d != '\n')
// {
// // if (c_d != '\r')
// {
// streamData[dNum] = (char)c_d;
// dNum++;
// streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
// }
// c_d = timedRead();
// }
// // dNum++;
// // // // streamData = (char*)realloc(streamData, dNum*sizeof(char));
// // streamData[dNum-1] = '\0';
// // stream->flush();
// // 7b2264657461696c223a207b2262726f6b6572223a2022616c6979756e222c20226465766963654e616d65223a20223237383636394232304d3235423634323230354e33435850222c2022696f744964223a20225346455467613255784b784e386a69716e4e516730303130356435343030222c2022696f74546f6b656e223a20226331353530643464346334623432666338376431343639373333363166353539222c202270726f647563744b6579223a20224a67434762486c6e64677a222c202275756964223a20223733633762356134623266323231633061373264376234313238653430323337227d2c20226d657373616765223a20313030307d
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// // BLINKER_F(" , dNum: "), dNum);
// BLINKER_LOG_FreeHeap_ALL();
// if (dNum < BLINKER_MAX_READ_SIZE && dNum > 0)
// {
// // if (streamData[dNum - 1] == '\r')
// streamData[dNum - 1] = '\0';
// BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// BLINKER_F(" , dNum: "), dNum);
// isFresh = true;
// return true;
// }
// else
// {
// return false;
// }
}
else
{
return false;
}
}
protected :
class BlinkerMasterAT * _masterAT;
blinker_callback_t listenFunc = NULL;
Stream* stream;
// char streamData[128];
char* streamData;
bool isFresh = false;
bool isHWS = false;
uint16_t _simTimeout = 1000;
// time_t _ntpTime = 0;
float _timezone = 8.0;
void streamPrint(const String & s, String from = "")
{
stream->println(s);
BLINKER_LOG_ALL(from, BLINKER_F(" SIM: "), s);
}
int timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
};
#endif

View File

@@ -0,0 +1,695 @@
#ifndef BLINKER_SIM7020_H
#define BLINKER_SIM7020_H
#if defined(ARDUINO)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#endif
#include "../Blinker/BlinkerATMaster.h"
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include "../Blinker/BlinkerStream.h"
#include "../Blinker/BlinkerUtility.h"
#include <time.h>
#define BLINKER_SIM7020_DATA_BUFFER_SIZE 1024
enum sim7020_status_t
{
sim7020_cpin_REQ,
sim7020_cpin_success,
sim7020_csq_REQ,
sim7020_csq_success,
sim7020_cgreg_REQ,
sim7020_cgreg_success,
sim7020_cgact_REQ,
sim7020_cgact_success,
sim7020_cops_REQ,
sim7020_cops_success,
sim7020_cgcontrdp_REQ,
sim7020_cgcontrdp_success,
};
class BlinkerSIM7020
{
public :
BlinkerSIM7020() :
isHWS(false)
{}
~BlinkerSIM7020() { flush(); }
time_t _ntpTime = 0;
void setStream(Stream& s, bool isHardware, blinker_callback_t _func)
{ stream = &s; isHWS = isHardware; listenFunc = _func; }
// int16_t year()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_year + 1900;
// }
// return -1;
// }
// int8_t month()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_mon + 1;
// }
// return -1;
// }
// int8_t mday()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_mday;
// }
// return -1;
// }
// int8_t wday()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_wday;
// }
// return -1;
// }
// int8_t hour()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_hour;
// }
// return -1;
// }
// int8_t minute()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_min;
// }
// return -1;
// }
// int8_t second()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_sec;
// }
// return -1;
// }
// time_t time()
// {
// if (_ntpTime)
// {
// return _ntpTime - _timezone * 3600;
// }
// return millis();
// }
// // time_t now_ntp()
// // {
// // if (_ntpTime)
// // {
// // return _ntpTime;
// // }
// // return millis();
// // }
// int32_t dtime()
// {
// if (_ntpTime)
// {
// struct tm timeinfo;
// #if defined(ESP8266) || defined(__AVR__)
// gmtime_r(&_ntpTime, &timeinfo);
// #elif defined(ESP32)
// localtime_r(&_ntpTime, &timeinfo);
// #endif
// return timeinfo.tm_hour * 60 * 60 + timeinfo.tm_min * 60 + timeinfo.tm_sec;
// }
// return -1;
// }
bool getSNTP(float _tz = 8.0, char _url[] = "120.25.108.11")
{
uint32_t os_time = millis();
_timezone = _tz;
streamPrint(STRING_format(BLINKER_CMD_CSNTPSTART_REQ) + \
"=" + STRING_format(_url));
while(millis() - os_time < _simTimeout * 10)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CSNTP)
{
struct tm timeinfo;
timeinfo.tm_year = _masterAT->getParam(0).substring(1, 3).toInt() + 130;
timeinfo.tm_mon = _masterAT->getParam(0).substring(4, 6).toInt() - 1;
timeinfo.tm_mday = _masterAT->getParam(0).substring(7, 9).toInt() - 1;
timeinfo.tm_hour = _masterAT->getParam(1).substring(0, 2).toInt();
timeinfo.tm_min = _masterAT->getParam(1).substring(3, 5).toInt();
timeinfo.tm_sec = _masterAT->getParam(1).substring(6, 8).toInt();
#if defined(ESP8266) || defined(ESP32)
_ntpTime = mktime(&timeinfo) + (uint32_t)(_timezone * 3600);
#else
_ntpTime = mk_gmtime(&timeinfo) + (uint32_t)(_timezone * 3600);
#endif
// _ntpTime = mk_gmtime(&timeinfo) + (uint32_t)(_timezone * 3600);
BLINKER_LOG_ALL(BLINKER_F("year: "), timeinfo.tm_year);
BLINKER_LOG_ALL(BLINKER_F("mon: "), timeinfo.tm_mon);
BLINKER_LOG_ALL(BLINKER_F("mday: "), timeinfo.tm_mday);
BLINKER_LOG_ALL(BLINKER_F("hour: "), timeinfo.tm_hour);
BLINKER_LOG_ALL(BLINKER_F("mins: "), timeinfo.tm_min);
BLINKER_LOG_ALL(BLINKER_F("secs: "), timeinfo.tm_sec);
BLINKER_LOG_ALL(BLINKER_F("_ntpTime: "), _ntpTime);
// BLINKER_LOG_ALL(BLINKER_F("==Current time: "), mk_gmtime(&timeinfo));
free(_masterAT);
return true;
}
free(_masterAT);
}
}
return false;
}
int checkPDN()
{
uint32_t sim_time = millis();
sim7020_status_t pdn_status = sim7020_cpin_REQ;
streamPrint(BLINKER_CMD_CPIN_REQ);
while(millis() - sim_time < _simTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CPIN &&
_masterAT->getParam(0) == BLINKER_CMD_READY)
{
BLINKER_LOG_ALL(BLINKER_F("sim7020_cpin_success"));
pdn_status = sim7020_cpin_success;
free(_masterAT);
break;
}
free(_masterAT);
}
}
if (pdn_status != sim7020_cpin_success) return false;
streamPrint(BLINKER_CMD_CSQ_REQ);
sim_time = millis();
while(millis() - sim_time < _simTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CSQ &&
_masterAT->getParam(0).toInt() != 99)
{
BLINKER_LOG_ALL(BLINKER_F("sim7020_csq_success"));
pdn_status = sim7020_csq_success;
free(_masterAT);
break;
}
free(_masterAT);
}
}
if (pdn_status != sim7020_csq_success) return false;
streamPrint(BLINKER_CMD_CGREG_REQ);
sim_time = millis();
while(millis() - sim_time < _simTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CGREG &&
_masterAT->getParam(0).toInt() == 0)
{
BLINKER_LOG_ALL(BLINKER_F("sim7020_cgact_success"));
pdn_status = sim7020_cgact_success;
free(_masterAT);
break;
}
free(_masterAT);
}
}
if (pdn_status != sim7020_cgact_success) return false;
streamPrint(BLINKER_CMD_COPS_REQ);
sim_time = millis();
while(millis() - sim_time < _simTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_COPS)
// &&
// _masterAT->getParam(3).toInt() == 9)
{
BLINKER_LOG_ALL(BLINKER_F("sim7020_cops_success"));
pdn_status = sim7020_cops_success;
free(_masterAT);
break;
}
free(_masterAT);
}
}
if (pdn_status != sim7020_cops_success) return false;
streamPrint(BLINKER_CMD_CGCONTRDP_REQ);
sim_time = millis();
while(millis() - sim_time < _simTimeout)
{
if (available())
{
_masterAT = new BlinkerMasterAT();
_masterAT->update(STRING_format(streamData));
if (_masterAT->getState() != AT_M_NONE &&
_masterAT->reqName() == BLINKER_CMD_CGCONTRDP &&
_masterAT->getParam(2).toInt() == 5)
{
BLINKER_LOG_ALL(BLINKER_F("sim7020_contrdp_success"));
// pdn_status = sim7020_cops_success;
free(_masterAT);
return true;
}
free(_masterAT);
}
}
return true;
}
String getIMEI()
{
uint32_t dev_time = millis();
streamPrint(BLINEKR_CMD_GSN_REQ);
char _imei[16];
while(millis() - dev_time < _simTimeout)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), streamData,
BLINKER_F(", length: "), strlen(streamData));
if (strlen(streamData) == 15)
{
strcpy(_imei, streamData);
}
}
}
dev_time = millis();
while(millis() - dev_time < _simTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), _imei,
BLINKER_F(", length: "), strlen(streamData));
return _imei;
}
}
}
BLINKER_LOG_ALL(BLINKER_F("get IMEI: "), _imei,
BLINKER_F(", length: "), strlen(streamData));
return _imei;
}
String getICCID()
{
uint32_t dev_time = millis();
streamPrint(BLINKER_CMD_CCID_REQ);
char _iccid[21];
while(millis() - dev_time < _simTimeout)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), streamData,
BLINKER_F(", length: "), strlen(streamData));
if (strlen(streamData) == 20)
{
strcpy(_iccid, streamData);
}
}
}
dev_time = millis();
while(millis() - dev_time < _simTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), _iccid,
BLINKER_F(", length: "), strlen(streamData));
return _iccid;
}
}
}
BLINKER_LOG_ALL(BLINKER_F("get ICCID: "), _iccid,
BLINKER_F(", length: "), strlen(streamData));
return _iccid;
}
bool powerCheck()
{
streamPrint(BLINKER_CMD_AT);
streamPrint("ATE0");
if (!checkPDN()) return false;
BLINKER_LOG_ALL(BLINKER_F("power check"));
streamPrint(BLINKER_CMD_AT);
uint32_t dev_time = millis();
while(millis() - dev_time < _simTimeout)
{
if (available())
{
if (strcmp(streamData, BLINKER_CMD_OK) == 0)
{
BLINKER_LOG_ALL(BLINKER_F("power on"));
// SAPBR();
return true;
}
}
}
return false;
}
bool isReboot()
{
streamPrint(BLINKER_CMD_AT);
uint32_t dev_time = millis();
while(millis() - dev_time < _simTimeout)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("data len: "), strlen(streamData));
if (strncmp(streamData, BLINKER_CMD_AT, 2) == 0 || \
(strlen(streamData) != 0 && strncmp(streamData, BLINKER_CMD_OK, 2) != 0 && \
strncmp(streamData, BLINKER_CMD_ERROR, 5) != 0))
{
BLINKER_LOG_ALL(BLINKER_F("device reboot"));
// SAPBR();
// streamPrint("ATE0");
return true;
}
if (strncmp(streamData, BLINKER_CMD_OK, 2) == 0) return false;
// else if (strcmp(streamData, BLINKER_CMD_OK) == 0)
// {
// BLINKER_LOG_ALL(BLINKER_F("device not reboot"));
// return false;
// }
}
}
return false;
}
bool isAlive()
{
streamPrint(BLINKER_CMD_AT, "isAlive");
uint32_t dev_time = millis();
while(millis() - dev_time < _simTimeout * 2)
{
if (available())
{
BLINKER_LOG_ALL(BLINKER_F("alive"));
return true;
}
}
BLINKER_LOG_ALL(BLINKER_F("not alive"));
return false;
}
void flush()
{
if (isFresh) free(streamData);
BLINKER_LOG_ALL(BLINKER_F("flush sim7020"));
}
bool checkStream(char * data)
{
bool _check_ = strcmp(data, streamData);
BLINKER_LOG_ALL(BLINKER_F("checkStream: "), _check_);
return _check_ == 0;
}
bool available()
{
yield();
if (!isHWS)
{
// #if defined(__AVR__) || defined(ESP8266)
// if (!SSerial_API->isListening())
// {
// SSerial_API->listen();
// ::delay(100);
// }
// #endif
if (listenFunc) listenFunc();
}
// char _data[BLINKER_SIM7020_DATA_BUFFER_SIZE];// = { '\0' };
// memset(_data, '\0', BLINKER_SIM7020_DATA_BUFFER_SIZE);
if (stream->available())
{
// strcpy(_data, stream->readStringUntil('\n').c_str());
String _data = stream->readStringUntil('\n');
BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), _data);
// _data[strlen(_data) - 1] = '\0';
if (isFresh)
{
free(streamData);
isFresh = false;
}
if (_data.length() <= 1) return false;
streamData = (char*)malloc((_data.length() + 1)*sizeof(char));
strcpy(streamData, _data.c_str());
if (_data.length() > 0) streamData[_data.length() - 1] = '\0';
isFresh = true;
return true;
// // streamData = "";
// // memset(streamData, '\0', 128);
// if (isFresh) free(streamData);
// streamData = (char*)malloc(1*sizeof(char));
// // strcpy(streamData, stream->readStringUntil('\n').c_str());
// // int16_t dNum = strlen(streamData);
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial rs: "), streamData,
// // BLINKER_F(", dNum: "), dNum);
// // // stream->readString();
// int16_t dNum = 0;
// int c_d = timedRead();
// while (dNum < BLINKER_MAX_READ_SIZE &&
// c_d >=0 && c_d != '\n')
// {
// // if (c_d != '\r')
// {
// streamData[dNum] = (char)c_d;
// dNum++;
// streamData = (char*)realloc(streamData, (dNum+1)*sizeof(char));
// }
// c_d = timedRead();
// }
// // dNum++;
// // // // streamData = (char*)realloc(streamData, dNum*sizeof(char));
// // streamData[dNum-1] = '\0';
// // stream->flush();
// // 7b2264657461696c223a207b2262726f6b6572223a2022616c6979756e222c20226465766963654e616d65223a20223237383636394232304d3235423634323230354e33435850222c2022696f744964223a20225346455467613255784b784e386a69716e4e516730303130356435343030222c2022696f74546f6b656e223a20226331353530643464346334623432666338376431343639373333363166353539222c202270726f647563744b6579223a20224a67434762486c6e64677a222c202275756964223a20223733633762356134623266323231633061373264376234313238653430323337227d2c20226d657373616765223a20313030307d
// // BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// // BLINKER_F(" , dNum: "), dNum);
// BLINKER_LOG_FreeHeap_ALL();
// if (dNum < BLINKER_MAX_READ_SIZE && dNum > 0)
// {
// // if (streamData[dNum - 1] == '\r')
// streamData[dNum - 1] = '\0';
// BLINKER_LOG_ALL(BLINKER_F("handleSerial: "), streamData,
// BLINKER_F(" , dNum: "), dNum);
// isFresh = true;
// return true;
// }
// else
// {
// return false;
// }
}
else
{
return false;
}
}
protected :
class BlinkerMasterAT * _masterAT;
blinker_callback_t listenFunc = NULL;
Stream* stream;
// char streamData[128];
char* streamData;
bool isFresh = false;
bool isHWS = false;
uint16_t _simTimeout = 1000;
// time_t _ntpTime = 0;
float _timezone = 8.0;
void streamPrint(const String & s, String from = "")
{
stream->println(s);
BLINKER_LOG_ALL(from, BLINKER_F(" SIM: "), s);
}
int timedRead()
{
int c;
uint32_t _startMillis = millis();
do {
c = stream->read();
if (c >= 0) return c;
} while(millis() - _startMillis < 1000);
return -1;
}
};
#endif

View File

@@ -0,0 +1,87 @@
#ifndef BlinkerSlider_H
#define BlinkerSlider_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerSlider
{
public :
BlinkerSlider(char _name[], blinker_callback_with_int32_arg_t _func = NULL)
// : sliderName(_name)
{
wNum = Blinker.attachWidget(_name, _func);
}
void attach(blinker_callback_with_int32_arg_t _func)
{
if (wNum == 0) return;
Blinker.freshAttachWidget(Blinker.widgetName_int(wNum), _func);
}
void color(const String & _clr)
{
if (_fresh >> 0 & 0x01) free(textClr);
textClr = (char*)malloc((_clr.length()+1)*sizeof(char));
strcpy(textClr, _clr.c_str());
_fresh |= 0x01 << 0;
}
void print(char value) { _print(STRING_format(value)); }
void print(unsigned char value) { _print(STRING_format(value)); }
void print(int value) { _print(STRING_format(value)); }
void print(unsigned int value) { _print(STRING_format(value)); }
void print(long value) { _print(STRING_format(value)); }
void print(unsigned long value) { _print(STRING_format(value)); }
void print(double value) { _print(STRING_format(value)); }
void print() { _print(""); }
private :
uint8_t wNum;
char * textClr;
uint8_t _fresh = 0;
void _print(const String & n)
{
if ((_fresh == 0 && n.length() == 0) || \
wNum == 0)
{
return;
}
String sliderData;
if (n.length())
{
sliderData += BLINKER_F("{\"");
sliderData += BLINKER_F(BLINKER_CMD_VALUE);
sliderData += BLINKER_F("\":");
sliderData += n;
}
if (_fresh >> 0 & 0x01)
{
if (sliderData.length()) sliderData += BLINKER_F(",");
else sliderData += BLINKER_F("{");
sliderData += BLINKER_F("\"");
sliderData += BLINKER_F(BLINKER_CMD_COLOR);
sliderData += BLINKER_F("\":\"");
sliderData += (textClr);
sliderData += BLINKER_F("\"");
free(textClr);
}
sliderData += BLINKER_F("}");
_fresh = 0;
Blinker.printArray(Blinker.widgetName_int(wNum), sliderData);
}
};
#endif

View File

@@ -0,0 +1,27 @@
#ifndef BlinkerSwitch_H
#define BlinkerSwitch_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerSwitch
{
public :
BlinkerSwitch()
// : sName(BLINKER_CMD_BUILTIN_SWITCH)
{}
void attach(blinker_callback_with_string_arg_t _func)
{
Blinker.attachSwitch(_func);
}
void print(const String & _state)
{
Blinker.print(BLINKER_CMD_BUILTIN_SWITCH, _state);
}
private :
// String sName;
};
#endif

View File

@@ -0,0 +1,91 @@
#ifndef BlinkerTab_H
#define BlinkerTab_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerTab
{
public :
BlinkerTab(char _name[], blinker_callback_with_table_arg_t _func = NULL,
blinker_callback_t _func2 = NULL)
{
wNum = Blinker.attachWidget(_name, _func, _func2);
tabSet = 0;
}
void attach(blinker_callback_with_table_arg_t _func, blinker_callback_t _func2)
{
if (wNum == 0) return;
Blinker.freshAttachWidget(Blinker.widgetName_tab(wNum), _func, _func2);
}
void tab(uint8_t num)
{
switch (num)
{
case 0:
tabSet |= 1 << 4;
break;
case 1:
tabSet |= 1 << 3;
break;
case 2:
tabSet |= 1 << 2;
break;
case 3:
tabSet |= 1 << 1;
break;
case 4:
tabSet |= 1 << 0;
break;
default:
break;
}
}
void tab_0() { tabSet |= 1 << 4; }
void tab_1() { tabSet |= 1 << 3; }
void tab_2() { tabSet |= 1 << 2; }
void tab_3() { tabSet |= 1 << 1; }
void tab_4() { tabSet |= 1 << 0; }
void print()
{
if (wNum == 0) return;
String tabData = "";
tabData += BLINKER_F("{\"");
tabData += BLINKER_F(BLINKER_CMD_VALUE);
tabData += BLINKER_F("\":\"");
// tabData += (_state);
if (tabSet & 1 << 4) tabData += BLINKER_F("1");
else tabData += BLINKER_F("0");
if (tabSet & 1 << 3) tabData += BLINKER_F("1");
else tabData += BLINKER_F("0");
if (tabSet & 1 << 2) tabData += BLINKER_F("1");
else tabData += BLINKER_F("0");
if (tabSet & 1 << 1) tabData += BLINKER_F("1");
else tabData += BLINKER_F("0");
if (tabSet & 1 << 0) tabData += BLINKER_F("1");
else tabData += BLINKER_F("0");
tabData += BLINKER_F("\"}");
tabSet = 0;
Blinker.printArray(Blinker.widgetName_tab(wNum), tabData);
}
private :
uint8_t wNum;
uint8_t tabSet;
};
#endif

View File

@@ -0,0 +1,86 @@
#ifndef BlinkerTable_H
#define BlinkerTable_H
#include "Blinker/BlinkerConfig.h"
#include "Blinker/BlinkerUtility.h"
class BlinkerTable
{
public :
BlinkerTable(char _name[], blinker_callback_with_table_arg_t _func = NULL)
{
wNum = Blinker.attachWidget(_name, _func);
}
void attach(blinker_callback_with_table_arg_t _func)
{
if (wNum == 0) return;
Blinker.freshAttachWidget(Blinker.widgetName_tab(wNum), _func);
}
void table_0()
{
tabSet = tabSet | (1 << 4);
}
void table_1()
{
tabSet = tabSet | (1 << 3);
}
void table_2()
{
tabSet = tabSet | (1 << 2);
}
void table_3()
{
tabSet = tabSet | (1 << 1);
}
void table_4()
{
tabSet = tabSet | (1 << 0);
}
void print()
{
if (wNum == 0) return;
String tabData;
tabData += BLINKER_F("{\"");
tabData += BLINKER_F(BLINKER_CMD_VALUE);
tabData += BLINKER_F("\":\"");
BLINKER_LOG_ALL(BLINKER_F("tabSet: "), tabSet);
if ((tabSet >> 4) & 0x01) { tabData += "1"; }
else { tabData += "0"; }
if ((tabSet >> 3) & 0x01) { tabData += "1"; }
else { tabData += "0"; }
if ((tabSet >> 2) & 0x01) { tabData += "1"; }
else { tabData += "0"; }
if ((tabSet >> 1) & 0x01) { tabData += "1"; }
else { tabData += "0"; }
if ((tabSet >> 0) & 0x01) { tabData += "1"; }
else { tabData += "0"; }
tabData += BLINKER_F("\"}");
tabSet = 0;
Blinker.printArray(Blinker.widgetName_tab(wNum), tabData);
}
private :
uint8_t wNum;
uint8_t tabSet = 0;
};
#endif

View File

@@ -0,0 +1,128 @@
#ifndef BlinkerText_H
#define BlinkerText_H
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerText
{
public :
BlinkerText(char _name[])
{
textName = (char*)malloc((strlen(_name)+1)*sizeof(char));
strcpy(textName, _name);
}
template <typename T>
void print(T _text)
{
// if (isnan(_text)) return;
String textData = BLINKER_F("{\"");
textData += BLINKER_F(BLINKER_CMD_TEXT);
textData += BLINKER_F("\":\"");
textData += STRING_format(_text);
textData += BLINKER_F("\"");
if (_fresh >> 0 & 0x01)
{
textData += BLINKER_F(",\"");
textData += BLINKER_F(BLINKER_CMD_ICON);
textData += BLINKER_F("\":\"");
textData += nicon;
textData += BLINKER_F("\"");
free(nicon);
}
if (_fresh >> 1 & 0x01)
{
textData += BLINKER_F(",\"");
textData += BLINKER_F(BLINKER_CMD_COLOR);
textData += BLINKER_F("\":\"");
textData += ncolor;
textData += BLINKER_F("\"");
free(ncolor);
}
textData += BLINKER_F("}");
_fresh = 0;
Blinker.printArray(textName, textData);
}
template <typename T1, typename T2>
void print(T1 _text1, T2 _text2)
{
// if (isnan(_text1) || isnan(_text2)) return;
String textData = BLINKER_F("{\"");
textData += BLINKER_F(BLINKER_CMD_TEXT);
textData += BLINKER_F("\":\"");
textData += STRING_format(_text1);
textData += BLINKER_F("\",\"");
textData += BLINKER_F(BLINKER_CMD_TEXT1);
textData += BLINKER_F("\":\"");
textData += STRING_format(_text2);
textData += BLINKER_F("\"");
if (_fresh >> 0 & 0x01)
{
textData += BLINKER_F(",\"");
textData += BLINKER_F(BLINKER_CMD_ICON);
textData += BLINKER_F("\":\"");
textData += nicon;
textData += BLINKER_F("\"");
free(nicon);
}
if (_fresh >> 1 & 0x01)
{
textData += BLINKER_F(",\"");
textData += BLINKER_F(BLINKER_CMD_COLOR);
textData += BLINKER_F("\":\"");
textData += ncolor;
textData += BLINKER_F("\"");
free(ncolor);
}
textData += BLINKER_F("}");
_fresh = 0;
Blinker.printArray(textName, textData);
}
void icon(const String & _icon)
{
if (_fresh >> 0 & 0x01) free(nicon);
nicon = (char*)malloc((_icon.length()+1)*sizeof(char));
strcpy(nicon, _icon.c_str());
_fresh |= 0x01 << 0;
}
void color(const String & _clr)
{
if (_fresh >> 1 & 0x01) free(ncolor);
ncolor = (char*)malloc((_clr.length()+1)*sizeof(char));
strcpy(ncolor, _clr.c_str());
_fresh |= 0x01 << 1;
}
private :
char * nicon;// = "";
char * ncolor;// = "";
char * textName;
uint8_t _fresh = 0;
};
#endif

View File

@@ -0,0 +1,72 @@
#ifndef BLINEKR_TICKER_H
#define BLINEKR_TICKER_H
#if defined(ARDUINO)
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#endif
#include "../Blinker/BlinkerDebug.h"
extern "C" {
typedef void (*blinker_callback_t)(void);
}
class BlinkerTicker
{
public :
BlinkerTicker() {}
// : day_time(0)
// , os_time(0)
// {}
// void freshTime(uint32_t time_s) { day_time = time_s; os_time = millis(); }
void attach(uint32_t seconds, blinker_callback_t func)
{
aim_time = seconds;
tickerFunc = func;
isRun = true;
}
void detach() { isRun = false; }
void run()
{
if (millis() - os_time >= 1000 && millis() - os_time < 2000)
{
os_time += 1000;
tick_time ++;
}
else if (millis() - os_time >= 2000)
{
tick_time += (millis() - os_time) / 1000;
os_time = millis();
}
if (isRun)
{
if (tick_time - aim_time >= 0 && tick_time - aim_time < 60)
{
BLINKER_LOG_ALL(BLINKER_F("ticker trigged"));
isRun = false;
if (tickerFunc) tickerFunc();
}
}
}
protected :
uint32_t tick_time;
uint32_t os_time;
uint32_t aim_time;
bool isRun = false;
blinker_callback_t tickerFunc = NULL;
};
#endif

View File

@@ -0,0 +1,112 @@
#ifndef BlinkerTimingTimer_H
#define BlinkerTimingTimer_H
#if defined(ESP8266) || defined(ESP32)
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerUtility.h"
class BlinkerTimingTimer
{
public :
BlinkerTimingTimer()
: timerState(false)
, isLoopTask(false)
{}
// BlinkerTimingTimer(uint32_t _timerData, String _action, String _text)
BlinkerTimingTimer(uint32_t _timerData, String _action)
: timerState(false)
, isLoopTask(false)
{
timerData = _timerData;
// actionData = _action;
actionData = (char*)malloc((_action.length()+1)*sizeof(char));
strcpy(actionData, _action.c_str());
// timerText = _text;
isLoopTask = timerData >> 31;
timerState = timerData >> 23 & 0x0001;
timingDay = timerData >> 11 & 0x007F;
timingTime = timerData & 0x07FF;
}
// BlinkerTimingTimer(bool _state, uint8_t _timingDay, uint16_t _timingTime, String _action, String _text, bool _isLoop)
BlinkerTimingTimer(bool _state, uint8_t _timingDay, uint16_t _timingTime, String _action, bool _isLoop)
: timerState(false)
, isLoopTask(false)
{
timerState = _state;
timingDay = _timingDay;
timingTime = _timingTime;
actionData = (char*)malloc((_action.length()+1)*sizeof(char));
strcpy(actionData, _action.c_str());
// actionData = _action;
// timerText = _text;
isLoopTask = _isLoop;
timerData = isLoopTask << 31 | timerState << 23 | timingDay << 11 | timingTime;
}
// void freshTimer(uint32_t _timerData, String _action, String _text) {
void freshTimer(uint32_t _timerData, String _action) {
timerData = _timerData;
actionData = (char*)malloc((_action.length()+1)*sizeof(char));
strcpy(actionData, _action.c_str());
// actionData = _action;
// timerText = _text;
isLoopTask = timerData >> 31;
timerState = timerData >> 23 & 0x0001;
timingDay = timerData >> 11 & 0x007F;
timingTime = timerData & 0x07FF;
}
bool isTimingDay(uint8_t _day) {
if (timingDay & (0x01 << _day)) return true;
else return false;
}
uint8_t getTimingday() { return timingDay; }
char * getAction() { return actionData; }
// String getText() { return timerText; }
uint32_t getTimerData() { return timerData; }
uint16_t getTime() { return timingTime; }
bool state() { return timerState; }
bool isLoop() { return isLoopTask; }
void disableTask() {
timerState = false;
timerData = isLoopTask << 31 | timerState << 23 | timingDay << 11 | timingTime;
}
private :
// - - - - - - - - | - - - - - - - - | - - - - - - - - | - - - - - - - -
// | | | | 11 0-0x7FF timingTime
// | | | 18 timingDay
// | | 24 timerState
// | 32 isLoopTask
uint32_t timerData;
uint8_t timingDay;
char* actionData;
// String timerText;
uint16_t timingTime;
bool timerState;
bool isLoopTask;
};
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,487 @@
#ifndef BLINKER_UPDATER_H
#define BLINKER_UPDATER_H
#if defined(ESP8266) || defined(ESP32)
#if defined(ESP8266)
// #define HTTPUPDATE_1_2_COMPATIBLE
// #include <Arduino.h>
// #include <ESP8266WiFi.h>
// #include <WiFiClient.h>
// #include <WiFiUdp.h>
// #include <ESP8266HTTPClient.h>
// #ifdef DEBUG_ESP_HTTP_UPDATE
// #ifdef DEBUG_ESP_PORT
// #define DEBUG_HTTP_UPDATE(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
// #endif
// #endif
// #ifndef DEBUG_HTTP_UPDATE
// #define DEBUG_HTTP_UPDATE(...) do { (void)0; } while(0)
// #endif
// /// note we use HTTP client errors too so we start at 100
// #define HTTP_UE_TOO_LESS_SPACE (-100)
// #define HTTP_UE_SERVER_NOT_REPORT_SIZE (-101)
// #define HTTP_UE_SERVER_FILE_NOT_FOUND (-102)
// #define HTTP_UE_SERVER_FORBIDDEN (-103)
// #define HTTP_UE_SERVER_WRONG_HTTP_CODE (-104)
// #define HTTP_UE_SERVER_FAULTY_MD5 (-105)
// #define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
// #define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
// enum BlinkerHTTPUpdateResult {
// HTTP_UPDATE_FAILED,
// HTTP_UPDATE_NO_UPDATES,
// HTTP_UPDATE_OK
// };
// typedef BlinkerHTTPUpdateResult t_httpUpdate_return; // backward compatibility
// class BlinkerHTTPUpdate
// {
// public:
// BlinkerHTTPUpdate(void);
// BlinkerHTTPUpdate(int httpClientTimeout);
// ~BlinkerHTTPUpdate(void);
// void rebootOnUpdate(bool reboot)
// {
// _rebootOnUpdate = reboot;
// }
// void followRedirects(bool follow)
// {
// _followRedirects = follow;
// }
// void setLedPin(int ledPin = -1, uint8_t ledOn = HIGH)
// {
// _ledPin = ledPin;
// _ledOn = ledOn;
// }
// #ifdef HTTPUPDATE_1_2_COMPATIBLE
// // This function is deprecated, use rebootOnUpdate and the next one instead
// t_httpUpdate_return update(const String& url, const String& currentVersion,
// const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
// t_httpUpdate_return update(const String& url, const String& currentVersion = "") __attribute__((deprecated));
// t_httpUpdate_return update(const String& url, const String& currentVersion,
// const String& httpsFingerprint) __attribute__((deprecated));
// t_httpUpdate_return update(const String& url, const String& currentVersion,
// const uint8_t httpsFingerprint[20]) __attribute__((deprecated)); // BearSSL
// #endif
// t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = "");
// t_httpUpdate_return update(WiFiClient& client, const String& url, const String& md5, const String& currentVersion);
// #ifdef HTTPUPDATE_1_2_COMPATIBLE
// // This function is deprecated, use one of the overloads below along with rebootOnUpdate
// t_httpUpdate_return update(const String& host, uint16_t port, const String& uri, const String& currentVersion,
// bool https, const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
// t_httpUpdate_return update(const String& host, uint16_t port, const String& uri = "/",
// const String& currentVersion = "") __attribute__((deprecated));
// t_httpUpdate_return update(const String& host, uint16_t port, const String& url,
// const String& currentVersion, const String& httpsFingerprint) __attribute__((deprecated));
// t_httpUpdate_return update(const String& host, uint16_t port, const String& url,
// const String& currentVersion, const uint8_t httpsFingerprint[20]) __attribute__((deprecated)); // BearSSL
// #endif
// t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/",
// const String& currentVersion = "");
// #ifdef HTTPUPDATE_1_2_COMPATIBLE
// // This function is deprecated, use rebootOnUpdate and the next one instead
// t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion,
// const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
// t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion = "") __attribute__((deprecated));
// t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint) __attribute__((deprecated));
// t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]) __attribute__((deprecated)); // BearSSL
// #endif
// t_httpUpdate_return updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion = "");
// int getLastError(void);
// String getLastErrorString(void);
// protected:
// t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false, const String& md5 = "");
// bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
// int _lastError;
// bool _rebootOnUpdate = true;
// private:
// int _httpClientTimeout;
// bool _followRedirects;
// int _ledPin;
// uint8_t _ledOn;
// };
// #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_HTTPUPDATE)
// extern BlinkerHTTPUpdate BlinkerhttpUpdate;
// #endif
#include <Arduino.h>
#include <flash_utils.h>
#include <MD5Builder.h>
#define UPDATE_ERROR_OK (0)
#define UPDATE_ERROR_WRITE (1)
#define UPDATE_ERROR_ERASE (2)
#define UPDATE_ERROR_READ (3)
#define UPDATE_ERROR_SPACE (4)
#define UPDATE_ERROR_SIZE (5)
#define UPDATE_ERROR_STREAM (6)
#define UPDATE_ERROR_MD5 (7)
#define UPDATE_ERROR_FLASH_CONFIG (8)
#define UPDATE_ERROR_NEW_FLASH_CONFIG (9)
#define UPDATE_ERROR_MAGIC_BYTE (10)
#define UPDATE_ERROR_BOOTSTRAP (11)
#define U_FLASH 0
#define U_SPIFFS 100
#define U_AUTH 200
// #ifdef DEBUG_ESP_UPDATER
// #ifdef DEBUG_ESP_PORT
// #define DEBUG_UPDATER DEBUG_ESP_PORT
// #endif
// #endif
class BlinkerUpdaterClass {
public:
typedef std::function<void(unsigned int, unsigned int)> THandlerFunction_Progress;
BlinkerUpdaterClass();
/*
This callback will be called when Update is receiving data
*/
BlinkerUpdaterClass& onProgress(THandlerFunction_Progress fn);
/*
Call this to check the space needed for the update
Will return false if there is not enough space
*/
bool begin(size_t size, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW);
/*
Run Updater from asynchronous callbacs
*/
void runAsync(bool async){ _async = async; }
/*
Writes a buffer to the flash and increments the address
Returns the amount written
*/
size_t write(uint8_t *data, size_t len);
/*
Writes the remaining bytes from the Stream to the flash
Uses readBytes() and sets UPDATE_ERROR_STREAM on timeout
Returns the bytes written
Should be equal to the remaining bytes when called
Usable for slow streams like Serial
*/
size_t writeStream(Stream &data);
/*
If all bytes are written
this call will write the config to eboot
and return true
If there is already an update running but is not finished and !evenIfRemaining
or there is an error
this will clear everything and return false
the last error is available through getError()
evenIfRemaining is helpful when you update without knowing the final size first
*/
bool end(bool evenIfRemaining = false);
/*
Prints the last error to an output stream
*/
String printError();
/*
sets the expected MD5 for the firmware (hexString)
*/
bool setMD5(const char * expected_md5);
/*
returns the MD5 String of the sucessfully ended firmware
*/
String md5String(void){ return _md5.toString(); }
/*
populated the result with the md5 bytes of the sucessfully ended firmware
*/
void md5(uint8_t * result){ return _md5.getBytes(result); }
//Helpers
uint8_t getError(){ return _error; }
void clearError(){ _error = UPDATE_ERROR_OK; }
bool hasError(){ return _error != UPDATE_ERROR_OK; }
bool isRunning(){ return _size > 0; }
bool isFinished(){ return _currentAddress == (_startAddress + _size); }
size_t size(){ return _size; }
size_t progress(){ return _currentAddress - _startAddress; }
size_t remaining(){ return _size - (_currentAddress - _startAddress); }
/*
Template to write from objects that expose
available() and read(uint8_t*, size_t) methods
faster than the writeStream method
writes only what is available
*/
template<typename T>
size_t write(T &data){
size_t written = 0;
if (hasError() || !isRunning())
return 0;
size_t available = data.available();
while(available) {
if(_bufferLen + available > remaining()){
available = remaining() - _bufferLen;
}
if(_bufferLen + available > _bufferSize) {
size_t toBuff = _bufferSize - _bufferLen;
data.read(_buffer + _bufferLen, toBuff);
_bufferLen += toBuff;
if(!_writeBuffer())
return written;
written += toBuff;
} else {
data.read(_buffer + _bufferLen, available);
_bufferLen += available;
written += available;
if(_bufferLen == remaining()) {
if(!_writeBuffer()) {
return written;
}
}
}
if(remaining() == 0)
return written;
delay(1);
available = data.available();
}
return written;
}
private:
void _reset();
bool _writeBuffer();
bool _verifyHeader(uint8_t data);
bool _verifyEnd();
void _setError(int error);
bool _async;
uint8_t _error;
uint8_t *_buffer;
size_t _bufferLen; // amount of data written into _buffer
size_t _bufferSize; // total size of _buffer
size_t _size;
THandlerFunction_Progress _progress_callback;
uint32_t _startAddress;
uint32_t _currentAddress;
uint32_t _command;
String _target_md5;
MD5Builder _md5;
// int _ledPin;
// uint8_t _ledOn;
};
extern BlinkerUpdaterClass BlinkerUpdater;
#elif defined(ESP32)
#include <Arduino.h>
#include <MD5Builder.h>
#include <functional>
#include "esp_partition.h"
#define UPDATE_ERROR_OK (0)
#define UPDATE_ERROR_WRITE (1)
#define UPDATE_ERROR_ERASE (2)
#define UPDATE_ERROR_READ (3)
#define UPDATE_ERROR_SPACE (4)
#define UPDATE_ERROR_SIZE (5)
#define UPDATE_ERROR_STREAM (6)
#define UPDATE_ERROR_MD5 (7)
#define UPDATE_ERROR_MAGIC_BYTE (8)
#define UPDATE_ERROR_ACTIVATE (9)
#define UPDATE_ERROR_NO_PARTITION (10)
#define UPDATE_ERROR_BAD_ARGUMENT (11)
#define UPDATE_ERROR_ABORT (12)
#define UPDATE_SIZE_UNKNOWN 0xFFFFFFFF
#define U_FLASH 0
#define U_SPIFFS 100
#define U_AUTH 200
class BlinkerUpdaterClass {
public:
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;
BlinkerUpdaterClass();
/*
This callback will be called when Update is receiving data
*/
BlinkerUpdaterClass& onProgress(THandlerFunction_Progress fn);
/*
Call this to check the space needed for the update
Will return false if there is not enough space
*/
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH);
/*
Writes a buffer to the flash and increments the address
Returns the amount written
*/
size_t write(uint8_t *data, size_t len);
/*
Writes the remaining bytes from the Stream to the flash
Uses readBytes() and sets UPDATE_ERROR_STREAM on timeout
Returns the bytes written
Should be equal to the remaining bytes when called
Usable for slow streams like Serial
*/
size_t writeStream(Stream &data);
/*
If all bytes are written
this call will write the config to eboot
and return true
If there is already an update running but is not finished and !evenIfRemainanig
or there is an error
this will clear everything and return false
the last error is available through getError()
evenIfRemaining is helpfull when you update without knowing the final size first
*/
bool end(bool evenIfRemaining = false);
/*
Aborts the running update
*/
void abort();
/*
Prints the last error to an output stream
*/
void printError(Stream &out);
/*
sets the expected MD5 for the firmware (hexString)
*/
bool setMD5(const char * expected_md5);
/*
returns the MD5 String of the sucessfully ended firmware
*/
String md5String(void){ return _md5.toString(); }
/*
populated the result with the md5 bytes of the sucessfully ended firmware
*/
void md5(uint8_t * result){ return _md5.getBytes(result); }
//Helpers
uint8_t getError(){ return _error; }
void clearError(){ _error = UPDATE_ERROR_OK; }
bool hasError(){ return _error != UPDATE_ERROR_OK; }
bool isRunning(){ return _size > 0; }
bool isFinished(){ return _progress == _size; }
size_t size(){ return _size; }
size_t progress(){ return _progress; }
size_t remaining(){ return _size - _progress; }
/*
Template to write from objects that expose
available() and read(uint8_t*, size_t) methods
faster than the writeStream method
writes only what is available
*/
template<typename T>
size_t write(T &data){
size_t written = 0;
if (hasError() || !isRunning())
return 0;
size_t available = data.available();
while(available) {
if(_bufferLen + available > remaining()){
available = remaining() - _bufferLen;
}
if(_bufferLen + available > 4096) {
size_t toBuff = 4096 - _bufferLen;
data.read(_buffer + _bufferLen, toBuff);
_bufferLen += toBuff;
if(!_writeBuffer())
return written;
written += toBuff;
} else {
data.read(_buffer + _bufferLen, available);
_bufferLen += available;
written += available;
if(_bufferLen == remaining()) {
if(!_writeBuffer()) {
return written;
}
}
}
if(remaining() == 0)
return written;
available = data.available();
}
return written;
}
/*
check if there is a firmware on the other OTA partition that you can bootinto
*/
bool canRollBack();
/*
set the other OTA partition as bootable (reboot to enable)
*/
bool rollBack();
private:
void _reset();
void _abort(uint8_t err);
bool _writeBuffer();
bool _verifyHeader(uint8_t data);
bool _verifyEnd();
uint8_t _error;
uint8_t *_buffer;
size_t _bufferLen;
size_t _size;
THandlerFunction_Progress _progress_callback;
uint32_t _progress;
uint32_t _command;
const esp_partition_t* _partition;
String _target_md5;
MD5Builder _md5;
};
extern BlinkerUpdaterClass BlinkerUpdater;
#endif
#endif
#endif

View File

@@ -0,0 +1,642 @@
// #ifndef BLINKER_WLAN_H
// #define BLINKER_WLAN_H
// #if defined(ESP8266) || defined(ESP32)
// // #if defined(BLINKER_PRO)
// // #include "Blinker/BlinkerConfig.h"
// // #include "modules/ArduinoJson/ArduinoJson.h"
// // #if defined(ESP8266)
// // #include <ESP8266WiFi.h>
// // #elif defined(ESP32)
// // #include <WiFi.h>
// // #endif
// // #include <EEPROM.h>
// // static WiFiServer *_server;
// // static WiFiClient _client;
// // static IPAddress apIP(192, 168, 4, 1);
// // #if defined(ESP8266)
// // static IPAddress netMsk(255, 255, 255, 0);
// // #endif
// #if ARDUINO >= 100
// #include <Arduino.h>
// #else
// #include <WProgram.h>
// #endif
// #include "../Blinker/BlinkerConfig.h"
// #include "../Blinker/BlinkerDebug.h"
// #include "../Blinker/BlinkerUtility.h"
// #include "BlinkerWlan.h"
// #ifndef ARDUINOJSON_VERSION_MAJOR
// #include "../modules/ArduinoJson/ArduinoJson.h"
// #endif
// #if defined(ESP8266)
// #include <ESP8266WiFi.h>
// #elif defined(ESP32)
// #include <WiFi.h>
// #endif
// #include <EEPROM.h>
// // static WiFiServer *_server;
// // static WiFiClient _client;
// static IPAddress apIP(192, 168, 4, 1);
// #if defined(ESP8266)
// static IPAddress netMsk(255, 255, 255, 0);
// #endif
// enum bwl_status_t
// {
// BWL_CONFIG_CKECK,
// BWL_CONFIG_FAIL,
// BWL_CONFIG_SUCCESS,
// BWL_CONNECTING,
// BWL_CONNECTED,
// BWL_DISCONNECTED,
// BWL_SMARTCONFIG_BEGIN,
// BWL_SMARTCONFIG_DONE,
// BWL_SMARTCONFIG_TIMEOUT,
// BWL_STACONFIG_BEGIN,
// BWL_APCONFIG_BEGIN,
// BWL_APCONFIG_DONE,
// BWL_APCONFIG_TIMEOUT,
// BWL_CONNECTED_CHECK,
// BWL_RESET
// };
// class BlinkerWlan
// {
// public :
// BlinkerWlan()
// : _status(BWL_CONFIG_CKECK)
// {}
// bool checkConfig();
// void loadConfig(char *_ssid, char *_pswd);
// void saveConfig(char *_ssid, char *_pswd);
// void deleteConfig();
// void smartconfigBegin(uint16_t _time = 15000);
// bool smartconfigDone();
// void connect();
// bool connected();
// void disconnect();
// void reset();
// bool run();
// bwl_status_t status() { return _status; }
// void setType(const char* _type) {
// _deviceType = _type;
// #ifdef BLINKER_DEBUG_ALL
// BLINKER_LOG(BLINKER_F("API deviceType: "), _type);
// #endif
// }
// void softAPinit();
// void serverClient();
// void parseUrl(String data);
// void connectWiFi(String _ssid, String _pswd);
// void connectWiFi(const char* _ssid, const char* _pswd);
// // uint8_t status() { return _status; }
// private :
// protected :
// char *SSID;
// char *PSWD;
// const char* _deviceType;
// uint32_t connectTime;
// uint16_t timeout;
// bwl_status_t _status;
// uint32_t debugStatusTime;
// };
// bool BlinkerWlan::checkConfig() {
// BLINKER_LOG_ALL(BLINKER_F("check wlan config"));
// char ok[2 + 1];
// EEPROM.begin(BLINKER_EEP_SIZE);
// EEPROM.get(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
// EEPROM.commit();
// EEPROM.end();
// if (String(ok) != String("OK")) {
// BLINKER_LOG(BLINKER_F("wlan config check,fail"));
// _status = BWL_CONFIG_FAIL;
// return false;
// }
// else {
// BLINKER_LOG(BLINKER_F("wlan config check,success"));
// _status = BWL_CONFIG_SUCCESS;
// return true;
// }
// }
// void BlinkerWlan::loadConfig(char *_ssid, char *_pswd) {
// char loadssid[BLINKER_SSID_SIZE];
// char loadpswd[BLINKER_PSWD_SIZE];
// EEPROM.begin(BLINKER_EEP_SIZE);
// EEPROM.get(BLINKER_EEP_ADDR_SSID, loadssid);
// EEPROM.get(BLINKER_EEP_ADDR_PSWD, loadpswd);
// // char ok[2 + 1];
// // EEPROM.get(EEP_ADDR_WIFI_CFG + BLINKER_SSID_SIZE + BLINKER_PSWD_SIZE, ok);
// EEPROM.commit();
// EEPROM.end();
// strcpy(_ssid, loadssid);
// strcpy(_pswd, loadpswd);
// BLINKER_LOG(BLINKER_F("SSID: "), _ssid, BLINKER_F(" PASWD: "), _pswd);
// }
// void BlinkerWlan::saveConfig(char *_ssid, char *_pswd) {
// char loadssid[BLINKER_SSID_SIZE];
// char loadpswd[BLINKER_PSWD_SIZE];
// memcpy(loadssid, _ssid, BLINKER_SSID_SIZE);
// memcpy(loadpswd, _pswd, BLINKER_PSWD_SIZE);
// EEPROM.begin(BLINKER_EEP_SIZE);
// EEPROM.put(BLINKER_EEP_ADDR_SSID, loadssid);
// EEPROM.put(BLINKER_EEP_ADDR_PSWD, loadpswd);
// char ok[2 + 1] = "OK";
// EEPROM.put(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
// EEPROM.commit();
// EEPROM.end();
// BLINKER_LOG(BLINKER_F("Save wlan config"));
// }
// void BlinkerWlan::deleteConfig() {
// char ok[3] = {0};
// EEPROM.begin(BLINKER_EEP_SIZE);
// // for (int i = BLINKER_EEP_ADDR_WLAN_CHECK; i < BLINKER_WLAN_CHECK_SIZE; i++)
// // EEPROM.write(i, 0);
// EEPROM.put(BLINKER_EEP_ADDR_WLAN_CHECK, ok);
// EEPROM.commit();
// EEPROM.end();
// BLINKER_LOG(BLINKER_F("Erase wlan config"));
// }
// void BlinkerWlan::smartconfigBegin(uint16_t _time) {
// WiFi.mode(WIFI_STA);
// delay(100);
// String softAP_ssid = STRING_format(_deviceType) + "_" + macDeviceName();
// #if defined(ESP8266)
// WiFi.hostname(softAP_ssid);
// #elif defined(ESP32)
// WiFi.setHostname(softAP_ssid.c_str());
// #endif
// WiFi.beginSmartConfig();
// connectTime = millis();
// timeout = _time;
// _status = BWL_SMARTCONFIG_BEGIN;
// BLINKER_LOG(BLINKER_F("Wait for Smartconfig"));
// }
// bool BlinkerWlan::smartconfigDone() {
// if (WiFi.smartConfigDone())
// {
// // WiFi.setAutoConnect(true);
// // WiFi.setAutoReconnect(true);
// connectTime = millis();
// _status = BWL_SMARTCONFIG_DONE;
// BLINKER_LOG(BLINKER_F("SmartConfig Success"));
// #if defined(ESP8266)
// BLINKER_LOG(BLINKER_F("SSID: "), WiFi.SSID(), BLINKER_F(" PSWD: "), WiFi.psk());
// // WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str());
// connectWiFi(WiFi.SSID().c_str(), WiFi.psk().c_str());
// // connectWiFi("有没有wifi", "i8888888");
// #endif
// return true;
// }
// else {
// return false;
// }
// }
// void BlinkerWlan::connect() {
// switch (_status) {
// case BWL_CONFIG_SUCCESS :
// // WiFi.setAutoConnect(false);
// // WiFi.setAutoReconnect(true);
// SSID = (char*)malloc(BLINKER_SSID_SIZE*sizeof(char));
// PSWD = (char*)malloc(BLINKER_PSWD_SIZE*sizeof(char));
// loadConfig(SSID, PSWD);
// // WiFi.begin(SSID, PSWD);
// connectWiFi(SSID, PSWD);
// free(SSID);
// free(PSWD);
// _status = BWL_CONNECTING;
// break;
// case BWL_DISCONNECTED :
// if (millis() - connectTime > 30000 && WiFi.status() != WL_CONNECTED) {
// BLINKER_LOG(BLINKER_F("status: "), WiFi.status());
// disconnect();
// // SSID = (char*)malloc(BLINKER_SSID_SIZE*sizeof(char));
// // PSWD = (char*)malloc(BLINKER_PSWD_SIZE*sizeof(char));
// // WiFi.reconnect();
// char _ssid_[BLINKER_SSID_SIZE];
// char _pswd_[BLINKER_PSWD_SIZE];
// loadConfig(_ssid_, _pswd_);
// connectWiFi(_ssid_, _pswd_);
// // WiFi.setAutoConnect(false);
// // WiFi.setAutoReconnect(true);
// // free(SSID);
// // free(PSWD);
// connectTime = millis();
// BLINKER_LOG(BLINKER_F("connecting BWL_DISCONNECTED"));
// BLINKER_LOG(BLINKER_F("_ssid_: "), _ssid_, BLINKER_F(" _pswd_: "), _pswd_);
// }
// else if(WiFi.status() == WL_CONNECTED) {
// _status = BWL_CONNECTED;
// }
// break;
// }
// }
// bool BlinkerWlan::connected() {
// switch (_status) {
// case BWL_SMARTCONFIG_DONE :
// if (WiFi.status() != WL_CONNECTED) {
// if (millis() - connectTime > 15000)
// {
// BLINKER_LOG(BLINKER_F("smartConfig time out"));
// WiFi.stopSmartConfig();
// _status = BWL_SMARTCONFIG_TIMEOUT;
// }
// return false;
// }
// else if (WiFi.status() == WL_CONNECTED) {
// // WiFi.stopSmartConfig();
// IPAddress deviceIP = WiFi.localIP();
// BLINKER_LOG(BLINKER_F("WiFi connected"));
// BLINKER_LOG(BLINKER_F("IP address: "));
// BLINKER_LOG(deviceIP);
// BLINKER_LOG(BLINKER_F("SSID: "), WiFi.SSID(), BLINKER_F(" PSWD: "), WiFi.psk());
// SSID = (char*)malloc(BLINKER_SSID_SIZE*sizeof(char));
// PSWD = (char*)malloc(BLINKER_PSWD_SIZE*sizeof(char));
// memcpy(SSID,"\0",BLINKER_SSID_SIZE);
// memcpy(SSID,WiFi.SSID().c_str(),BLINKER_SSID_SIZE);
// memcpy(PSWD,"\0",BLINKER_PSWD_SIZE);
// memcpy(PSWD,WiFi.psk().c_str(),BLINKER_PSWD_SIZE);
// saveConfig(SSID, PSWD);
// free(SSID);
// free(PSWD);
// // WiFi.setAutoConnect(true);
// // WiFi.setAutoReconnect(true);
// _status = BWL_CONNECTED_CHECK;
// BLINKER_LOG_FreeHeap_ALL();
// return true;
// }
// // break;
// case BWL_APCONFIG_DONE :
// if (WiFi.status() != WL_CONNECTED) {
// if (millis() - connectTime > 15000)
// {
// BLINKER_LOG(BLINKER_F("APConfig time out"));
// // WiFi.stopSmartConfig();
// _status = BWL_APCONFIG_TIMEOUT;
// }
// return false;
// }
// else if (WiFi.status() == WL_CONNECTED) {
// IPAddress deviceIP = WiFi.localIP();
// BLINKER_LOG(BLINKER_F("WiFi connected"));
// BLINKER_LOG(BLINKER_F("IP address: "));
// BLINKER_LOG(deviceIP);
// BLINKER_LOG(BLINKER_F("SSID: "), WiFi.SSID(), BLINKER_F(" PSWD: "), WiFi.psk());
// // SSID = (char*)malloc(BLINKER_SSID_SIZE*sizeof(char));
// // PSWD = (char*)malloc(BLINKER_PSWD_SIZE*sizeof(char));
// // memcpy(SSID,"\0",BLINKER_SSID_SIZE);
// // memcpy(SSID,WiFi.SSID().c_str(),BLINKER_SSID_SIZE);
// // memcpy(PSWD,"\0",BLINKER_PSWD_SIZE);
// // memcpy(PSWD,WiFi.psk().c_str(),BLINKER_PSWD_SIZE);
// saveConfig(SSID, PSWD);
// free(SSID);
// free(PSWD);
// // WiFi.setAutoConnect(true);
// // WiFi.setAutoReconnect(true);
// _status = BWL_CONNECTED_CHECK;
// return true;
// }
// break;
// case BWL_CONNECTING :
// if (WiFi.status() == WL_CONNECTED) {
// IPAddress deviceIP = WiFi.localIP();
// BLINKER_LOG(BLINKER_F("WiFi connected"));
// BLINKER_LOG(BLINKER_F("IP address: "));
// BLINKER_LOG(deviceIP);
// BLINKER_LOG(BLINKER_F("SSID: "), WiFi.SSID(), BLINKER_F(" PSWD: "), WiFi.psk());
// _status = BWL_CONNECTED_CHECK;
// return true;
// }
// else if (WiFi.status() != WL_CONNECTED) {
// return false;
// }
// case BWL_CONNECTED_CHECK :
// // if (WiFi.status() != WL_CONNECTED)
// // _status = BWL_DISCONNECTED;
// if (WiFi.status() == WL_CONNECTED)
// {
// return true;
// }
// else
// {
// _status = BWL_DISCONNECTED;
// return false;
// }
// case BWL_RESET :
// return false;
// default :
// if (WiFi.status() == WL_CONNECTED) {
// IPAddress deviceIP = WiFi.localIP();
// BLINKER_LOG(BLINKER_F("WiFi connected"));
// BLINKER_LOG(BLINKER_F("IP address: "));
// BLINKER_LOG(deviceIP);
// BLINKER_LOG(BLINKER_F("SSID: "), WiFi.SSID(), BLINKER_F(" PSWD: "), WiFi.psk());
// _status = BWL_CONNECTED_CHECK;
// return true;
// }
// return false;
// }
// return false;
// }
// void BlinkerWlan::disconnect() {
// WiFi.disconnect();
// delay(100);
// _status = BWL_DISCONNECTED;
// BLINKER_LOG(BLINKER_F("WiFi disconnected"));
// }
// void BlinkerWlan::reset() {
// disconnect();
// _status = BWL_RESET;
// }
// void BlinkerWlan::softAPinit() {
// // _server = new WiFiServer(80);
// WiFi.mode(WIFI_AP);
// String softAP_ssid = STRING_format(_deviceType) + "_" + macDeviceName();
// #if defined(ESP8266)
// WiFi.hostname(softAP_ssid);
// #elif defined(ESP32)
// WiFi.setHostname(softAP_ssid.c_str());
// #endif
// #if defined(ESP8266)
// WiFi.softAPConfig(apIP, apIP, netMsk);
// #elif defined(ESP32)
// WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
// #endif
// WiFi.softAP(softAP_ssid.c_str(), NULL);
// delay(100);
// // _server->begin();
// // BLINKER_LOG(BLINKER_F("AP IP address: "), WiFi.softAPIP());
// // BLINKER_LOG(BLINKER_F("HTTP _server started"));
// // BLINKER_LOG(String("URL: http://" + WiFi.softAPIP()));
// _status = BWL_APCONFIG_BEGIN;
// BLINKER_LOG(BLINKER_F("Wait for APConfig"));
// }
// void BlinkerWlan::serverClient()
// {
// // if (!_client)
// // {
// // _client = _server->available();
// // }
// // else
// // {
// // // if (_client.status() == CLOSED)
// // if (!_client.connected())
// // {
// // _client.stop();
// // BLINKER_LOG(BLINKER_F("Connection closed on _client"));
// // }
// // else
// // {
// // if (_client.available())
// // {
// // String data = _client.readStringUntil('\r');
// // // data = data.substring(4, data.length() - 9);
// // _client.flush();
// // // BLINKER_LOG("clientData: ", data);
// // if (STRING_contains_string(data, "ssid") && STRING_contains_string(data, "pswd")) {
// // String msg = BLINKER_F("{\"hello\":\"world\"}");
// // String s= BLINKER_F("HTTP/1.1 200 OK\r\nContent-Type: application/json;charset=utf-8\r\n");
// // s += BLINKER_F("Content-Length: ");
// // s += String(msg.length());
// // s += BLINKER_F("\r\nConnection: Keep Alive\r\n\r\n");
// // s += msg;
// // s += BLINKER_F("\r\n");
// // _client.print(s);
// // _client.stop();
// // parseUrl(data);
// // }
// // }
// // }
// // }
// }
// void BlinkerWlan::parseUrl(String data)
// {
// BLINKER_LOG(BLINKER_F("APCONFIG data: "), data);
// // DynamicJsonBuffer jsonBuffer;
// // JsonObject& wifi_data = jsonBuffer.parseObject(data);
// DynamicJsonDocument jsonBuffer(1024);
// DeserializationError error = deserializeJson(jsonBuffer, data);
// JsonObject wifi_data = jsonBuffer.as<JsonObject>();
// // if (!wifi_data.success())
// if (error)
// {
// return;
// }
// String _ssid = wifi_data["ssid"];
// String _pswd = wifi_data["pswd"];
// BLINKER_LOG(BLINKER_F("ssid: "), _ssid);
// BLINKER_LOG(BLINKER_F("pswd: "), _pswd);
// // free(_server);
// SSID = (char*)malloc(BLINKER_SSID_SIZE*sizeof(char));
// PSWD = (char*)malloc(BLINKER_PSWD_SIZE*sizeof(char));
// strcpy(SSID, _ssid.c_str());
// strcpy(PSWD, _pswd.c_str());
// connectWiFi(_ssid, _pswd);
// connectTime = millis();
// _status = BWL_APCONFIG_DONE;
// BLINKER_LOG(BLINKER_F("APConfig Success"));
// }
// void BlinkerWlan::connectWiFi(String _ssid, String _pswd)
// {
// connectWiFi(_ssid.c_str(), _pswd.c_str());
// }
// void BlinkerWlan::connectWiFi(const char* _ssid, const char* _pswd)
// {
// uint32_t connectTime = millis();
// BLINKER_LOG(BLINKER_F("Connecting to "), _ssid);
// WiFi.mode(WIFI_STA);
// String _hostname = STRING_format(_deviceType) + "_" + macDeviceName();
// #if defined(ESP8266)
// WiFi.hostname(_hostname);
// #elif defined(ESP32)
// WiFi.setHostname(_hostname.c_str());
// #endif
// if (_pswd && strlen(_pswd)) {
// WiFi.begin(_ssid, _pswd);
// }
// else {
// WiFi.begin(_ssid);
// }
// // while (WiFi.status() != WL_CONNECTED) {
// // ::delay(50);
// // if (millis() - connectTime > BLINKER_CONNECT_TIMEOUT_MS && WiFi.status() != WL_CONNECTED) {
// // connectTime = millis();
// // BLINKER_LOG(("WiFi connect timeout, please check ssid and pswd!"));
// // BLINKER_LOG(("Retring WiFi connect again!"));
// // }
// // }
// // BLINKER_LOG(("Connected"));
// // IPAddress myip = WiFi.localIP();
// // BLINKER_LOG(("Your IP is: "), myip);
// // mDNSInit();
// }
// bool BlinkerWlan::run()
// {
// // if (millis() - debugStatusTime > 10000) {
// // debugStatusTime = millis();
// // BLINKER_LOG_ALL("WLAN status: ", _status);
// // }
// switch (_status) {
// case BWL_CONFIG_CKECK :
// checkConfig();
// break;
// case BWL_CONFIG_FAIL :
// #if defined(BLINKER_ESP_SMARTCONFIG)
// smartconfigBegin();
// #elif defined(BLINKER_APCONFIG)
// softAPinit();
// #endif
// break;
// case BWL_CONFIG_SUCCESS :
// connect();
// break;
// case BWL_CONNECTING :
// return connected();
// break;
// case BWL_CONNECTED :
// return connected();
// break;
// case BWL_DISCONNECTED :
// connect();
// break;
// case BWL_SMARTCONFIG_BEGIN :
// smartconfigDone();
// break;
// case BWL_SMARTCONFIG_DONE :
// return connected();
// break;
// case BWL_SMARTCONFIG_TIMEOUT :
// _status = BWL_CONFIG_FAIL;
// break;
// case BWL_STACONFIG_BEGIN :
// connect();
// break;
// case BWL_APCONFIG_BEGIN :
// serverClient();
// break;
// case BWL_APCONFIG_DONE :
// return connected();
// break;
// case BWL_APCONFIG_TIMEOUT :
// _status = BWL_CONFIG_FAIL;
// break;
// case BWL_CONNECTED_CHECK :
// return connected();
// break;
// case BWL_RESET:
// break;
// default :
// break;
// }
// return false;
// }
// // #endif
// #endif
// #endif

View File

@@ -0,0 +1,24 @@
#ifndef BLINKER_SERVER_H
#define BLINKER_SERVER_H
#ifndef BLINKER_SERVER_HTTPS
#ifndef BLINKER_WITHOUT_SSL
#define BLINKER_SERVER_HTTPS "https://iot.diandeng.tech"
#else
#define BLINKER_SERVER_HTTPS "http://iot.diandeng.tech"
#endif
#endif
#ifndef BLINKER_SERVER_HOST
#define BLINKER_SERVER_HOST "iot.diandeng.tech"
#endif
#ifndef BLINKER_STORAGE_HTTPS
#ifndef BLINKER_WITHOUT_SSL
#define BLINKER_STORAGE_HTTPS "https://storage.diandeng.tech"
#else
#define BLINKER_STORAGE_HTTPS "http://storage.diandeng.tech"
#endif
#endif
#endif

View File

@@ -0,0 +1,17 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#pragma once
#ifdef __cplusplus
#include "ArduinoJson.hpp"
using namespace ArduinoJson;
#else
#error ArduinoJson requires a C++ compiler, please change file extension to .cc or .cpp
#endif

Some files were not shown because too many files have changed in this diff Show More