Seeed
Published © MIT

WioTerminal IoTs Temp&Humi display

The Temperature and humidity data can upload to could via the Wio terminal.

IntermediateFull instructions provided6 hours2,376
WioTerminal IoTs Temp&Humi display

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 - Temperature&Humidity Sensor (DHT11)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Wio terminal IoTs temp&humi

C/C++
Wio terminal IoTs temp&humi
#include "defines.h"

#include "SinricPro_Generic.h"
#include "SinricProTemperaturesensor.h"

#define USING_LCD     false
#if USING_LCD
#include <LovyanGFX.hpp>              // https://github.com/lovyan03/LovyanGFX
LGFX lcd;
LGFX_Sprite spr = LGFX_Sprite(&lcd);
#define tft lcd
#else

#include <TFT_eSPI.h>                 // https://github.com/Bodmer/TFT_eSPI
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI();            // Invoke custom library
TFT_eSprite spr = TFT_eSprite(&tft);  // Sprite
TFT_eSprite spr2 = TFT_eSprite(&tft);  // Sprite
#endif

DHT dht(0, DHT11);

bool  deviceIsOn;                               // Temeprature sensor on/off state
float temperature = 0;                              // actual temperature
float humidity = 0;                                 // actual humidity
float lastTemperature;                          // last known temperature (for compare)
float lastHumidity;                             // last known humidity (for compare)
unsigned long lastEvent = (-EVENT_WAIT_TIME);   // last time event has been sent

bool onPowerState(const String &deviceId, bool &state)
{
  Serial.println("TemperatureSensor turned " + String(state ? "on" : "off"));
  deviceIsOn = state; // turn on / off temperature sensor
  return true; // request handled properly
}

void handleTemperaturesensor()
{
  // device is off...do nothing
  if (deviceIsOn == false)
    return;

  unsigned long actualMillis = millis();

  if (actualMillis - lastEvent < EVENT_WAIT_TIME)
    return; //only check every EVENT_WAIT_TIME milliseconds

  temperature = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //  temperature = dht.readTemperature(true);
  humidity = dht.readHumidity();                          // get actual humidity

  // Serial.print("1111111111");
  // Serial.println(temperature);
  // Serial.println(humidity);
  // Serial.print("22222222222");

  //  spr2.fillSprite(TFT_BLACK);
  //  spr2.createSprite(280, 60);
  //  spr2.setFreeFont(&FreeSansBoldOblique12pt7b);
  //  spr2.setTextColor(TFT_WHITE);
  //  spr2.drawNumber(temperature, 40 , 0);
  //  spr2.drawNumber(humidity, 220 , 0);
  //  spr2.pushSprite(0, 110);


  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.drawNumber(temperature, 40 , 110, 4);
  tft.drawNumber(humidity, 220 , 110, 4);



  if (isnan(temperature) || isnan(humidity))
  {
    // reading failed...
    Serial.println("DHT reading failed");       // print error message
    return;                                     // try again next time
  }

  // Check if any reads failed and exit early (to try again).
  if (temperature == lastTemperature || humidity == lastHumidity)
    return;

  SinricProTemperaturesensor &mySensor = SinricPro["DEVICE ID"];     // get temperaturesensor device
  bool success = mySensor.sendTemperatureEvent(temperature, humidity);  // send event

  if (success)
  {
    // if event was sent successfuly, print temperature and humidity to serial
    Serial.println("Temperature: " + String(temperature, 1) + " Celsius\tHumidity: " + String(humidity, 1) + " %");
  }
  else
  {
    // if sending event failed, print error message
    Serial.println("Something went wrong...Could not send Event to server!");
  }

  lastTemperature = temperature;  // save actual temperature for next compare
  lastHumidity = humidity;        // save actual humidity for next compare
  lastEvent = actualMillis;       // save actual time for next compare


}


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

  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(250);
  }
  Serial.print("[WiFi]: IP-Address is ");
  Serial.println(WiFi.localIP());
}

