Контроллер беспроводных датчиков
В задачи данного модуля входит получение из радио-эфира пакетов от различных радио-датчиков? и отправка соответствующих уведомлений в центральную систему с помощью HTTP-запроса. Система правил позволяет самостоятельно обрабатывать входящие сигналы, но в данном примере эта возможность не рассматривается
Скетч Arduino
#include <VirtualWire.h> #include <EasyTransferVirtualWire.h> #include <EEPROM.h> //Needed to access the eeprom read write functions const int led_pin = 13; const int receive_pin = 2; unsigned int unique_device_id = 0; unsigned int uptime = 0; unsigned int old_uptime = 0; String inData; //create object EasyTransferVirtualWire ET; unsigned int last_packet_id = 0; struct SEND_DATA_STRUCTURE{ //put your variable definitions here for the data you want to send //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO //Struct can'e be bigger then 26 bytes for VirtualWire version unsigned int device_id; unsigned int destination_id; unsigned int packet_id; byte command; int data; }; //give a name to the group of data SEND_DATA_STRUCTURE mydata; //This function will write a 2 byte integer to the eeprom at the specified address and address + 1 void EEPROMWriteInt(int p_address, unsigned int p_value) { byte lowByte = ((p_value >> 0) & 0xFF); byte highByte = ((p_value >> 8) & 0xFF); EEPROM.write(p_address, lowByte); EEPROM.write(p_address + 1, highByte); } //This function will read a 2 byte integer from the eeprom at the specified address and address + 1 unsigned int EEPROMReadInt(int p_address) { byte lowByte = EEPROM.read(p_address); byte highByte = EEPROM.read(p_address + 1); return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00); } void setup() { pinMode(led_pin, OUTPUT); Serial.begin(9600); // Debugging only ET.begin(details(mydata)); // Initialise the IO and ISR vw_set_rx_pin(receive_pin); vw_setup(2000); // Bits per sec vw_rx_start(); // Start the receiver PLL running // Device ID Serial.print("Getting Device ID... "); unique_device_id=EEPROMReadInt(0); if (unique_device_id<10000 || unique_device_id>60000) { Serial.print("N/A, updating... "); unique_device_id=random(10000, 60000); EEPROMWriteInt(0, unique_device_id); } Serial.println(unique_device_id); } void loop() { uptime=round(millis()/1000); if (uptime!=old_uptime) { Serial.print("Uptime: "); Serial.println(uptime); old_uptime=uptime; } if (Serial.available()) { char c=Serial.read(); if (c == '\n' || c == ';') { Serial.println(inData); if (inData.equals("blink")) { Serial.println("BLINKING!"); } else { Serial.print("Unknown command: "); Serial.println(inData); } inData=""; Serial.flush(); } else { inData += (c); } } if(ET.receiveData()) { digitalWrite(led_pin, HIGH); if (last_packet_id!=(int)mydata.packet_id) { Serial.print("P:"); Serial.print(mydata.packet_id); Serial.print(";F:"); Serial.print(mydata.device_id); Serial.print(";T:"); Serial.print(mydata.destination_id); Serial.print(";C:"); Serial.print(mydata.command); Serial.print(";D:"); Serial.print(mydata.data); Serial.println(";"); last_packet_id=(int)mydata.packet_id; } digitalWrite(led_pin, LOW); } }
Настройка правил
Для текущего предназначения устройства нам достаточно одного правила следующего вида:
Вот и всё, мы получили устройство-ретранслятор показаний датчиков на нашу центральную веб-систему. Вместо рентранслятора может быть сделан, к примеру, обработчик, который посылает определённую команду назад в контроллер при превышении каких-то контрольных значений.
Соединение с интернетом требуется только для первоначальной загрузки правила, далее модуль работает совершенно автономно.