Live data from photon shown on server


#1

Hi, I am very new to this and maybe don’t fully understand the entire what’s going ons. I have a photon set up with the bm180 temp and pressure sensor connected via i2c I have followed a tutorial and have manage to see the live data on a webpage on localhost:3000 . What I would like to do is have this webpage on a remote server but am unsure of how everything works together. Would I have to install node.is on a server, any help or explanation on the process would be fantastic…
Thanks…Ben


#2

Maybe I’ve been out of the Particle loop for too long, but I’m not sure what tutorial you followed? And I’m assuming since it’s port 3000, you set up a ruby on rails server? If you wanted to use the saeme methods, you’d have to most likely have to install the same on whatever server elsewhere you wanted to use. However, there are likely various easier ways to accomplish what you would like to do. For example, with the Particle cloud you could easily just directly access a variable with the address shown in the docs:

https://api.particle.io/v1/devices/0123456789abcdef/analogvalue?access_token=123412341234

#3

Would you mind sharing the code you’ve used for the webpage, along with details of what setup you’re using (ruby, HTML, Node.js, etc)? That’d save us the guessing and would help us help you better.


#4

Thanks for your replay

ok so i attended a short 1 Day course which was based around the photon.
i will try and explain what i did here
sorry if it does not make sense, i dont think iam the best at explaining things.

ok so first we had the photon and set up a particle account, downloaded the app and then had a play with turning on an led on an digital output.

then flashed the code as follows (sorry if there is a different way to add code please let me know)

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_BMP085/Adafruit_BMP085.h"

Adafruit_BMP085 bmp;

void InitializeBMP085(){
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {
        Serial.println("Faaaaaale");
    }
  }
}

// Publish Pressure, Altitude
void PublishBMP085Info(){
    char szEventInfo[64];

    sprintf(szEventInfo, "Temperature=%.2f °C, Pressure=%.2f hPa", bmp.readTemperature(), bmp.readPressure()/100.0);

    Serial.println(szEventInfo);
    Spark.publish("reading", szEventInfo);
}

// Initialize applicaiton
void InitializeApplication(){
    Serial.begin(9600);
    pinMode(D7, OUTPUT);
}

// Blink LED and wait for some time
void BlinkLED(){
    digitalWrite(D7, HIGH);
    delay(500);
    digitalWrite(D7, LOW);
}

void setup() {
    BlinkLED();
    InitializeApplication();
    InitializeBMP085();
}

void loop() {
    // Publish events. Wait for 2 seconds between publishes
    PublishBMP085Info();
    BlinkLED();
    delay(2000);
}

i think we then installed node.js on my local machine and was given a simple webpage to veiw the data.

<!doctype html>
<html>
<head>
  <title>Monitoring</title>
  <script src="/socket.io/socket.io.js"></script>
  <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
  <script src="/client.js"></script>
</head>
<body>
  <h1>Remote weather station readings</h1>
  <p id="content"></p>
</body>
</html>

in the same folder was given a file called socket.js which i believe to be a javascript file

var express = require('express');
var spark = require('spark');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

// !!! swap these for yours !!! //
var sokkit = {
  deviceId: 'id of my photon',
  accessToken: 'access token'
};

// express setup
app.use(express.static(__dirname + '/public'));

// spark setup
spark.on('login', function(err, body) {
  spark.getDevice(sokkit.deviceId, function(err, device) {
    sokkit.device = device;
  });
});
spark.login({accessToken: sokkit.accessToken});

// socket.io setup
io.on('connection', function(socket){
  console.log('a user connected');
  if (!sokkit.subscribed) {
    var device = sokkit.device;
    device.subscribe('reading', function(data) {
      console.log(data);
      io.emit('reading', data);
    });
    sokkit.subscribed = true;
  }
});

// launch the app
http.listen(3000, function(){
  console.log('listening on *:3000');
});

then in terminal navigated to the folder which contained this file and ran node sokkit.js & my terminal then said listening on 3000.

ok so i firstly want to understand what is goinig on. Am i right in saying that the photon forces data to the particle cloud after every time delay has expired then i have view the data on my local machine? where does node.js come in to it?? if i could just get my head round what is doing what i think i could reverse engineer it to get it to do what i want.

Thanks again and sorry if it does not make sense


#5

I’ve reformatted your code blocks.
To see how it’s done tap the pencil icon to edit your post and see how I did it.

Also look at
Forum Tips and Tricks

Code hints:

    while (1) {
        Serial.println("Faaaaaale");
    }

This will stall the cloud connection and hence stop you from flashing new code over the air.
Add a call to Particle.process() wherever you could be kept for more than 10sec.


#6

Thanks. so what i have is:

  1. photon
  2. partical cloud
  3. html page displaying the data
  4. node

so the way i see it is the photon is pushing the data to the cloud and my html page is getting it from there

so why is the need for node??? or what is it


#7

Mostly, yeah.

void loop() {
    // Publish events. Wait for 2 seconds between publishes
    PublishBMP085Info();
    BlinkLED();
    delay(2000);
}

That loops, as the name might suggest. It calls PublishBMP085Info(); followed by BlinkLED(); and then waits 2000ms before repeating. The comments says there’s a 2s delay, which isn’t really true.

void BlinkLED(){
    digitalWrite(D7, HIGH);
    delay(500);
    digitalWrite(D7, LOW);
}

In there, you’re waiting for another 500ms, so the total delay with be more like 2.5s. Not too big of a deal, just though you might want to know.

// Publish Pressure, Altitude
void PublishBMP085Info(){
    char szEventInfo[64];

    sprintf(szEventInfo, "Temperature=%.2f °C, Pressure=%.2f hPa", bmp.readTemperature(), bmp.readPressure()/100.0);

    Serial.println(szEventInfo);
    Spark.publish("reading", szEventInfo);
}

That will combine the data into a nice string, and publish it to the cloud using Server Sent Events.

Yeah, makes sense. SSE are something like Twitter/Snapchat. You listen to events, and can see them when they’re published. They’re currently not being stored on the cloud, so you can only retrieve them if you’re already listening, not after the fact.

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

In this case, it’s being used as a webserver to serve the HTML, and for piping through the Socket data, though I’m not sure what that’s actually being used for.

So, to conclude, depending on what you’re trying to do, you don’t need node.js. If you on;y want a page that works when it’s open, then HTML will do just fine. If you want to store data, and have it do stuff in the background (some kind of logic), then you’ll need a server of sorts. Even then, Node.js is not the only option, though it’s a very nice one.

If you could tell us more about what you’re trying to do, we might be able to offer some more specific advise.


#8

Thank you very much for your explanation. it has cleared a few things up for me.

so what i am trying to achieve is the same as this really (maybe read the value of a Digital input (On/Off) at a later date, but i want the HTML index page i will be opening to be on my ipage server and not on my local machine so i could open the page on any computer or phone and see the temp without listening on port 3000 what ever that is. for now im not to bothered about security.

once again thanks…Ben


#9

just starting to break the code down to see what does what.

// mycode
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_BMP085/Adafruit_BMP085.h" // this is the library needed for the bmp180



Adafruit_BMP085 bmp; //-------------What is this--------------------

//-----------------when does this run-------------------------------
//------------------------------------------------------------------

void InitializeBMP085(){
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {
        Serial.println("Faaaaaale");
    }
  }
}

// Publish Pressure, Altitude---------------------------------------------
// -----------------------------------------------------------------------
//------------------------------------------------------------------------

void PublishBMP085Info()
{
    char szEventInfo[64];

    sprintf(szEventInfo, "Temperature=%.2f °C, Pressure=%.2f hPa", bmp.readTemperature(), bmp.readPressure()/100.0);

    Serial.println(szEventInfo);
    Spark.publish("reading", szEventInfo);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// Initialize applicaiton-------------------------------------------------

void InitializeApplication(){
    Serial.begin(9600);
    pinMode(D7, OUTPUT);
}

// Blink LED and wait for some time--------------------------------------
//-----------------------------------------------------------------------
//-----------Class to blink and LED
void BlinkLED()
{
    digitalWrite(D7, HIGH);
    delay(500);
    digitalWrite(D7, LOW);
}
//------------------------------------------------------------------------
//-----------------------------------------------------------------------
//--------------setup define the classes-------------------------------
void setup()
{
    BlinkLED();
    InitializeApplication();
    InitializeBMP085();
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//-------------------Main loop of the program-----------------------------
void loop() {
    // Publish events. Wait for 2 seconds between publishes
    PublishBMP085Info();
    BlinkLED();
    delay(2000);
}

i hope i have added the code ok. i dont understand when “void InitializeBMP085(){” gets used

thanks