Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sensing temperature & humidity & UV using Grove and sending with LoRaWAN
/* ** Hardware setup **
* Arduino UNO R3 + LoRaWAN Shield + Grove Shield
* LCD => I2C
* Temp&Humd => I2C
* UV => A0
* Button => D2 (optional) => Not use LoRaWAN when pushed button in bootup (default is used LoRaWAN)
*/
/* ** Software setup **
* Grove Temper&Humd : https://github.com/Seeed-Studio/Grove_Temper_Humidity_TH02
* Grove LCD : https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight
* GUVA-S12SD : https://github.com/ma2shita/GUVA-S12SD
* SORACOM-LoRaWAN : https://github.com/soracom/SORACOM-LoRaWAN
*/
#define INTERVAL 5000 /* msec */
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 t = 9 9 . 9 h = 9 9 . 9
1 u = 9 9 . 9 9 9 9 9 9 t x o k
*/
#define RUN_COUNT_ROW 1
#define RUN_COUNT_COL 7
#define TX_ROW 1
#define TX_COL 12
#define TEMPER_ROW 0
#define TEMPER_COL 0
#define HUMD_ROW 0
#define HUMD_COL 7
#define UV_ROW 1
#define UV_COL 0
#include <Wire.h>
#include <TH02_dev.h>
#include <rgb_lcd.h>
rgb_lcd lcd;
#include <lorawan_client.h>
LoRaWANClient lorawanClient;
#include <GUVA-S12SD.h>
GUVAS12SD uv(A0);
#define BUTTON_PIN 2
volatile long run_count = 0;
int usingLoRaWAN;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.noBlink();
lcd.setRGB(255, 255, 255);
lcd.clear();
lcd.write("Bootup...");
delay(3000);
pinMode(BUTTON_PIN, INPUT);
usingLoRaWAN = !digitalRead(BUTTON_PIN);
lcd.clear();
if (usingLoRaWAN) {
lcd.write("Try LoRaWAN");
lcd.setCursor(0, 1);
lcd.write("Connecting...");
if (!lorawanClient.connect()) {
Serial.println(" failed to connect. Halt...");
for(;;){};
}
} else {
lcd.write("Sensor only mode");
delay(500);
}
TH02.begin();
lcd.clear();
}
void loop()
{
char json[16];
char buf[10];
run_count++;
dtostrf(run_count, 5, 0, buf);
lcd.setCursor(RUN_COUNT_COL, RUN_COUNT_ROW); lcd.write(buf);
lcd.setCursor(TX_COL, TX_ROW); lcd.write(" ");
float temper = TH02.ReadTemperature();
dtostrf(temper, 4, 1, buf);
lcd.setCursor(TEMPER_COL, TEMPER_ROW); lcd.write("t="); lcd.write(buf);
sprintf(json, "{\"t\":%s}", buf);
Serial.println(json);
if (usingLoRaWAN) {
lcd.setCursor(TX_COL, TX_ROW); lcd.write("tx");
lorawanClient.sendData(json);
lcd.setCursor(TX_COL+2, TX_ROW); lcd.write("ok");
}
delay(INTERVAL);
lcd.setCursor(TX_COL, TX_ROW); lcd.write(" ");
float humd = TH02.ReadHumidity();
dtostrf(humd, 4, 1, buf);
lcd.setCursor(HUMD_COL, HUMD_ROW); lcd.write("h="); lcd.write(buf);
sprintf(json, "{\"h\":%s}", buf);
Serial.println(json);
if (usingLoRaWAN) {
lcd.setCursor(TX_COL, TX_ROW); lcd.write("tx");
lorawanClient.sendData(json);
lcd.setCursor(TX_COL+2, TX_ROW); lcd.write("ok");
}
delay(INTERVAL);
lcd.setCursor(TX_COL, TX_ROW); lcd.write(" ");
float uv_index = uv.index(uv.read());
dtostrf(uv_index, 4, 1, buf);
lcd.setCursor(UV_COL, UV_ROW); lcd.write("u="); lcd.write(buf);
sprintf(json, "{\"u\":%s}", buf);
Serial.println(json);
if (usingLoRaWAN) {
lcd.setCursor(TX_COL, TX_ROW); lcd.write("tx");
lorawanClient.sendData(json);
lcd.setCursor(TX_COL+2, TX_ROW); lcd.write("ok");
}
delay(INTERVAL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment