Hi,
I am new to arduino as well as NodeMCU, I was trying to make a sketch for send gps data to thingsboard. I tried edit few code that were available. NodeMCU GPIO part is okay. But with GPS it seems like Thingsboard is not getting any telemetry data.
`#include
include
include
include <TinyGPS++.h>
include
define WIFI_AP "Elora"
define WIFI_PASSWORD "eltkfgp622"
define TOKEN "M9pMFEcB5jPbQbyXXXXX"
define GPIO0 0
define GPIO2 2
define GPIO0_PIN 3
define GPIO2_PIN 4
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
char thingsboardServer[] = "demo.thingsboard.io";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
// We assume that all GPIOs are LOW
boolean gpioState[] = {false, false};
void setup() {
Serial.begin(115200);
// Set output mode for all GPIO pins
pinMode(GPIO0, OUTPUT);
pinMode(GPIO2, OUTPUT);
delay(10);
InitWiFi();
client.setServer( thingsboardServer, 1883 );
client.setCallback(on_message);
if (GPIO2_PIN == LOW){
ss.begin(GPSBaud);
Serial.println(F("KitchenSink.ino"));
Serial.println(F("Demonstrating nearly every feature of TinyGPS++ with an attached GPS module"));
Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
Serial.println();
}
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0)
if (gps.encode(ss.read()))
displayInfo();
Hi, I am new to arduino as well as NodeMCU, I was trying to make a sketch for send gps data to thingsboard. I tried edit few code that were available. NodeMCU GPIO part is okay. But with GPS it seems like Thingsboard is not getting any telemetry data. `#include
include
include
include <TinyGPS++.h>
include
define WIFI_AP "Elora"
define WIFI_PASSWORD "eltkfgp622"
define TOKEN "M9pMFEcB5jPbQbyXXXXX"
define GPIO0 0
define GPIO2 2
define GPIO0_PIN 3
define GPIO2_PIN 4
static const int RXPin = 12, TXPin = 13; static const uint32_t GPSBaud = 9600; // The TinyGPS++ object TinyGPSPlus gps; // The serial connection to the GPS device SoftwareSerial ss(RXPin, TXPin);
char thingsboardServer[] = "demo.thingsboard.io";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;
// We assume that all GPIOs are LOW boolean gpioState[] = {false, false};
void setup() { Serial.begin(115200); // Set output mode for all GPIO pins pinMode(GPIO0, OUTPUT); pinMode(GPIO2, OUTPUT); delay(10); InitWiFi(); client.setServer( thingsboardServer, 1883 ); client.setCallback(on_message); if (GPIO2_PIN == LOW){ ss.begin(GPSBaud); Serial.println(F("KitchenSink.ino")); Serial.println(F("Demonstrating nearly every feature of TinyGPS++ with an attached GPS module")); Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion()); Serial.println(); }
// This sketch displays information every time a new sentence is correctly encoded. while (ss.available() > 0) if (gps.encode(ss.read())) displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10) { Serial.println(F("No GPS detected: check wiring.")); while(true); } } void displayInfo() { // Serial.print(F("Location: ")); if (gps.location.isValid()) {
String payload = "{"; payload += "\"latbuf\":"; payload += latbuf; payload += ","; payload += "\"lonbuf\":"; payload += lonbuf; payload += "}";
// Send payload char attributes[100]; payload.toCharArray( attributes, 100 ); client.publish( "v1/devices/me/telemetry", attributes ); Serial.println( attributes ); } } void loop() { if ( !client.connected() ) { reconnect(); }
client.loop(); }
// The callback for when a PUBLISH message is received from the server. void on_message(const char topic, byte payload, unsigned int length) {
Serial.println("On message");
char json[length + 1]; strncpy (json, (char*)payload, length); json[length] = '\0';
Serial.print("Topic: "); Serial.println(topic); Serial.print("Message: "); Serial.println(json);
// Decode JSON request StaticJsonBuffer<200> jsonBuffer; JsonObject& data = jsonBuffer.parseObject((char*)json);
if (!data.success()) { Serial.println("parseObject() failed"); return; }
// Check request method String methodName = String((const char*)data["method"]);
if (methodName.equals("getGpioStatus")) { // Reply with GPIO status String responseTopic = String(topic); responseTopic.replace("request", "response"); client.publish(responseTopic.c_str(), get_gpio_status().c_str()); } else if (methodName.equals("setGpioStatus")) { // Update GPIO status and reply set_gpio_status(data["params"]["pin"], data["params"]["enabled"]); String responseTopic = String(topic); responseTopic.replace("request", "response"); client.publish(responseTopic.c_str(), get_gpio_status().c_str()); client.publish("v1/devices/me/attributes", get_gpio_status().c_str()); } }
String get_gpio_status() { // Prepare gpios JSON payload string StaticJsonBuffer<200> jsonBuffer; JsonObject& data = jsonBuffer.createObject(); data[String(GPIO0_PIN)] = gpioState[0] ? true : false; data[String(GPIO2_PIN)] = gpioState[1] ? true : false; char payload[256]; data.printTo(payload, sizeof(payload)); String strPayload = String(payload); Serial.print("Get gpio status: "); Serial.println(strPayload); return strPayload; }
void set_gpio_status(int pin, boolean enabled) { if (pin == GPIO0_PIN) { // Output GPIOs state digitalWrite(GPIO0, enabled ? HIGH : LOW); // Update GPIOs state gpioState[0] = enabled; } else if (pin == GPIO2_PIN) { // Output GPIOs state digitalWrite(GPIO2, enabled ? HIGH : LOW); // Update GPIOs state gpioState[1] = enabled; } }
void InitWiFi() { Serial.println("Connecting to AP ..."); // attempt to connect to WiFi network
WiFi.begin(WIFI_AP, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Connected to AP"); }
void reconnect() { // Loop until we're reconnected while (!client.connected()) { status = WiFi.status(); if ( status != WL_CONNECTED) { WiFi.begin(WIFI_AP, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Connected to AP"); } Serial.print("Connecting to ThingsBoard node ..."); // Attempt to connect (clientId, username, password) if ( client.connect("ESP8266 Device", TOKEN, NULL) ) { Serial.println( "[DONE]" ); // Subscribing to receive RPC requests client.subscribe("v1/devices/me/rpc/request/+"); // Sending current GPIO status Serial.println("Sending current GPIO status ..."); client.publish("v1/devices/me/attributes", get_gpio_status().c_str()); } else { Serial.print( "[FAILED] [ rc = " ); Serial.print( client.state() ); Serial.println( " : retrying in 5 seconds]" ); // Wait 5 seconds before retrying delay( 5000 ); } } }`