Program test for Coragem

Dependencies:   SX1272 SPI_MX25R

Revision:
4:05d5aa4d3f2d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lora.txt	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,246 @@
+#include "SX1272.h"
+
+InterruptIn dio0(P0_12);
+
+//#define BAND868
+#define MAX_DBM 14
+#define DATA 0
+#define PING 1
+#define PONG 2
+
+
+#define ADDRESS 10
+
+const uint32_t DEFAULT_CHANNEL=CH_12_900;//915Mhz CH_12_900 =>0xE4C000;
+
+///////////////////////////////////////////////////////////////////
+// CHANGE HERE THE LORA MODE, NODE ADDRESS
+#define LORAMODE  1
+#define node_addr 6
+//////////////////////////////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////
+// CHANGE HERE THE THINGSPEAK FIELD BETWEEN 1 AND 4
+#define field_index 1
+///////////////////////////////////////////////////////////////////
+#define DEFAULT_DEST_ADDR 6
+
+
+///////////////////////////////////////
+// Globals variables
+///////////////////////////////////////
+
+//___________SX1272____________________
+int e;
+
+//___________mensagem_____________________
+uint8_t message[255];
+uint8_t buffer[255];
+uint8_t message_pong[]="Pong";
+uint8_t message_ping[]="Ping";
+uint8_t r_size;
+uint8_t lora_mode_ant;
+char float_temp[10];
+char float_press[10];
+char float_hum[10];
+int cont =0;
+int loraMode=LORAMODE;
+
+
+////////////////////////////////////////
+// SETUP SX1272 initialisation
+////////////////////////////////////////
+void lora_setup()
+{
+
+
+    //printf("------Coragem LoRa temperature sensor-------------\n");
+    //sx1272.ON();  // Power ON the module
+
+    int error_config_sx1272=0;
+
+    // Set transmission mode and print the result
+    e = sx1272.setMode(loraMode);
+    //printf("Mode: %d\n",loraMode);
+    if (e) error_config_sx1272=1;
+    //printf("Setting Mode: state %d\n",e);
+
+    // enable carrier sense
+    sx1272._enableCarrierSense=true;
+
+    // for LOW POWER
+    sx1272._RSSIonSend=false;
+
+
+    // Select frequency channel
+    e = sx1272.setChannel(DEFAULT_CHANNEL);
+    if (e) error_config_sx1272=1;
+    //printf("Setting Channel: state %d\n",e);
+
+    // Select amplifier line; PABOOST or RFO
+//    #ifdef PABOOST
+//        printf("pabboost\n");
+//        sx1272._needPABOOST=true;
+//    // previous way for setting output power
+//    // powerLevel='x';
+//    #else
+//    // previous way for setting output power
+//    // powerLevel='M';
+//    #endif
+
+    // previous way for setting output power
+    // e = sx1272.setPower(powerLevel);
+
+    e = sx1272.setPowerDBM((uint8_t)MAX_DBM);
+    if (e) error_config_sx1272=1;
+    printf("Setting Power: state %d\n",e);
+
+    // Set the node address and print the result
+    e = sx1272.setNodeAddress(node_addr);
+    if (e) error_config_sx1272=1;
+    printf("Setting node addr: state %d\n",e);
+
+    // Print a success message
+    if (!error_config_sx1272) printf("SX1272 successfully configured\n");
+    else printf("ERROR CONFIGURATION SX1272\n");
+
+    wait_ms(400);
+}
+void lora_send_packet (uint8_t *payload, uint8_t length8)   // envia pacote
+{
+
+    //write on FIFO
+    sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
+    sx1272.writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);  // Stdby LoRa mode to write in FIFO
+    sx1272.writeRegister(REG_PAYLOAD_LENGTH_LORA, length8);
+    sx1272.writeRegister(REG_FIFO_TX_BASE_ADDR,0x00);
+    sx1272.writeRegister(REG_FIFO_ADDR_PTR,0x00);
+
+    for(unsigned int i = 0; i <= length8; i++) {
+        sx1272.writeRegister(REG_FIFO, payload[i]);  // Writing the payload in FIFO
+    }
+
+    //________________Send Data__________________
+    sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
+    sx1272.writeRegister(REG_OP_MODE, LORA_TX_MODE);  // LORA mode - Tx
+
+    unsigned long exitTime = millis()+1400;//2 segundos para sair do for
+    unsigned long Time= millis();
+    char value = sx1272.readRegister(REG_IRQ_FLAGS);
+
+    while ((bitRead(value, 3) == 0) && (Time < exitTime)) {
+        value=sx1272.readRegister(REG_IRQ_FLAGS);
+        Time= millis();
+        wait_ms(50);
+    }
+
+    wait_ms(50);
+
+}
+
+void lora_send_data(int mode)  //data predefined
+{
+    led1=1;
+    sx1272.writeRegister(REG_OP_MODE,129);//standby
+    
+    
+
+    if (mode == DATA ) {
+        #ifdef  BME280  
+        sprintf(float_temp,"%2.2f",getTemperature());
+        sprintf(float_press,"%04.2f",getPressure());
+        sprintf(float_hum,"%2.2f",getHumidity());
+
+        //============= internet of turtles =============
+        sprintf(float_breathing_time,"%04.2f", last_breathing_time);
+        sprintf(float_diving_time,"%04.2f", last_diving_time);
+        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);
+        // ==============================================
+
+        //size=sprintf((char*)message,"\\!#%s°C_%shPa_%s%%",float_temp,float_press,float_hum);
+
+        lora_send_packet(message,r_size);
+//        sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR,/* (uint8_t*)*/message, r_size);
+        printf("packet send :\n%s\nrsize=%d\n",message,r_size);
+        #endif
+
+    } else if (mode == PING ) {
+
+
+        //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_ping, sizeof(message_ping));
+        lora_send_packet(message_ping,sizeof(message_ping));
+        printf("packet send :\n%s\n",message_ping);
+
+
+    } else if (mode == PONG )  {
+
+        //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_pong, sizeof(message_pong));
+        lora_send_packet(message_pong,sizeof(message_pong));
+        printf("packet send :\n%s\n",message_pong);
+    }
+
+    wait_ms(300);
+
+    sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
+    sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
+    cont++;
+    printf("number=%d\n",cont);
+
+    led1=0;
+}
+
+
+void lora_print_packet()
+{
+
+    led2=1;
+    sx1272.writeRegister(REG_OP_MODE,129);//standby
+
+    uint8_t pac_size;
+
+    sx1272.writeRegister(REG_FIFO_ADDR_PTR,sx1272.readRegister(REG_FIFO_RX_CURRENT_ADDR));//set fifo pointer to read packet
+    pac_size = sx1272.readRegister(REG_RX_NB_BYTES);//read size of packet
+    for(int i =0 ; i<pac_size ; i++) { //print packet
+        buffer[i]=sx1272.readRegister(REG_FIFO);
+        if (buffer[i]== '\n')  printf(" \\n");
+        if (buffer[i]== 0x0B)  printf("tab");
+        printf("%c",buffer[i]);//print packet
+    }
+    printf("\n");
+
+
+    for(int i =0 ; i<pac_size ; i++) { 
+        if ((buffer[i] =='P') && (buffer[i+1] =='i') && (buffer[i+2] =='n') && (buffer[i+3] =='g') ) { //look for Ping in packet
+            sx1272.writeRegister(0x12,255);//clear flags
+            lora_send_data(PONG);
+            i=pac_size;
+        }
+        if ((buffer[i] =='R') && (buffer[i+1] =='e') && (buffer[i+2] =='s') && (buffer[i+3] =='e') && (buffer[i+4] =='t')  ) {// look reset
+            uint32_t *ptr;
+            ptr = (uint32_t *)1073743132;// endereço gpregret 0x4000051C
+            *ptr = 0xB1;//BOOTLOADER_DFU_START
+            NVIC_SystemReset();
+        }
+       
+    }
+
+
+    wait_ms(30);
+    sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
+    sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
+    led2=0;
+
+}
+void lora_sleep(){
+    lora_mode_ant = sx1272.readRegister(REG_OP_MODE);
+    sx1272.writeRegister(REG_OP_MODE,LORA_SLEEP_MODE); //leitura continua
+    
+    }
+    
+int lora_wake(){
+    if (lora_mode_ant!= 0){
+        sx1272.writeRegister(REG_OP_MODE,lora_mode_ant); //leitura continua
+        return 0; 
+    }
+    else return -1;
+}
\ No newline at end of file