ESP8266 Battery Monitor fails to connect to losant

deep-sleep
esp8266
#1

Please I am having difficulty connecting my esp8266 battery monitor device to losant. I kept having this failure message: “esp8266 failed to authorize device to losant.validation error: the device id, access key, or access secret is not in the proper format”.

Here is my code:

/**
   An example showing how to put ESP8266 into Deep-sleep mode
*/

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Losant.h>

// WiFi credentials.
const char* WIFI_SSID = "Infinix Note 5";
const char* WIFI_PASS = "xxxxxxxxx";

// Losant credentials.
const char* LOSANT_DEVICE_ID = "5c6d7cbb9833d90008816887";
const char* LOSANT_ACCESS_KEY = "***";
const char* LOSANT_ACCESS_SECRET = "***";

WiFiClientSecure wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

void connect() {

  // Connect to Wifi.
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // WiFi fix: https://github.com/esp8266/Arduino/issues/2186
  WiFi.persistent(false);
  WiFi.mode(WIFI_OFF);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  unsigned long wifiConnectStart = millis();

  while (WiFi.status() != WL_CONNECTED) {
    // Check to see if
    if (WiFi.status() == WL_CONNECT_FAILED) {
      Serial.println("Failed to connect to WiFi. Please verify credentials: ");
      delay(10000);
    }

    delay(500);
    Serial.println("...");
    // Only try for 5 seconds.
    if (millis() - wifiConnectStart > 15000) {
      Serial.println("Failed to connect to WiFi");
      return;
    }
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println();
  Serial.print("Connecting to Losant...");

  Serial.print("Authenticating Device...");
  HTTPClient http;
  http.begin("http://api.losant.com/auth/device");
  http.addHeader("Content-Type", "application/json");
  http.addHeader("Accept", "application/json");

  /* Create JSON payload to sent to Losant
       {
         "deviceId": "575ecf887ae143cd83dc4aa2",
         "key": "this_would_be_the_key",
         "secret": "this_would_be_the_secret"
       }
  */

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["5c6d7cbb9833d90008816887"] = LOSANT_DEVICE_ID;
  root["***"] = LOSANT_ACCESS_KEY;
  root["***"] = LOSANT_ACCESS_SECRET;
  String buffer;
  root.printTo(buffer);

  int httpCode = http.POST(buffer);

  if (httpCode > 0) {
    if (httpCode == HTTP_CODE_OK) {
      Serial.println("This device is authorized!");
    } else {
      Serial.println("Failed to authorize device to Losant.");
      if (httpCode == 400) {
        Serial.println("Validation error: The device ID, access key, or access secret is not in the proper format.");
      } else if (httpCode == 401) {
        Serial.println("Invalid credentials to Losant: Please double-check the device ID, access key, and access secret.");
      } else {
        Serial.println("Unknown response from API");
      }
      Serial.println("Current Credentials: ");
      Serial.println("Device id: ");
      Serial.println(LOSANT_DEVICE_ID);
      Serial.println("Access Key: ");
      Serial.println(LOSANT_ACCESS_KEY);
      Serial.println("Access Secret: ");
      Serial.println(LOSANT_ACCESS_SECRET);
      return;
    }
  } else {
    Serial.println("Failed to connect to Losant API.");
    return;
  }

  http.end();

  device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  while (!device.connected()) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("Connected!");
  Serial.println("This device is now ready for use!");
}

void reportTemp(double degreesC) {
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["tempC"] = degreesC;
  device.sendState(root);
}

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);

  // Wait for serial to initialize.
  while (!Serial) { }

  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running Deep Sleep Firmware!");
  Serial.println("-------------------------------------");

  connect();

  int temp = analogRead(A0);

  double degreesC = temp*(27/1024.0)*1.0;

  Serial.println();
  Serial.print("Voltage V: ");
  Serial.println(degreesC);
  Serial.println();

  reportTemp(degreesC);

  Serial.println("Going into deep sleep for 20 seconds");
  ESP.deepSleep(20e6); // 20e6 is 20 microseconds
}

void loop() {
}

Please anyone with idea what I could do to resolve my connection issue?

#2

Hi @Johnson_Ogbe,

First thing, you shouldn’t share your access key/secret :). Device IDs are okay. I edited your post to take those out. But, I recommend regenerating those.

Second, I think I see your problem. It’s in this line:

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["5c6d7cbb9833d90008816887"] = LOSANT_DEVICE_ID;
  root["**"] = LOSANT_ACCESS_KEY;
  root["***"] = LOSANT_ACCESS_SECRET;
  String buffer;
  root.printTo(buffer);

