Bojarlukasz / IoT_NodeMCU_ESP8266

IoT - Nginx, Node.js, mongoDB, Express, NodeMCU_v3 with ESP8266 and DHT22 sensor
15 stars 4 forks source link

ESP8266 Only #2

Open SherlockHolmes95 opened 6 years ago

SherlockHolmes95 commented 6 years ago

Hi, First I want to thank you so much for your work. I am new to Javascipt and ESP8266 and I'm working on a school project and I should communicate DHT22 sensor data from ESP8266 to MongoDB with node.js as you did, and since you used some LEDs in your project, I cant seem to find the lines that I should remove and keep communicating only the DHT22 temperature/humidity data to MongoDB. I really need some help here since I'm new to this and the deadline of my project presentation is getting closer. Thank you so much.

Bojarlukasz commented 6 years ago

​​ ​​ ​​

2018-05-16 12:27 GMT+02:00 SherlockHolmes95 notifications@github.com:

Hi, First I want to thank you so much for your work. I am new to Javascipt and ESP8266 and I'm working on a school project and I should communicate DHT22 sensor data from ESP8266 to MongoDB with node.js as you did, and since you used some LEDs in your project, I cant seem to find the lines that I should remove and keep communicating only the DHT22 temperature/humidity data to MongoDB. I really need some help here since I'm new to this and the deadline of my project presentation is getting closer. Thank you so much.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Bojarlukasz/IoT_NodeMCU_ESP8266/issues/2, or mute the thread https://github.com/notifications/unsubscribe-auth/AOZJ7C3DJZhHnen7yULB4tlSoljyCVtPks5ty_75gaJpZM4UBDNg .

​Hi,

No problem. 1) We have define button element in our template to control the LED diode status (red LED is connected to the ESP8266 module to line D2 as shown as bellow https://github.com/Bojarlukasz/IoT_NodeMCU_ESP8266#board-scheme). This html form element (https://www.w3schools.com/tags/att_form_action.asp) is sending the form-data (GET request) to our node.js when a form is submitted.

Button declaration in file: ./Node.js/views/chart.handlebars :

....

LED IS \{{led_state}}

...

2) In file ./Node.js/server.js : we define that variables and routes which will be used (among others) to operate this button, so when http GET request arrives we call js functions. Our js functions to operate button:

//Save switch status - ON app.get("/button_ledOn", function(req, res){ db2.updateOne({"name" : "button"},{ $set: {"color" : "green"}}, function(err, results) { //console.log(err, results); res.render("chart"); }); }); //Save switch status - OFF app.get("/button_ledOff", function(req, res){ db2.updateOne({"name" : "button"},{ $set: {"color" : "red"}}, function(err, results) { //console.log(err, results); res.render("chart"); }); });

Bojarlukasz commented 6 years ago

​​ In this functions button state is saved in our DB.

3) Then when nodeMCU is calling to check this state : ./NodeMCU/iRoom.ino :

... led_on_off(nodejs_ip); ... void led_on_off(String nodejs_ip){ HTTPClient http; http.begin("http://" + (String)nodejs_ip + "/switchState"); http.addHeader("Content-Type", "text/plain"); http.GET(); //Send the request String payload = http.getString(); //Get the response payload Serial.println(payload); //Print request response payload http.end(); Serial.println(payload); if(payload == "ON"){ digitalWrite(LED, HIGH); }else{ digitalWrite(LED, LOW); } } ...

we have route in the same js file ( ./Node.js/server.js) to check status in DB and return to NodeMcu: ... //Check switch state app.get("/switchState", function(req, res){ db2.find({}).toArray(function(err, results) { if (err) { res.send("There was a problem adding the information to the database."); }else if (results[0]['color'] == 'red') { res.send("OFF"); }else if (results[0]['color'] == 'green') { res.send("ON"); }else{ res.send("Error"); } }); }); .... and then we check the return value ' payload' to turn on or off the LED.

Bojarlukasz commented 6 years ago

So you have to remove all of this code and that will probably solve your problem. Sorry for the mistakes ;)

SherlockHolmes95 commented 6 years ago

Hi, Thank you so much for your help.

On 16 May 2018 at 14:53, Bodzaro notifications@github.com wrote:

So you have to remove all of this code and that will probably solve your problem. Sorry for the mistakes ;)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Bojarlukasz/IoT_NodeMCU_ESP8266/issues/2#issuecomment-389548470, or mute the thread https://github.com/notifications/unsubscribe-auth/AjLWz8UBu9RiPNJUdVpH2s7pIzUq25A0ks5tzD2GgaJpZM4UBDNg .