Sandwich IoT
Published © GPL3+

Prototype a BLE Smart Socket (MCU control)

The smart socket is a common smart device that can be used to remotely control the connected electrical appliances and check their real-time

BeginnerFull instructions provided6 hours334
Prototype a BLE Smart Socket (MCU control)

Things used in this project

Hardware components

Tuya Sandwich socket board
AC on/off control and energy monitoring. The board adopts the BPS BT01 power chip, Belling BL9037 metering chip, Belling BL1117-3.3 LDO chip, Hongfa relay, and other key components to implement the functions of metering and power cut
×1
NUCLEO-G071RB
Official development board with STM32G071RB MCU. Control socket on/off and module communication. The NUCLEO-G071RB development board supports the Arduino interface.
×1
Tuya Sandwich BLE MCU communication board (BT3L)
Onboard Tuya BT3L module implements networking functionality. The module has been burned with general firmware, and the MCU is connected to the Tuya serial port protocol to enable smart one-stop service of Tuya module, app, cloud, and more.
×1

Software apps and online services

The Tuya Smart app
https://developer.tuya.com/cn/docs/iot/tuya-smart-app-smart-life-app-advantages?id=K989rqa49rluq

Story

Read more

Code

Code snippet #1

Plain text
#include "bluetooth.h"

#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // RX, TX

#define _SS_MAX_RX_BUFF 300

#define relay 10

int time_cnt = 0, cnt = 0, init_flag = 0;


void setup() 
{
// put your setup code here, to run once:

pinMode(relay, OUTPUT);   // Initialize relay I/O 

digitalWrite(relay, LOW);

pinMode(PC13, INPUT);     // Initialize Bluetooth reset key

pinMode(8, OUTPUT);       // Initialize Bluetooth status indicator

mySerial.begin(9600);     // Initialize software serial port

mySerial.println("myserial init SUCCESS_ful!");

Serial.begin(115200);     //PA3 RX   PA2 TX

Serial.println("serial init SUCCESS_ful!");

bt_protocol_init();
}

void loop() 
{
// put your main code here, to run repeatedly:
if (init_flag == 0) 
  {
  time_cnt++;
  if (time_cnt % 6000 == 0) 
    {
    time_cnt = 0;
    cnt ++;
    }
  bt_stat_led(&cnt);   // Process Bluetooth status
  }
bt_uart_service();
myserialEvent();      // Process serial port receiving
key_scan();           // Detect pairing reset key
}

void myserialEvent() 
{
if (mySerial.available()) 
  {
  unsigned char ch = (unsigned char)mySerial.read();
  uart_receive_input(ch);
  }
}

void bt_stat_led(int *cnt)
{
#define bt_stat_led 8
switch (mcu_get_bt_work_state())
  {
  case 0x00:  // 0x00
  init_flag = 0;
    if (*cnt == 2) 
      {
      *cnt = 0;
      }
    if (*cnt % 2 == 0)  // LED flicker quickly
      { 
      digitalWrite(bt_stat_led, LOW);
      } 
    else
      {
      digitalWrite(bt_stat_led, HIGH);
      }
    break;

  case 0x01:  // 0x01
  init_flag = 0;
    if (*cnt >= 30) 
      {
      *cnt = 0;
      }
    if (*cnt  == 0)    // LED flickers slowly
      {
      digitalWrite(bt_stat_led, LOW);
      }
    else if (*cnt == 15)
      {
      digitalWrite(bt_stat_led, HIGH);
      }
    break;

  case 0x02:  // 0x02
  digitalWrite(bt_stat_led, HIGH); // LED is off
    break;

  case 0x03:  // 0x03
    break;

  case 0x04:  // 0x04
    if ( 0 == init_flag )
      {
      digitalWrite(bt_stat_led, LOW); //  LED is always on

      init_flag = 1;                  
      // The light is controllable with Bluetooth connected

      *cnt = 0;

      }
    break;


  default:
  digitalWrite(bt_stat_led, HIGH); 
  break; 
  } 
}

Code snippet #2

Plain text
#include "bluetooth.h"

#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // RX, TX

#define _SS_MAX_RX_BUFF 300

#define relay 10

int time_cnt = 0, cnt = 0, init_flag = 0;


void setup() 
{
// put your setup code here, to run once:

pinMode(relay, OUTPUT);   // Initialize relay I/O 

digitalWrite(relay, LOW);

pinMode(PC13, INPUT);     // Initialize Bluetooth reset key

pinMode(8, OUTPUT);       // Initialize Bluetooth status indicator

mySerial.begin(9600);     // Initialize software serial port

mySerial.println("myserial init SUCCESS_ful!");

Serial.begin(115200);     //PA3 RX   PA2 TX

Serial.println("serial init SUCCESS_ful!");

bt_protocol_init();
}

void loop() 
{
// put your main code here, to run repeatedly:
if (init_flag == 0) 
  {
  time_cnt++;
  if (time_cnt % 6000 == 0) 
    {
    time_cnt = 0;
    cnt ++;
    }
  bt_stat_led(&cnt);   // Process Bluetooth status
  }
bt_uart_service();
myserialEvent();      // Process serial port receiving
key_scan();           // Detect pairing reset key
}

void myserialEvent() 
{
if (mySerial.available()) 
  {
  unsigned char ch = (unsigned char)mySerial.read();
  uart_receive_input(ch);
  }
}

void bt_stat_led(int *cnt)
{
#define bt_stat_led 8
switch (mcu_get_bt_work_state())
  {
  case 0x00:  // 0x00
  init_flag = 0;
    if (*cnt == 2) 
      {
      *cnt = 0;
      }
    if (*cnt % 2 == 0)  // LED flicker quickly
      { 
      digitalWrite(bt_stat_led, LOW);
      } 
    else
      {
      digitalWrite(bt_stat_led, HIGH);
      }
    break;

  case 0x01:  // 0x01
  init_flag = 0;
    if (*cnt >= 30) 
      {
      *cnt = 0;
      }
    if (*cnt  == 0)    // LED flickers slowly
      {
      digitalWrite(bt_stat_led, LOW);
      }
    else if (*cnt == 15)
      {
      digitalWrite(bt_stat_led, HIGH);
      }
    break;

  case 0x02:  // 0x02
  digitalWrite(bt_stat_led, HIGH); // LED is off
    break;

  case 0x03:  // 0x03
    break;

  case 0x04:  // 0x04
    if ( 0 == init_flag )
      {
      digitalWrite(bt_stat_led, LOW); //  LED is always on

      init_flag = 1;                  
      // The light is controllable with Bluetooth connected

      *cnt = 0;

      }
    break;


  default:
  digitalWrite(bt_stat_led, HIGH); 
  break; 
  } 
}

Credits

Sandwich IoT

Sandwich IoT

40 projects • 4 followers

Comments