Article Index

Page 224

void setup() {
  Serial.begin(9600);
}

void loop() {
  pinMode(2, OUTPUT);
  digitalWrite(2, 1);
  delay(1000);
  digitalWrite(2, 0);
  delay(1);
  pinMode(2, INPUT);
  while (digitalRead(2) == 1) {};
  while (digitalRead(2) == 0) {};
  while (digitalRead(2) == 1) {};

  int64_t t2;
  uint32_t data = 0;
  int64_t t1 = micros();
  for (int i = 0; i < 32; i++) {
    while (digitalRead(2) == 0) {
    };
    while (digitalRead(2) == 1) {
    };

    t2 = micros();
    data = data << 1;
    data = data | ((t2 - t1) > 100);
    t1 = t2;
  }

  uint8_t checksum = 0;
  for (int i = 0; i < 8; i++) {
    while (digitalRead(2) == 0) {};
    while (digitalRead(2) == 1) {};
    t2 = micros();
    checksum = checksum << 1;
    checksum = checksum | ((t2 - t1) > 100);
    t1 = t2;
  }


  Serial.printf("data %ld\n", data);
  uint8_t byte1 = (data >> 24 & 0xFF);
  uint8_t byte2 = (data >> 16 & 0xFF);
  uint8_t byte3 = (data >> 8 & 0xFF);
  uint8_t byte4 = (data & 0xFF);
  Serial.printf("Checksum %X %X\n", checksum,
                (byte1 + byte2 + byte3 + byte4) & 0xFF);
  float humidity = (float)((byte1 << 8) | byte2) / 10.0;
  Serial.printf("Humidity= %f %%\n", humidity);
  float temperature;
  int neg = byte3 & 0x80;
  byte3 = byte3 & 0x7F;
  temperature = (float)(byte3 << 8 | byte4) / 10.0;
  if (neg > 0)
    temperature = -temperature;
  Serial.printf("Temperature= %f C\n", temperature);
  delay(1000);
}

 

Page 225

void setup() {
  Serial.begin(9600);
}

void loop() {
  pinMode(2, OUTPUT);
  digitalWrite(2, 1);
  delay(1000);
  digitalWrite(2, 0);
  delay(1);
  pinMode(2, INPUT);
  while (digitalRead(2) == 1) {};
  while (digitalRead(2) == 0) {};
  while (digitalRead(2) == 1) {};

  uint32_t data = 0;
  for (int i = 0; i < 32; i++)
  {
    while (digitalRead(2) == 0) {};
    delayMicroseconds(50);
    data = data << 1;
    data = data | digitalRead(2);
    while (digitalRead(2) == 1) {};

  }
  uint8_t checksum = 0;
  for (int i = 0; i < 8; i++)
  {
    while (digitalRead(2) == 0) {};
    delayMicroseconds(50);
    checksum = checksum << 1;
    checksum = checksum | digitalRead(2);
    while (digitalRead(2) == 1) {};
  }
  Serial.printf("data %ld\n", data);
  uint8_t byte1 = (data >> 24 & 0xFF);
  uint8_t byte2 = (data >> 16 & 0xFF);
  uint8_t byte3 = (data >> 8 & 0xFF);
  uint8_t byte4 = (data & 0xFF);
  Serial.printf("Checksum %X %X\n", checksum,
                       (byte1 + byte2 + byte3 + byte4) & 0xFF);
  float humidity = (float)((byte1 << 8) | byte2) / 10.0;
  Serial.printf("Humidity= %f %%\n", humidity);
  float temperature;
  int neg = byte3 & 0x80;
  byte3 = byte3 & 0x7F;
  temperature = (float)(byte3 << 8 | byte4) / 10.0;
  if (neg > 0)
    temperature = -temperature;
  Serial.printf("Temperature= %f C\n", temperature);
  delay(1000);
}

 

Page 240

