AnalogDemo: plot data and upload to Pachube in C#

luca 08/11/2011 2

Let’s analyze the source code:

In Form1 constructor the main objects are created and configured; especially the chartArea of the object chart1, with changes on axis properties (intervals, minimum and maximum values, colors).
X axis is configured to show 300 seconds of data (CHART_SECONDS constant):

void setup() {
void loop() {
  int randomValue = random(0, 1023);
  Serial.println(randomValue, DEC);


void loop() {

int randomValue = random(0, 1023);
Serial.println(randomValue, DEC);

Also in constructor is created the data series to contain received data and  some of its parameters are changed (name, chart type, line width and color, value type for X axis):

mySerie = new Series("AnalogInput");
mySerie.ChartType = SeriesChartType.FastLine;
mySerie.BorderWidth = 1;
mySerie.Color = Color.Red;
mySerie.XValueType = ChartValueType.DateTime;

In the methods called by form load and click on Connect button, we manage the serial port, as we learned in different tutorials on this site:

  • list the available ports and add them to a combobox
  • create a SerialPort object and open the connection (9600 baud 8N1)
  • add an handler to the event DataReceived

serialPort_DataReceived method, handler of DataReceived, calls – using a delegate method – AddData() which updates chart and datastream.

That method read incoming value from serial port and add it to the chart, setting actual timestamp as the value on X axis:

DateTime timeStamp = DateTime.Now;
string stringValue = serialPort.ReadLine();
chart1.Series[0].Points.AddXY(timeStamp, Double.Parse(stringValue));

Next, it calculates the first value to be shown, substracting 300 seconds from the actual timestamp and removing values older than the calculated timestamp. Finally it moves X axis to be centered on the remaning values (setting the first of them origin of the axis) and it invalidates the chart for redraw it:

double removeBefore = timeStamp.AddSeconds(-CHART_SECONDS).ToOADate();
while (mySerie.Points[0].XValue < removeBefore)
chart1.ChartAreas[0].AxisX.Minimum = mySerie.Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(mySerie.Points[0].XValue).AddSeconds(CHART_SECONDS).ToOADate();

If Pachube update is flagged, it checks if all the parameters are present and uses WebClient object to call with PUT method the URL to update your datastream:

string updatePath = "" + tbFeedId.Text + "/datastreams/" + tbDatastreamId.Text + ".csv";

adding in the HTTP headers the specified API Key and  catching any connection errors:

webClient.Headers.Set("X-PachubeApiKey", tbAPIKey.Text);
    webClient.UploadString(updatePath, "PUT", stringValue);

To simplify, I choosed to update data in csv format, as shown in the  API documentation.

In summary, I remember you that all the material (pre-compiled program and source code) is available in my GitHub repository and that I’m here for questions in this site’s forum.

Pages: 1 2 3


  1. mario 25/01/2012 at 09:13 - Reply

    Ciao Luca, progettino interessante, ho trovato in giro questa utility per Excel, solo fino al 2003, per rappresentazione grafica di dati da seriale.
    molto interessante se non l’hai già vista.
    Ci si vede

Leave A Response »

This website uses cookies to ensure you get the best experience on our website maggiori informazioni

Questo sito utilizza i cookie per fonire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o clicchi su "Accetta" permetti al loro utilizzo.