*La API también tiene una versión wifi con las mismas definiciones y funcionalidad en caso de que desee beneficiarse directamente de la placa ESP32 o ESP8266 WiFi o SIM800L GPRS sin usar Lora Gateway
** Ejemplos de ULP (Ultra Low Power) también disponibles para la API WiFi y la API Lora en/ejemplos/ESP32/ULP
*** SONOFF/ESP8266-01 1,2,4 Ejemplos de retransmisión en:/ejemplos/esp8266-01
#FRECUENCIA
Los oficialmente compatibles son: 433MHz, 868MHz y 915MHz: el valor predeterminado es 868MHz para cambiar a otro, pase el parámetro de range de requisito (las opciones compatibles son: IOTTHING_433, IOTTHING_868, IOTTHING_915) en el lado de la puerta de enlace también debe elegir una frecuencia apropiada apropiada::::::
Para los nodos en 433 Gateway puede ser: 433.1, 433.3, 433.5
Para los nodos en 868 Gateway puede ser: 868.1, 868.3, 868.5
Para los nodos en 915 Gateway puede ser: 915.1, 915.3, 915.5
Ejemplo: Iotththing Thing (cs_pin, int_pin, rst_pin, key, thing_id, null, iotththing_433);
#EXPLOTACIÓN FLORESTAL
Para habilitar el registro dentro de la biblioteca, debe agregar en la mendicidad de su fuente principal de falla/boceto:
///////////////////////////////////////////////////////////////////////////////////
// constantes de registro
#define log64_enabled
#Include <log64.h>
Y también asegúrese de en algún lugar dentro de la función "Configuración" que tenga: Serial.Begin (115200);
#Nodo/dispositivo/cosa Data Enviar/Recibir API
En el lado del nodo/cosa, las veces proporcionan una clase sencilla y sencilla que se puede usar para entregar datos a la autoridad/servidor.
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();
};
Ejemplo de cómo usar la 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);
}
}
}
IMPORTANTE
Para la biblioteca de nodo de código abierto simple, también deberá descargar y poner a disposición de la biblioteca Radiohead: Radiohead.zip: el proporcionado tiene una interrupción de errores en la plataforma ESP32.
Practicidad y eficiencia
En la API se encuentran funciones estáticas muy útiles relacionadas con una forma fácil y eficiente de representar los datos en el mensaje de búfer de byte (matriz de bytes):
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_);
Usando la adición/get discreta, puede agregar al búfer y leer desde los valores discretos del búfer, por ejemplo, si desea almacenar templán en el intervalo entre -20c y +60c de manera eficiente en un solo byte pero aún tiene una mejor resolución, entonces 1/2 grado, puede usar las funciones discretas de la siguiente manera:
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);
Usando la adición/get uint, puede agregar al búfer y leer desde los valores enteros positivos del búfer entre 0 y 4611686018427387904. El valor se almacenará en el mínimo posible bytes, por ejemplo, si el valor puede encajar en un byte, solo ocupará un byte, si puede ajustarse en 2 bytes, solo ocupará dos bytes y etc. hasta 8 por 8. De esta manera, puede ahorrar espacio en el mensaje, ser eficiente y optimizar su tráfico:
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);