#WioTerminal – Open / Close the #DigitalTwin Door πŸšͺ using Wio Terminal buttons and Azure IoT ☁️ + Azure Functions βš‘

Hi !

My current goal is simple:

Press Wio Terminal button A will open the Azure IoT Door, press B button will close the door.

press button A on WioTerminal open the Azure IoT Door

A demo video is available here:

I already wrote about how to use the buttons, however, let’s refresh this.

Using Wio Terminal Buttons

We need to initialize the buttons, usually in setup(), and in the loop() we can check for the press status of each button.

void setup() {
  Serial.begin(115200);
  // init buttons
  Serial.println("Init buttons");  
  pinMode(WIO_KEY_A, INPUT_PULLUP);
  pinMode(WIO_KEY_B, INPUT_PULLUP);
  pinMode(WIO_KEY_C, INPUT_PULLUP);
}

void loop(){

  if (digitalRead(WIO_KEY_A) == LOW) 
  { 
    Serial.println("[A] open door");  
  }
  if (digitalRead(WIO_KEY_B) == LOW) 
  { 
    Serial.println("[B ] close door");     
  }
  if (digitalRead(WIO_KEY_C) == LOW) 
  {
    Serial.println("[C] refresh");     
  }  
}

It’s easy, it follows the Arduino style.

Perform a HTTP Post to an Azure Function ⚑from Wio Terminal

Now it’s time to make a HTTP POST to our Azure Function. I already wrote on how to perform a HTTP GET call. This is similar.

  • This function is invoked from loop().
  • Lines 3 to 20 are checking which button was pressed.
  • Line 23 implement a routine to only call this every N seconds.
    Important: the loop() will be executed several times per seconds. I don’t want N calls to the Azure Function.
  • Lines 27 to 43, perform the HTTP POST call and free resources.
void validateWioButtons(){

  if (digitalRead(WIO_KEY_A) == LOW) 
  { 
    Serial.println("Button A Pressed, open door");     
    url = urlOpen;
    performHttpPost = true;
  }
  if (digitalRead(WIO_KEY_B) == LOW) 
  { 
    Serial.println("Button B Pressed, close door");     
    url = urlClose;
    performHttpPost = true;
  }
  if (digitalRead(WIO_KEY_C) == LOW) 
  {
    Serial.println("[C] refresh");     
    lastTime = 0;
  }  

  // validate last time http post call  
  if ((millis() - lastTimeHttpPost ) > timerDelayHttpPost) {  
    if(performHttpPost == true){    
      performHttpPost = false;      
    
      http.end();  
      http.setTimeout(3000);  
      http.begin(url);
    
      int httpResponseCode = http.POST();
     
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
      lastTime = 0;
    }

    // update last time http post call
    lastTimeHttpPost = millis();
    performHttpPost = false;
  }
}
 

One more time, this is super easy.

Happy coding!

Greetings

El Bruno


References

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: