Skip to content

Commit

Permalink
feat: wait for serial port & measure on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotak committed Nov 12, 2023
1 parent c9ebee1 commit 2b33d62
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 74 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Just send
{ "restart": true }
```

> In fact sending just `restart` without beeing json is fine too
Responds with

```json
Expand Down
155 changes: 84 additions & 71 deletions include/1wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,13 @@ uint32_t previousMillis = 0;
void oneWireCheck() {
if (!oneWire.checkPresence()) {
Serial.println(F("{\"error\": \"no_device\"}"));
Serial.flush();
digitalWrite(POWER_PIN, LOW);
delay(5000);
resetFunc();
}
}

void oneWireSetup() {
pinMode(POWER_PIN, OUTPUT);
pinMode(STATUS_PIN, OUTPUT);

digitalWrite(STATUS_PIN, LOW);
digitalWrite(POWER_PIN, HIGH);
delay(10); // give time to power-up

oneWireCheck();
oneWire.deviceReset();
}

void oneWirePrint(uint8_t deviceAddress[], float temp) {
Serial.print(F(", \"0x"));

Expand All @@ -44,90 +33,114 @@ void oneWirePrint(uint8_t deviceAddress[], float temp) {
Serial.print(temp, 2);
}

void oneWireLoop() {
unsigned long currentMillis = millis();
void oneWireRead() {
digitalWrite(STATUS_PIN, HIGH);

if (currentMillis - previousMillis >= 20000) { // 20 sec interval
previousMillis = currentMillis;
Serial.print(F("{\"error\": \"ok\""));

digitalWrite(STATUS_PIN, HIGH);
for (int i = 0; i < NO_CHANNELS; i++) {
oneWire.setChannel(i);

oneWireCheck();
Serial.print(F("{\"error\": \"ok\""));
// check if sensor on bus
if (!oneWire.reset()) {
continue;
}

// check now due to delays
serialRead();

for (int i = 0; i < NO_CHANNELS; i++) {
oneWire.setChannel(i);
// write to all sensors
oneWire.skip();

// start conversion
oneWire.write(0x44, true);

// wait for conversion
delay(750);

oneWire.reset();

while (oneWire.wireSearch(rom)) {
// read Scratchpad
oneWire.write(0xBE);

for (uint8_t i = 0; i < 9; i++) {
data[i] = oneWire.read();
}

// check if sensor on bus
if (!oneWire.reset()) {
// check CRC
if (!OneWire::crc8(data, 8)) {
continue;
}

// check now due to delays
serialRead();
int16_t raw = (data[1] << 8) | data[0];

// write to all sensors
oneWire.skip();
switch (rom[0]) {
case 0x10: { // DS18S20
raw = raw << 3;

// start conversion
oneWire.write(0x44, true);
if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} break;

// wait for conversion
delay(750);
case 0x28: { // DS18B20

oneWire.reset();
uint8_t cfg = (data[4] & 0x60); // default is 12 bit resolution

while (oneWire.wireSearch(rom)) {
// read Scratchpad
oneWire.write(0xBE);
if (cfg == 0x00) { // 9 bit resolution
raw &= ~7;

for (uint8_t i = 0; i < 9; i++) {
data[i] = oneWire.read();
}
} else if (cfg == 0x20) { // 10 bit res
raw &= ~3;

// check CRC
if (!OneWire::crc8(data, 8)) {
continue;
}
} else if (cfg == 0x40) { // 11 bit res
raw &= ~1;
}
} break;

int16_t raw = (data[1] << 8) | data[0];
default:
// skip unknown devices
continue;
}

switch (rom[0]) {
case 0x10: { // DS18S20
raw = raw << 3;
// print data
oneWirePrint(rom, ((float)raw) / 16);
}

if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} break;
oneWire.reset_search();
}
Serial.println(F("}"));
digitalWrite(STATUS_PIN, LOW);
}

case 0x28: { // DS18B20
void oneWireSetup() {
pinMode(POWER_PIN, OUTPUT);
pinMode(STATUS_PIN, OUTPUT);

uint8_t cfg = (data[4] & 0x60); // default is 12 bit resolution
digitalWrite(POWER_PIN, HIGH);

if (cfg == 0x00) { // 9 bit resolution
raw &= ~7;
// blink for start
for (uint8_t i = 0; i < 2; i++) {
digitalWrite(STATUS_PIN, HIGH);
delay(300);
digitalWrite(STATUS_PIN, LOW);
delay(300);
}

} else if (cfg == 0x20) { // 10 bit res
raw &= ~3;
oneWireCheck();
oneWire.deviceReset();

} else if (cfg == 0x40) { // 11 bit res
raw &= ~1;
}
} break;
oneWireRead();
}

default:
// skip unknown devices
continue;
}
void oneWireLoop() {
unsigned long currentMillis = millis();

// print data
oneWirePrint(rom, ((float)raw) / 16);
}
if (currentMillis - previousMillis >= 20000) { // 20 sec interval
previousMillis = currentMillis;

oneWire.reset_search();
}
Serial.println(F("}"));
digitalWrite(STATUS_PIN, LOW);
oneWireCheck();
oneWireRead();
}
}
3 changes: 2 additions & 1 deletion include/flag.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
void flag() {
Serial.println(F("{\"error\": \"overcurrent\"}"));
digitalWrite(POWER_PIN, LOW);
Serial.println(F("{\"error\": \"overcurrent\"}"));
Serial.flush();
delay(5000);
resetFunc();
}
Expand Down
5 changes: 4 additions & 1 deletion include/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void serialParse() {
if (strstr(buffer, "restart") != NULL) {
digitalWrite(POWER_PIN, LOW);
Serial.println(F("{\"restart\": \"ok\"}"));
Serial.flush();
delay(1000);
resetFunc();
}
Expand All @@ -33,7 +34,9 @@ void serialRead() {
last_data_timeout = millis();

if (buffer_index == BUFFER_SIZE) { // prevent overflow
Serial.flush();
while (Serial.available()) {
Serial.read();
}
buffer_index = 0;
}
}
Expand Down
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ lib_deps =
platform = atmelmegaavr
framework = arduino
board = ATtiny1614
board_build.f_cpu = 8000000L
board_build.f_cpu = 10000000L
board_hardware.oscillator = internal
board_hardware.bod = 4.3v
board_hardware.updipin = updi
upload_protocol = serialupdi
build_flags =
${common.build_flags}
'-D NO_CHANNELS=6'
Expand Down

0 comments on commit 2b33d62

Please sign in to comment.