It should just be:

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["deviceId"] = LOSANT_DEVICE_ID;
  root["key"] = LOSANT_ACCESS_KEY;
  root["secret"] = LOSANT_ACCESS_SECRET;
  String buffer;
  root.printTo(buffer);

No need to recopy your credentials here. The only place you should put your Losant credentials are here:

// Losant credentials.
const char* LOSANT_DEVICE_ID = "device-id";
const char* LOSANT_ACCESS_KEY = "access-key";
const char* LOSANT_ACCESS_SECRET = "access-key";
1 Like
#3

Thanks bro, you did it. your suggestion solved it.
Please I am stuck sending my payload: my voltage is not been sent/received by losant. the code seems not to be sending the voltage to losant: please help me out.

My code again pls:

/**
An example showing how to put ESP8266 into Deep-sleep mode
*/

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Losant.h>

// WiFi credentials.
const char* WIFI_SSID = “Infinix Note 5”;
const char* WIFI_PASS = “xxxxxxxxxxxxxxx”;

// Losant credentials.
const char* LOSANT_DEVICE_ID = “5c6d7cbb9833d90008816887”;
const char* LOSANT_ACCESS_KEY = “xxxxxxxx-xxxxxxxxx-xxxxxxx”;
const char* LOSANT_ACCESS_SECRET = “xxxxxxxxxxxxx-xxxxxxxxxxxxxxxx-xxxxxxxxxxx”;

WiFiClientSecure wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

void connect() {

// Connect to Wifi.
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);

WiFi.begin(WIFI_SSID, WIFI_PASS);

// WiFi fix: https://github.com/esp8266/Arduino/issues/2186
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);

unsigned long wifiConnectStart = millis();

while (WiFi.status() != WL_CONNECTED) {
// Check to see if
if (WiFi.status() == WL_CONNECT_FAILED) {
Serial.println("Failed to connect to WiFi. Please verify credentials: ");
delay(10000);
}

delay(500);
Serial.println("...");
// Only try for 5 seconds.
if (millis() - wifiConnectStart > 15000) {
  Serial.println("Failed to connect to WiFi");
  return;
}

}

Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

Serial.println();
Serial.print(“Connecting to Losant…”);

Serial.print(“Authenticating Device…”);
HTTPClient http;
http.begin(“http://api.losant.com/auth/device”);
http.addHeader(“Content-Type”, “application/json”);
http.addHeader(“Accept”, “application/json”);

/* Create JSON payload to sent to Losant
{
“deviceId”: “575ecf887ae143cd83dc4aa2”,
“key”: “this_would_be_the_key”,
“secret”: “this_would_be_the_secret”
}
*/

StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root[“deviceId”] = LOSANT_DEVICE_ID;
root[“key”] = LOSANT_ACCESS_KEY;
root[“secret”] = LOSANT_ACCESS_SECRET;
String buffer;
root.printTo(buffer);

int httpCode = http.POST(buffer);

if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
Serial.println(“This device is authorized!”);
} else {
Serial.println(“Failed to authorize device to Losant.”);
if (httpCode == 400) {
Serial.println(“Validation error: The device ID, access key, or access secret is not in the proper format.”);
} else if (httpCode == 401) {
Serial.println(“Invalid credentials to Losant: Please double-check the device ID, access key, and access secret.”);
} else {
Serial.println(“Unknown response from API”);
}
Serial.println("Current Credentials: ");
Serial.println("Device id: ");
Serial.println(LOSANT_DEVICE_ID);
Serial.println("Access Key: ");
Serial.println(LOSANT_ACCESS_KEY);
Serial.println("Access Secret: ");
Serial.println(LOSANT_ACCESS_SECRET);
return;
}
} else {
Serial.println(“Failed to connect to Losant API.”);
return;
}

http.end();

device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

while (!device.connected()) {
delay(1000);
Serial.print(".");
}

Serial.println(“Connected!”);
Serial.println(“This device is now ready for use!”);
}

void reportTemp(double degreesC) {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root[“tempC”] = degreesC;
device.sendState(root);
}

void setup() {
Serial.begin(115200);
Serial.setTimeout(2000);

// Wait for serial to initialize.
while (!Serial) { }

Serial.println(“Device Started”);
Serial.println("-------------------------------------");
Serial.println(“Running Deep Sleep Firmware!”);
Serial.println("-------------------------------------");

connect();

int temp = analogRead(A0);

double degreesC = temp*(27/1024.0)*1.0;

Serial.println();
Serial.print("Voltage V: ");
Serial.println(degreesC);
Serial.println();

reportTemp(degreesC);

Serial.println(“Going into deep sleep for 20 seconds”);
ESP.deepSleep(20e6); // 20e6 is 20 microseconds
}

void loop() {
}