Lähettimen lähdekoodi V2: Difference between revisions

From Pessin randon wiki
Created page with "=== Versiohistoria === ==== 2.0 ==== Lisätty CRC-16 tarkistussummafunktio. Lähettää singalin 4-10 sekunnin välein, jolla tarkoitus pienentää paristojen kulutusta. Voisi implementoida suoraan virransäästöominaisuudet.<syntaxhighlight lang="c#"> #include <CRC16.h> // include CRC16 library #include <RH_ASK.h> // include RadioHead library RH_ASK rf_driver(1000, 11, 12, 10); // speed = 1000 bits/sec, tx pin = 12 byte data[4]; // create byte..."
(No difference)

Revision as of 14:42, 3 October 2025

Versiohistoria

2.0

Lisätty CRC-16 tarkistussummafunktio. Lähettää singalin 4-10 sekunnin välein, jolla tarkoitus pienentää paristojen kulutusta. Voisi implementoida suoraan virransäästöominaisuudet.

#include <CRC16.h>  // include CRC16 library
#include <RH_ASK.h>  // include RadioHead library

RH_ASK rf_driver(1000, 11, 12, 10); 	// speed = 1000 bits/sec, tx pin = 12

byte data[4];                     // create byte array to store DIP switch values
uint16_t checksumValue;           // variable to store the checksum value

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A0));   // seed the random number generator with noise from an unconnected analog pin
  pinMode(2, INPUT_PULLUP);     // set DIP switch pins as input pull-up
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

  data[0] = digitalRead(2);      // read DIP switches and store in the array
  data[1] = digitalRead(3);
  data[2] = digitalRead(4);
  data[3] = digitalRead(5);

  CRC16 crc;                     // create an instance of CRC16 class with default parameters (you can customize them if needed)
  crc.add(data, sizeof(data));    // add the DIP switch values to the CRC calculation
  checksumValue = crc.calc();    // calculate CRC-16 checksum value for the DIP switch values

  Serial.print("DIP switches: ");
  Serial.print(data[0]);
  Serial.print(", ");
  Serial.print(data[1]);
  Serial.print(", ");
  Serial.print(data[2]);
  Serial.print(", ");
  Serial.println(data[3]);

  Serial.print("CRC-16 checksum value: 0x");
  Serial.println(checksumValue, HEX);


  //rf_driver.init();             // initialize the radio driver
  if (!rf_driver.init()) {        // initialize the radio driver and check for errors
    Serial.println("RF driver initialization failed.");
    while (1);                   // stop execution in case of failure
  } else {
    Serial.println("RF driver initialized successfully.");
  }
}

void loop() {

  rf_driver.send((uint8_t*)&checksumValue, sizeof(checksumValue)); // send the checksum value first
  rf_driver.waitPacketSent();                                     // wait for the packet to be sent
  
  Serial.println(checksumValue, HEX);

  rf_driver.send(data, sizeof(data)); // then send the DIP switch values
  rf_driver.waitPacketSent();         // wait for the packet to be sent
 
  Serial.print("DIP switches: ");
  Serial.print(data[0]);
  Serial.print(", ");
  Serial.print(data[1]);
  Serial.print(", ");
  Serial.print(data[2]);
  Serial.print(", ");
  Serial.println(data[3]);
 
  delay(random(4000, 10000));          // add random delay of between 4-10 seconds before transmitting again
 
  Serial.println("ping");
}