Skip to content

Instantly share code, notes, and snippets.

@djdunc
Created March 10, 2023 12:57
Show Gist options
  • Save djdunc/bb7658b4e26b42901a9320f6464e4201 to your computer and use it in GitHub Desktop.
Save djdunc/bb7658b4e26b42901a9320f6464e4201 to your computer and use it in GitHub Desktop.
TTN Uno Example for sending temperature from TMP36
/*
Circuit:
* TMP36 middle pin attached to pin A0, left to VCC, right to GND
created Feb 2023
by Duncan Wilson
*/
#include <TheThingsNetwork.h>
// Set your AppEUI and AppKey
const char *appEui = "xxx";
const char *appKey = "xxx";
#define loraSerial Serial1
#define debugSerial Serial
// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
//TMP36 Pin Variables
int sensorPin = A0; //the analog pin the TMP36's Vout (sense)
void setup()
{
loraSerial.begin(57600);
debugSerial.begin(9600);
// Wait a maximum of 10s for Serial Monitor
while (!debugSerial && millis() < 10000)
;
debugSerial.println("-- STATUS");
ttn.showStatus();
debugSerial.println("-- JOIN");
ttn.join(appEui, appKey);
}
void loop()
{
debugSerial.println("-- LOOP");
// Read sensor values and multiply by 100 to effictively have 2 decimals
uint16_t temperature = analogRead(sensorPin) * 100;
// Split both words (16 bits) into 2 bytes of 8
byte payload[2];
payload[0] = highByte(temperature);
payload[1] = lowByte(temperature);
debugSerial.print("Temperature: ");
debugSerial.println(temperature);
ttn.sendBytes(payload, sizeof(payload));
delay(60000); // wait a minute
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment