Marumbo Sichinga / Mbed 2 deprecated SmartWatchTopicDisplay

Dependencies:   mbed

Revision:
2:a128db827b61
Parent:
1:a06fd6ffdb3e
Child:
3:ab4d9bcf8dd1
--- a/main.cpp	Thu Jul 08 21:11:12 2021 +0000
+++ b/main.cpp	Fri Jul 09 20:27:22 2021 +0000
@@ -30,8 +30,13 @@
 
 static void send_message(int topicId, long int duration, string group) {
     uint8_t tx_buffer[50] = { 0 };
+    
+    char *deviceId = "My Device";
+    long int timestamp = 1234567892;
+    int value = 2;
+      sprintf((char*) tx_buffer, "{\"deviceId\":\"%s\",\"TopicId\":%d,\"Group\":%s}}", deviceId, topicId, group.c_str());
     // Sending strings over LoRaWAN is not recommended
-    sprintf((char*) tx_buffer, "{\"topicId\":%d, \"duration\":%ld,\"group\":\"%s\"}", topicId, duration, group.c_str());
+   // sprintf((char*) tx_buffer, "{\"topicId\":%d, \"duration\":%ld,\"group\":\"%s\"}", topicId, duration, group.c_str());
     int packet_len = strlen((char*) tx_buffer);
     printf("Sending %d bytes: \"%s\"\n", packet_len, tx_buffer);
     int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG);
@@ -39,22 +44,25 @@
     if (retcode < 0) {
         retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - duty cycle violation\n")
                 : printf("send() - Error code %d\n", retcode);
-        return;
+          return;
     }
+   
     printf("%d bytes scheduled for transmission\n", retcode);
+     callbacks.events = mbed::callback(lora_event_handler);
+    lorawan.add_app_callbacks(&callbacks);
 }
 
 int initialize_lora() {
     if (DEV_EUI[0] == 0x0 && DEV_EUI[1] == 0x0 && DEV_EUI[2] == 0x0 && DEV_EUI[3] == 0x0 && DEV_EUI[4] == 0x0 && DEV_EUI[5] == 0x0 && DEV_EUI[6] == 0x0 && DEV_EUI[7] == 0x0) {
         printf("Set your LoRaWAN credentials first!\n");
-       // return -1;
+        return -1;
     }
-    printf("Initializing Lora device!\n");
+    printf("Initializing LORA!\n");
     // Enable trace output for this demo, so we can see what the LoRaWAN stack does
     mbed_trace_init();
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
         printf("LoRa initialization failed!\n");
-       // return -1;
+        return -1;
     }  
         // prepare application callbacks
     callbacks.events = mbed::callback(lora_event_handler);
@@ -62,7 +70,7 @@
     // Disable adaptive data rating
     if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) {
         printf("disable_adaptive_datarate failed!\n");
-      //  return -1;
+        return -1;
     }
     lorawan.set_datarate(5); // SF7BW125
     lorawan_connect_t connect_params;
@@ -78,17 +86,17 @@
         retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
     } else {
         printf("Connection error, code = %d\n", retcode);
-       // return -1;
+        return -1;
     }
 
     printf("Connection - In Progress ...\r\n");
     // make your event queue dispatching events forever
     ev_queue.dispatch_forever();
     return 0;
-    
 }
 
 
+
 //declare lcd
 C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); 
 
@@ -96,6 +104,7 @@
 Sht31 sht31(I2C_SDA, I2C_SCL);
 
 //declare components
+//InterruptIn pushButton(BUTTON1); //p11
 InterruptIn pushButton(p11);
 DigitalOut whiteLed(p8);
 DigitalOut redLed(p9);
@@ -118,7 +127,7 @@
 
 Timer t;
 int direction = 0; // Forward is 0 False and Reverse 1true
-int current_topic;
+int current_topic = 0;;
 time_t loraTimerStart;
 time_t loraTimerStop;
 time_t buttonTimerStart;
@@ -196,14 +205,14 @@
 
 void NextTopic(){
     if(direction == 0){
-        if(current_topic >= 4){
+        if(current_topic == 4){
            current_topic = -1; 
         }
         current_topic++;
     }
     
     if(direction ==1){
-        if(current_topic < 0){
+        if(current_topic == 0){
             current_topic = 5;
         }
         current_topic --;
@@ -217,24 +226,24 @@
     printf("Duration Lora: %d \n", loraDuration); 
     printf("Duration Button: %d \n", buttonDuration); 
     
-    if(loraDuration> 7) {
+    if(loraDuration> 5) {
         //add lora sending function
         printf("Id: %d, duration: %d \n", current_topic, loraDuration );
-        send_message(current_topic, loraDuration, "Group2");
+        send_message(current_topic, loraDuration, "Grp2");
     }
     
-    if(buttonDuration < 5)
+    if(buttonDuration < 3)
     {
         //call next topic
         NextTopic();
     }
     
-    if(buttonDuration >= 5 && buttonDuration <= 9){
+    if(buttonDuration >= 3 && buttonDuration <= 5){
         direction = !direction; 
         NextTopic();
     }
     
-    if(buttonDuration > 9){
+    if(buttonDuration > 5){
         Display("Reset to factory ....");
         wait(10);
         WelcomeMessage(companyName);
@@ -265,14 +274,19 @@
 }
 
 void PrintContent(){
+loraTimerStart = 0;
+ loraTimerStop= 0;
+ buttonTimerStart = 0;
+buttonTimerStop = 0;
+    
     loraTimerStart = time(NULL);
     
     while(1){
         Display(content[current_topic][1]);
         wait(0.5);
         pushButton.mode(PullUp);
-        pushButton.fall(callback(&StartButtonTimer)); 
-        pushButton.rise(callback(&StopButtonTimer));
+        pushButton.fall(callback(&StopButtonTimer)); 
+        pushButton.rise(callback(&StartButtonTimer));
     }
 }
 
@@ -309,7 +323,8 @@
         blueLed = 0;
         direction = 0;
         current_topic = 0;
-        
+         printf("Mode 1... \n");
+        printf("current topic %s \n", content[current_topic][0].c_str());
         blink(&whiteLed);
         wait(0.5);
     }
@@ -333,6 +348,7 @@
         
         Display("Initializing ..............");
         initialize_lora();
+       
      
         WelcomeMessage(companyName);
       
@@ -340,13 +356,12 @@
     }
 }
 
-
+// Event handler
 static void lora_event_handler(lorawan_event_t event) {
     switch (event) {
         case CONNECTED:
             printf("Connection - Successful\n");
-             ev_queue.break_dispatch();
-            
+            ev_queue.break_dispatch();
             break;
         case DISCONNECTED:
             ev_queue.break_dispatch();
@@ -354,11 +369,13 @@
             break;
         case TX_DONE:
             printf("Message Sent to Network Server\n");
+            ev_queue.break_dispatch();
             break;
         case TX_TIMEOUT:
         case TX_ERROR:
         case TX_CRYPTO_ERROR:
         case TX_SCHEDULING_ERROR:
+            ev_queue.break_dispatch();
             printf("Transmission Error - EventCode = %d\n", event);
             break;
         case JOIN_FAILURE:
@@ -367,4 +384,4 @@
         default:
             MBED_ASSERT("Unknown Event");
     }
-}
+}
\ No newline at end of file