Coragem Adaptation
lora.txt@5:ef21a65a3450, 2019-12-04 (annotated)
- Committer:
- marcoantonioara
- Date:
- Wed Dec 04 11:50:35 2019 +0000
- Revision:
- 5:ef21a65a3450
Test Coragem using Lora
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcoantonioara | 5:ef21a65a3450 | 1 | #include "SX1272.h" |
marcoantonioara | 5:ef21a65a3450 | 2 | |
marcoantonioara | 5:ef21a65a3450 | 3 | //#define BAND868 |
marcoantonioara | 5:ef21a65a3450 | 4 | #define MAX_DBM 14 |
marcoantonioara | 5:ef21a65a3450 | 5 | #define DATA 0 |
marcoantonioara | 5:ef21a65a3450 | 6 | #define PING 1 |
marcoantonioara | 5:ef21a65a3450 | 7 | #define PONG 2 |
marcoantonioara | 5:ef21a65a3450 | 8 | |
marcoantonioara | 5:ef21a65a3450 | 9 | |
marcoantonioara | 5:ef21a65a3450 | 10 | #define ADDRESS 10 |
marcoantonioara | 5:ef21a65a3450 | 11 | |
marcoantonioara | 5:ef21a65a3450 | 12 | const uint32_t DEFAULT_CHANNEL=CH_12_900;//915Mhz CH_12_900 =>0xE4C000; |
marcoantonioara | 5:ef21a65a3450 | 13 | |
marcoantonioara | 5:ef21a65a3450 | 14 | /////////////////////////////////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 15 | // CHANGE HERE THE LORA MODE, NODE ADDRESS |
marcoantonioara | 5:ef21a65a3450 | 16 | #define LORAMODE 1 |
marcoantonioara | 5:ef21a65a3450 | 17 | #define node_addr 6 |
marcoantonioara | 5:ef21a65a3450 | 18 | ////////////////////////////////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 19 | |
marcoantonioara | 5:ef21a65a3450 | 20 | /////////////////////////////////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 21 | // CHANGE HERE THE THINGSPEAK FIELD BETWEEN 1 AND 4 |
marcoantonioara | 5:ef21a65a3450 | 22 | #define field_index 1 |
marcoantonioara | 5:ef21a65a3450 | 23 | /////////////////////////////////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 24 | #define DEFAULT_DEST_ADDR 6 |
marcoantonioara | 5:ef21a65a3450 | 25 | |
marcoantonioara | 5:ef21a65a3450 | 26 | |
marcoantonioara | 5:ef21a65a3450 | 27 | /////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 28 | // Globals variables |
marcoantonioara | 5:ef21a65a3450 | 29 | /////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 30 | |
marcoantonioara | 5:ef21a65a3450 | 31 | //___________SX1272____________________ |
marcoantonioara | 5:ef21a65a3450 | 32 | int e; |
marcoantonioara | 5:ef21a65a3450 | 33 | |
marcoantonioara | 5:ef21a65a3450 | 34 | //___________mensagem_____________________ |
marcoantonioara | 5:ef21a65a3450 | 35 | uint8_t message[255]; |
marcoantonioara | 5:ef21a65a3450 | 36 | uint8_t buffer[255]; |
marcoantonioara | 5:ef21a65a3450 | 37 | uint8_t message_pong[]="Pong"; |
marcoantonioara | 5:ef21a65a3450 | 38 | uint8_t message_ping[]="Ping"; |
marcoantonioara | 5:ef21a65a3450 | 39 | uint8_t r_size; |
marcoantonioara | 5:ef21a65a3450 | 40 | char float_temp[10]; |
marcoantonioara | 5:ef21a65a3450 | 41 | char float_press[10]; |
marcoantonioara | 5:ef21a65a3450 | 42 | char float_hum[10]; |
marcoantonioara | 5:ef21a65a3450 | 43 | int cont =0; |
marcoantonioara | 5:ef21a65a3450 | 44 | int loraMode=LORAMODE; |
marcoantonioara | 5:ef21a65a3450 | 45 | |
marcoantonioara | 5:ef21a65a3450 | 46 | |
marcoantonioara | 5:ef21a65a3450 | 47 | //////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 48 | // SETUP SX1272 initialisation |
marcoantonioara | 5:ef21a65a3450 | 49 | //////////////////////////////////////// |
marcoantonioara | 5:ef21a65a3450 | 50 | void setup() |
marcoantonioara | 5:ef21a65a3450 | 51 | { |
marcoantonioara | 5:ef21a65a3450 | 52 | |
marcoantonioara | 5:ef21a65a3450 | 53 | |
marcoantonioara | 5:ef21a65a3450 | 54 | printf("------Coragem LoRa temperature sensor-------------\n"); |
marcoantonioara | 5:ef21a65a3450 | 55 | //sx1272.ON(); // Power ON the module |
marcoantonioara | 5:ef21a65a3450 | 56 | |
marcoantonioara | 5:ef21a65a3450 | 57 | int error_config_sx1272=0; |
marcoantonioara | 5:ef21a65a3450 | 58 | |
marcoantonioara | 5:ef21a65a3450 | 59 | // Set transmission mode and print the result |
marcoantonioara | 5:ef21a65a3450 | 60 | e = sx1272.setMode(loraMode); |
marcoantonioara | 5:ef21a65a3450 | 61 | printf("Mode: %d\n",loraMode); |
marcoantonioara | 5:ef21a65a3450 | 62 | if (e) error_config_sx1272=1; |
marcoantonioara | 5:ef21a65a3450 | 63 | printf("Setting Mode: state %d\n",e); |
marcoantonioara | 5:ef21a65a3450 | 64 | |
marcoantonioara | 5:ef21a65a3450 | 65 | // enable carrier sense |
marcoantonioara | 5:ef21a65a3450 | 66 | sx1272._enableCarrierSense=true; |
marcoantonioara | 5:ef21a65a3450 | 67 | |
marcoantonioara | 5:ef21a65a3450 | 68 | // for LOW POWER |
marcoantonioara | 5:ef21a65a3450 | 69 | sx1272._RSSIonSend=false; |
marcoantonioara | 5:ef21a65a3450 | 70 | |
marcoantonioara | 5:ef21a65a3450 | 71 | |
marcoantonioara | 5:ef21a65a3450 | 72 | // Select frequency channel |
marcoantonioara | 5:ef21a65a3450 | 73 | e = sx1272.setChannel(DEFAULT_CHANNEL); |
marcoantonioara | 5:ef21a65a3450 | 74 | if (e) error_config_sx1272=1; |
marcoantonioara | 5:ef21a65a3450 | 75 | printf("Setting Channel: state %d\n",e); |
marcoantonioara | 5:ef21a65a3450 | 76 | |
marcoantonioara | 5:ef21a65a3450 | 77 | // Select amplifier line; PABOOST or RFO |
marcoantonioara | 5:ef21a65a3450 | 78 | // #ifdef PABOOST |
marcoantonioara | 5:ef21a65a3450 | 79 | // printf("pabboost\n"); |
marcoantonioara | 5:ef21a65a3450 | 80 | // sx1272._needPABOOST=true; |
marcoantonioara | 5:ef21a65a3450 | 81 | // // previous way for setting output power |
marcoantonioara | 5:ef21a65a3450 | 82 | // // powerLevel='x'; |
marcoantonioara | 5:ef21a65a3450 | 83 | // #else |
marcoantonioara | 5:ef21a65a3450 | 84 | // // previous way for setting output power |
marcoantonioara | 5:ef21a65a3450 | 85 | // // powerLevel='M'; |
marcoantonioara | 5:ef21a65a3450 | 86 | // #endif |
marcoantonioara | 5:ef21a65a3450 | 87 | |
marcoantonioara | 5:ef21a65a3450 | 88 | // previous way for setting output power |
marcoantonioara | 5:ef21a65a3450 | 89 | // e = sx1272.setPower(powerLevel); |
marcoantonioara | 5:ef21a65a3450 | 90 | |
marcoantonioara | 5:ef21a65a3450 | 91 | e = sx1272.setPowerDBM((uint8_t)MAX_DBM); |
marcoantonioara | 5:ef21a65a3450 | 92 | if (e) error_config_sx1272=1; |
marcoantonioara | 5:ef21a65a3450 | 93 | printf("Setting Power: state %d\n",e); |
marcoantonioara | 5:ef21a65a3450 | 94 | |
marcoantonioara | 5:ef21a65a3450 | 95 | // Set the node address and print the result |
marcoantonioara | 5:ef21a65a3450 | 96 | e = sx1272.setNodeAddress(node_addr); |
marcoantonioara | 5:ef21a65a3450 | 97 | if (e) error_config_sx1272=1; |
marcoantonioara | 5:ef21a65a3450 | 98 | printf("Setting node addr: state %d\n",e); |
marcoantonioara | 5:ef21a65a3450 | 99 | |
marcoantonioara | 5:ef21a65a3450 | 100 | // Print a success message |
marcoantonioara | 5:ef21a65a3450 | 101 | if (!error_config_sx1272) printf("SX1272 successfully configured\n"); |
marcoantonioara | 5:ef21a65a3450 | 102 | else printf("ERROR CONFIGURATION SX1272\n"); |
marcoantonioara | 5:ef21a65a3450 | 103 | |
marcoantonioara | 5:ef21a65a3450 | 104 | wait_ms(400); |
marcoantonioara | 5:ef21a65a3450 | 105 | } |
marcoantonioara | 5:ef21a65a3450 | 106 | void send_packet (uint8_t *payload, uint8_t length8) // envia pacote |
marcoantonioara | 5:ef21a65a3450 | 107 | { |
marcoantonioara | 5:ef21a65a3450 | 108 | |
marcoantonioara | 5:ef21a65a3450 | 109 | //write on FIFO |
marcoantonioara | 5:ef21a65a3450 | 110 | sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags |
marcoantonioara | 5:ef21a65a3450 | 111 | sx1272.writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Stdby LoRa mode to write in FIFO |
marcoantonioara | 5:ef21a65a3450 | 112 | sx1272.writeRegister(REG_PAYLOAD_LENGTH_LORA, length8); |
marcoantonioara | 5:ef21a65a3450 | 113 | sx1272.writeRegister(REG_FIFO_TX_BASE_ADDR,0x00); |
marcoantonioara | 5:ef21a65a3450 | 114 | sx1272.writeRegister(REG_FIFO_ADDR_PTR,0x00); |
marcoantonioara | 5:ef21a65a3450 | 115 | |
marcoantonioara | 5:ef21a65a3450 | 116 | for(unsigned int i = 0; i <= length8; i++) { |
marcoantonioara | 5:ef21a65a3450 | 117 | sx1272.writeRegister(REG_FIFO, payload[i]); // Writing the payload in FIFO |
marcoantonioara | 5:ef21a65a3450 | 118 | } |
marcoantonioara | 5:ef21a65a3450 | 119 | |
marcoantonioara | 5:ef21a65a3450 | 120 | //________________Send Data__________________ |
marcoantonioara | 5:ef21a65a3450 | 121 | sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags |
marcoantonioara | 5:ef21a65a3450 | 122 | sx1272.writeRegister(REG_OP_MODE, LORA_TX_MODE); // LORA mode - Tx |
marcoantonioara | 5:ef21a65a3450 | 123 | |
marcoantonioara | 5:ef21a65a3450 | 124 | unsigned long exitTime = millis()+2000;//2 segundos para sair do for |
marcoantonioara | 5:ef21a65a3450 | 125 | unsigned long Time= millis(); |
marcoantonioara | 5:ef21a65a3450 | 126 | char value = sx1272.readRegister(REG_IRQ_FLAGS); |
marcoantonioara | 5:ef21a65a3450 | 127 | |
marcoantonioara | 5:ef21a65a3450 | 128 | while ((bitRead(value, 3) == 0) && (Time < exitTime)) { |
marcoantonioara | 5:ef21a65a3450 | 129 | value=sx1272.readRegister(REG_IRQ_FLAGS); |
marcoantonioara | 5:ef21a65a3450 | 130 | Time= millis(); |
marcoantonioara | 5:ef21a65a3450 | 131 | wait_ms(50); |
marcoantonioara | 5:ef21a65a3450 | 132 | } |
marcoantonioara | 5:ef21a65a3450 | 133 | |
marcoantonioara | 5:ef21a65a3450 | 134 | wait_ms(50); |
marcoantonioara | 5:ef21a65a3450 | 135 | |
marcoantonioara | 5:ef21a65a3450 | 136 | } |
marcoantonioara | 5:ef21a65a3450 | 137 | |
marcoantonioara | 5:ef21a65a3450 | 138 | void send_data(int mode) //data predefined |
marcoantonioara | 5:ef21a65a3450 | 139 | { |
marcoantonioara | 5:ef21a65a3450 | 140 | led1=1; |
marcoantonioara | 5:ef21a65a3450 | 141 | sx1272.writeRegister(REG_OP_MODE,129);//standby |
marcoantonioara | 5:ef21a65a3450 | 142 | |
marcoantonioara | 5:ef21a65a3450 | 143 | if (mode == DATA ) { |
marcoantonioara | 5:ef21a65a3450 | 144 | |
marcoantonioara | 5:ef21a65a3450 | 145 | sprintf(float_temp,"%2.2f",getTemperature()); |
marcoantonioara | 5:ef21a65a3450 | 146 | sprintf(float_press,"%04.2f",getPressure()); |
marcoantonioara | 5:ef21a65a3450 | 147 | sprintf(float_hum,"%2.2f",getHumidity()); |
marcoantonioara | 5:ef21a65a3450 | 148 | |
marcoantonioara | 5:ef21a65a3450 | 149 | //============= internet of turtles ============= |
marcoantonioara | 5:ef21a65a3450 | 150 | sprintf(float_breathing_time,"%04.2f", last_breathing_time); |
marcoantonioara | 5:ef21a65a3450 | 151 | sprintf(float_diving_time,"%04.2f", last_diving_time); |
marcoantonioara | 5:ef21a65a3450 | 152 | r_size=sprintf((char*)message,"\\!#Dt%04.2f_Bt%04.2f_%s°C_%shPa_%s%%",last_diving_time,last_breathing_time,float_temp,float_press,float_hum); |
marcoantonioara | 5:ef21a65a3450 | 153 | // ============================================== |
marcoantonioara | 5:ef21a65a3450 | 154 | |
marcoantonioara | 5:ef21a65a3450 | 155 | //size=sprintf((char*)message,"\\!#%s°C_%shPa_%s%%",float_temp,float_press,float_hum); |
marcoantonioara | 5:ef21a65a3450 | 156 | |
marcoantonioara | 5:ef21a65a3450 | 157 | send_packet(message,r_size); |
marcoantonioara | 5:ef21a65a3450 | 158 | // sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR,/* (uint8_t*)*/message, r_size); |
marcoantonioara | 5:ef21a65a3450 | 159 | printf("packet send :\n%s\nrsize=%d\n",message,r_size); |
marcoantonioara | 5:ef21a65a3450 | 160 | |
marcoantonioara | 5:ef21a65a3450 | 161 | } else if (mode == PING ) { |
marcoantonioara | 5:ef21a65a3450 | 162 | |
marcoantonioara | 5:ef21a65a3450 | 163 | |
marcoantonioara | 5:ef21a65a3450 | 164 | //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_ping, sizeof(message_ping)); |
marcoantonioara | 5:ef21a65a3450 | 165 | send_packet(message_ping,sizeof(message_ping)); |
marcoantonioara | 5:ef21a65a3450 | 166 | printf("packet send :\n%s\n",message_ping); |
marcoantonioara | 5:ef21a65a3450 | 167 | |
marcoantonioara | 5:ef21a65a3450 | 168 | |
marcoantonioara | 5:ef21a65a3450 | 169 | } else if (mode == PONG ) { |
marcoantonioara | 5:ef21a65a3450 | 170 | |
marcoantonioara | 5:ef21a65a3450 | 171 | //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_pong, sizeof(message_pong)); |
marcoantonioara | 5:ef21a65a3450 | 172 | send_packet(message_pong,sizeof(message_pong)); |
marcoantonioara | 5:ef21a65a3450 | 173 | printf("packet send :\n%s\n",message_pong); |
marcoantonioara | 5:ef21a65a3450 | 174 | } |
marcoantonioara | 5:ef21a65a3450 | 175 | |
marcoantonioara | 5:ef21a65a3450 | 176 | wait_ms(300); |
marcoantonioara | 5:ef21a65a3450 | 177 | |
marcoantonioara | 5:ef21a65a3450 | 178 | sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags |
marcoantonioara | 5:ef21a65a3450 | 179 | sx1272.writeRegister(REG_OP_MODE,133); //leitura continua |
marcoantonioara | 5:ef21a65a3450 | 180 | cont++; |
marcoantonioara | 5:ef21a65a3450 | 181 | printf("number=%d\n",cont); |
marcoantonioara | 5:ef21a65a3450 | 182 | |
marcoantonioara | 5:ef21a65a3450 | 183 | led1=0; |
marcoantonioara | 5:ef21a65a3450 | 184 | } |
marcoantonioara | 5:ef21a65a3450 | 185 | |
marcoantonioara | 5:ef21a65a3450 | 186 | |
marcoantonioara | 5:ef21a65a3450 | 187 | void print_packet() |
marcoantonioara | 5:ef21a65a3450 | 188 | { |
marcoantonioara | 5:ef21a65a3450 | 189 | |
marcoantonioara | 5:ef21a65a3450 | 190 | led2=1; |
marcoantonioara | 5:ef21a65a3450 | 191 | sx1272.writeRegister(REG_OP_MODE,129);//standby |
marcoantonioara | 5:ef21a65a3450 | 192 | |
marcoantonioara | 5:ef21a65a3450 | 193 | uint8_t pac_size; |
marcoantonioara | 5:ef21a65a3450 | 194 | |
marcoantonioara | 5:ef21a65a3450 | 195 | sx1272.writeRegister(REG_FIFO_ADDR_PTR,sx1272.readRegister(REG_FIFO_RX_CURRENT_ADDR));//set fifo pointer to read packet |
marcoantonioara | 5:ef21a65a3450 | 196 | pac_size = sx1272.readRegister(REG_RX_NB_BYTES);//read size of packet |
marcoantonioara | 5:ef21a65a3450 | 197 | for(int i =0 ; i<pac_size ; i++) { //print packet |
marcoantonioara | 5:ef21a65a3450 | 198 | buffer[i]=sx1272.readRegister(REG_FIFO); |
marcoantonioara | 5:ef21a65a3450 | 199 | /* if (buffer[i]== '\n') printf(" \\n"); |
marcoantonioara | 5:ef21a65a3450 | 200 | if (buffer[i]== 0x0B) printf("tab");*/ |
marcoantonioara | 5:ef21a65a3450 | 201 | printf("%c",buffer[i]);//print packet |
marcoantonioara | 5:ef21a65a3450 | 202 | } |
marcoantonioara | 5:ef21a65a3450 | 203 | printf("\n"); |
marcoantonioara | 5:ef21a65a3450 | 204 | |
marcoantonioara | 5:ef21a65a3450 | 205 | |
marcoantonioara | 5:ef21a65a3450 | 206 | for(int i =0 ; i<pac_size ; i++) { //look for Ping in packet |
marcoantonioara | 5:ef21a65a3450 | 207 | if ((buffer[i] =='P') && (buffer[i+1] =='i') && (buffer[i+2] =='n') && (buffer[i+3] =='g') ) { |
marcoantonioara | 5:ef21a65a3450 | 208 | sx1272.writeRegister(0x12,255);//clear flags |
marcoantonioara | 5:ef21a65a3450 | 209 | send_data(PONG); |
marcoantonioara | 5:ef21a65a3450 | 210 | i=pac_size; |
marcoantonioara | 5:ef21a65a3450 | 211 | } |
marcoantonioara | 5:ef21a65a3450 | 212 | } |
marcoantonioara | 5:ef21a65a3450 | 213 | |
marcoantonioara | 5:ef21a65a3450 | 214 | for(int i =0 ; i<pac_size ; i++) { //look for Reset in packet |
marcoantonioara | 5:ef21a65a3450 | 215 | if ((buffer[i] =='R') && (buffer[i+1] =='e') && (buffer[i+2] =='s') && (buffer[i+3] =='e') && (buffer[i+4] =='t') ) { |
marcoantonioara | 5:ef21a65a3450 | 216 | uint32_t *ptr; |
marcoantonioara | 5:ef21a65a3450 | 217 | ptr = (uint32_t *)1073743132;// endereço gpregret 0x4000051C |
marcoantonioara | 5:ef21a65a3450 | 218 | *ptr = 0xB1;//BOOTLOADER_DFU_START |
marcoantonioara | 5:ef21a65a3450 | 219 | NVIC_SystemReset(); |
marcoantonioara | 5:ef21a65a3450 | 220 | } |
marcoantonioara | 5:ef21a65a3450 | 221 | } |
marcoantonioara | 5:ef21a65a3450 | 222 | |
marcoantonioara | 5:ef21a65a3450 | 223 | wait_ms(30); |
marcoantonioara | 5:ef21a65a3450 | 224 | sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags |
marcoantonioara | 5:ef21a65a3450 | 225 | sx1272.writeRegister(REG_OP_MODE,133); //leitura continua |
marcoantonioara | 5:ef21a65a3450 | 226 | led2=0; |
marcoantonioara | 5:ef21a65a3450 | 227 | |
marcoantonioara | 5:ef21a65a3450 | 228 | } |