Reletiv

TTN UNO

Feb 18th, 2022
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. #include <TheThingsNetwork.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BME280.h>
  4. #include <LowPower.h>
  5.  
  6. #define SEALEVELPRESSURE_HPA (1015)
  7. #define loraSerial Serial1
  8. #define debugSerial Serial
  9. #define freqPlan TTN_FP_EU868
  10. #define batPin A5
  11.  
  12. TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
  13. Adafruit_BME280 bme; // I2C
  14.  
  15. const char *appEui = "REMOVED";
  16. const char *appKey = "REMOVED";
  17.  
  18. void setup()
  19. {
  20.   loraSerial.begin(57600);
  21.   debugSerial.begin(9600);
  22.  
  23.   pinMode(batPin,INPUT);
  24.   analogReference(INTERNAL);
  25.  
  26.   // Wait a maximum of 10s for Serial Monitor
  27.   while (!debugSerial && millis() < 10000)
  28.     ;
  29.  
  30.   debugSerial.println("-- STATUS");
  31.   ttn.showStatus();
  32.  
  33.   debugSerial.println("-- JOIN");
  34.   ttn.join(appEui, appKey);
  35.  
  36.   // Testing BME280 Sensor
  37.   unsigned status;
  38.   status = bme.begin(0x76);
  39.   if (!status) {
  40.     Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  41.     Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
  42.     Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  43.     Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
  44.     Serial.print("        ID of 0x60 represents a BME 280.\n");
  45.     Serial.print("        ID of 0x61 represents a BME 680.\n");
  46.     while (1) delay(10);
  47.   }
  48. }
  49.  
  50. void loop()
  51. {
  52.   ttn.wake();
  53.   delay(100);
  54.   debugSerial.println("-- LOOP");
  55.  
  56.   uint16_t humidity = bme.readHumidity() * 100;
  57.   uint16_t temperature = bme.readTemperature() * 100;
  58.   uint16_t pressure = bme.readPressure();
  59.   uint16_t voltage = (analogRead(batPin) * 4.2/1023) * 100;
  60.  
  61.   byte payload[8];
  62.   payload[0] = highByte(temperature);
  63.   payload[1] = lowByte(temperature);
  64.   payload[2] = highByte(humidity);
  65.   payload[3] = lowByte(humidity);
  66.   payload[4] = highByte(pressure);
  67.   payload[5] = lowByte(pressure);
  68.   payload[6] = highByte(voltage);
  69.   payload[7] = lowByte(voltage);
  70.  
  71.   debugSerial.print("Temperature: ");
  72.   debugSerial.println(temperature);
  73.   debugSerial.print("Humidity: ");
  74.   debugSerial.println(humidity);
  75.   debugSerial.print("Pressure: ");
  76.   debugSerial.println(pressure / 100.0F);
  77.   debugSerial.print("Voltage");
  78.   debugSerial.println(voltage);
  79.  
  80.   ttn.sendBytes(payload, sizeof(payload));
  81.  
  82.   ttn.sleep(2000000);    //sleep RN module in [ms]
  83.   delay(100);
  84.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  85.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  86.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  87.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  88.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  89.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  90.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  91.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  92.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  93.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  94.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  95.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  96.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  97.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  98.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  99.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  100.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  101.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  102.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  103.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  104.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  105.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  106.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  107.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  108.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  109.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  110.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  111.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  112.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  113.   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment