Hello,
I am trying to send data to ubidots from GPS module using Nodemcu, and the problem is that the device could not be connected with ubidots. I’m trying solved with changing the code in the void loop Ubidots post functions but still get an error. I attach my code here.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
/********************************
* Constants and objects
*******************************/
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
char const * SSID_NAME = "predator@09"; // Put your SSID name here
char const * SSID_PASS = "qwertyuiop12345"; // Put your password here
char* TOKEN = "BBFF-Kv6D95BTYclDXDgpgUxRlWNU"; // Put your TOKEN here
char* DEVICE_LABEL = "honda-civic"; // Your Device label
/* Put your variable labels here */
char const * VARIABLE_LABEL_1 = "location";
/* HTTP Settings */
char const * HTTPSERVER = "industrial.api.ubidots.com";
const int HTTPPORT = 80;
char const * USER_AGENT = "ESP8266";
char const * VERSION = "1.0";
TinyGPSPlus gps;
WiFiClient clientUbi;
SoftwareSerial ss(RXPin, TXPin);
/********************************
* Auxiliar Functions
*******************************/
void SendToUbidots(char* payload) {
int contentLength = strlen(payload);
/* Connecting the client */
clientUbi.connect(HTTPSERVER, HTTPPORT);
if (clientUbi.connected()) {
/* Builds the POST request - For all request structures, refer to https://ubidots.com/docs/api/ */
clientUbi.print(F("POST /api/v1.6/devices/"));
clientUbi.print(DEVICE_LABEL);
clientUbi.print(F(" HTTP/1.1\r\n"));
clientUbi.print(F("Host: "));
clientUbi.print(HTTPSERVER);
clientUbi.print(F("\r\n"));
clientUbi.print(F("User-Agent: "));
clientUbi.print(USER_AGENT);
clientUbi.print(F("/"));
clientUbi.print(VERSION);
clientUbi.print(F("\r\n"));
clientUbi.print(F("X-Auth-Token: "));
clientUbi.print(TOKEN);
clientUbi.print(F("\r\n"));
clientUbi.print(F("Connection: close\r\n"));
clientUbi.print(F("Content-Type: application/json\r\n"));
clientUbi.print(F("Content-Length: "));
clientUbi.print(contentLength);
clientUbi.print(F("\r\n\r\n"));
clientUbi.print(payload);
clientUbi.print(F("\r\n"));
Serial.println(F("Making request to Ubidots:\n"));
Serial.print("POST /api/v1.6/devices/");
Serial.print(DEVICE_LABEL);
Serial.print(" HTTP/1.1\r\n");
Serial.print("Host: ");
Serial.print(HTTPSERVER);
Serial.print("\r\n");
Serial.print("User-Agent: ");
Serial.print(USER_AGENT);
Serial.print("/");
Serial.print(VERSION);
Serial.print("\r\n");
Serial.print("X-Auth-Token: ");
Serial.print(TOKEN);
Serial.print("\r\n");
Serial.print("Connection: close\r\n");
Serial.print("Content-Type: application/json\r\n");
Serial.print("Content-Length: ");
Serial.print(contentLength);
Serial.print("\r\n\r\n");
Serial.print(payload);
Serial.print("\r\n");
} else {
Serial.println("Connection Failed ubidots - Try Again");
}
/* Reach timeout when the server is unavailable */
int timeout = 0;
while (!clientUbi.available() && timeout < 5000) {
timeout++;
delay(1);
if (timeout >= 5000) {
Serial.println(F("Error, max timeout reached"));
break;
}
}
/* Reads the response from the server */
Serial.println(F("\nUbidots' Server response:\n"));
while (clientUbi.available()) {
char c = clientUbi.read();
Serial.print(c); // Uncomment this line to visualize the response on the Serial Monitor
}
/* Disconnecting the client */
clientUbi.stop();
}
/********************************
* Main Functions
*******************************/
void setup() {
Serial.begin(115200);
ss.begin(GPSBaud);
/* Connects to AP */
WiFi.begin(SSID_NAME, SSID_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
WiFi.setAutoReconnect(true);
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
if (clientUbi.connect(HTTPSERVER, HTTPPORT)) {
Serial.println("connected to Ubidots cloud");
} else {
Serial.println("could not connect to Ubidots cloud");
}
}
void loop(){
// Space to store values to send
char payload[200];
char str_val_1[30];
char str_lat[30];
char str_lng[30];
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0){
gps.encode(ss.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
}
/* IMPORTANT : Avoid sending a very long char as it take up a lot of memory. Send small char arrays instead */
sprintf(payload, "{\"");
sprintf(payload, "%s%s\":{\"value\":%s", payload, VARIABLE_LABEL_1, str_val_1);
sprintf(payload, "%s,\"context\":{\"lat\":%s, \"lng\":%s}}", payload, str_lat, str_lng);
sprintf(payload, "%s}", payload);
/* Calls the Ubidots POST function */
SendToUbidots(payload);
delay(5000);
}