Seeed
Published

Wio Terminal-Based Simple Smart Home Central Control System

Wio Terminal is a highly integrated development board very suitable in the smart home control system. Author: Bopei Zheng

IntermediateShowcase (no instructions)2 days251
Wio Terminal-Based Simple Smart Home Central Control System

Things used in this project

Hardware components

Seeed Wio Terminal
×1
Grove - Temperature, Humidity, Pressure and Gas Sensor (BME680)
Seeed Grove - Temperature, Humidity, Pressure and Gas Sensor (BME680)
×1
Seeeduino V4.2
Seeeduino V4.2
×1

Software apps and online services

Arduino IDE
Arduino IDE
Djang
python
PyCharm

Story

Read more

Code

Full code

C/C++
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include"LIS3DHTR.h"
#include"Free_Fonts.h"
#include"TFT_eSPI.h"


TFT_eSPI tft;
LIS3DHTR<TwoWire> lis;
WiFiClient client;

const char* ssid     = "zh213";
const char* password = "zh213wei";
const char*  server = "192.168.1.102";  // Server URL
String data;
float accelerator_readings[3];

 
void setup() {
  
    //Initialize serial and wait for port to open:
    Serial.begin(115200);
    delay(100);

    pinMode(WIO_MIC, INPUT);
    pinMode(WIO_KEY_A, INPUT_PULLUP);
    pinMode(WIO_KEY_B, INPUT_PULLUP);
    pinMode(WIO_KEY_C, INPUT_PULLUP);
 
    lis.begin(Wire1);
    lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ);
    lis.setFullScaleRange(LIS3DHTR_RANGE_2G);

    float x_raw = lis.getAccelerationX();
    float y_raw = lis.getAccelerationY();
    float z_raw = lis.getAccelerationZ();
    accelerator_readings[0] = x_raw; //store x-axis readings
    accelerator_readings[1] = y_raw; //store y-axis readings
    accelerator_readings[2] = z_raw; //store z-axis readings
 
//    Serial.print("Attempting to connect to SSID: ");
//    Serial.println(ssid);
    WiFi.begin(ssid, password);
 
    tft.begin();
    tft.setRotation(3);
    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FMB12);
    tft.setCursor((320 - tft.textWidth("Connecting to Wi-Fi.."))/2, 120);
    tft.print("Connecting to Wi-Fi..");
 
    // attempt to connect to Wifi network:
    while (WiFi.status() != WL_CONNECTED) {
//        Serial.print(".");
        // wait 1 second for re-trying
        delay(1000);
    }
 
//    Serial.print("Connected to ");
//    Serial.println(ssid);
 
    tft.fillScreen(TFT_BLACK);
    tft.setCursor((320 - tft.textWidth("Connected!"))/2, 120);
    tft.print("Connected!");
 
    getFirstData();
}
 
void loop()
{
    int val_first = analogRead(WIO_MIC);
    float x_raw = lis.getAccelerationX();
    float y_raw = lis.getAccelerationY();
    float z_raw = lis.getAccelerationZ();
    int val_next = analogRead(WIO_MIC);

    if (abs(val_first - val_next) >= 100){
      Serial.println("send message!");
      }
    if (digitalRead(WIO_KEY_A) == LOW) {
      Serial.println("A Key pressed");
     }
    if (digitalRead(WIO_KEY_B) == LOW) {
      Serial.println("B Key pressed");
     }
    if (digitalRead(WIO_KEY_C) == LOW) {
      Serial.println("C Key pressed");
     }
    
    if (abs(accelerator_readings[0] - x_raw) >= 0.1 && abs(accelerator_readings[1] - y_raw) >= 0.1 && abs(accelerator_readings[2] - z_raw) >= 0.1){
      // Turning on the LCD backlight
      digitalWrite(LCD_BACKLIGHT, HIGH);
      getFirstData();
      delay(3000);
      getLastData();
      delay(3000);
    }
    else {
      // Turning off the LCD backlight
      digitalWrite(LCD_BACKLIGHT, LOW);
      delay(500);
      }
      
    for (uint8_t i = 0; i<3; i++){
        accelerator_readings[i] = 0.0; //this is used to remove the first read variable
      }
    
    accelerator_readings[0] = x_raw; //store x-axis readings
    accelerator_readings[1] = y_raw; //store y-axis readings
    accelerator_readings[2] = z_raw; //store z-axis readings
}
 
