ESP-12E Device Type Error

Hey all,

Please help me before I pull all my hair out. I am fairly new to Arduino/ESP/coding and am learning a lot but continue to be stumped. I am trying to upload temperature and humidity readings from two separate DHT22’s connected to an ESP-12E. I can get both sensors reading logical data from my basic Updated_Dual_Sensors code but cannot get any of the data to go to Losant. The main error I am getting seems to claim that my device does not name a type. The code for a single sensor, dual sensor, and Losant dual sensor are posted to my GitHub, link below. The errors are shown below in a SS. Any help would be appreciated.

GitHub Link

Hey @Aaron_Field,

Welcome to the Losant Forums! We’re happy you’re here.

I took a look at your code, specifically Losant_Dual_Sensor.ino, and have two things:

First, it looks like you’re creating a connection to Losant twice with this:

LosantDevice device(LOSANT_DEVICE_ID);
device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

and then having device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET); in your connect() function. This might be the reason for the device not naming a type error because you’re trying to connect to Losant before your device is connected to the internet.

Second, it looks like you’re trying to send an arduinojson JSON Document as state, where the Losant library accepts an object. You can cast your JSON Document to an object with:

JsonObject object = jsonbuffer.to<JsonObject>();
device.sendState(object);

Let me know if these suggestions help you out!

Thank you,
Heath

1 Like

While the first issue may be resolved (I am no longer getting the type error), the JSON change did not work. I have posted that code fragment below as well as the error.

void report(double t1, double h1, double t2, double h2) {
 StaticJsonDocument<1024> jsonbuffer;
 jsonbuffer["t1"] = t1;
 jsonbuffer["h1"] = h1;
 jsonbuffer["t2"] = t2;
 jsonbuffer["h2"] = h2;
 JsonObject object = jsonbuffer.to<JsonObject>();
 device.sendState(object);
 Serial.println("Reported!");
}

The error I am getting is:
\Losant_Dual_Sensor.ino: In function ‘void report(double, double, double, double)’: Losant_Dual_Sensor:128:3: error: ‘device’ was not declared in this scope
device.sendState(jsonbuffer);
^
exit status 1
‘device’ was not declared in this scope

From my understanding, the device was created in the LosantDevice command and named “device”. Does this in some way need to be brought into the void report section?

EDIT:

When adding the following:
LosantDevice device(LOSANT_DEVICE_ID);

before the StaticJsonDocument section, I can connect to Losant but get a keepalive timeout in 15 seconds. Could this be an issue with how I have the Losant device attribute set up? I also do not get “Reported!” printed on the serial monitor like I would expect.

EDIT:

I realize my LosantDevice was not defined globally where I had it before.

Small victories! I have gotten it to stay connected and report MQTT states. Unfortunately, those MQTT receipts are empty. This is the Losant error I am getting:

image

I am reporting at least so I know there is a consistent connection. I do need to figure out why my variables are not being put in correctly. I am assuming this has something to do with my JSON part of the code. I will work/learn and report back.

Finally got it!!! The code you suggested Heath worked with some coercion. The report section now looks like:

 void report(double t1, double h1, double t2, double h2) {
 StaticJsonDocument<400> jsonBuffer;
 JsonObject root = jsonBuffer.to<JsonObject>();
 root["t1"] = t1;
 root["h1"] = h1;
 root["t2"] = t2;
 root["h2"] = h2;
 device.sendState(root);
 Serial.println("Reported!");
}

Will update the GitHub with my final code after some messing around!