*API에는 ESP32 또는 ESP8266 보드 Wi -Fi 또는 SIM800L GPR에서 직접 혜택을 받으려면 동일한 정의 및 기능이있는 WiFi 버전이 있습니다.
** ULP (Ultra Low Power) Wi -Fi API 및 LORA API IN/EXACEPLES/ESP32/ULP에도 사용할 수 있습니다.
*** Sonoff/ESP8266-01 1,2,4 릴레이 예 :/examples/esp8266-01
#빈도
공식적으로 지원되는 것은 다음과 같습니다. 433MHz, 868MHz 및 915MHz- 기본값은 868MHz입니다. 다른 하나로 전환하십시오. requencyRange 매개 변수 (지원되는 옵션은 : IoTthing_433, IoTthing_868, IoTthing_868, IoTthing_915)를 통과하십시오. 또한 적절한 주파수를 선택해야합니다.
433 게이트웨이의 노드의 경우 : 433.1, 433.3, 433.5
868 게이트웨이의 노드의 경우 : 868.1, 868.3, 868.5
915 게이트웨이의 노드의 경우 : 915.1, 915.3, 915.5
예 : iotthing thing (cs_pin, int_pin, rst_pin, key, the_id, null, iotthing_433);
#벌채 반출
라이브러리 내부에서 로깅을 활성화하려면 메인 소스 실패/스케치를 구걸 할 때 추가해야합니다.
///////////////////////////////////////////////////////////////////////////////////
// 상수 로그
#define log64_enabled
#include <log64.h>
또한 "설정"기능 안에있는 어딘가에 다음과 같이하십시오. Serial.begin (115200);
#노드/장치/사물 데이터 보내기/수신 API
노드/사물에서 i4things는 데이터를 권위/서버에 전달하는 데 사용할 수있는 간단한 간단한 클래스를 제공합니다.
class IoTThing
{
public:
// constructor
IoTThing(uint8_t slaveSelectPin_,
uint8_t interruptPin_,
uint8_t resetPin_,
uint8_t key_[16],
uint64_t id_,
void (* receive_callback_)(uint8_t buf_[], uint8_t size_, int16_t rssi_) = NULL,
uint8_t requencyRange = IoTThing_868,
uint64_t gateway_id_ = 10); // default open gateway id
// call before using
void init();
// set id ( in case after start needs to be changed)
void set_id(uint64_t id_ );
// set key ( in case after start needs to be changed)
void set_key(uint8_t key_[16]);
// set gateway id ( in case after start needs to be changed)
void set_gateway_id(uint64_t gateway_id_ );
// register to receive callback after the message has been acknowledged from the gateway
void register_ack(void (* ack_callback_)(int16_t rssi_));
// register to receive callback after the message has failed/ in msec
void register_timeout(void (* timeout_callback_)(uint16_t timeout_));
// return signal strength in %
uint8_t signal_strength();
// return total messages sent
uint32_t total_messages();
//return total acknowledged messages send
uint32_t ack_messages();
//return received messages count
uint32_t recv_messages();
//return total retransmit messages send
uint32_t retransmit_messages()
//add discrete data
//use this if you want for example to store decimal(floating-point) temperature between -20 and +60 in one byte
// pos is the position from which the data will be written
// e.g. : add_discrete(buf, 0, -20.0, 60.0, 23.65, 1);
//you need to ensure that you at least have container_size bytes available in the buffer
//1 <= conainer_size <= 4
//on return pos will have the value of first free byte(next postion after the data)
static void add_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, double value_, uint8_t container_size_);
//read discrete value
static double get_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, uint8_t container_size_);
//add unsigned integer data up to 62bit ( 0 - 4611686018427387904) - the container size will be adjusted automatically depending on the value
// pos is the position from which the data will be written
//you need to ensure that you have at least 8 bytes available in the buffer - as this is the maximum bytes that the number can occupy in worst case scenario
//on return pos will have the value of first free byte(next postion after the data)
static void add_uint(uint8_t buf_[], uint8_t &pos_, uint64_t value_);
// get unsigned integer value
static uint64_t get_uint(uint8_t buf_[], uint8_t &pos_)
// return false if a error is logged
// max message size in bytes is 31
bool send(uint8_t buf_[], uint8_t size_);
// return true - if all previous tasks has been completed and ready to accept new data
bool is_ready();
// call if you want to stop retry's to send the message
void cancel();
// return true - if last message sending has hit timeout and failed
bool timeout_hit();
// please call in the main loop to be able to dispatch data and menage logic
void work();
};
예제 API 사용 방법 :
#include "IoTThing.h"
// exmaple PINS for Feather 32u4
#define CS_PIN 8
#define RST_PIN 4
#define INT_PIN 7
#define DEVICE_ID 1000
uint8_t key[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
// you can also optionally pass:
// - function pointer/address for callback messages when received
// - gateway id - in case you want to use a private/closed gateway
//
// example for message received callback:
//
// called when packet received from node
// void received(uint8_t buf_[], uint8_t size_, int16_t rssi)
// {
// //process message in buf_[] here with length = size_
// };
//
// IoTThing thing(CS_PIN, INT_PIN, RST_PIN, key, DEVICE_ID, received);
//
IoTThing thing(CS_PIN, INT_PIN, RST_PIN, key, DEVICE_ID);
// 2 minutes
#define MESSAGE_INTERVAL 120000
uint32_t MESSAGE_LAST_SEND;
void setup() {
MESSAGE_LAST_SEND = millis() + MESSAGE_INTERVAL * 2;
thing.init();
}
void loop() {
// let IoT layer do it job
thing.work();
// try send message every 2 min
if (((uint32_t)(((uint32_t)millis()) - MESSAGE_LAST_SEND)) >= MESSAGE_INTERVAL) {
MESSAGE_LAST_SEND = millis();
uint8_t msg[2];
msg[0] = 22; // e.g. temperature
msg[1] = 78; // e.g. humidity
// check if IoT layer is ready to accept new message
if (thing.is_ready()) {
thing.send(msg, 2);
}
else {
// cancel previous work and send new message
thing.cancel();
thing.send(msg, 2);
}
}
}
중요한
간단한 오픈 소스 노드 라이브러리의 경우 Radiohead 라이브러리를 다운로드하여 사용할 수 있도록해야합니다.
실용성과 효율성
API에는 바이트 버퍼 (바이트 배열) 메시지에서 데이터를 쉽고 효율적으로 표현하는 방법과 관련된 매우 유용한 정적 기능이 발견되었습니다.
static void add_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, double value_, uint8_t container_size_);
static double get_discrete(uint8_t buf_[], uint8_t &pos_, double min_, double max_, uint8_t container_size_);
static void add_uint(uint8_t buf_[], uint8_t &pos_, uint64_t value_);
static uint64_t get_uint(uint8_t buf_[], uint8_t &pos_);
애드/get 이산을 사용하여 버퍼에 추가하고 버퍼 이산 값에서 읽을 수 있습니다. 예를 들어, 하나의 바이트에서만 -20C에서 +60C 사이의 간격으로 온대를 저장하려는 경우 1/2도에도 더 나은 해상도를 사용하면 다음 방식으로 이산 기능을 사용할 수 있습니다.
uint8_t buf[2];
uint8_t buf_pos = 0;
double temp1 = 23.5;
double temp2 = 18.1;
//insert into the buffer
IoTThing::add_discrete(buf, buf_pos,-20.0, 60.0, temp1, 1);
IoTThing::add_discrete(buf, buf_pos,-20.0, 60.0, temp2, 1);
buf_pos = 0;
double temp1_read_from_buffer = IoTThing::get_discrete(buf,buf_pos, -20.0, 60.0, 1);
double temp2_read_from_buffer = IoTThing::get_discrete(buf,buf_pos, -20.0, 60.0, 1);
Add/Get UINT를 사용하여 버퍼에 추가하고 0과 4611686018427387904 사이의 버퍼 양성 정수 값에서 읽을 수 있습니다. 값은 가능한 최소 바이트에 저장됩니다. 예를 들어 값이 1 바이트에 맞을 수 있다면 바이트에 맞는 경우, 2 바이트에 적합 할 수 있다면 2 바이트 등을 차지할 수 있습니다. 이렇게하면 메시지에 공간을 절약하고 효율적이며 트래픽을 최적화 할 수 있습니다.
uint8_t buf[8]; // make sure we have space for maximum size
uint8_t buf_pos = 0;
//insert into the buffer
IoTThing::add_uint(buf, buf_pos, 11); // will occupy only 1 byte
IoTThing::add_uint(buf, buf_pos, 500); // will occupy only 2 bytes
buf_pos = 0;
int val1 = (int)IoTThing::get_uint(buf, buf_pos);
int val2 = (int)IoTThing::get_uint(buf, buf_pos);