uwizeyimana abdulkarim / Mbed 2 deprecated mobileproject

Dependencies:   mbed

Revision:
4:51ec2290a0fc
Parent:
3:6076b262dc5e
Child:
5:97e965dd4bff
--- a/main.cpp	Sun Jul 11 08:19:38 2021 +0000
+++ b/main.cpp	Sun Jul 11 08:59:47 2021 +0000
@@ -32,7 +32,6 @@
 Ticker topicTicker;
 Ticker durationTime;
 Ticker resetted;
-//Ticker backagain;
 int direction=0;
 int i=0;
 int pushDuration;
@@ -77,13 +76,13 @@
 
 // LoRaWAN stack event handler
 static void lora_event_handler(lorawan_event_t event);
+//function to enable display to work
 void display(string sms){
     lcd.cls(); // change dane here
     lcd.locate(0,0);
     lcd.printf(sms.c_str());
 }
- 
- 
+ //function to display time and temp
 void displayState1(){
         float temp = theo.readTemperature();
         time_t seconds = time(NULL);
@@ -97,12 +96,12 @@
         lcd.locate(5, 9);
         lcd.printf("Temperature: %.2f C", temp);
 }
- 
- 
+ //function to blink white led
  void whiteled()
 {
     wed=!wed;
 }
+//message to display when in forward mode
 void forwards(){
    display(topics[i]);
    current=i;
@@ -111,6 +110,7 @@
        i=0;
    }
 }
+//message to display in backward mode
 void backwards(){
   display(topics[i]);
   current=i;
@@ -119,6 +119,7 @@
       i=4;
   }
 }
+//direction control function
 void displayState2(){
     printf("direction %d \n",direction);
      if(direction==0){
@@ -134,49 +135,38 @@
          topicTicker.attach(ev_queue.event(& backwards),2.0f);
      }
  }
- 
+ //message to call when button has been let go
   void fallBtn(){
      state = 0;
      durationTime.detach();
  
  }
+ //message to display after welcome message on the screen
  void mode1(){
     k.attach(ev_queue.event(& whiteled),1.0f);
      displayState1();
      t.attach(ev_queue.event(&displayState2),3.0f);
     
  }
+ //message to be displayed on the screen while on start up
  void welcomeMsg(){
     display(str2);
     t.attach(ev_queue.event(&mode1),5.0f);
 }
-  
+  //message to call when switch is rised
 void riseSwt(){
     time(&startReading);
      display(content[current]);
     topicTicker.detach();
-    
-    
 }
+// declaration messages to be sent on the cloud 
  static uint8_t* create_message(char device[], int topic, int duration) {
      // initialize the buffer
      uint8_t tx_buffer[50] = { 0 };
-     
-     // DATA TO SEND (THE BELOW VALUES ARE THE ONES TO BE MODIFIED)
-    //   char device[] = "infant";
-    //  int topic = 4; // topic identifier
-    //  long int start = 1606037871;  // start reading
-    //  long int end =   1606038090; // end reading
-     
-    // long int duration = 20;
-     
      // Writing string data into a buffer
      sprintf((char*) tx_buffer, "{\"device\":\"%s\",\"topic\":%d,\"duration\":%d }", device, topic,duration);
      return tx_buffer;
 }
-
-
-
 // Send a message over LoRaWAN
 static void send_message(uint8_t *tx_buffer) {
    
@@ -196,7 +186,7 @@
 
     printf("%d bytes scheduled for transmission\n", retcode);
 }
-
+//action to be processed when the button has been let go
 void fallSwt(){
     topicTicker.detach();
     time(&stopReading);
@@ -213,8 +203,7 @@
     }
     displayState2();    
 }
-
-
+//count how long the button have been pushed
 void countDuration(){
      time(&stopTime);
          pushDuration=stopTime-startTime;
@@ -225,25 +214,21 @@
              topicTicker.detach();
               direction=!direction;
             resetted.attach(ev_queue.event(&welcomeMsg),10.0f);
-            
-            //backagain.attach(callback(&mode1),15.0f);
-            
             }
         else if(pushDuration>=3 && state==0 ){
              state =1;
              printf("direction changed %d \n",direction);
              direction=!direction;
              displayState2();
-             
          }
  }
+ //action to be done when button is pushed
  void riseBtn(){
      time(&startTime);
      durationTime.attach(ev_queue.event(&countDuration),0.5f);
  
  }
 
- 
 int main() {
     
    
@@ -251,10 +236,6 @@
         printf("Set your LoRaWAN credentials first!\n");
         return -1;
     }
-
-    printf("Press BUTTON1 to send the current value of the temperature sensor!\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) {
@@ -298,6 +279,7 @@
     }
 
     printf("Connection - In Progress ...\r\n");
+    display("Connection - In Progress ...\r\n");
 
     // make your event queue dispatching events forever
     ev_queue.dispatch_forever();
@@ -332,12 +314,15 @@
     switch (event) {
         case CONNECTED:
             printf("Connection - Successful\n");
+            display("Connection - Successful\n");
+            wait_ms(1000);
                 welcomeMsg();
 
             break;
         case DISCONNECTED:
             ev_queue.break_dispatch();
-            printf("Disconnected Successfully\n");
+            printf("unable to connect\n");
+            display("unable to connect\n");
             break;
         case TX_DONE:
             printf("Message Sent to Network Server\n");