Seeed
Published © MIT

WioTerminal IoTs MultiSwitch

WioTerminal IoTs MultiSwitch using the IoTs technology to control three LED, also you are able to control by the buttons on the WioTerminal.

IntermediateFull instructions provided24 hours580

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1
Seeed Studio Seeed Wio Terminal Chassis - Battery (650mAh)
×1
Seeed Studio Grove - LED Pack
×3

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Wio terminal IoTs multiSwitch

C/C++
this is Wio terminal IoTs multiSwitch code
#include "defines.h"
#include "SinricPro_Generic.h"
#include "SinricProSwitch.h"

#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
TFT_eSprite spr = TFT_eSprite(&tft);  // Sprite

bool myPowerState1 = false;
bool myPowerState2 = false;
bool myPowerState3 = false;

int LED1_PIN = 2;
int LED2_PIN = 3;
int LED3_PIN = 4;

typedef struct
{
  int LED1_PIN = 2;
  int LED2_PIN = 3;
  int LED3_PIN = 4;
} deviceConfig_t;

// this is the main configuration
// please put in your deviceId, the PIN for Relay and PIN for flipSwitch
// this can be up to N devices...depending on how much pin's available on your device ;)
// right now we have 4 devicesIds going to 4 relays and 4 flip switches to switch the relay manually
std::map<String, deviceConfig_t> devices =
{
  //{deviceId, {relayPIN,  flipSwitchPIN}}
  // You have to set the pins correctly. This is for Nano 33 IoT
  { SWITCH_ID_1, {  2, 6 }},
  { SWITCH_ID_2, {  3, 7 }},
  { SWITCH_ID_3, {  4, 8 }},
  { SWITCH_ID_4, {  5, 9 }}
};

bool onPowerState1(const String &deviceId, bool &state)
{
  Serial.printf("Device 1 turned %s\r\n", state ? "on" : "off");
  int LED1_PIN = devices[deviceId].LED1_PIN; // get the relay pin for corresponding device
  digitalWrite(LED1_PIN, state);             // set the new relay state

  myPowerState1 = state;
  digitalWrite(LED1_PIN, myPowerState1 ? HIGH : LOW);
  spr.fillSprite(TFT_BLACK);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 5);
  spr.setTextColor(TFT_GREEN, TFT_BLACK);
  spr.drawString("conneted", 100 , 5);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Device1: ", 10, 65);
  spr.drawString("Device2: ", 10, 105);
  spr.drawString("Device3: ", 10, 145);
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  tft.drawString(state ? " turn on" : " turn off", 120 , 70, 4);
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state)
{
  Serial.printf("Device 2 turned %s\r\n", state ? "on" : "off");
  int LED2_PIN = devices[deviceId].LED2_PIN; // get the relay pin for corresponding device
  digitalWrite(LED2_PIN, state);             // set the new relay state

  myPowerState2 = state;
  digitalWrite(LED2_PIN, myPowerState2 ? HIGH : LOW);
  spr.fillSprite(TFT_BLACK);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 5);
  spr.setTextColor(TFT_GREEN, TFT_BLACK);
  spr.drawString("conneted", 100 , 5);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Device1: ", 10, 65);
  spr.drawString("Device2: ", 10, 105);
  spr.drawString("Device3: ", 10, 145);
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  tft.drawString(state ? " turn on" : " turn off", 120 , 110, 4);
  return true; // request handled properly
}

bool onPowerState3(const String &deviceId, bool &state)
{
  Serial.printf("Device 3 turned %s\r\n", state ? "on" : "off");
  int LED3_PIN = devices[deviceId].LED3_PIN; // get the relay pin for corresponding device
  digitalWrite(LED3_PIN, state);             // set the new relay state
  myPowerState3 = state;
  digitalWrite(LED3_PIN, myPowerState3 ? HIGH : LOW);
  spr.fillSprite(TFT_BLACK);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 5);
  spr.setTextColor(TFT_GREEN, TFT_BLACK);
  spr.drawString("conneted", 100 , 5);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Device1: ", 10, 65);
  spr.drawString("Device2: ", 10, 105);
  spr.drawString("Device3: ", 10, 145);
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  tft.drawString(state ? " turn on" : " turn off", 120 , 150, 4);
  return true; // request handled properly
}

