Coding for temperature measurement using LM35 and NodeMCU


#1

hy all,
i have a project that is measuring temperature using ic lm35 and nodemcu. When the program is compiled successfully. but when connected to the web thinger.io, the device can only connect for less than 10 seconds, then disconnect. is there any wrong code ?? the following programs:

#define SSID “Redmi 3”
#define SSID_PASSWORD “sandiku123”

#define SUHU_PIN A0

String suhu = “”;
int suhu1 = 0;

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
pinMode(SUHU_PIN, INPUT);
Serial.begin(9600);
thing.add_wifi(SSID, SSID_PASSWORD);

}

void loop() {
thing.handle();

float temp = analogRead(SUHU_PIN);
float suhu1 = temp*0.322580645;
Serial.println(“suhu1”);
delay(500);
if(suhu1>=80){
thing[“suhu”] >>[](pson& out){
String suhu = String(“FIRE AT KITCHEN!!!”);
out = suhu;
};
}
else {
thing[“suhu”] >>[](pson& out){
String suhu = String(“NONE”);
out = suhu;
};
};
}

thanks in advance.


#2

Hello @omen_emen!

The problem here is that you have defined the resources in the loop() function, and that is not right. This code should be created in the setup(), and then the thing.handle() instruction will run it only when it is necessary.

By other side, I can see that you want to program an alert behavior. The correct way to do this is making and stream (you can read how to do that here), or event better, making an e-mail endpoint call (which is documented here)

so to fix the problem you have to make two things:
1-create the output resource in the setup
2-create a global status variabe (suhu in your code)
3-add some stream or call_endpoint instructions, being careful to call this instructions allways (at any loop execution) with some limitation, because if you make thousland calls or streams per second, the server may lock your device.

hope you find this usefull