Since 2 months I have an ethernet Arduino running that fills a mysql database with data from the analog ports.
That runs without any issue.
Data - analog in - ethernet (with client.print("GET http://www.... etc).
That way I can take the data with php on my website and put it in in the database. I take a sample every 20 seconds.
However, next to that I want to stream the data to a website (by example every second) to show them live on screen (on the internet).
What is the best way to put that together? Pushing the data every second in the database is no option; that is too many.
for the good understanding: I want to stream the output every second to a webpage, but only every 20 seconds perform a storage in the database to analyse it afterwards.
OK I misunderstood.
Why not store all data in the database. It takes quite some time before you have a GB of data?
6 analog ports = 12 bytes per second = 1 MB/day + timestamp = 1.5 MB/day max.
For analysis you can use every 20th sample by adjusting your query.
SELECT * FROM samples WHERE seconds % 20 = 0;
sampling at the highest rate allows you to do analysis on every granularity afterwards.
I'm not sure what this means. Are you saying that your Arduino captures a sample from the analog ports every 20 seconds and sends it to the web server?
If not, how often does the Arduino take a sample and how often does it send a sample to the web server and how often does the web server write the sample to the database?
WannesNaf:
\That way I can take the data with php on my website and put it in in the database. I take a sample every 20 seconds.
...
I want to stream the data to a website (by example every second)
What is the best way to put that together?
So take a sample every second and send that to the web site via PHP. Every 20 requests, write one to the database.
Now I use this code to send the data.
I cannot make it faster bij decrementing the interval between 2 posts, otherwise the program stucks.
Is there a better way to send the data?
#include <DallasTemperature.h>
#include <OneWire.h>
#include <SPI.h>
#include <Ethernet.h>
// Data wire temp sensor is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DeviceAddress temp1 = { 0x28, 0xD2, 0x56, 0xF3, 0x03, 0x00, 0x00, 0xA0 };
DeviceAddress temp2 = { 0x28, 0xE1, 0x76, 0xF3, 0x03, 0x00, 0x00, 0xEC };
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = { 0xEF, 0x12, 0xDA, 0xAB, 0x17, 0x24};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress server(78,22,214,56); //IP of mysite
// initialize the library instance:
EthernetClient client;
unsigned long lastConnectionTime = 10; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 20*1000; // delay between updates, in milliseconds
void setup() {
// start serial port:
Serial.begin(9600);
//instellen temperatuursensors
sensors.begin();
sensors.setResolution(temp1, 10);
sensors.setResolution(temp2, 10);
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// temperaturen lezen
sensors.requestTemperatures();
float temp11 = sensors.getTempC(temp1);
float temp12 = sensors.getTempC(temp2);
delay(1000);
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
client.print("GET http://www.mysite.be/fillDB.php?t1=");
//fillDB.php = code to put the values in database
client.print(temp11);
client.print("&t2=");
client.print(temp12);
client.println(" HTTP/1.0");
client.println();
Serial.print(temp11);
Serial.print(" ");
Serial.println(temp12);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
A nice easy way which I am currently playing around with is to have your web page request the data from the Arduino using a RESTful web service setup and using json formatted data packets.
For the database logging that can happen at slower intervals is to either store to local storage, say SD card on your Arduino or to push to server remote database, which sounds it seems like you have under control ok.
Have you considered using a RESTful web service together with json formatted data?
How much data are you needing to request within the one second period?
Is the request period timing critical?
Have you looked into using the process of data change of state (COS) and push data to a server and then only poll from the server every so often to refresh all data?
I use the WebDuino library together with some javascript libraries to present live data on my web page using small json data packets while recording long term logged data for trending on the web page.
These things are what I am currently working on implementing.