MQTT یک پروتکل پیام رسانی سبک برای اینترنت اشیا است . این پست به جزئیات استفاده از پشتیبانی Stream برای ذخیره سازی بار بزرگ در کتابخانه MQTT Arduino می پردازد . MQTT یک پروتکل سبک وزن است، اما این بدان معنا نیست که محموله ها باید کوچک باشند. این مشخصات به گونهای طراحی شده است که امکان ارسال پیامها تا 256 مگابایت را فراهم میکند، اما استفاده از آن با آردوینو میتواند دشوار باشد، زیرا آنها معمولاً فقط مقدار کمی حافظه در سه استخر دارند:
- فلش مموری محل ذخیره طرح آردوینو (32k در Atmega328)
- SRAM که در آن طرح هنگام اجرا متغیرها را ایجاد و دستکاری می کند (2k در Atmega328)
- EEPROM که برنامه نویسان می توانند از آن برای ذخیره اطلاعات طولانی مدت استفاده کنند (1k در Atmega328)
که دریافت هرگونه محموله با هر اندازه ای از طریق MQTT را مشکل ساز می کند. گزینه های رایج حافظه خارجی برای تقویت پروژه آردوینو شما شامل کارت های SD و تراشه های SRAM است . اینها با کتابخانههای کنترلی ارائه میشوند که از کلاس Arduino Stream به ارث میبرند - یک رابط استاندارد برای دستکاری کاراکترها و جریانهای مبتنی بر باینری (همچنین توسط اترنت و سریال در میان دیگران استفاده میشود).
برای آخرین پروژهام، یک تراشه 23LC1024 - یک تراشه SPI SRAM 1024 کیلوبایتی با سطوح منطقی 5 ولتی در اختیار گرفتم که کار با آردوینو 5 ولتی من را آسان میکند. من میخواستم بیتمپهای باینری نسبتاً بزرگی را توسط MQTT ارسال کنم، بنابراین پشتیبانی را به کتابخانه آردوینو MQTT Nick برای ذخیره بارها با استفاده از اشیاء Stream اضافه کردم . من با سیمکشی SRAM به پایههای SPI آردوینو به صورت زیر شروع کردم: در حال حاضر هیچ کتابخانه استاندارد آردوینو SRAM وجود ندارد (سایت آردوینو یکی را فهرست میکند، اما فایلها 404 هستند) بنابراین یک کتابخانه ساده ایجاد کردم که از 23LC1024 پشتیبانی میکند. برای 23K256 هم باید خوب باشه ولی من تا حالا تستی نداشتم. در کد مثال ساده زیر، ما هر پیامی را که از طریق نوشتن آن به SRAM میآید، میگیریم و سپس آن را میخوانیم و روی کنسول سریال مینویسیم. توجه داشته باشید که شی Stream (sram) به عنوان آخرین آرگومان instantiator به PubSubClient ارسال میشود تا بداند که میخواهید بارهای پرداختی را در SRAM بنویسد. من بارها را در موقعیت حافظه 1 ذخیره می کنم، بنابراین با روش به آنجا منتقل می شوم.inTopic
setup
seek
هنگامی که یک بار دریافت می شود callback
اجرا می شود. من به موقعیت حافظه 1 می روم، پیام را دوباره می خوانم و آن را روی کنسول می نویسم، سپس دوباره به موقعیت حافظه 1 برمی گردم تا پیام بعدی نوشته شود (اگر از کارت SD برای ذخیره سازی استفاده می کردید، ممکن است در حال باز و بسته شدن باشید. فایل ها به جای انتقال به موقعیت های حافظه).
نکته خوب در مورد این رویکرد برای ذخیره سازی این است که بسیاری از کتابخانه ها یک شی Stream را برای دستکاری می گیرند که ارسال پیام برای پردازش را آسان می کند. من در پست بعدی وبلاگم کمی بیشتر در مورد نحوه استفاده از این روش برای استفاده از کتابخانه های شخص ثالث برای پردازش تصاویر خود خواهم گفت.
برای جزئیات بیشتر: پشتیبانی جریانی برای کتابخانه Arduino MQTT
در ادامه، متن انگلیسی این مطلب را میتوانید مشاهده نمایید:
MQTT is a lightweight messaging protocol for the Internet of Things. This post details the use of Stream support for large payload storage in the Arduino MQTT library.
MQTT is a lightweight protocol, but that doesn’t mean that the payloads have to be small. The spec was designed to allow for messages up to 256MB, but taking advantage of this with an Arduino can be difficult as they usually only have small amounts of memory in three pools:
- Flash memory where the Arduino sketch is stored (32k in an Atmega328)
- SRAM where the sketch creates and manipulates variables when it runs (2k in an Atmega328)
- EEPROM that programmers can use to store long-term information (1k in an Atmega328)
Which makes receiving any payloads of any size via MQTT problematic. Common external storage options to beef up your Arduino project include SD cards and SRAM chips. These come with control libraries that inherit from the Arduino Stream class – a standard interface for manipulating character and binary based streams (also used by Ethernet and Serial amongst others).
For my latest project I got hold of a 23LC1024 – a 1024KB SPI SRAM chip with 5V logic levels making it easy to work with my 5V Arduino. I wanted to pass fairly large binary bitmaps around by MQTT, so I added support to Nick’s MQTT Arduino library for storing payloads using Stream objects.
I started by wiring the SRAM up to the Arduino’s SPI pins as follows:
There is currently no standard Arduino SRAM library (the Arduino site lists one, but the files are 404) so I knocked up a simple library which supports the 23LC1024. It should be good for the 23K256 as well, but I haven’t had one handy to do a test.
In the trivial example code below, we’re taking any message that comes through on inTopic
writing it to SRAM and then reading it back and writing it to the serial console.
Note that the Stream object (sram) is passed to PubSubClient as the last argument of the instantiator to let it know you want it to write payloads to the SRAM. I’m storing the payloads at memory position 1, so I move there in setup
with the seek
method.
Once a payload is received callback
executes. I move to memory position 1, read the message back and write it to the console, then move back to memory position 1 again ready for the next message to be written (if you were using an SD card for storage you might be opening and closing Files instead of moving to memory postitions).
The nice thing about this approach to storage is that many libraries will take a Stream object for manipulation which makes it easy to pass off a message for processing. I’ll take a bit more in my next blog post about how I used this approach to make use of 3rd party libraries to process my images.
For more detail: Stream support for the Arduino MQTT library