2021 Aizhi Pioneer - the core of energy control for automated houses

preface

When winter comes, I wake up in the morning and don't bother to get out of bed, but for everyone's sake, I still resist the cold and risk my life to prepare this issue of articles for you. I wonder if this is worth your praise o(´ ^ ') O. of course, after so long, I know you won't praise it if you don't see the goods. I'd better do it first. You're free.

After so much foreshadowing, it's time to end! This must be the choice of stone door! In the previous articles, I completed the light sensor, remote light control switch, human body presence sensor and remote air conditioning remote control. Today, all the thin lines have been strung into one, and the plan has entered the final stage! Today we will take you to complete our automatic house energy control core!

My side has also transformed a room in my house, and the effect is still good. Air conditioning and lighting can be controlled automatically according to room personnel detection. Of course, in winter, many people may not want to get out of bed to switch on and off the lights or air conditioner. The application here also provides the function of manual mode to control the lights and air conditioner, so that you can also control the mobile phone when you play in the quilt.

Equipment explanation

The scene equipment involved has been introduced before. I won't introduce it too much here. Let's put the portal directly:

Light sensor

Light switch control

Human body detection sensor

Infrared learning module (air conditioner remote control)

Application explanation

It's too cold. It hurts to knock your hands. Here's a brief introduction.

The main purpose of this scenario is to reduce the power consumption at home (after all, there are several air conditioners and headlights at home)

Interested friends can go to: The secret treasure house of inspiration desktop Download the code.
Or directly git clone:

https://gitee.com/inspiration-desktop/eap-energy-conservation.git

The implementation idea of the application is roughly the following logic:

First, the light sensor will regularly obtain the latest real-time data, and then when the human body sensor detects the human body change, it will carry out logic processing. If there is no one in the room during the control time period, it will turn on and turn off the timer of air conditioning and light. During the timer period, if people appear in the room again, remove the timer processing (because someone may come back after going to the toilet). Otherwise, when the timer is triggered, there is still no one in the room. At this time, the air conditioner will be turned off first, and then the lighting sensor data will be compared to detect whether the current indoor lights are on. If so, go back and turn off all lights and equipment in the room.

Note: the light sensor needs to be used here. Because the light control equipment on my side cannot save the state, it needs to compare the light. At the same time, the effective time of the light control on my side is generally set at night, so it is still a little defective.

code analysis

Due to the complexity of this scenario, there are many points to pay attention to when writing code. Here is a brief talk about the code.

Like the previous series, it uses a Device module of JSRE, which is mainly used to obtain the information transmitted by the Device to Aizhi, and it is also the way for the application to send instructions to the Device through Aizhi.

First of all, the data of the light and temperature sensors here are not actively reported, but the logic of this part is shared by the AI Zhi application, so my device can be more portable. Therefore, a timer is added to the back end of my application to obtain the latest data status regularly.

...

switch (type) {   
  case 'illu-sensor':
    // illumination
    times[devid] = setInterval(() => {
      devManager.sendDeviceInfo(devid, { method: 'get', obj: ['illu'] });
    }, 5 * 1000);
  break;
  case 'temp-sensor':
    times[devid] = setInterval(() => {
      devManager.sendDeviceInfo(devid, { method: 'get', obj: ['temp'] });
    }, 30 * 60 * 1000);
  break;
  default:
  break;
}

...

After the data is obtained, the data will be processed accordingly. Due to the large amount of code, the processed code will be briefly described here;

After obtaining the values of light and temperature, synchronize the data to the front end and save the log at the same time;

The main logic is to control the air conditioner and light when the human body sensing change is detected. Here, the control time period processing and delayed response processing are required.

...

value = data.data['micro_state'] === 'ON';
// The status is stored in the log
if (data.method === 'report') {
   saveLog(room_id, 'operation', `Room[ ${room.name}]Medium detection ${value ? 'have' : 'no'}Man!`);
}
// Control air conditioning and lighting
if (value) {
   clearTimeout(roomTimes[`ac-${room_id}`]);
   clearTimeout(roomTimes[`light-${room_id}`]);
   roomTimes[`ac-${room_id}`] = undefined;
   roomTimes[`light-${room_id}`] = undefined;
} else {
   if (dev && roomTimes[`ac-${room_id}`] === undefined && roomTimes[`light-${room_id}`] === undefined) {
      const date = new Date();
      const curTimeMinute = date.getHours() * 60 + date.getMinutes();
      const {devids,settings: { ac_cp, light_cp }} = room;
      const ac_devids = []; // Online air conditioning equipment in the room
      const light_devids = []; // Online lighting equipment in the room
      devids.map((item) => {
        const d = devManager.devMap.get(item);
        if (d) {
          const type = getDeviceType(d);
          if (type === 'ac-control') {
            ac_devids.push(item);
          } else if (type === 'light-control') {
            light_devids.push(item);
          }
        }
      });
      if (ac_devids.length) {
        const ac_start_times = ac_cp[0].split(':');
        const ac_end_times = ac_cp[1].split(':');
        const ac_start_time_minute = Number(ac_start_times[0]) * 60 + Number(ac_start_times[1]);
        const ac_end_time_minute = Number(ac_end_times[0]) * 60 + Number(ac_end_times[1]);
        if (ac_start_time_minute <= ac_end_time_minute) {
          // If the start time is less than or equal to the end time, it means that it does not cross the zero point of the next day
          // Between start time and end time
          if (curTimeMinute >= ac_start_time_minute && curTimeMinute < ac_end_time_minute) {
            // During air conditioning control time
            // Turn off the air conditioner
            // Add timer delay
            roomTimes[`ac-${room_id}`] = setTimeout(() => {
              ac_devids.forEach((id) => {
                devManager.sendDeviceInfo(id, { method: 'set', ac: 'OFF' });
              });
              // saveLog(room_id, 'operation', ` the air conditioner in room [${room.name}] is turned off! `);
            }, 30 * 1000);
          }
        } else {
          // If the start time is greater than the end time, it means that the zero point of the next day is crossed
          // Go to start time - 23:59 and 00:00 - end time
          if (curTimeMinute > ac_start_time_minute || curTimeMinute < ac_end_time_minute) {
            // During air conditioning control time
            // Turn off the air conditioner
            // Add timer delay
            roomTimes[`ac-${room_id}`] = setTimeout(() => {
              ac_devids.forEach((id) => {
                devManager.sendDeviceInfo(id, { method: 'set', ac: 'OFF' });
              });
              // saveLog(room_id, 'operation', ` the air conditioner in room [${room.name}] is turned off! `);
            }, 30 * 1000);
          }
       }
     }
       
     if (light_devids.length) {
       const light_start_times = light_cp[0].split(':');
       const light_end_times = light_cp[1].split(':');
       const light_start_time_minute = Number(light_start_times[0]) * 60 + Number(light_start_times[1]);
       const light_end_time_minute = Number(light_end_times[0]) * 60 + Number(light_end_times[1]);
       if (light_start_time_minute <= light_end_time_minute) {
       // If the start time is less than or equal to the end time, it means that it does not cross the zero point of the next day
       // Between start time and end time
         if (curTimeMinute >= light_start_time_minute && curTimeMinute < light_end_time_minute) {
         // During lamp control time
         // Turn off the lights
         // Add timer delay
           roomTimes[`light-${room_id}`] = setTimeout(() => {
             if (roomRealValue[room_id] &&
                 roomRealValue[room_id]['illu'] &&
                 roomRealValue[room_id]['illu'] > 30
             ) {
               // If the illumination intensity is > 30, it is judged as on state
               light_devids.forEach((id) => {
                 devManager.sendDeviceInfo(id, { method: 'set', SW_ctrl: 'OFF' });
               });
               // Save operation log
               saveLog(room_id, 'operation', `Room[ ${room.name}]The lights are off!`, 'success');
             }
           }, 30 * 1000);
         }
       } else {
         // If the start time is greater than the end time, it means that the zero point of the next day is crossed
         // Go to start time - 23:59 and 00:00 - end time
         if (curTimeMinute > light_start_time_minute || curTimeMinute < light_end_time_minute) {
           // During lamp control time
           // Turn off the lights
           // Add timer delay
           roomTimes[`light-${room_id}`] = setTimeout(() => {
             if (roomRealValue[room_id] &&
                 roomRealValue[room_id]['illu'] &&
                 roomRealValue[room_id]['illu'] > 30
              ) {
                // If the illumination intensity is > 30, it is judged as on state
                light_devids.forEach((id) => {
                  devManager.sendDeviceInfo(id, { method: 'set', SW_ctrl: 'OFF' });
                });
                // Save operation log
                saveLog(room_id, 'operation', `Room[ ${room.name}]The lights are off!`, 'success');
              }
            }, 30 * 1000);
          }
        }
      }
   }
}

...

Scene demonstration

It's not convenient to show you the scene here. Let's directly show you the application scenario.

  • Room list home page

  • inside of the room

    The main purpose here is to add the equipment in the room. Because the logic has been written in the back-end service, the equipment will automatically take effect and run.

  • Room configuration

    Here, some basic information is mainly configured, such as room name, action time of human body induction control air conditioner and light.

  • journal

    Any application is inseparable from log. Of course, this is no exception. Here you can understand the real-time log information of each room in real time.

summary

Today, I integrated all the previous equipment together, and the effect is still good. It's the Savior of a poor man who often forgets to turn off the home appliances, doesn't want to get up and turn off the lights and air conditioning, but doesn't want to give up the electricity bill. Ha ha ha.

My hands have lost consciousness. It's too cold. Fortunately, I have two cats. When winter comes, I can just be a warm hand treasure. Let's finish it hastily today. I'll choose a warm hand treasure.

This article is only for personal learning and use. If there are errors, please correct them ˙ ᗜ ˙ ) Thank you, boss!

Tags: Javascript IoT arduino

Posted on Fri, 26 Nov 2021 02:08:07 -0500 by mjlogan