树莓派ADC采样光敏电阻来做智能家居控制雏形

又是ADC,还是ADS1115的模块,16bit采样率,很适合做模拟采样。接驳方式:

然后,刻录最新的raspbian镜像,可以从官方下载:https://www.raspberrypi.org/downloads/ 并将TF卡插回Raspberry Pi并打开电源。

请记住打开ssh服务并将你的Pi连接到互联网,以便我们可以下载并安装必要的软件包。

  • 打开终端编辑一个文件:
vim.tiny adc.c
  • 然后用下面的代码:
#include <stdio.h> 
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>

void main() 
{
// Create I2C bus
int file;
char *bus = "/dev/i2c-1";

 if ((file = open(bus, O_RDWR)) < 0)
 {
  printf("Failed to open the bus.\n");
  exit(1);
 }
// Get I2C device, ADS1115 I2C address is 0x48(72)
 ioctl(file, I2C_SLAVE, 0x48);
// Select configuration register(0x01)
// AINP = AIN1 and AINN = GND, +/- 2.048V
// Continuous conversion mode, 128 SPS(0x84, 0x83)
  char config[3] = {0};
  config[0] = 0x01;
  config[1] = 0xD4;
  config[2] = 0x83;
  write(file, config, 3);
  sleep(1);
  // Read 2 bytes of data from register(0x00)
  // raw_adc msb, raw_adc lsb
  char reg[1] = {0x00};
  write(file, reg, 1);
  char data[2]={0};
  if(read(file, data, 2) != 2)
   {
    printf("Error : Input/Output Error\n");
    }
    else
    {
    // Convert the data
    int raw_adc = (data[0] * 256 + data[1]);
    if (raw_adc > 32767)
    {
     raw_adc -= 65535;
     }
     // Output data to screen
     printf("Data: %d\n", raw_adc);
     }
} 

* 保存退出后编译:
 ```bash
gcc -o adc adc.c  -O3  
./adc
  • 执行后可以读取一次,用shell脚本简单调用一下,间隔0.2秒刷新,看看数据状态。
  • 然后简单写个脚本检测抓拍并上传到我的博客后台的服务器,当然了,现在已经关闭了,怕不法分子看到乱上传东西,就这么个意思吧,你们理解了操作步骤就好。然后把光敏电阻放在门夹缝的位置上,如果你没有回家,你家人没有回家,但是光敏电阻值发生了变化, 你就会收到一份邮件,拍到一个尝试盗窃的贼? 或者是一个不明飞行物?
    有兴趣就试试看吧,白了个白~