Seeed
Published © MIT

WioTerminal-IoTs-Dimmer

WioTerminal-IoTs-Dimmer using the IoTs technology to change the brightness of the LED on the Wio terminal.

IntermediateFull instructions provided12 hours441

Things used in this project

Hardware components

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

Software apps and online services

Arduino IDE
Arduino IDE
IFTTT Sinric Pro

Story

Read more

Code

Wio terminal IoTs Dimmer

C/C++
this is Wio terminal IoTs Dimmer code
#include "defines.h"
#include "SinricPro_Generic.h"
#include "SinricProDimSwitch.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

int LED_PIN = 0;
int val = 0;
bool powerState = false;

// we use a struct to store all states and values for our dimmable switch
struct
{
  int LED_PIN = 0;
  bool powerState = false;
  int powerLevel = 0;
} device_state;

bool onPowerState(const String &deviceId, bool &state)
{
  Serial.println("Device " + deviceId + " power turned " + String(state ? "on" : "off"));
  powerState = state;
  digitalWrite(LED_PIN, powerState ? HIGH : LOW);
  spr.fillSprite(TFT_BLACK);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);
  spr.fillRect(0, 0, 320, 45, TFT_BLUE); //Drawing rectangle with blue fill
  spr.setTextColor(TFT_WHITE); //Setting text color
  spr.drawString("LED Dimmer", 80, 10); //Drawing text string
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 55);
  spr.setTextColor(TFT_GREEN, TFT_BLACK);
  spr.drawString("conneted", 100 , 55);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Dimmer Power: ", 10, 95);
  spr.drawString("brightness: ", 10, 135);
  spr.drawString("%", 200, 135);
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  tft.drawString(String(state ? "on" : "off"), 195, 95, 4);

  if (powerState == HIGH) {
    val = 1;
  } else {
    val = 0;
  }
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel)
{
  device_state.powerLevel = powerLevel;
  analogWrite(LED_PIN, device_state.powerLevel);
  Serial.println("Device " + deviceId + " power level level changed to " + String(device_state.powerLevel));
  spr.fillSprite(TFT_BLACK);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);
  spr.fillRect(0, 0, 320, 45, TFT_BLUE); //Drawing rectangle with blue fill
  spr.setTextColor(TFT_WHITE); //Setting text color
  spr.drawString("LED Dimmer", 80, 10); //Drawing text string
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 55);
  spr.setTextColor(TFT_GREEN, TFT_BLACK);
  spr.drawString("conneted", 100 , 55);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Dimmer Power: ", 10, 95);
  spr.drawString("brightness: ", 10, 135);
  spr.drawString("%", 200, 135);

  if (val == 1) {
    if (device_state.powerLevel < 100 && device_state.powerLevel >= 10) {
      analogWrite(LED_PIN, powerLevel);
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawNumber(device_state.powerLevel, 150, 140, 4);
      tft.drawString("     ", 178, 140, 4);
    } else if (device_state.powerLevel < 10) {
      analogWrite(LED_PIN, powerLevel);
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawNumber(device_state.powerLevel, 150, 140, 4);
      tft.drawString("     ", 165, 140, 4);
    } else {
      tft.setTextColor(TFT_YELLOW, TFT_BLACK);
      tft.drawString("100", 150, 140, 4);
    }
  } else {
    tft.setTextColor(TFT_YELLOW, TFT_BLACK);
    tft.drawString("0", 150, 140, 4);
  }
  return true;
}


// setup function for WiFi connection
void setupWiFi()
{
  Serial.print("\n[Wifi]: Connecting");
  WiFi.begin("WIFI name", "WIFI password");   // put your WIFI 

  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(250);
  }
  Serial.print("[WiFi]: IP-Address is ");
  Serial.println(WiFi.localIP());
  spr.fillSprite(TFT_BLACK);
  spr.createSprite(320, 175);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);
  spr.fillRect(0, 0, 320, 45, TFT_BLUE); //Drawing rectangle with blue fill
  spr.setTextColor(TFT_WHITE); //Setting text color
  spr.drawString("LED Dimmer", 80, 10); //Drawing text string
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 55);
  spr.setTextColor(TFT_GREEN, TFT_BLACK);
  spr.drawString("conneted", 100 , 55);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Dimmer Power: ", 10, 95);
  spr.drawString("brightness: ", 10, 135);
  spr.drawString("%", 200, 135);
  spr.pushSprite(0, 0);
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  tft.drawString("0", 150, 140, 4);
  tft.drawString("off", 195, 95, 4);
}

void setupSinricPro()
{
  SinricProDimSwitch &myDimSwitch = SinricPro["device ID"];  // put device ID

  // set callback function to device
  myDimSwitch.onPowerState(onPowerState);
  myDimSwitch.onPowerLevel(onPowerLevel);

  // setup SinricPro
  SinricPro.onConnected([]()
  {
    Serial.println("Connected to SinricPro");
  });
  SinricPro.onDisconnected([]()
  {
    Serial.println("Disconnected from SinricPro");
  });
  SinricPro.begin("API KEY", "APP SECRET");    //Put your API key and secret
}

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

  pinMode(LED_PIN, OUTPUT); // define LED GPIO as output
  digitalWrite(LED_PIN, LOW); // turn off LED on bootup

  tft.begin();
  tft.init();
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);
  spr.fillSprite(TFT_BLACK);
  spr.createSprite(320, 175);
  spr.setFreeFont(&FreeSansBoldOblique12pt7b);

  spr.fillRect(0, 0, 320, 45, TFT_BLUE); //Drawing rectangle with blue fill
  spr.setTextColor(TFT_WHITE); //Setting text color
  spr.drawString("LED Dimmer", 80, 10); //Drawing text string
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("WIFI :", 10 , 55);
  spr.setTextColor(TFT_RED, TFT_BLACK);
  spr.drawString("disconneted", 100 , 55);
  spr.setTextColor(TFT_WHITE, TFT_BLACK);
  spr.drawString("Dimmer Power: ", 10, 95);
  spr.drawString("brightness: ", 10, 135);
  spr.drawString("%", 200, 135);
  spr.pushSprite(0, 0);

  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  tft.drawString("0", 150, 140, 4);
  tft.drawString("off", 195, 95, 4);

  Serial.println("\nStarting WIO_Terminal_DimSwitch on " + String(BOARD_NAME));
  Serial.println("Version : " + String(SINRICPRO_VERSION_STR));

  setupWiFi();
  setupSinricPro();
}

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

Credits

Seeed

Seeed

102 projects • 168 followers
Seeed R&D Team

Comments