*يحتوي واجهة برمجة التطبيقات أيضًا على إصدار WiFi مع نفس التعاريف والوظائف في حالة رغبتك في الاستفادة مباشرة من ESP32 أو ESP8266 Board WiFi أو Sim800L GPRS دون استخدام Gateway
** أمثلة ULP (Ultra Low Power) متاحة أيضًا على واجهة برمجة تطبيقات WiFi و Lora API في/أمثلة/ESP32/ULP
*** SONOFF/ESP8266-01 1،2،4 أمثلة الترحيل في:/أمثلة/ESP8266-01
#تكرار
المدعومة رسميًا هي: 433 ميجا هرتز ، 868 ميجا هرتز و 915 ميجا هرتز - الافتراضي هو 868 ميجا هرتز للتبديل إلى آخر ، يرجى تمرير المعلمة المدعومة (الخيارات المدعومة هي: Iotthing_433 ، 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 (cs_pin ، int_pin ، rst_pin ، المفتاح ، الشيء _id ، null ، iotthing_433) ؛
#logging
لتمكين التسجيل داخل المكتبة ، تحتاج إلى إضافتها إلى تسول الفشل/رسم المصدر الرئيسي لك:
///////////////////////////////////////////////////////////////////////////////////
// ثوابت سجل
#define log64_enabled
#include <log64.h>
وأيضًا تأكد في مكان ما داخل وظيفة "الإعداد" لديك: serial.begin (115200) ؛
#العقدة/الجهاز/الشيء بيانات إرسال/تلقي واجهة برمجة التطبيقات
على العقدة/الشيء ، توفر Side 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: RadioHead.zip - يحتوي واحد على Bugfix على منصة ESP32.
التطبيق العملي والكفاءة
في واجهة برمجة التطبيقات ، توجد وظائف ثابتة مفيدة للغاية تتعلق بالطريقة السهلة والفعالة لتمثيل البيانات في رسالة Byte Buffer (Array):
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_);
باستخدام الإضافة/الحصول على منفصلة ، يمكنك إضافتها إلى المخزن المؤقت والقراءة من القيم المنفصلة العازلة - على سبيل المثال ، إذا كنت ترغب في تخزين المعتدلة في الفاصل الزمني بين -20 درجة مئوية و +60 ج بكفاءة في بايت واحد فقط ولكن لا يزال لديك دقة أفضل ثم 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. سيتم تخزين القيمة في الحد الأدنى الممكن أن تتناسب القيمة إذا كانت القيمة يمكن أن تتناسب مع 8 بايت ، فستشغل واحدة فقط من البايت ، إذا كان بإمكانها أن تتناسب مع 2 bytes. وبهذه الطريقة ، يمكنك توفير مساحة في الرسالة ، وأن تكون فعالًا وتحسين حركة المرور الخاصة بك:
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);