Arduino Code Generator

Generate custom Arduino firmware for your ESP32 water quality monitoring device with your specific TTN credentials.

OTAA
ESP32
LoRaWAN
Water Sensors

Required Arduino Libraries

Ensure the following libraries are installed via Arduino IDE → Tools → Manage Libraries:

MCCI LoRaWAN LMIC
OneWire
DallasTemperature
Adafruit SSD1306
Adafruit GFX Library
ESP32 board package

Install all libraries from the Arduino Library Manager or via PlatformIO. See the Installation Guide for detailed instructions.

Generate Your Custom Code

Paste your TTN credentials below. The Arduino sketch will update in real-time with your specific DevEUI, AppEUI, and AppKey. Copy the code or download the .ino file and flash it to your ESP32.

Enter TTN Device Credentials

TTN Console → Your Application → End Devices → Device Overview

Typically all zeros. Accepts any hex format (e.g. 0x00, 0x01...).

From TTN Console → End device → DevEUI. Use the ↕ LSB copy button.

From TTN Console → End device → AppKey. Use the MSB copy button.

Generated Arduino Sketch

Fill credentials above

Code Features & Capabilities

LoRaWAN & Communication

  • OTAA (Over-The-Air Activation) for secure join
  • 60-second transmission interval (battery-friendly)
  • Automatic rejoin on network failure
  • Adaptive Data Rate (ADR) support

Sensor Management

  • pH sensor with calibration support
  • TDS sensor with baseline calibration
  • DS18B20 temperature compensation
  • OLED display for live sensor readings

Data Payload

  • 6-byte efficient payload format
  • Temperature (÷10) • TDS • pH (÷100)
  • Compatible with TTN uplink decoder
  • Automatic data type conversion

Debugging & Monitoring

  • Serial monitor output (115200 baud)
  • Join status and uplink confirmations
  • Real-time sensor value display on OLED
  • Error handling and recovery mechanisms

How to Use

  1. 1

    Get TTN Credentials

    From your TTN Console device page, copy DevEUI (LSB format) and AppKey (MSB format). See TTN Setup guide for details.

  2. 2

    Paste Credentials

    Enter your DevEUI and AppKey in the form above. The code will automatically update with your credentials.

  3. 3

    Verify Configuration

    Check that the LMIC library region is configured correctly (CFG_in866 for India). See Installation guide.

  4. 4

    Upload to ESP32

    Copy the generated code or download the .ino file. Open in Arduino IDE, select ESP32 board, and upload.

  5. 5

    Monitor Serial Output

    Open Serial Monitor (115200 baud) to see join attempts, sensor readings, and uplink confirmations.

Sensor Calibration

Calibration Required

For accurate readings, calibrate your sensors before deployment. The generated code includes calibration constants that you can adjust.

TDS Sensor Calibration

Adjust the baseline value:

float baseline = 1.83; // TDS voltage offset

Measure distilled water (should read 0 ppm) and adjust baseline accordingly.

pH Sensor Calibration

Adjust the calibrationOffset value:

float calibrationOffset = 13.50; // pH probe intercept

Use a pH 7.0 buffer solution to calibrate. Adjust offset until reading matches 7.0.

Troubleshooting Tips

❌ Device not joining TTN network

  • Verify DevEUI and AppKey match TTN Console exactly (check byte order: LSB vs MSB)
  • Ensure LMIC library region configuration matches your frequency plan (CFG_in866 for India)
  • Check gateway coverage using TTN Coverage Map
  • Verify antenna is properly connected to LoRa module

❌ Sensor readings inaccurate

  • Calibrate sensors using known reference solutions (pH 7.0 buffer, distilled water)
  • Check sensor connections and ensure proper voltage levels (3.3V)
  • Verify DS18B20 has 4.7kΩ pull-up resistor on data line
  • Allow sensors to stabilize for 2-3 minutes after immersion

❌ Uplinks successful but dashboard not updating

  • Check webhook configuration in TTN Console (see Webhooks guide)
  • Verify payload decoder is correctly set up in TTN
  • Ensure device ID in TTN matches device ID in dashboard
  • Check Serial Monitor for payload structure confirmation

Code Generated Successfully!

Upload the code to your ESP32 and configure TTN webhook integration.