p2p-pingpong-clickbutton

Dependencies:   PingPong-P2P-ClickButtonToWork-SX1272 Lora_SX1272_Coragem

Files at this revision

API Documentation at this revision

Comitter:
afzalsamira
Date:
Wed Mar 17 20:14:13 2021 +0000
Parent:
5:ef21a65a3450
Commit message:
p2p-clickbuttonToPpingPong

Changed in this revision

Lora_SX1272_Coragem.lib Show annotated file Show diff for this revision Revisions of this file
SX1272.lib Show annotated file Show diff for this revision Revisions of this file
lora.txt Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ef21a65a3450 -r c305f2aa79a4 Lora_SX1272_Coragem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lora_SX1272_Coragem.lib	Wed Mar 17 20:14:13 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/marcoantonioara/code/Lora_SX1272_Coragem/#ef21a65a3450
diff -r ef21a65a3450 -r c305f2aa79a4 SX1272.lib
--- a/SX1272.lib	Wed Dec 04 11:50:35 2019 +0000
+++ b/SX1272.lib	Wed Mar 17 20:14:13 2021 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/marcoantonioara/code/SX1272/#f77a79f4239a
+https://os.mbed.com/users/afzalsamira/code/PingPong-P2P-ClickButtonToWork-SX1272/#d7c7e731da3a
diff -r ef21a65a3450 -r c305f2aa79a4 lora.txt
--- a/lora.txt	Wed Dec 04 11:50:35 2019 +0000
+++ b/lora.txt	Wed Mar 17 20:14:13 2021 +0000
@@ -1,7 +1,7 @@
 #include "SX1272.h"
 
 //#define BAND868
-#define MAX_DBM 14
+#define MAX_DBM 20
 #define DATA 0
 #define PING 1
 #define PONG 2
@@ -9,6 +9,7 @@
 
 #define ADDRESS 10
 
+//samira //const uint32_t DEFAULT_CHANNEL=CH_04_868;//to test
 const uint32_t DEFAULT_CHANNEL=CH_12_900;//915Mhz CH_12_900 =>0xE4C000;
 
 ///////////////////////////////////////////////////////////////////
@@ -32,10 +33,12 @@
 int e;
 
 //___________mensagem_____________________
-uint8_t message[255];
+uint8_t message[255];  // message[MESSAGELENGTH]
+                       
 uint8_t buffer[255];
 uint8_t message_pong[]="Pong";
 uint8_t message_ping[]="Ping";
+
 uint8_t r_size;
 char float_temp[10];
 char float_press[10];
@@ -51,7 +54,7 @@
 {
 
 
-    printf("------Coragem LoRa temperature sensor-------------\n");
+    printf("------Coragem LoRa temperature sensor -------------\n");
     //sx1272.ON();  // Power ON the module
 
     int error_config_sx1272=0;
@@ -105,6 +108,7 @@
 }
 void send_packet (uint8_t *payload, uint8_t length8)   // envia pacote
 {
+     printf("Samira - send_packet ( ) - ENTER..\n"); 
 
     //write on FIFO
     sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
@@ -114,6 +118,7 @@
     sx1272.writeRegister(REG_FIFO_ADDR_PTR,0x00);
 
     for(unsigned int i = 0; i <= length8; i++) {
+           printf("Samira - send_packet ( ) - FOR..\n"); 
         sx1272.writeRegister(REG_FIFO, payload[i]);  // Writing the payload in FIFO
     }
 
@@ -124,92 +129,134 @@
     unsigned long exitTime = millis()+2000;//2 segundos para sair do for
     unsigned long Time= millis();
     char value = sx1272.readRegister(REG_IRQ_FLAGS);
-
+/*
+int bitCounter = 0; 
     while ((bitRead(value, 3) == 0) && (Time < exitTime)) {
+        bitCounter ++;
+           printf("bitCounter = %d", bitCounter); 
+           printf("\n");
         value=sx1272.readRegister(REG_IRQ_FLAGS);
         Time= millis();
         wait_ms(50);
     }
-
-    wait_ms(50);
+  */  
+    printf("Samira - send_packet ( ) - OUT..\n"); 
+    //wait_ms(50);
 
 }
 
 void send_data(int mode)  //data predefined
 {
     led1=1;
+    led2=1;
+    printf("Samira - enter send_data () ..\n"); 
+    printf("number=%d\n",cont);
     sx1272.writeRegister(REG_OP_MODE,129);//standby
 
     if (mode == DATA ) {
+         printf("Samira - send_data (DATA) ..\n"); 
+         r_size=sprintf((char*)message,"RANDOM MESSAGE");
+         send_packet(message,r_size);
+         printf("packet send :\n%s\nrsize=%d\n",message,r_size);
 
-        sprintf(float_temp,"%2.2f",getTemperature());
-        sprintf(float_press,"%04.2f",getPressure());
-        sprintf(float_hum,"%2.2f",getHumidity());
+
+
+
+// //samira// it is sending sensors data 
+//        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);
+//        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);
 
-        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);
+ //       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);
+
 
     } else if (mode == PING ) {
 
-
+        printf("Samira - send_data (PING) ..\n"); 
         //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_ping, sizeof(message_ping));
         send_packet(message_ping,sizeof(message_ping));
-        printf("packet send :\n%s\n",message_ping);
+        printf("packet sent : %s ",message_ping);
+        printf("packet size : %d \n",sizeof(message_ping));//samira//
+        printf("waiting for POng ... \n");
 
 
     } else if (mode == PONG )  {
-
+        printf("sending a PONG ... ");
         //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_pong, sizeof(message_pong));
         send_packet(message_pong,sizeof(message_pong));
         printf("packet send :\n%s\n",message_pong);
     }
 
-    wait_ms(300);
+    led1=0;
+    led2=0;
+    //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 print_packet()
 {
 
-    led2=1;
+ printf("Samira - print_packet () Enter ..\n"); 
+    //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
+    printf("Samira - print_packet () pac_size = %d ..\n", pac_size);
     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("%c",buffer[i]);//print packet
     }
     printf("\n");
 
 
     for(int i =0 ; i<pac_size ; i++) { //look for Ping in packet
         if ((buffer[i] =='P') && (buffer[i+1] =='i') && (buffer[i+2] =='n') && (buffer[i+3] =='g') ) {
+            led1=1;
+            led2=1;
+            wait(1);
             sx1272.writeRegister(0x12,255);//clear flags
+            led1=0;
+            led2=0;
+            printf("There is a wait before sending PONG \n");
+            printf("sending PONG");
             send_data(PONG);
             i=pac_size;
         }
     }
+    
+        for(int i =0 ; i<pac_size ; i++) { //look for Pong in packet
+        if ((buffer[i] =='P') && (buffer[i+1] =='o') && (buffer[i+2] =='n') && (buffer[i+3] =='g') ) {
+            led1=1;
+            led2=1;
+            sx1272.writeRegister(0x12,255);//clear flags
+            printf("pong received \n");
+            wait(1);
+            led1=0;
+            led2=0;
+            
+            i=pac_size;
+        }
+    }
+
 
     for(int i =0 ; i<pac_size ; i++) { //look for Reset in packet
         if ((buffer[i] =='R') && (buffer[i+1] =='e') && (buffer[i+2] =='s') && (buffer[i+3] =='e') && (buffer[i+4] =='t')  ) {
@@ -224,5 +271,6 @@
     sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
     sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
     led2=0;
+     printf("Samira - print_packet () OUT ..\n"); 
 
 }
\ No newline at end of file
diff -r ef21a65a3450 -r c305f2aa79a4 main.cpp
--- a/main.cpp	Wed Dec 04 11:50:35 2019 +0000
+++ b/main.cpp	Wed Mar 17 20:14:13 2021 +0000
@@ -25,6 +25,10 @@
 char float_breathing_time[10];
 char float_diving_time[10];
 
+/////////////
+//tESTING INPUT AND OUTPUT
+//////////////
+
 //Timer seconds;
 //================================================
 
@@ -51,7 +55,7 @@
     // Configuration
     ///////////////////////////////////////
     
-    
+    printf(" Turning the LEDs on ..\n");
     led1=1;//----on leds---
     led2=1;
 
@@ -74,7 +78,8 @@
         
     sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
     sx1272.writeRegister(REG_IRQ_FLAGS_MASK,187);//configure interrupt mask to interrupt only when a packet receive and packet envied 
-  
+ 
+     printf("Turning the LEDs off ..\n"); 
     led1=0;//----off leds--- 
     led2=0;
     
@@ -86,13 +91,24 @@
     ///////////////////////////////////////
     
     while(1){
-        
+    
         if (!button1){
-            led2=1;
-            send_data(PING);
-            led2=0;
+            //test scanf
+            //led1=1;
+            //led2=1;
+            //char ch;
+           // printf("Enter one char"); 
+            //scanf("%c", &ch);
+          //  printf("%c\n",ch);
+
+           
+           printf("SENDING PING\n");
+              send_data(PING);
+          // send_data(DATA);      
+
+           
             }
-        
+     
 //         if (button3 != beathing) {
 //                beathing = !beathing;
 //                if(button3){
@@ -126,3 +142,57 @@
     }
 
 }
+
+/*
+
+////////
+//TESTING fREQUENCY CONFIGURATION
+////////////////
+
+
+
+#include "lora.txt"
+
+int main(void) {
+    
+     uint8_t config_message[] = "1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm01234567890123456789";
+    
+
+    led1=1;//----on leds---
+    led2=1;
+
+    //____Configuration bme280
+    bme_init();
+    
+    //____Configuration Lora    
+    Thread eventThread;
+    eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
+ 
+    dio0.rise(queue.event(&print_packet)); //configure interrupt rotine ro recieve packet
+    setup(); //configura sx1272
+          
+    sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
+    sx1272.writeRegister(REG_IRQ_FLAGS_MASK,187);//configure interrupt mask to interrupt only when a packet receive and packet envied 
+ 
+    led1=0;//----off leds--- 
+    led2=0;
+    
+    printf("PROGRAM STARTS\n"); 
+     while(1){
+                 
+                led1=1;
+                led2=1;
+                send_data(PING);
+                led2=0;
+                            
+ 
+//            if (button3){
+// //               send_data(DATA);
+////                send_packet(big_message,sizeof(big_message));
+////                send_packet(reset_message,sizeof(reset_message));
+////                wait_ms(400);
+                wait_ms(500);
+                }
+
+}
+*/
\ No newline at end of file