// setup function for SinricPro
void setupSinricPro()
{
  // add device to SinricPro
  SinricProTemperaturesensor &mySensor = SinricPro["DEVICE ID"];
  mySensor.onPowerState(onPowerState);

  // setup SinricPro
  SinricPro.onConnected([]()
  {
    Serial.println("Connected to SinricPro");

    //    spr.createSprite(320, 50);
    spr.fillSprite(TFT_BLACK);
    spr.setFreeFont(&FreeSansBoldOblique18pt7b);
    spr.setTextColor(TFT_BLUE);
    spr.drawString("Temp&humi IoTs", 60 - 40, 10 , 1);// Print the test text in the custom font
    for (int8_t line_index = 0; line_index < 5 ; line_index++)
    {
      spr.drawLine(0, 50 + line_index, 320, 50 + line_index, TFT_GREEN);
    }
    spr.setFreeFont(&FreeSansBoldOblique9pt7b);
    spr.setTextColor(TFT_GREEN);
    spr.drawString("Temp:", 60 - 24, 100 - 24 , 1); // Print the test text in the custom font
    spr.drawRoundRect(60 - 24, 100, 80, 40, 5, TFT_WHITE);
    spr.setTextColor(TFT_WHITE);
    //  spr.drawNumber(val, 60 - 20, 100 + 10, 1);
    spr.setTextColor(TFT_GREEN);
    spr.drawString("C", 60 + 32, 100 + 8, 1);

    spr.drawString("Humi:", 230 - 24 , 100 - 24 , 1); // Print the test text in the custom font
    spr.drawRoundRect(230 - 24, 100, 80, 40, 5, TFT_WHITE);
    spr.setTextColor(TFT_WHITE);
    //  spr.drawNumber(val, 230 - 20, 100 + 10, 1);
    spr.setTextColor(TFT_GREEN);
    spr.drawString("%", 230 + 32, 100 + 8, 1);
    tft.setTextColor(TFT_WHITE, TFT_BLACK);
    tft.drawNumber(temperature, 40 , 110, 4);
    tft.drawNumber(humidity, 220 , 110, 4);
    spr.pushSprite(0, 0);

  });

  SinricPro.onDisconnected([]()
  {
    Serial.println("Disconnected from SinricPro");
    tft.setTextColor(TFT_WHITE, TFT_BLACK);
    tft.drawNumber(temperature, 40 , 110, 4);
    tft.drawNumber(humidity, 220 , 110, 4);
  });

  SinricPro.begin("app KEY", "APP SECRET");

  SinricPro.restoreDeviceStates(true); // get latest known deviceState from server (is device turned on?)
}

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

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

  dht.begin();

  tft.begin();
  tft.init();
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);
  //  spr.createSprite(tft.width(), tft.height());
  spr.createSprite(320, 140);
  spr.fillSprite(TFT_BLACK);
  spr.setFreeFont(&FreeSansBoldOblique18pt7b);
  spr.setTextColor(TFT_BLUE);
  spr.drawString("Temp&humi IoTs", 60 - 40, 10 , 1);// Print the test text in the custom font
  for (int8_t line_index = 0; line_index < 5 ; line_index++)
  {
    spr.drawLine(0, 50 + line_index, 320, 50 + line_index, TFT_RED);
  }

  spr.setFreeFont(&FreeSansBoldOblique9pt7b);
  spr.setTextColor(TFT_GREEN);
  spr.drawString("Temp:", 60 - 24, 100 - 24 , 1); // Print the test text in the custom font
  spr.drawRoundRect(60 - 24, 100, 80, 40, 5, TFT_WHITE);
  spr.setTextColor(TFT_WHITE);
  //  spr.drawNumber(val, 60 - 20, 100 + 10, 1);
  spr.setTextColor(TFT_GREEN);
  spr.drawString("C", 60 + 32, 100 + 8, 1);

  spr.drawString("Humi:", 230 - 24 , 100 - 24 , 1); // Print the test text in the custom font
  spr.drawRoundRect(230 - 24, 100, 80, 40, 5, TFT_WHITE);
  spr.setTextColor(TFT_WHITE);
  //  spr.drawNumber(val, 230 - 20, 100 + 10, 1);
  spr.setTextColor(TFT_GREEN);
  spr.drawString("%", 230 + 32, 100 + 8, 1);

  spr.pushSprite(0, 0);


  setupWiFi();
  setupSinricPro();
}

void loop()
{
  SinricPro.handle();
  handleTemperaturesensor();
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.drawNumber(lastTemperature, 40 , 110, 4);
  tft.drawNumber(lastHumidity, 220 , 110, 4);
}

Credits

Seeed

Seeed

102 projects • 168 followers
Seeed R&D Team

Comments