express - How to handle a post request in JavaScript from Arduino ESP8266 -


hey stackoverflow community. have entered world of javascript , node.js. know c# quite well. i'm trying make arduino collect data , send ubuntu server(rented @ digitalocean). i'm using esp8266 arduino make post request(arduino wifi module), , i'm sure that part works.

the problem is, want recieve , display data arduino sent on server. javascript part not seem work (on server).

the esp8266 uses library here: https://github.com/esp8266/arduino. here code esp8266 arduino uses.

include arduino.h include esp8266wifi.h include esp8266wifimulti.h include esp8266httpclient.h  #define use_serial serial esp8266wifimulti wifimulti;  string ssid = "yourwifiname"; string password = "yourwifipassword"; string website = "http://165.227.138.230/"; string data = "20";  void setup() {  use_serial.begin(115200); // use_serial.setdebugoutput(true);  use_serial.println(); use_serial.println(); use_serial.println();  for(uint8_t t = 4; t > 0; t--) {     use_serial.printf("[setup] wait %d...\n", t);     use_serial.flush();     delay(1000); }  wifimulti.addap("drach", "a5b4c3d2e1"); }  void senddata(string data) { httpclient http; use_serial.print("attempting upload to"); use_serial.println(website); http.begin("http://165.227.138.230/" + data); int code = http.post("doge");  use_serial.print("response code = "); use_serial.println(code);  if ( code > 0 ) { use_serial.println("upload succeeded"); } if ( code == 0 ) { use_serial.println("upload failed"); } http.end(); use_serial.println("http.end"); }  void loop() {  use_serial.println("waiting connecting"); if((wifimulti.run() == wl_connected)) {   senddata(data); } else {   use_serial.println("connecting faled, trying again"); }  delay(5000); } 

the way thought of handling post request javascript using express , body-parser module. express has example on setting express module , request up. there no example of post request. link express's example: https://expressjs.com/en/starter/hello-world.html. here code ended with.

var express = require('express'); var bodyparser = require('body-parser'); var app = express();  var urlencodedparser = bodyparser.urlencoded({ extended: false });  app.get('/', function (req, res) { res.send('hello world!'); });  app.post('/', urlencodedparser, function(req, res){ console.log("post triggered"); console.log(req.body); });   app.listen(3000, function () { console.log('example app listening on port 3000!'); }); 

our goal receive , log data arduino sent our server.

we hope nice feedback! thank much.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -