Thinger32 NB-IoT
The Thinger32NB-IoT is a compact dev board that combines Quectel BC660 with an Espressif ESP32-PICO-D4 to provide a simple-to-use development board for IoT projects.
Last updated
```cpp
#define THINGER_SERVER "acme.thinger.io"
#define HEXACORE_DEVELOPMENT
#define THINGER_SERIAL_DEBUG
#define _DISABLE_TLS_
#include <Arduino.h>
#include <EEPROM.h>
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
#define SerialAT Serial1
// Select the modem:
#define TINY_GSM_MODEM_BC660
#define TINY_GSM_DEBUG SerialMon
//#define DUMP_AT_COMMANDS
#ifndef TINY_GSM_RX_BUFFER
#define TINY_GSM_RX_BUFFER 1024
#endif
//set hibernation parameters
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 10 /* Time ESP32 will go to sleep (in seconds) */
// Can be installed from Library Manager or https://github.com/vshymanskyy/TinyGSM
#include <TinyGsmClient.h>
#include <ThingerTinyGSM.h>
#include <ThingerESP32OTA.h>
#include "arduino_secrets.h"
//Sensor libraries
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(Serial1, Serial);
ThingerTinyGSM thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL, debugger);
#else
ThingerTinyGSM thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL, SerialAT);
#endif
ThingerESP32OTA ota(thing);
String iccid;
// module config version
#define MODULE_CONFIG_VERSION 3
#define PIN_MODEM_RESET 13
#define PIN_MODEM_WAKEUP 12
#define PIN_MODEM_PWR_KEY 19
#define PIN_MODULE_LED 21
#define PIN_MODEM_RX 32
#define PIN_MODEM_TX 33
#define RELAY_PIN 27
#define DOOR_STATE_PIN 14
String id2 = "My_device";
void setup() {
pinMode(PIN_MODEM_RESET, OUTPUT);
pinMode(PIN_MODEM_WAKEUP, OUTPUT);
pinMode(PIN_MODEM_PWR_KEY, OUTPUT);
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, PIN_MODEM_RX, PIN_MODEM_TX);
delay(400);
sensors_begin();
thing["my_data"] >> [](pson & out){
out["millis"] = millis();
};
thing["modem"] >> [](pson & out){
out["modem"] = thing.getTinyGsm().getModemInfo().c_str();
out["IMEI"] = thing.getTinyGsm().getIMEI().c_str();
out["CCID"] = thing.getTinyGsm().getSimCCID().c_str();
out["operator"] = thing.getTinyGsm().getOperator().c_str();
};
// set APN
thing.setAPN(APN_NAME, APN_USER, APN_PSWD);
// configure hardware module reset
thing.setModuleReset([]{
// modem reset
digitalWrite(PIN_MODEM_RESET, 1);
delay(100);
digitalWrite(PIN_MODEM_RESET, 0);
});
thing.initModem([](TinyGsm& modem){
// read SIM ICCID
iccid = modem.getSimCCID();
THINGER_DEBUG_VALUE("NB-IOT", "SIM ICCID: ", iccid.c_str());
// disable power save mode
modem.sendAT("+CPSMS=0");
modem.waitResponse();
// disable eDRX
modem.sendAT("+CEDRXS=0");
modem.waitResponse();
// edRX and PTW -> disabled
modem.sendAT("+QEDRXCFG=0");
modem.waitResponse();
// initialize module configuration for the first time
EEPROM.begin(1);
auto state = EEPROM.readByte(0);
if(state!=MODULE_CONFIG_VERSION){
THINGER_DEBUG_VALUE("NB-IOT", "Configuring module with version: ", MODULE_CONFIG_VERSION);
// stop modem functionality
modem.sendAT("+CFUN=0");
modem.waitResponse();
// configure APN
modem.sendAT("+QCGDEFCONT=\"IP\",\"" APN_NAME "\"");
modem.waitResponse();
// preferred search bands (for Spain)
modem.sendAT("+QBAND=3,20,8,3");
modem.waitResponse();
// set preferred operators
modem.sendAT("+COPS=4,2,\"21407\""); // movistar
//modem.sendAT("+COPS=4,2,\"21401\""); // vodafone
modem.waitResponse();
// enable net led
modem.sendAT("+QLEDMODE=1");
modem.waitResponse();
// full functionality
modem.sendAT("+CFUN=1");
modem.waitResponse();
EEPROM.writeByte(0, MODULE_CONFIG_VERSION);
EEPROM.commit();
}else{
THINGER_DEBUG_VALUE("NB-IOT", "Module already configured with version: ", MODULE_CONFIG_VERSION);
}
EEPROM.end();
});
// configure ota block size to
ota.set_block_size(512);
id2+=iccid.c_str();
thing.set_credentials(USERNAME, id2.c_str(), id2.c_str());
}
void loop() {
// iotmp handle, modem pwr on
thing.handle();
}
```