Lähettimen lähdekoodi V2: Difference between revisions

From Pessin randon wiki
No edit summary
 
Line 43: Line 43:
   rf_driver.waitPacketSent();        // wait for the packet to be sent
   rf_driver.waitPacketSent();        // wait for the packet to be sent
   delay(random(4000, 10000));          // add random delay of between 4-10 seconds before transmitting again
   delay(random(4000, 10000));          // add random delay of between 4-10 seconds before transmitting again
}
</syntaxhighlight>
==== v2.2 ====
Lisätty virransäästöominaisuus. Mitattuna virrankulutus 0.3-0.1A 9V<syntaxhighlight lang="c#">
#include <CRC16.h>  // include CRC16 library
#include <RH_ASK.h>  // include RadioHead library
#include <LowPower.h>  // include LowPower library for sleep functionality
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
  if (!rf_driver.init()) {
  //if(rf_driver.init();            // initialize the radio driver
    Serial.println("radioerror");
  }
}
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
  rf_driver.send(data, sizeof(data)); // then send the DIP switch values
  rf_driver.waitPacketSent();        // wait for the packet to be sent
  int sleepTime = random(4000, 10000); // calculate the next sleep time randomly between 4-10 seconds
  while (sleepTime > 8000) {          // if sleep time is greater than 8 seconds
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); // put the microcontroller into deep sleep for 8 seconds
    sleepTime -= 8000;                              // reduce sleep time by 8 seconds
  }
  if (sleepTime > 0) {
    delay(sleepTime);                // delay for the remaining time, if any
  }
}
</syntaxhighlight>
==== v2.3 ====
Lisätty lähetysintervalli ja vaihdettu eri Arduino virransäästölibraryyn (EI TESTATTU)<syntaxhighlight lang="c#">
#include <CRC16.h>  // include CRC16 library
#include <RH_ASK.h>  // include RadioHead library
#include <ArduinoLowPower.h>  // include ArduinoLowPower library for sleep functionality
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
unsigned long lastTransmissionTime = 0;  // initialize variable to keep track of the time since the last transmission
const unsigned long firstFiveMinutesInterval = 1000;  // set interval for transmissions during the first 5 minutes (in milliseconds)
const unsigned long thirtySecondsInterval = 30000;    // set interval for transmissions after the first 5 minutes (in milliseconds)
const unsigned long oneHourInterval = 60 * 60 * 1000;  // set interval for transmissions after each hour (in milliseconds)
const unsigned long tenMinutesInterval = 10 * 60 * 1000; // set interval for transmissions between hours (in milliseconds)
const unsigned long deepSleepDuration = 8000;          // set duration for deep sleep mode (in milliseconds)
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
  rf_driver.init();            // initialize the radio driver
}
void loop() {
  unsigned long currentTime = millis();  // get the current time since the Arduino started running (in milliseconds)
  if ((currentTime - lastTransmissionTime >= firstFiveMinutesInterval && currentTime < 5 * 60 * 1000) ||  // check if it's time to transmit during the first 5 minutes
      (currentTime - lastTransmissionTime >= thirtySecondsInterval && currentTime >= 5 * 60 * 1000 && currentTime % oneHourInterval >= tenMinutesInterval)) {  // check if it's time to transmit after the first 5 minutes and between hours
    rf_driver.send((uint8_t*)&checksumValue, sizeof(checksumValue)); // send the checksum value first
    rf_driver.waitPacketSent();                                    // wait for the packet to be sent
    rf_driver.send(data, sizeof(data)); // then send the DIP switch values
    rf_driver.waitPacketSent();        // wait for the packet to be sent
    lastTransmissionTime = currentTime;  // update the time of the last transmission
  } else if (currentTime - lastTransmissionTime >= oneHourInterval) {  // check if it's been an hour since the last transmission
    unsigned long sleepTime = oneHourInterval - (currentTime % oneHourInterval); // calculate remaining time until the next hour
    while (sleepTime > 0) {                // enter deep sleep mode for the remainder of the current hour
      if (sleepTime > deepSleepDuration) {
        LowPower.deepSleep(deepSleepDuration);
        sleepTime -= deepSleepDuration;
      } else {
        delay(sleepTime);
        break;
      }
    }
  } else {                              // enter deep sleep mode for the remaining time until the next transmission interval
    unsigned long sleepTime = min(firstFiveMinutesInterval, thirtySecondsInterval) - (currentTime - lastTransmissionTime);
    while (sleepTime > 0) {
      if (sleepTime > deepSleepDuration) {
        LowPower.deepSleep(deepSleepDuration);
        sleepTime -= deepSleepDuration;
      } else {
        delay(sleepTime);
        break;
      }
    }
  }
}
}


</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 21:05, 4 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.

2.1

Poistettu ylimääräiset kommentit

#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

  rf_driver.init();             // initialize the radio driver
}

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
  rf_driver.send(data, sizeof(data)); // then send the DIP switch values
  rf_driver.waitPacketSent();         // wait for the packet to be sent
  delay(random(4000, 10000));          // add random delay of between 4-10 seconds before transmitting again
}

v2.2

Lisätty virransäästöominaisuus. Mitattuna virrankulutus 0.3-0.1A 9V

#include <CRC16.h>  // include CRC16 library
#include <RH_ASK.h>  // include RadioHead library
#include <LowPower.h>  // include LowPower library for sleep functionality

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

  if (!rf_driver.init()) {
  //if(rf_driver.init();             // initialize the radio driver
    Serial.println("radioerror");
  }
}

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
  rf_driver.send(data, sizeof(data)); // then send the DIP switch values
  rf_driver.waitPacketSent();         // wait for the packet to be sent

  int sleepTime = random(4000, 10000); // calculate the next sleep time randomly between 4-10 seconds
  while (sleepTime > 8000) {          // if sleep time is greater than 8 seconds
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); // put the microcontroller into deep sleep for 8 seconds
    sleepTime -= 8000;                              // reduce sleep time by 8 seconds
  }

  if (sleepTime > 0) {
    delay(sleepTime);                // delay for the remaining time, if any
  }
}

v2.3

Lisätty lähetysintervalli ja vaihdettu eri Arduino virransäästölibraryyn (EI TESTATTU)

#include <CRC16.h>  // include CRC16 library
#include <RH_ASK.h>  // include RadioHead library
#include <ArduinoLowPower.h>  // include ArduinoLowPower library for sleep functionality

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

unsigned long lastTransmissionTime = 0;  // initialize variable to keep track of the time since the last transmission
const unsigned long firstFiveMinutesInterval = 1000;   // set interval for transmissions during the first 5 minutes (in milliseconds)
const unsigned long thirtySecondsInterval = 30000;     // set interval for transmissions after the first 5 minutes (in milliseconds)
const unsigned long oneHourInterval = 60 * 60 * 1000;   // set interval for transmissions after each hour (in milliseconds)
const unsigned long tenMinutesInterval = 10 * 60 * 1000; // set interval for transmissions between hours (in milliseconds)
const unsigned long deepSleepDuration = 8000;          // set duration for deep sleep mode (in milliseconds)

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

  rf_driver.init();             // initialize the radio driver
}

void loop() {
  unsigned long currentTime = millis();  // get the current time since the Arduino started running (in milliseconds)

  if ((currentTime - lastTransmissionTime >= firstFiveMinutesInterval && currentTime < 5 * 60 * 1000) ||   // check if it's time to transmit during the first 5 minutes
      (currentTime - lastTransmissionTime >= thirtySecondsInterval && currentTime >= 5 * 60 * 1000 && currentTime % oneHourInterval >= tenMinutesInterval)) {  // check if it's time to transmit after the first 5 minutes and between hours
    rf_driver.send((uint8_t*)&checksumValue, sizeof(checksumValue)); // send the checksum value first
    rf_driver.waitPacketSent();                                     // wait for the packet to be sent
    rf_driver.send(data, sizeof(data)); // then send the DIP switch values
    rf_driver.waitPacketSent();         // wait for the packet to be sent

    lastTransmissionTime = currentTime;  // update the time of the last transmission
  } else if (currentTime - lastTransmissionTime >= oneHourInterval) {   // check if it's been an hour since the last transmission
    unsigned long sleepTime = oneHourInterval - (currentTime % oneHourInterval); // calculate remaining time until the next hour
    while (sleepTime > 0) {                // enter deep sleep mode for the remainder of the current hour
      if (sleepTime > deepSleepDuration) {
        LowPower.deepSleep(deepSleepDuration);
        sleepTime -= deepSleepDuration;
      } else {
        delay(sleepTime);
        break;
      }
    }
  } else {                               // enter deep sleep mode for the remaining time until the next transmission interval
    unsigned long sleepTime = min(firstFiveMinutesInterval, thirtySecondsInterval) - (currentTime - lastTransmissionTime);
    while (sleepTime > 0) {
      if (sleepTime > deepSleepDuration) {
        LowPower.deepSleep(deepSleepDuration);
        sleepTime -= deepSleepDuration;
      } else {
        delay(sleepTime);
        break;
      }
    }
  }
}