Skip to content

Instantly share code, notes, and snippets.

@erijpkema
Last active February 2, 2017 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erijpkema/b6990d56fe6c1e14cdf2ac29f91d939a to your computer and use it in GitHub Desktop.
Save erijpkema/b6990d56fe6c1e14cdf2ac29f91d939a to your computer and use it in GitHub Desktop.
Post temperature from a OneWire and voltage from light sensor to the things network
#include "TheThingsUno.h"
#include <OneWire.h>
// Set your AppEUI and AppKey
const byte appEui[8] = //for example: {0x70, 0xB3, 0xD5, 0x7E, 0xE0, 0xE0, 0x01, 0x4A1};
const byte appKey[16] = //for example: {0x73, 0x6D, 0x24, 0xD2, 0x69, 0xBE, 0xE3, 0xAE, 0x0E, 0xCE, 0xF0, 0xBB, 0x6C, 0xA4, 0xBA, 0xFE};
#define debugSerial Serial
#define loraSerial Serial1
#define debugPrintLn(...) { if (debugSerial) debugSerial.println(__VA_ARGS__); }
#define debugPrint(...) { if (debugSerial) debugSerial.print(__VA_ARGS__); }
TheThingsUno ttu;
//Onewire Stuff
OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary)
int led = 7;
void setup()
{
debugSerial.begin(115200);
loraSerial.begin(57600);
delay(1000);
ttu.init(loraSerial, debugSerial);
ttu.reset();
ttu.join(appEui, appKey);
delay(6000);
ttu.showStatus();
debugPrintLn("Setup for The Things Network complete");
delay(1000);
}
void loop() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
if ( !ds.search(addr)) {
//Serial.println("No more addresses.");
//Serial.println();
ds.reset_search();
delay(250);
return;
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
debugPrintLn("Temperature:");
debugPrintLn(celsius);
debugPrintLn("\n");
// Read a photodiode on A0
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
//debugPrintLn("Light: " + String(voltage, DEC));
debugPrintLn(voltage);
//Todo: remove the conversion to float above and back to raw again, here.
int data2 = (int)(celsius * 100); // 2150
byte buf[4];
//Temperature
buf[0] = data2 >> 8;
buf[1] = data2 & 0xff;
//Light voltage
buf[2] = sensorValue >> 8;
buf[3] = sensorValue & 0xff;
ttu.sendBytes(buf, 4);
//digitalWrite(led, HIGH);
delay(20000);
digitalWrite(led, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment