Need help to read sensor data with node.js server
Posted: Fri Jun 30, 2017 7:17 pm
Hello!
I have a pulse sensor connected to A0 of an ESP8266 Huzzah board.
I want to send the sensor data to a node.js server.
In the final version of this, I will be deploying everything on a local server without internet so I don't want to use ThingSpeak or other internet-based servers.
I'm having trouble figuring out how to access the sensor data from the server. The data comes in on ESP8266 analog pin and is processed so that it goes out serially:
I've combined two example codes, from the pulse sensor examples and from an excellent ESP8266 example found here: www.diyprojects.io
Here's the node.js server
Here's my loop() Thanks in advance!
I have a pulse sensor connected to A0 of an ESP8266 Huzzah board.
I want to send the sensor data to a node.js server.
In the final version of this, I will be deploying everything on a local server without internet so I don't want to use ThingSpeak or other internet-based servers.
I'm having trouble figuring out how to access the sensor data from the server. The data comes in on ESP8266 analog pin and is processed so that it goes out serially:
Code: Select all
pulseSensor.outputSample(); // returns Sensors, SensorCount
I've combined two example codes, from the pulse sensor examples and from an excellent ESP8266 example found here: www.diyprojects.io
Here's the node.js server
Code: Select all
/*
* Test server
* http://www.projetsdiy.fr
*/
var express = require('express');
var moment = require('moment');
var app = express();
app.get('/', function(req, res) {
res.send('HTTP ESP8266 Test Server')
});
app.post('/api/users', function(req, res) {
// not sure what goes here
res.send(''); // or here
});
app.use('/watchdog', function (req, res, next) {
var t = moment.duration(parseInt(req.param('uptime')), 'milliseconds')
var _message = req.param('ip') + " uptime " + t.hours() + "h " + t.minutes() + "m " + t.seconds() +"s";
console.log("watchdog from " + _message);
res.send("You are alive!\n");
});
app.listen(8080);
Here's my loop() Thanks in advance!
Code: Select all
void loop() {
unsigned long currentMillis = millis();
if ( currentMillis - previousMillis > watchdog ) {
previousMillis = currentMillis;
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
String url = "/watchdog?command=watchdog&uptime=";
url += String(millis());
url += "&ip=";
url += WiFi.localIP().toString();
// Send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
if (pulseSensor.sawNewSample()) {
if (--samplesUntilReport == (byte) 0) {
samplesUntilReport = SAMPLES_PER_SERIAL_SAMPLE;
pulseSensor.outputSample(); // returns Sensors, SensorCount
}
}
}
}