In this example, we will create a device that transmits temperature and humidity readings from a warehouse to the VizIoT server every 5 minutes. A SIM800L GSM module will be used for internet connectivity, and an SHT31-D sensor for measurements.
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include <Wire.h>
#include "Adafruit_SHT31.h" // https://github.com/adafruit/Adafruit_SHT31
#define SerialMon Serial
#define SerialAT Serial1
Adafruit_SHT31 sht31 = Adafruit_SHT31();
// Access Point settings
const char apn[] = "YourAPN";
const char user[] = "";
const char pass[] = "";
// VizIoT device credentials
String VizIoT_Device_key = "................";
String VizIoT_Device_pass = "....................";
// VizIoT Server
const char server[] = "VizIoT.com";
const int port = 48656;
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
HttpClient http(client, server, port);
void setup() {
SerialMon.begin(115200);
delay(10);
SerialAT.begin(115200);
delay(3000);
SerialMon.println(F("Initializing modem..."));
modem.restart();
String modemInfo = modem.getModemInfo();
SerialMon.print(F("Modem: "));
SerialMon.println(modemInfo);
Serial.println("Searching for SHT31-D sensor");
if (!sht31.begin(0x44)) {
Serial.println("Couldn't find SHT31-D (check wiring)");
}
}
void loop() {
SerialMon.print(F("Waiting for network..."));
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
SerialMon.print(F("Connecting to APN "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
SerialMon.print(F("Sending HTTP GET request... "));
int err = http.get(getResource().c_str());
if (err != 0) {
SerialMon.println(F("connection failed"));
delay(10000);
return;
}
http.stop();
SerialMon.println(F("Disconnected from server"));
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
delay(5 * 60 * 1000); // sleep for 5 minutes
}
String getResource() {
String resource = String("/update?key=") + VizIoT_Device_key +
"&pass=" + VizIoT_Device_pass;
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (!isnan(t)) {
Serial.print("Temperature °C = ");
Serial.println(t);
resource += String("&tem=") + t;
} else {
Serial.println("Failed to read temperature");
}
if (!isnan(h)) {
Serial.print("Humidity % = ");
Serial.println(h);
resource += String("&hum=") + h;
} else {
Serial.println("Failed to read humidity");
}
return resource;
}
apn — GSM access point nameuser — APN userpass — passwordVizIoT_Device_key — access keyVizIoT_Device_pass — access password| Sensor Pin | Arduino Pin |
|---|---|
| VIN | 3.3V |
| GND | GND |
| SCL | SCL |
| SDA | SDA |
⚠️ Important! The SIM800L module cannot be powered directly from the Arduino — an external 4.0V power source is required.
| SIM800L Pin | Connection |
|---|---|
| VIN | +4V external source |
| GND | Source GND and Arduino GND |
| TX | Arduino RX1 |
| RX | Arduino TX1 via a voltage divider |
Arduino GND ----[10kΩ]---+
+---- RX SIM800L
Arduino TX ----[10kΩ]----+
For more connection details, see the article on SIM800L at Codius.ru.
Connect the Arduino to your computer, select the port, and upload the sketch.
temhumtem and humThe device is configured — now just wait for its first connection to the VizIoT server. After connecting, you will start receiving temperature and humidity data every 5 minutes.