I'm using Arduino and sim800L to send some data.
I designed the following code to help send some temperature and humidity data sans sensors to thingsboard.
#define TINY_GSM_MODEM_SIM800
#define TOKEN "h17GvtecqCIkFkDPLhdK"
#include <TinyGsmClient.h>
#include <PubSubClient.h>
// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = "airtelgprs.com";
const char user[] = "";
const char pass[] = "";
#include <SoftwareSerial.h>
SoftwareSerial SerialAt(8, 7); // RX, TX
TinyGsm modem(SerialAt);
TinyGsmClient client(modem);
PubSubClient mqtt(client);
const char* broker = "demo.thingsboard.io";
unsigned long lastSend;
void setup() {
// Set console baud rate
Serial.begin(9600);
delay(10);
// Set GSM module baud rate
SerialAt.begin(9600);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.print("Connecting to ");
Serial.println(apn);
simCommand("AT+CIPMUX=0");
simCommand("AT+CIPSTATUS");
simCommand("AT+CSTT=\"airtelgprs.com\",\"\",\"\"");
//simCommand("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");
simCommand("AT+CIPSTATUS");
simCommand("AT+CIICR");
simCommand("AT+CIPSTATUS");
simCommand("AT+CIFSR");
simCommand("AT+CIPSTATUS");
// MQTT Broker setup
mqtt.setServer(broker, 1883);
lastSend = 0;
//mqtt.setCallback(mqttCallback);
}
void loop() {
if ( !mqtt.connected() ) {
reconnect();
}
if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
getAndSendData();
lastSend = millis();
}
mqtt.loop();
}
void simCommand(String command)
{
SerialAt.println(command);
Serial.println(command);
delay(5000);
while (!SerialAt.available())
{
Serial.println(F("No response yet"));
delay(1000);
}
while (SerialAt.available())
{
Serial.write(SerialAt.read());
// delay(1000);
}
Serial.println("");
delay(10);
}
void getAndSendData()
{
float val1 = 70;
float val2 = 86;
String payload = "{";
payload += "\"temperature\":"; payload += val1; payload += ",";
payload += "\"humidity\":"; payload += val2;
payload += "}";
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
mqtt.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
}
void reconnect() {
// Loop until we're reconnected
while (!mqtt.connected()) {
Serial.print("Connecting to ThingsBoard node ...");
// Attempt to connect (clientId, username, password)
if ( mqtt.connect("soldier sensor", TOKEN, NULL) ) {
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( mqtt.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}
I'm stuck at the moment because of this error at Serial Monitor
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...[FAILED] [ rc = -2 : retrying in 5 seconds]
Connecting to ThingsBoard node ...
Can someone please help debug it?