void getFirstData() {
//    Serial.println("\nStarting connection to server...");
    if (!client.connect(server, 9000)) {
//        Serial.println("Connection failed!");
        tft.fillScreen(TFT_BLACK);
        tft.setCursor((320 - tft.textWidth("Connection failed!"))/2, 120);
        tft.print("Connection failed!");
    } else {
//        Serial.println("Connected to server!");
 
        // Make a HTTP request:
        String postRequest =(String)("GET ") + "/ HTTP/1.1\r\n" + "Connection: close\r\n\r\n";  
//        Serial.println(postRequest);  
        client.print(postRequest);

        while (client.connected()) {
            String line = client.readStringUntil('\n');
            if (line == "\r") {
//                Serial.println("headers received");
                break;
            }
        }
 
        while(client.available())
        {
          String line = client.readStringUntil('\r');
          data = line;
        }
//        Serial.println(data);
        client.stop();
//        Serial.println("closing connection");
    }
 
    //ArduinoJson to parse data, plesae check ArduinoJson for more info
    const size_t capacity = JSON_OBJECT_SIZE(5) + 100;
    DynamicJsonDocument doc(capacity);
    deserializeJson(doc, data);
 
    float temperature = doc["temperature"];
    float pressure = doc["pressure"];
    float humidity = doc["humidity"];
 
// -----------------LCD---------------------
    tft.setFreeFont(FF17);
    tft.setTextColor(tft.color565(224,225,232));
    tft.drawString("Current Data At Home",20,10);
 
    tft.fillRoundRect(10, 45, 300, 55, 5, tft.color565(40,40,86));
    tft.fillRoundRect(10, 105, 300, 55, 5, tft.color565(40,40,86));
    tft.fillRoundRect(10, 165, 300, 55, 5, tft.color565(40,40,86));
 
    tft.setFreeFont(FM9);
    tft.drawString("temperature:", 75, 50);
    tft.drawString("pressure:",75, 110);
    tft.drawString("humidity:",75, 170);
 
    tft.setFreeFont(FMB12);
    tft.setTextColor(TFT_RED);
    tft.drawFloat(temperature,2 , 140, 75);
    tft.setTextColor(tft.color565(224,225,232));
    tft.drawFloat(pressure,2 , 140, 135);
    tft.setTextColor(TFT_GREEN);
    tft.drawFloat(humidity,2 , 140, 195);

    tft.drawString("", 210, 75);
    tft.drawString("KPa",210, 135);
    tft.drawString("%",210, 195);
}

void getLastData() {
//    Serial.println("\nStarting connection to server...");
    if (!client.connect(server, 9000)) {
//        Serial.println("Connection failed!");
        tft.fillScreen(TFT_BLACK);
        tft.setCursor((320 - tft.textWidth("Connection failed!"))/2, 120);
        tft.print("Connection failed!");
    } else {
//        Serial.println("Connected to server!");

        // Make a HTTP request:
        String postRequest =(String)("GET ") + "/ HTTP/1.1\r\n" + "Connection: close\r\n\r\n";  
//        Serial.println(postRequest);  
        client.print(postRequest);

        while (client.connected()) {
            String line = client.readStringUntil('\n');
            if (line == "\r") {
//                Serial.println("headers received");
                break;
            }
        }
 
        while(client.available())
        {
          String line = client.readStringUntil('\r');
          data = line;
        }
//        Serial.println(data);
        client.stop();
//        Serial.println("closing connection");
    }
 
    //ArduinoJson to parse data, plesae check ArduinoJson for more info
    const size_t capacity = JSON_OBJECT_SIZE(5) + 100;
    DynamicJsonDocument doc(capacity);
    deserializeJson(doc, data);

    float humidity = doc["humidity"];
    float gas = doc["gas"];
    String updataTime = doc["updataTime"];
 
// -----------------LCD---------------------
    tft.setFreeFont(FF17);
    tft.setTextColor(tft.color565(224,225,232));
    tft.drawString("Current Data At Home",20,10);
 
    tft.fillRoundRect(10, 45, 300, 55, 5, tft.color565(40,40,86));
    tft.fillRoundRect(10, 105, 300, 55, 5, tft.color565(40,40,86));
    tft.fillRoundRect(10, 165, 300, 55, 5, tft.color565(40,40,86));
 
    tft.setFreeFont(FM9);
    tft.drawString("humidity:", 75, 50);
    tft.drawString("gas:",75, 110);
    tft.drawString("updataTime:",75, 170);
 
    tft.setFreeFont(FMB12);
    tft.setTextColor(TFT_RED);
    tft.drawFloat(humidity,2 , 140, 75);
    tft.setTextColor(tft.color565(224,225,232));
    tft.drawFloat(gas,2 , 140, 135);
    tft.setTextColor(TFT_GREEN);
    tft.drawString(updataTime , 30, 195);

    tft.drawString("%", 210, 75);
    tft.drawString("Kohms",210, 135);
}

Wio terminal code

Python
import serial
import re

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")

def send():
    #  SMTP 
    mail_host="smtp.qq.com"  #
    mail_user="2733821739@qq.com"    #
    mail_pass="oycokbuorrvhddji"   #

    sender = '2733821739@qq.com'
    receivers = ['2733821739@qq.com']  # QQ

    #
    message = MIMEMultipart()
    message['From'] = Header("Wio Terimal", 'utf-8')
    message['To'] =  Header("", 'utf-8')
    subject = ''
    message['Subject'] = Header(subject, 'utf-8')

    #
    message.attach(MIMEText('', 'plain', 'utf-8'))

    #  test.txt 
    att = MIMEText(open('D:\TemperatureHumidityPressureGasData.txt', 'rb').read(), 'base64', 'utf-8')
    att["Content-Type"] = 'application/octet-stream'
    # filename
    att["Content-Disposition"] = 'attachment; filename="TemperatureHumidityPressureGasData.txt"'
    message.attach(att)
    server = smtplib.SMTP_SSL(mail_host, 465) # SMTP25
    server.set_debuglevel(1)
    server.login(mail_user, mail_pass)

    try:
        server.sendmail(sender, receivers, message.as_string())
        print ("")
        speaker = win32com.client.Dispatch("SAPI.SpVoice")
        text = "Message sent successfully"
        speaker.Speak(text)
    except smtplib.SMTPException:
        print ("Error: ")




infrared_ser = serial.Serial('COM10', 9600, timeout=0.2)
Wio_terminal = serial.Serial('COM14', 115200, timeout=0.2)

# 
while True:
    strs = Wio_terminal.readline().decode('utf-8')
    if strs.strip()!='':
        print(strs)
        if (re.match(r"C",strs)):
            send_data = 'FD FD 30 03 53 4B 00 34 17 01 3B 02 65 00 26 00 1E 00 27 00 D9 09 26 00 8A 00 40 02 C3 17 26 00 00 00 21 00 FF FF FF FF 01 22 22 22 22 11 11 11 11 12 11 22 22 21 22 11 13 45 46 F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 76 00 22 DF DF'
            send_data = bytes.fromhex(send_data)
            infrared_ser.write(send_data)
            text = "OK executed"
            speaker.Speak(text)
        elif (re.match(r"B",strs)):
            send_data = 'FD FD 30 03 52 47 00 34 16 01 3A 02 66 00 27 00 20 00 27 00 D9 09 25 00 8A 00 41 02 00 00 21 00 FF FF FF FF FF FF FF FF 01 22 22 22 22 11 11 11 12 21 11 22 21 12 22 11 13 45 F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 76 3F 6D DF DF '
            send_data = bytes.fromhex(send_data)
            infrared_ser.write(send_data)
            text = "Brightness up"
            speaker.Speak(text)
        elif (re.match(r"A",strs)):
            send_data = 'FD FD 30 03 53 4B 00 34 16 01 3C 02 63 00 27 00 1F 00 27 00 DA 09 25 00 8B 00 3D 02 C4 17 24 00 00 00 20 00 FF FF FF FF 01 22 22 22 22 11 11 11 12 11 11 22 21 22 22 11 13 45 46 F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 76 3F 2E DF DF '
            send_data = bytes.fromhex(send_data)
            infrared_ser.write(send_data)
            text = "Brightness down"
            speaker.Speak(text)
        elif (re.match(r"send",strs)):
            try:
                send()
            except:
                text = "Failed to send mail. Please try again later"
                speaker.Speak(text)


infrared_ser.close()
Wio_terminal.close()

Credits

Seeed

Seeed

59 projects • 74 followers
Seeed R&D Team
Thanks to Bopei Zheng.

Comments

Add projectSign up / Login