void handleButtonPress()
{
  if (digitalRead(WIO_KEY_C) == LOW)
  {
    // is button pressed (inverted logic! button pressed = LOW) and debounced?
    if (myPowerState1)
    {
      // flip myPowerState: if it was true, set it to false, vice versa
      myPowerState1 = false;
      spr.fillSprite(TFT_BLACK);
      spr.setFreeFont(&FreeSansBoldOblique12pt7b);
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawString(" turn off", 120 , 70, 4);
    }
    else
    {
      myPowerState1 = true;
      spr.fillSprite(TFT_BLACK);
      spr.setFreeFont(&FreeSansBoldOblique12pt7b);
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawString(" turn on", 120 , 70, 4);
    }
    digitalWrite(LED1_PIN, myPowerState1 ? HIGH : LOW); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)
    // get Switch device back
    SinricProSwitch& mySwitch1 = SinricPro["put your first device ID"];
    // send powerstate event
    mySwitch1.sendPowerStateEvent(myPowerState1); // send the new powerState to SinricPro server
    Serial.print("Device ");
    Serial.print(mySwitch1.getDeviceId().toString());
    Serial.print(myPowerState1 ? "turned on" : "turn off");
    Serial.println(" (manually via flashbutton)");
  }

  if (digitalRead(WIO_KEY_B) == LOW)
  {
    // is button pressed (inverted logic! button pressed = LOW) and debounced?
    if (myPowerState2)
    {
      // flip myPowerState: if it was true, set it to false, vice versa
      myPowerState2 = false;
      spr.fillSprite(TFT_BLACK);
      spr.setFreeFont(&FreeSansBoldOblique12pt7b);
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawString(" turn off", 120 , 110, 4);
    }
    else
    {
      myPowerState2 = true;
      spr.fillSprite(TFT_BLACK);
      spr.setFreeFont(&FreeSansBoldOblique12pt7b);
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawString(" turn on", 120 , 110, 4);
    }
    digitalWrite(LED2_PIN, myPowerState2 ? HIGH : LOW); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)
    // get Switch device back
    SinricProSwitch& mySwitch2 = SinricPro["put your cecond device ID"];
    // send powerstate event
    mySwitch2.sendPowerStateEvent(myPowerState2); // send the new powerState to SinricPro server
    Serial.print("Device ");
    Serial.print(mySwitch2.getDeviceId().toString());
    Serial.print(myPowerState2 ? "turned on" : "turn off");
    Serial.println(" (manually via flashbutton)");
  }

  if (digitalRead(WIO_KEY_A) == LOW)
  {
    // is button pressed (inverted logic! button pressed = LOW) and debounced?
    if (myPowerState3)
    {
      // flip myPowerState: if it was true, set it to false, vice versa
      myPowerState3 = false;
      spr.fillSprite(TFT_BLACK);
      spr.setFreeFont(&FreeSansBoldOblique12pt7b);
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawString(" turn off", 120 , 150, 4);
    }
    else
    {
      myPowerState3 = true;
      spr.fillSprite(TFT_BLACK);
      spr.setFreeFont(&FreeSansBoldOblique12pt7b);
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawString(" turn on", 120 , 150, 4);
    }
    digitalWrite(LED3_PIN, myPowerState3 ? HIGH : LOW); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)
    // get Switch device back
    SinricProSwitch& mySwitch3 = SinricPro["put your third device ID"];
    // send powerstate event
    mySwitch3.sendPowerStateEvent(myPowerState3); // send the new powerState to SinricPro server
    Serial.print("Device ");
    Serial.print(mySwitch3.getDeviceId().toString());
    Serial.print(myPowerState3 ? "turned on" : "turn off");
    Serial.println(" (manually via flashbutton)");
  }
}

// setup function for WiFi connection
void setupWiFi()
{
  Serial.println("\n[Wifi]: Connecting");
  WiFi.begin("WIFI name", "WIFI password");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(250);
  }
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.print("\n[WiFi]: IP-Address is ");
  Serial.println(WiFi.localIP());

  spr.fillSprite(TFT_BLACK);
  spr.createSprite(280, 175);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 5);
  spr.setTextColor(TFT_GREEN, TFT_BLACK);
  spr.drawString("conneted", 100 , 5);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Device1: ", 10, 65);
  spr.drawString("Device2: ", 10, 105);
  spr.drawString("Device3: ", 10, 145);
  spr.pushSprite(0, 0);

}

void setupSinricPro()
{
  for (auto &device : devices)
  {
    const char *deviceId = device.first.c_str();
    SinricProSwitch& mySwitch1 = SinricPro["put your first device ID"];    //temp
    mySwitch1.onPowerState(onPowerState1);

    SinricProSwitch& mySwitch2 = SinricPro["put your cecond device ID"];    //light
    mySwitch2.onPowerState(onPowerState2);

    SinricProSwitch& mySwitch3 = SinricPro["put your thrid device ID"];    //humi
    mySwitch3.onPowerState(onPowerState3);
  }

  SinricPro.begin("fc3ac9fc-0985-42b1-9e92-dc390374b59c", "1cd018e4-c012-4d5a-8f51-0d0b9e12745d-523d8467-ea69-49ae-92fe-865537caca80");
  SinricPro.restoreDeviceStates(true);
}

void setup()
{
  Serial.begin(BAUD_RATE);
//  while (!Serial);

  pinMode(LED1_PIN, OUTPUT); // define LED GPIO as output
  pinMode(LED2_PIN, OUTPUT); // define LED GPIO as output
  pinMode(LED3_PIN, OUTPUT); // define LED GPIO as output

  pinMode(WIO_KEY_A, INPUT_PULLUP);
  pinMode(WIO_KEY_B, INPUT_PULLUP);
  pinMode(WIO_KEY_C, INPUT_PULLUP);
  pinMode(WIO_5S_UP, INPUT_PULLUP);
  pinMode(WIO_5S_DOWN, INPUT_PULLUP);
  pinMode(WIO_5S_LEFT, INPUT_PULLUP);
  pinMode(WIO_5S_RIGHT, INPUT_PULLUP);
  pinMode(WIO_5S_PRESS, INPUT_PULLUP);

  digitalWrite(LED1_PIN, LOW); // turn off LED on bootup
  digitalWrite(LED2_PIN, LOW); // turn off LED on bootup
  digitalWrite(LED3_PIN, LOW); // turn off LED on bootup

  tft.begin();
  tft.init();
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);
  spr.fillSprite(TFT_BLACK);
  spr.createSprite(280, 175);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 5);
  spr.setTextColor(TFT_RED, TFT_BLACK);
  spr.drawString("disconneted", 100 , 5);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Device1: ", 10, 65);
  spr.drawString("Device2: ", 10, 105);
  spr.drawString("Device3: ", 10, 145);
  spr.pushSprite(0, 0);

  setupWiFi();
  setupSinricPro();
}

void loop()
{
  SinricPro.handle();
  handleButtonPress();
}

Credits

Seeed

Seeed

102 projects • 168 followers
Seeed R&D Team

Comments