l'ESP 32 fréquence 868

identification (Device EUI) dans lora server DEVICE Name ESP32

Fichier de base pour un ESP 32 Node.

DEVICE EUI 1203030405060708

1
2
3
/*******************************************************************************
4
 * Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
5
 * Modifié par NG et RB (IUT de Blagnac)
6
 *
7
 * This uses OTAA (Over-the-air activation), where where a DevEUI and
8
 * application key is configured, which are used in an over-the-air
9
 * activation procedure where a DevAddr and session keys are
10
 * assigned/generated for use with all further communication.
11
 *
12
 * To use this sketch, first register your application and device with
13
 * the tloraserver, to set or generate an AppEUI, DevEUI and AppKey.
14
 * Multiple devices can use the same AppEUI, but each device has its own
15
 * DevEUI and AppKey.
16
 *
17
 * Do not forget to define the radio type correctly in config.h.
18
 *
19
 *******************************************************************************/
20
21
#include <lmic.h>
22
#include <hal/hal.h>
23
#include <SPI.h>
24
25
26
#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
27
  // Required for Serial on Zero based boards
28
  #define Serial SERIAL_PORT_USBVIRTUAL
29
#endif
30
/******************************************************************************/
31
/* LoRaWAN                                                                    */
32
/******************************************************************************/
33
34
// This EUI must be in *little-endian format* (least-significant-byte first)
35
// Necessaire pour le protocole mais inutile pour l'implémentation dans loraserver
36
// On peut donc mettre de l'aléatoire ou :
37
38
static const u1_t APPEUI[8]={ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  };
39
40
// DEVEUI should also be in *little endian format*
41
42
static const u1_t DEVEUI[8]={ 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x03, 0x12 };
43
44
// This key should be in big endian format
45
46
static const u1_t APPKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
47
48
// Copie en mémoire des EUI et APPKEY
49
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}
50
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}
51
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16);}
52
53
// Schedule TX every this many seconds (might become longer due to duty
54
// cycle limitations).
55
const unsigned TX_INTERVAL = 60;
56
57
/******************************************************************************/
58
/* pin mapping                                                                */
59
/******************************************************************************/
60
61
const lmic_pinmap lmic_pins = {
62
    .nss = 18,
63
    .rxtx = LMIC_UNUSED_PIN,
64
    .rst = 14,
65
    .dio = {26,33,32},//io1 pin is connected to pin 6, io2 vers pin 11
66
};
67
68
/******************************************************************************/
69
/* payload                                                                    */
70
/******************************************************************************/
71
72
static uint8_t mydata[] = "RB";
73
74
/******************************************************************************/
75
/* Automate LMIC                                                              */
76
/******************************************************************************/
77
78
79
static osjob_t sendjob;
80
81
void onEvent (ev_t ev) {
82
    Serial.print(os_getTime());
83
    Serial.print(": ");
84
    switch(ev) {
85
        case EV_SCAN_TIMEOUT:
86
            Serial.println(F("EV_SCAN_TIMEOUT"));
87
            break;
88
        case EV_BEACON_FOUND:
89
            Serial.println(F("EV_BEACON_FOUND"));
90
            break;
91
        case EV_BEACON_MISSED:
92
            Serial.println(F("EV_BEACON_MISSED"));
93
            break;
94
        case EV_BEACON_TRACKED:
95
            Serial.println(F("EV_BEACON_TRACKED"));
96
            break;
97
        case EV_JOINING:
98
            Serial.println(F("EV_JOINING"));
99
            break;
100
        case EV_JOINED:
101
            Serial.println(F("EV_JOINED"));
102
103
            // Disable link check validation (automatically enabled
104
            // during join, but not supported by TTN at this time).
105
            LMIC_setLinkCheckMode(1);
106
            break;
107
        case EV_RFU1:
108
            Serial.println(F("EV_RFU1"));
109
            break;
110
        case EV_JOIN_FAILED:
111
            Serial.println(F("EV_JOIN_FAILED"));
112
            break;
113
        case EV_REJOIN_FAILED:
114
            Serial.println(F("EV_REJOIN_FAILED"));
115
            break;
116
            break;
117
        case EV_TXCOMPLETE:
118
            Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
119
            if (LMIC.txrxFlags & TXRX_ACK)
120
              Serial.println(F("Received ack"));
121
            if (LMIC.dataLen) {
122
              Serial.println(F("Received "));
123
              Serial.println(LMIC.dataLen);
124
              Serial.println(F(" bytes of payload"));
125
            }
126
            // Schedule next transmission
127
            os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
128
            break;
129
        case EV_LOST_TSYNC:
130
            Serial.println(F("EV_LOST_TSYNC"));
131
            break;
132
        case EV_RESET:
133
            Serial.println(F("EV_RESET"));
134
            break;
135
        case EV_RXCOMPLETE:
136
            // data received in ping slot
137
            Serial.println(F("EV_RXCOMPLETE"));
138
            break;
139
        case EV_LINK_DEAD:
140
            Serial.println(F("EV_LINK_DEAD"));
141
            break;
142
        case EV_LINK_ALIVE:
143
            Serial.println(F("EV_LINK_ALIVE"));
144
            break;
145
         default:
146
            Serial.println(F("Unknown event"));
147
            break;
148
    }
149
}
150
151
void do_send(osjob_t* j){
152
    // Check if there is not a current TX/RX job running
153
    if (LMIC.opmode & OP_TXRXPEND) {
154
        Serial.println(F("OP_TXRXPEND, not sending"));
155
    } else {
156
        // Prepare upstream data transmission at the next possible time.
157
        LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
158
        Serial.println(F("Packet queued"));
159
    }
160
    // Next TX is scheduled after TX_COMPLETE event.
161
}
162
163
164
void setup() {
165
 while (! Serial);
166
 Serial.begin(9600);
167
    while (millis() < 5000) {
168
    Serial.print("millis() = "); Serial.println(millis());
169
    delay(500);
170
  }
171
  Serial.println(F("Starting"));
172
173
    #ifdef VCC_ENABLE
174
    // For Pinoccio Scout boards
175
    pinMode(VCC_ENABLE, OUTPUT);
176
    digitalWrite(VCC_ENABLE, HIGH);
177
    delay(1000);
178
    #endif
179
180
    // LMIC init
181
    os_init();
182
    // Reset the MAC state. Session and pending data transfers will be discarded.
183
    LMIC_reset();
184
    LMIC_setClockError(MAX_CLOCK_ERROR * 10 / 100);
185
    
186
    // Start job (sending automatically starts OTAA too)
187
    do_send(&sendjob);
188
}
189
190
void loop() {
191
 os_runloop_once();
192
193
}