Chat freely about the open source Javascript projects for ESP8266

User avatar
By EdwardSMaTeresa
#94400 Hi
I have just entered the world of JavaScript and Node.JS. But i know C# quite well. I'm trying to make an Arduino collect some data and send it to my Ubuntu server(rented at DigitalOcean). I'm using an ESP8266 Arduino to make a post request(Arduino wifi module), and i'm sure that that part works.

The problem is, i want to recieve and display the data my Arduino sent on the server. But the JavaScript part does not seem to work (on the server).

Here is the code that the 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 for connecting");
if((WiFiMulti.run() == WL_CONNECTED)) {
sendData(data);
} else {
USE_SERIAL.println("Connecting faled, trying again");
}

delay(5000);
}
The way we thought of handling the post request was with JavaScript using the Express and body-parser module. Express has an example on setting the Express module and a get request up. But there is no example of a post request. Link to express's example: https://expressjs.com/en/starter/hello-world.html. Here is the code we ended up 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 is to receive and log the data which the Arduino sent to our server.

We hope for a nice feedback! Thank you very much.
User avatar
By quackmore
#94404 you'll have to specify the content-type for body-parser to work
this example will work if you add an header with "Content-type: text/plain;" to the POST in your arduino code

Code: Select allconst express = require('express')
const bodyParser = require('body-parser')
const app = express()

app.use(bodyParser.text()) // body-parse will look for 'text' content

app.get('/', (req, res) => {
    res.send('hello world')
})

app.post('/', (req, res) => {
    console.log("post triggered")
    console.log(req.body)
    res.send('got it')
})

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
})