Displaying Serial data to web page


i'm attempting gather data 1 arduino via serial , display on second running web server.  initially doing using string on web arduino, had lots of mis-reads.  i did lots of reading , searching , found examples of code (probably written of reading this) uses char arrays.

i have 2 problems not critical, annoyances , don't understand how fix them (like people beginner @ programming).


- if hit "refresh" button on web page (not browser) page reloads, without data. if hit again, data.

- if repeatedly (and quickly) click "refresh" button data, partial data. it's if "weather arduino in middle of sending data, stopped , resent it.

i think both of these problems related somehow not receiving data before displaying it, i'm @ loss how fix it. think 2 clicks refresh means on 2nd click i'm displaying data first click.

i tried inserting delay() statements in various locations, did not help, after more reading gathered should using millis(). while works delay, doesn't appear solve problem. (or maybe don't understand place them?)

the weather arduino code code page:
https://learn.sparkfun.com/tutorials/weather-station-wirelessly-connected-to-wunderground
https://github.com/sparkfun/wimp_weather_station/blob/master/wimp_weather_station.ino

the uno's connected serially pins 0 , 1

my code long show inline, have attached it.
[removed attachment awol posted me]

if have thoughts of how should go fixing this, please share!

thanks,
joel

code: [select]
#include <spi.h>
#include <ethernet.h>

byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed };
//byte ip[] = { 192, 168, 2, 201 };   // static ip of arduino
//byte gateway[] = { 192, 168, 2, 254 };  // gateway address
//byte subnet[] = { 255, 255, 255, 0 };  //subnet mask
ethernetserver server(80);   //web server port

//these variables parsing & display
const byte numchars = 1000;
char receivedchars[numchars];
char winddir[32] = {0};
char windspeedmph[32] = {0};
char windgustmph[32] = {0};
char windgustdir[32] = {0};
char windspdmph_avg2m[32] = {0};
char winddir_avg2m[32] = {0};
char windgustmph_10m[32] = {0};
char windgustdir_10m[32] = {0};
char humidity[32] = {0};
char tempf[32] = {0};
char rainin[32] = {0};
char dailyrainin[32] = {0};
char pressure[32] = {0};
char batt_lvl[32] = {0};
char light_lvl[32] = {0};
char recvchar;
boolean newdata = false;

unsigned long interval=2000; //time needed wait (in milliseconds)
unsigned long previousmillis=0; // millis() returns unsigned long.

void setup() {
  serial.begin(9600);
  // put setup code here, run once:

  //start ethernet
 // ethernet.begin(mac, ip, gateway, subnet);
  ethernet.begin(mac);  // mac means ethernet dhcp.

}
ethernetclient client;
void loop() {
  // put main code here, run repeatedly:

  //start string serial receiver/parser
  recvwithstartendmarkers();

  //start web server (note, following commented-out line broken outside loop() showparseddata() can use "client."
  //ethernetclient client = server.available();
  client = server.available();
  if ((unsigned long)(millis() - previousmillis) >= interval) {
              previousmillis = millis();
  if (client) {
    // http request ends blank line
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //the next block of code figures out if have been asked send control codes weather station
        string header(c);
        if (header.indexof("!") > -1) {
            //get data serial
            serial.write("!"); //tell weather arduino want fresh data! (this trigger recvwithstartendmarkers() run sent data via serial)
        }
        if (header.indexof("@") > -1) {
          //midnight reset (this need setup machine via cron)
          serial.write("@");
        }
        if (header.indexof("%") > -1) {
          serial.write("#"); //send hardreset/reboot
        }
       
        header=""; //clearing string next read
         if (c == '\n') {
           //if ((unsigned long)(millis() - previousmillis) >= interval) {
           //   previousmillis = millis();
     
          // send standard http response header
          client.println("http/1.1 200 ok");
          client.println("content-type: text/html");
          client.println("connection: close");  // connection closed after completion of response
          client.println();
          client.println("<head><title>clarknet manhouse weather station</title></head>");
          client.println("<html>");
          client.println("<br><a href=\"/!\">get data</a>");  //have escape ""'s aorund link /!
          client.println("<br><a href=\"/@\">reset daily</a>");
          client.println("<br><a href=\"/%\">reboot weather arduino</a><br>");
         
          shownewdata();         
          client.println("</html>");
          break;
          //}
        }
      }
    }
    // give web browser time receive data
    delay(1);
    // close connection:
    client.stop();
   
    ethernet.maintain();
  }
  }
}

void recvwithstartendmarkers() {
  static boolean recvinprogress = false;
  static byte ndx = 0;
  char startmarker = '$';
  char endmarker = '#';
  char rc;
 
  while (serial.available() > 0 && newdata == false) {
    rc = serial.read();

    if (recvinprogress == true) {
      if (rc != endmarker) {
        receivedchars[ndx] = rc;
        ndx++;
        if (ndx >= numchars) {
          ndx = numchars - 1;
        }
      }
      else {
        receivedchars[ndx] = '\0'; // terminate string
        recvinprogress = false;
        ndx = 0;
        newdata = true;
      }
    }

    else if (rc == startmarker) {
      recvinprogress = true;
    }
  }
}
 
void parsedata() {

  // split data parts
   
  char * strtokindx; // used strtok() index

  //we take comma separated values , break them apart own variables.
  strtokindx = strtok(receivedchars,",");      // first part - string
  strcpy(winddir, strtokindx); // copy variable
  strtokindx = strtok(null, ","); // continues previous call left off
  strcpy(windspeedmph, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(windgustmph, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(windgustdir, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(windspdmph_avg2m, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(winddir_avg2m, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(windgustmph_10m, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(windgustdir_10m, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(humidity, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(tempf, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(rainin, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(dailyrainin, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(pressure, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(batt_lvl, strtokindx); // copy variable
  strtokindx = strtok(null, ",");
  strcpy(light_lvl, strtokindx); // copy variable
}


void showparseddata() {
  client.print("<br>");
  client.println(winddir);
  client.print("<br>");
  client.println(windspeedmph);
  client.print("<br>");;
  client.println(windgustmph);
  client.print("<br>");
  client.println(windgustdir);
  client.print("<br>");
  client.println(windspdmph_avg2m);
  client.print("<br>");
  client.println(winddir_avg2m);
  client.print("<br>");
  client.println(windgustmph_10m);
  client.print("<br>");
  client.println(windgustdir_10m);
  client.print("<br>");
  client.println(humidity);
  client.print("<br>");
  client.println(tempf);
  client.print("<br>");
  client.println(rainin);
  client.print("<br>");
  client.println(dailyrainin);
  client.print("<br>pressure=");
  client.println(pressure);
  client.print("<br>");
  client.println(batt_lvl);
  client.print("<br>");
  client.println(light_lvl);
}


void shownewdata() {
  if (newdata == true) {
    parsedata();
    showparseddata();
    newdata = false;
  }
}



Arduino Forum > Using Arduino > Programming Questions > Displaying Serial data to web page


arduino

Comments