I have a magnetic switch sensor. I want to set that sensor. when the magnetic is in the connected position, it will bring up the OPEN on the device API, and if released, it will show CLOSE. anyone know the coding is?i have newbie here. thanks before
With arduino or raspberry
NodeMCU, I want to display a string on “out resource”. but I can not find the right coding. if only display on the serial monitor in IDE Arduino can already, just can not display on the API. this is my code =
#define SENSOR_PIN D2
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
String sensor = “OPEN”, “CLOSE”;
void setup() {
pinMode(SENSOR_PIN, INPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
thing[“sensor”] >>(pson& out){ out = !digitalRead(SENSOR_PIN); };
}
void loop() {
thing.handle();
if (SENSOR_PIN!=HIGH){
out[“pintu 1”] >> “OPEN”;
}
else{
out[“pintu 1”] >> “CLOSE”;
}
}
}
which is in bold is the error
you can define a new function
const char * checksensor()
{
String reponse
if (SENSOR_PIN!=HIGH){
reponse = “open”
}
else{
reponse = “close”
}
return reponse;
}
in setup
thing[“sensor”] >>(pson& out){ out =checkseensor(); };
void loop() {
thing.handle();
}
okay,it works,thank you very much, but i had another problem. look at this program below.
#define DOOR_PIN D2
#define WINDOW_PIN D3
#define SUHU_PIN A0
String pintu = “”;
String jendela = “”;
String suhu = “”;
float vref = 3.3;
float resolution = vref/1023;
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
pinMode(DOOR_PIN, INPUT);
pinMode(WINDOW_PIN, INPUT);
pinMode(SUHU_PIN, INPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
}
void loop() {
thing.handle();
int baca1=digitalRead(DOOR_PIN);
int baca2=digitalRead(WINDOW_PIN);
float temperature = analogRead(SUHU_PIN);
if(baca1==LOW){
thing[“pintu”] >>[](pson& out){
String pintu = String(“DOOR OPEN”);
out = pintu;
};
}
else {
thing[“pintu”] >>[](pson& out){
String pintu = String(“DOOR CLOSED”);
out = pintu;
};
};
if(baca2==LOW){
thing[“jendela”] >>[](pson& out){
String jendela = String(“WINDOW OPEN”);
out = jendela;
};
}
else {
thing[“jendela”] >>[](pson& out){
String jendela = String(“WINDOW CLOSED”);
out = jendela;
};
};
temperature = (temperatureresolution);
temperature = temperature100;
if(temperature>=80){.
thing[“suhu”] >>[](pson& out){
String suhu = String(“FIRE AT KITCHEN!!!”);
out = suhu;
};
}
else {
thing[“suhu”] >>[](pson& out){
String suhu = String(“NONE”);
out = suhu;
};
};
}
there’s no error when i compiled it can be transferred succesfully. but, the device was connected under 10seconds then disconnected. previously, the program only uses 2 magnetic door and no problem. after I added for temperature readings, the device is always disconnected and only connected for less than 10 seconds. any solution???thanks before.
Hello @omen_emen,
Same mistake as in the other post, remember: never create resources into the loop function
best!
sorry, i made a program just use “pintu” and “jendela” resources before it, and it works, no errors.
the problem is just the reading temperature. something wrong with it. i cant find the errors because when i compiled in Arduino IDE, everything just fine.
sorry, i said “it works” not by your function above, but another function i made. i try your function,then an error comes. expected initializer before ‘if’