Events

:

:

Elektronik | Funk | Software

Der Technik-Blog

  • Social Media

    YouTube

    Werbung:


    Neue Artikel


    Events

    • Keine zukünftigen Events vorhanden

    Der Technik-Blog

    LoRaWAN Wetterstation 2021 Source Code AEQ-WEB

    LoRaWAN Wetterstation Source Code

    Alex @ AEQ-WEB

    Auf dieser Seite befindet sich der vollständige Beispielcode für das Projekt LoRaWAN Wetterstation 2021 (Version 3.1). Als Aktivierung für das LoRaWAN-Netzwerk wird OTTA verwendet.

    Hinweis: Die Pinbelegungen (LoRa, SPI & Serial) sind für eine bestimmte Hardware-Version definiert und müssen möglicherweise angepasst werden. Nähere Information dazu nachfolgend bzw. im Hauptartikel.

    Felder mit {FILLMEIN} müssen durch die jeweiligen Schlüssel vom LoraWAN Netzwerk ersetzt werden. Dieses Projekt ist mit TTN (V2) und dem Nachfolger TTS (V3) bzw. mit dem LoRaWAN-Standard 1.0.3 kompatibel.

    Werbung:

    Versionsinformation zum Testzeitpunkt:

    -> IDE Version: Arduino 1.8.10
    -> Library Version: Arduino LMIC (von MCCI Catena) | V. 3.2.0
    -> Hardware: Heltec ESP32 LoRa Node V2

    Weitere Bibliotheken:

    -> BME280: Adafruit Unified Sensor
    -> BME280: Adafruit BME280 Library

    Achtung: Die I2C-Adresse vom BME280 muss möglicherweise in der Library (Datei: Adafruit_BME280.h) in der Zeile "#define BME280_ADDRESS (0x76)" angepasst werden.


    /***************************************************************************************************
       Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
    
       Permission is hereby granted, free of charge, to anyone
       obtaining a copy of this document and accompanying files,
       to do whatever they want with them without any restriction,
       including, but not limited to, copying, modification and redistribution.
       NO WARRANTY OF ANY KIND IS PROVIDED.
    
       Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in
       g1, 0.1% in g2), but not the TTN fair usage policy (which is probably
       violated by this sketch when left running for longer)!
    
       Do not forget to define the radio type correctly in config.h.
    
       -------------------------------------------------------------------------------------------------
       This sample code has been adapted for HELTEC ESP32 LoRa Board.
       Further information: https://www.aeq-web.com/
    
     ***************************************************************************************************/
    
    #include <Wire.h>
    #include <Adafruit_Sensor.h>
    #include <Adafruit_BME280.h>
    
    #include <lmic.h>
    #include <hal/hal.h>
    #include <SPI.h>
    
    int ldrpin = 37; //PIN for LDR input
    int anopin = 36; //PIN for anemometer input
    
    float temp;
    float pressure;
    float humidity;
    float sunlight;
    float wind;
    
    int wind_cnt;
    
    Adafruit_BME280 bme;
    
    static const u1_t PROGMEM APPEUI[8] = {FILLMEIN};
    void os_getArtEui (u1_t* buf) {
      memcpy_P(buf, APPEUI, 8);
    }
    
    static const u1_t PROGMEM DEVEUI[8] = {FILLMEIN};
    void os_getDevEui (u1_t* buf) {
      memcpy_P(buf, DEVEUI, 8);
    }
    
    static const u1_t PROGMEM APPKEY[16] = {FILLMEIN};
    void os_getDevKey (u1_t* buf) {
      memcpy_P(buf, APPKEY, 16);
    }
    boolean join_success = false;
    uint8_t tx_payload[8];
    
    static osjob_t sendjob;
    
    // Schedule TX every this many seconds (might become longer due to duty
    // cycle limitations).
    const unsigned TX_INTERVAL = 180;
    
    //LoRa pin mapping ESP32 (HELTEC LORA Board V2)
    const lmic_pinmap lmic_pins = {
      .nss = 18,
      .rxtx = LMIC_UNUSED_PIN,
      .rst = 14,
      .dio = {26, 34, 35},
    };
    
    void printHex2(unsigned v) {
      v &= 0xff;
      if (v < 16)
        Serial.print('0');
      Serial.print(v, HEX);
    }
    
    void onEvent (ev_t ev) {
      Serial.print(os_getTime());
      Serial.print(": ");
      switch (ev) {
        case EV_SCAN_TIMEOUT:
          Serial.println(F("EV_SCAN_TIMEOUT"));
          break;
        case EV_BEACON_FOUND:
          Serial.println(F("EV_BEACON_FOUND"));
          break;
        case EV_BEACON_MISSED:
          Serial.println(F("EV_BEACON_MISSED"));
          break;
        case EV_BEACON_TRACKED:
          Serial.println(F("EV_BEACON_TRACKED"));
          break;
        case EV_JOINING:
          Serial.println(F("EV_JOINING"));
          break;
        case EV_JOINED:
          Serial.println(F("EV_JOINED"));
          {
            u4_t netid = 0;
            devaddr_t devaddr = 0;
            u1_t nwkKey[16];
            u1_t artKey[16];
            LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
            Serial.print("netid: ");
            Serial.println(netid, DEC);
            Serial.print("devaddr: ");
            Serial.println(devaddr, HEX);
            Serial.print("AppSKey: ");
            for (size_t i = 0; i < sizeof(artKey); ++i) {
              if (i != 0)
                Serial.print("-");
              printHex2(artKey[i]);
            }
            Serial.println("");
            Serial.print("NwkSKey: ");
            for (size_t i = 0; i < sizeof(nwkKey); ++i) {
              if (i != 0)
                Serial.print("-");
              printHex2(nwkKey[i]);
            }
            Serial.println();
            join_success = true;
          }
          // Disable link check validation (automatically enabled
          // during join, but because slow data rates change max TX
          // size, we don't use it in this example.
          LMIC_setLinkCheckMode(0);
          break;
        case EV_JOIN_FAILED:
          Serial.println(F("EV_JOIN_FAILED"));
          break;
        case EV_REJOIN_FAILED:
          Serial.println(F("EV_REJOIN_FAILED"));
          break;
        case EV_TXCOMPLETE:
    
          Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
          if (LMIC.txrxFlags & TXRX_ACK)
            Serial.println(F("Received ack"));
          if (LMIC.dataLen) {
            Serial.print(F("Received "));
            Serial.print(LMIC.dataLen);
            Serial.println(F(" bytes of payload"));
          }
          // Schedule next transmission
          os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
          break;
        case EV_LOST_TSYNC:
          Serial.println(F("EV_LOST_TSYNC"));
          break;
        case EV_RESET:
          Serial.println(F("EV_RESET"));
          break;
        case EV_RXCOMPLETE:
          // data received in ping slot
          Serial.println(F("EV_RXCOMPLETE"));
          break;
        case EV_LINK_DEAD:
          Serial.println(F("EV_LINK_DEAD"));
          break;
        case EV_LINK_ALIVE:
          Serial.println(F("EV_LINK_ALIVE"));
          break;
        case EV_TXSTART:
          Serial.println(F("EV_TXSTART"));
          break;
        case EV_TXCANCELED:
          Serial.println(F("EV_TXCANCELED"));
          break;
        case EV_RXSTART:
          /* do not print anything -- it wrecks timing */
          break;
        case EV_JOIN_TXCOMPLETE:
          Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
          break;
    
        default:
          Serial.print(F("Unknown event: "));
          Serial.println((unsigned) ev);
          break;
      }
    }
    
    void do_send(osjob_t* j) {
      // Check if there is not a current TX/RX job running
      if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println(F("OP_TXRXPEND, not sending"));
      } else {
        // Prepare upstream data transmission at the next possible time.
    
        getSensorData();
        generate_payload(temp, pressure, humidity, wind, sunlight);
    
        Serial.print("Payload: ");
        int x = 0;
        while (x < sizeof(tx_payload)) {
          printHex2(tx_payload[x]);
          Serial.print(" ");
          x++;
        }
        Serial.println();
    
        LMIC_setTxData2(1, tx_payload, sizeof(tx_payload), 0);
        Serial.println(F("Packet queued"));
      }
      // Next TX is scheduled after TX_COMPLETE event.
    }
    
    void setup() {
      delay(5000);
      Serial.begin(115200);
      pinMode(anopin, INPUT_PULLDOWN);
      bme.begin(0x76); //define I2C-Address
    
      //SPI pin mapping ESP32 (HELTEC LORA Board V2)
      SPI.begin(5, 19, 27, 18);
      getSensorData();
    
    #ifdef VCC_ENABLE
      // For Pinoccio Scout boards
      pinMode(VCC_ENABLE, OUTPUT);
      digitalWrite(VCC_ENABLE, HIGH);
      delay(1000);
    #endif
    
      // LMIC init
      os_init();
      // Reset the MAC state. Session and pending data transfers will be discarded.
      LMIC_reset();
    
      // Start job (sending automatically starts OTAA too)
      do_send(&sendjob);
    
      //Set FS for RX2 (SF9 = TTN,TTS EU)
      LMIC.dn2Dr = DR_SF9;
    
    }
    
    void loop() {
      os_runloop_once();
    }
    
    void getSensorData() {
      temp = bme.readTemperature();
      pressure = bme.readPressure() / 100;
      humidity = bme.readHumidity();
    
      int LDRValue = analogRead(ldrpin);
      sunlight = (LDRValue * 0.02442);
    
      meassure_wind();
    }
    
    void meassure_wind() {
      wind_cnt = 0;
      attachInterrupt(anopin, intrrupt_cnt, RISING);
      delay(3000); //Time for measure counts
      detachInterrupt(anopin);
      wind = ((float)wind_cnt / (float)3 * 2.4) / 2; //Convert counts & time to km/h
    }
    
    void intrrupt_cnt() {//On count detected
      wind_cnt++;//Add one count
    }
    
    void generate_payload(float temperature, float airpressure, float humidity, float windspeed, float sunlight) {
    
      uint8_t payload[8];
      
      int tmp = ((int)(temperature * 100)) + 5000;
      int pre = (int)(airpressure * 10);
      byte hum = (int)(humidity * 2);
      int wnd = (int)(windspeed * 10);
      byte sun = (int)(sunlight * 2);
    
      payload[0] = tmp >> 8;
      payload[1] = tmp;
      payload[2] = pre >> 8;
      payload[3] = pre;
      payload[4] = hum;
      payload[5] = wnd >> 8;
      payload[6] = wnd;
      payload[7] = sun;
    
      int i = 0;
      while (i < sizeof(payload)) {
        tx_payload[i] = payload[i];
        i++;
      }
    }
    


    122X122

    Über den Autor

    Alex, der Gründer von AEQ-WEB. Seit über 10 Jahren beschäftigt er sich mit Computern und elektronischen Bauteilen aller Art. Neben den Hardware-Projekten entwickelt er auch Webseiten, Apps und Software für Computer.

    Top Artikel in dieser Kategorie:

    Arduino PT100 Temperature Sensor Converter

    PT100 Arduino Sketch

    • Video
    • DE/EN

    Beispielcode für den PT100 Temperatursensor an einem Arduino

    Weiterlesen
    Vaisala RS41 Radiosonde Firmware Flash

    Radiosonde RS41 Firmware Flash

    • Video
    • DE/EN

    Täglich fallen Hunderte meteorologische Radiosonden vom Himmel. In diesem Artikel bauen wir eine Radiosonde zu einen GPS-Tracker für APRS, RTTY & CW um

    Weiterlesen

    Social Media

    YouTube

    Werbung:


    Neue Artikel


    Events

    • Keine zukünftigen Events vorhanden