uint8_t readBit(uint8_t pin) {
  digitalWrite(pin, 0);
  delayMicroseconds(2);
  digitalWrite(pin, 1);
  delayMicroseconds(5);
  uint8_t b = digitalRead(pin);
  delayMicroseconds(60);
  return b;
}

void writeBit(uint8_t pin, int b) {
  int delay1, delay2;
  if (b == 1) {
    delay1 = 6;
    delay2 = 64;
  } else {
    delay1 = 60;
    delay2 = 10;
  }
  digitalWrite(pin, 0);
  delayMicroseconds(delay1);
  digitalWrite(pin, 1);
  delayMicroseconds(delay2);
}

int readByte(uint8_t pin) {
  int byte = 0;
  for (int i = 0; i < 8; i++) {
    byte = byte | readBit(pin) << i;
  };
  return byte;
}
void writeByte(uint8_t pin, int byte) {
  for (int i = 0; i < 8; i++) {
    if (byte & 1) {
      writeBit(pin, 1);
    } else {
      writeBit(pin, 0);
    }
    byte = byte >> 1;
  }
}
int presence(int pin) {
  pinMode(pin, OUTPUT |OPEN_DRAIN);
  digitalWrite(pin, 1);
  delayMicroseconds(1000);
  digitalWrite(pin, 0);
  delayMicroseconds(480);
  digitalWrite(pin, 1);
  delayMicroseconds(70);
  int res = digitalRead(pin);
  delayMicroseconds(410);
  return res;
}
int convert(uint8_t pin) {
  writeByte(pin, 0x44);
  int i;
  for (i = 0; i < 500; i++) {
    delayMicroseconds(10000);
    if (digitalRead(pin) == 1)
      break;
  }
  return i;
}
uint8_t crc8(uint8_t* data, uint8_t len) {
  uint8_t temp;
  uint8_t databyte;
  uint8_t crc = 0;
  for (int i = 0; i < len; i++) {
    databyte = data[i];
    for (int j = 0; j < 8; j++) {
      temp = (crc ^ databyte) & 0x01;
      crc >>= 1;
      if (temp)
        crc ^= 0x8C;

      databyte >>= 1;
    }
  }
  return crc;
}


float getTemperature(uint8_t pin) {
  if (presence(pin) == 1) return -1000;
  writeByte(pin, 0xCC);
  if (convert(pin) == 500) return -3000;
  presence(pin);
  writeByte(pin, 0xCC);
  writeByte(pin, 0xBE);
  uint8_t data[9];
  for (int i = 0; i < 9; i++) {
    data[i] = readByte(pin);
  }
  uint8_t crc = crc8(data, 9);
  if (crc != 0) return -2000;
  int t1 = data[0];
  int t2 = data[1];
  int16_t temp1 = (t2 << 8 | t1);
  float temp = (float)temp1 / 16;
  return temp;
}

void setup() {
  Serial.begin(9600);
}

void loop() {
  float temp = getTemperature(2);
  Serial.printf("temperature=%f\n", temp);
  delay(1000);
}

Page 318  ESP32 S3

void convertGRB(int GRB, rmt_data_t *rawdata) {
  int mask = 0x800000;
  for (int j = 0; j < 24; j++) {
    if ((GRB & mask)) {
      rawdata[j].level0 = 1;
      rawdata[j].duration0 = 7;
      rawdata[j].level1 = 0;
      rawdata[j].duration1 = 12 - 7;
    } else {
      rawdata[j].level0 = 1;
      rawdata[j].duration0 = 3;
      rawdata[j].level1 = 0;
      rawdata[j].duration1 = 12 - 3;
    }
    mask = mask >> 1;
  }
}

rmt_data_t raw_symbols[100];
int pin =  48;

void setup() {
  Serial.begin(9600);
  rmtInit(pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000);
}

void loop() {
  int color = 0x00FFFF;
  convertGRB(color, raw_symbols);
  rmtWrite(pin, raw_symbols,
              24 * sizeof(rmt_data_t), RMT_WAIT_FOR_EVER);
  delay(1000);
}