Consider an example that sends temperature and humidity to a warehouse every 5 minutes.
Required materials:
Device adding and setting:
Firmware Arduino:
#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
// Use physical Serial port for Mega, Leonardo, Micro
#define SerialAT Serial1
// or software Serial for Uno, Nano
//#include
//SoftwareSerial SerialAT(2, 3); // RX, TX
Adafruit_SHT31 sht31 = Adafruit_SHT31();
// GPRS access point settings
const char apn[] = "YourAPN";
const char user[] = "";
const char pass[] = "";
//VizIoT Device access key and password (can be found in the device settings)
String VizIoT_Device_key = "................";
String VizIoT_Device_pass = "....................";
//Server address and port
const char server[] = "VizIoT.com";
const int port = 48656;
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
HttpClient http(client, server, port);
void setup() {
// Set baud rate for Serial Monitor
SerialMon.begin(115200);
delay(10);
// Set baud rate for Serial GSM module
SerialAT.begin(115200);
delay(3000);
SerialMon.println(F("Modem initialization..."));
modem.restart();
String modemInfo = modem.getModemInfo();
SerialMon.print(F("Modem: "));
SerialMon.println(modemInfo);
Serial.println("Sensor search SHT31-D");
if (! sht31.begin(0x44)) { // specify 0x45 for alternative i2c sensor address
Serial.println("I can not find SHT31-D");
}
}
void loop() {
SerialMon.print(F("Waiting for the network..."));
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
SerialMon.print(F("Access point connection "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
SerialMon.println(" unsuccessfully");
delay(10000);
return;
}
SerialMon.println(" OK");
SerialMon.print(F("sending an HTTP GET request... "));
int err = http.get(getResource().c_str());
if (err != 0) {
SerialMon.println(F("Connection error"));
delay(10000);
return;
}
http.stop();
SerialMon.println(F("Disconnected from the server"));
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnect"));
//Спать 5 минут
delay(5 * 60 * 1000);
}
String getResource() {
String resource = String("/update?key=") + VizIoT_Device_key + "&" + "pass=" + VizIoT_Device_pass ;
//read data from sensors
float t = sht31.readTemperature();
float h = sht31.readHumidity();
//checking the temperature information
if (! isnan(t)) {
Serial.print("Temperature *C = ");
Serial.println(t);
resource += String("&tem=") + t;
} else {
Serial.println("Error reading temperature");
}
//checking the humidity information
if (! isnan(h)) {
Serial.print("Hum. % = ");
Serial.println(h);
resource += String("&hum=") + h;
} else {
Serial.println("Error reading humidity");
}
return resource;
}
Arduino GND-----[10kΩ]---|
|--------RX SIM800L
Arduino TX------[10kΩ]---|
Read more here.
Adding widgets:
Now the setup is complete and you have to wait until your device connected to the server.