en-USnl-NL
  |  Login

The code that drives the Arduino sensors

Minimize
Your Arduino microcontroller is a mini computer that is linked with your PC or laptop to the Internet thru an USB port.

Before your Arduino can communicate with your computer you have to install the Arduino driver and software first.
This is all available for free onthe Arduino website: Arduino Homepage
Go and download the code and driver if you haven't already done so, and install the driver to enable your Arduino to communicate with your computer.
The Arduino website will provide you with all the information you need on this topic.
After all is done and you have tested your Arduino installation sucessfully, you can proceed with the next step:

Programming your sensoring system.
An Arduino program is called: "A Sketch".
This is a copy of the sketch that I developed for this demo project. It is pretty self explaning with all the comments and remarks I put int it, but if you have questions, don't hasitate to use the forum to get informed.....
You can download it from this page.

/*
 Sketch START
 Analog Input demonstrates analog input by reading an analog sensor on analog pin 0 and
 
 * Note: Start Gobetwino before starting this sketch.
 
 */

int numSensors = 2; // Two sensors are defined: A Lightsensor and a Temperaturesensor. Every sensor counts for this value.

//To set the number of readings per hour you can set this variabele to tha number if readings you need.
//Example 1: For one reding on every minute set the numIntervalPerHour to 60. This will write the sensor every 1 hour to the sensor logfile.
//Example 2: For ten redings on every hour set the numIntervalPerHour to 6. This will write the sensor once every 10 minutes to the sensor logfile.
//Example 3: For 30 redings on every hour set the numIntervalPerHour to 30. This will write the sensor once every 2 minutes to the sensor logfile.
//Example 3: For 15 redings on every hour set the numIntervalPerHour to 15. This will write the sensor once every 15 minutes to the sensor logfile.
int numIntervalPerHour = 15; // A sensorreading will be done on every x minutes. An interval is one minute.

int boolHeaderFooter = 1;                                        //If 0 (Zero) NO hearder, nor footer will be printed; 1 (One) wille print the header and footer in the sensor file.
int numHours = 24;                                               //The sensor readings for Daylight and Temperature will span over one day. A day is defined as 24 hours.
int numWriteLinesPerHour = 60 / numIntervalPerHour;              //A sensor reading will be done on every interval, thus resulting in 60 minutes / inteval = 10 readings per hour when an interval is 6.
int maxnumLineCountDay = numHours * numWriteLinesPerHour;        //The maximum number of lines that will be written in 24 hours. Calculation: #OfSensors * #OfHours * #OfWritesPerHour
int maxnumLineCountWeek = numHours * numWriteLinesPerHour * 7;   //The maximum number of lines that will be written in 1 week. Calculation: #OfSensors * #OfHours * #OfWritesPerHour * 7 DaysPerWeek
int maxnumLineCountMonth = numHours * numWriteLinesPerHour * 31; //The maximum number of lines that will be written in 1 month. Calculation: #OfSensors * #OfHours * #OfWritesPerHour * 31 DaysPerMonth
long delayValue = numIntervalPerHour * 60000;                    //The variable to store the delay; ## intervals times ## milliseconds per minute (= x * 360000).

int numLineCount = 1;       //Start with numLineCount #1. Thie counter determines the moment that the sensor file will be writen to the DNN portal.
String strLineCount = "";   //This variabele holds the numLineCount to be printed in the sensor file.
int serInLen = 25;          //Thie variabele is used by Gobetwino. Do NOT alter this!
char serInString[25];       //Thie variabele is used by Gobetwino. Do NOT alter this!

// maxnumLineCount will determine the number of readings per sensor before the file is send to the DNN server.
// Select the needed sensor readings by un-commenting the maxLineCout that you need; leave the others as comment.
//int maxnumLineCount = 4;                         // Write the lines per reading to the server.
int maxnumLineCount = numWriteLinesPerHour;    // Write the lines per hour to the server.
//int maxnumLineCount = maxnumLineCountDay;       // Write the lines per day to the server.
//int maxnumLineCount = maxnumLineCountWeek;      // Write the lines per week to the server.
//int maxnumLineCount = maxnumLineCountMonth;     // Write the lines per month to the server.

// The sensorname and also determines the name of the table in the DotNetNuke database. They must be the same!
String A0_SensorName = "LightSensor";
int sensorPinLight = A0;          //Select the input pin for the lightsensor
String A1_SensorName = "TemperatureSensor";
int sensorPinTemperature = A1;    //Select the input pin for the temperaturesensor

int ledPin = 13;      // select the pin for the LED
int sensorValueLight = 0;  // variable to store the value coming from the lightsensor
int sensorValueTemperature = 0;  // variable to store the value coming from the temperaturesensor

String remarkTemperature;
int valueColdLowTolerance = 80;
int valueCold = 100;
int valueFresh = 450;
int valueModerate = 515;
int valueWarm = 610;
int valueHot = 650;
int valueHotHighTolerance = 750;

String remarkLight;
int valueDarkLowTolerance = 10;
int valueDark = 15;
int valueTwilight = 35;
int valueCloudy = 120;
int valueBright = 150;
int valueBrightHighTolerance = 200;

void setup() {
  //delayValue = 10000;  //For testing purposes. Set the delay to one second to shorten the testloop.

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);       // use the serial port
  Serial.flush();
  Serial.println("#S|COPYFILE|[]#");
  readSerialString(serInString,1000);
 
  // Write the first header to the sensor output file.
  if (boolHeaderFooter > 0) {
    Header();
  }
}

void loop() {
  // Read the sensor values into the value variabeles.
  sensorValueLight = analogRead(sensorPinLight);
  sensorValueTemperature = analogRead(sensorPinTemperature);
    strLineCount = numLineCount;
  // Convert the reading to meanigful information about the measured objects.
   if (sensorValueLight < valueDark){
    remarkLight = " - Dark";
  } else if ((sensorValueLight >= valueDark) && (sensorValueLight < valueTwilight)) {
    remarkLight = " - Twilight";
  } else if ((sensorValueLight >= valueTwilight) && (sensorValueLight < valueCloudy)) {
    remarkLight = " - Cloudy";
  } else if (sensorValueLight >= valueCloudy) {
    remarkLight = " - Bright";
  }
  logData(sensorValueLight, A0_SensorName, remarkLight);
    if (sensorValueTemperature < valueCold){
    remarkTemperature = " - Cold";
  } else if ((sensorValueTemperature >= valueCold) && (sensorValueTemperature < valueFresh)) {
    remarkTemperature = "- Fresh";
  } else if ((sensorValueTemperature > valueFresh) && (sensorValueTemperature < valueModerate)) {
    remarkTemperature = " - Moderate";
  } else if ((sensorValueTemperature >= valueModerate) && (sensorValueTemperature < valueWarm)) {
    remarkTemperature = " - Warm";
  } else if (sensorValueTemperature >= valueWarm) {
    remarkTemperature = " - Hot";
  }

  // Write the measured values to the sensor output file.
  logData(sensorValueTemperature, A1_SensorName, remarkTemperature);
 
  // Check wether the maxnumLineCount is reached.
  // If so; Save the sensor uotputfile, send it to the DNN server and crate e new one.
 
  if (numLineCount >= maxnumLineCount) {
    if (boolHeaderFooter > 0) {
      Footer();
    }
    Serial.println("#S|RENAME|[]#");
    Serial.println("#S|FTP|[]#");
    Serial.println("#S|COPYFILE|[]#");
    if (boolHeaderFooter > 0) {
      Header();
    }
    numLineCount = 1;  //Start the numLineCounter again for the next sensor file.
  } else if(numLineCount < maxnumLineCount) {
    // wait for the delaytime...
    delay(delayValue);
    numLineCount = numLineCount + 1;
  }
} // void loop

void logData( int value1, String sensorname, String remark)
{
   char buffer[5];
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(sensorname);
   Serial.print(";"); 
   Serial.print(itoa((value1), buffer, 10));
   Serial.print(";");
   Serial.print(remark + "; LC: " + strLineCount);
   Serial.println("]#");
   readSerialString(serInString,1000);
   Serial.flush();
   // There ought to be a check here for a non 0 return value indicating an error and some error handeling
} // void logData

void Header()
{
   Serial.flush();
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" =================================================================");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == Visual Data Webservices and Visual Data's Jumpstart" );
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == This file contains ");
   Serial.print(numSensors);
   Serial.print(" sensor objects");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == There are ");
   Serial.print(numIntervalPerHour);
   Serial.print(" intervals every hour per sensor");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == There are < ");
   Serial.print(numWriteLinesPerHour);
   Serial.print(" > lines every hour per sensor");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == There are << ");
   Serial.print(maxnumLineCountDay);
   Serial.print(" >> lines every day per sensor");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == There are <<< ");
   Serial.print(maxnumLineCountWeek);
   Serial.print(" >>> lines every week per sensor");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == There are <<<< ");
   Serial.print(maxnumLineCountMonth);
   Serial.print(" >>>> lines every month per sensor");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == The sensordelay is ");
   Serial.print(delayValue);
   Serial.print(" milliseconds per interval");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == Do NOT remove or change these lines!" );
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == They are used in your DNN portal to determine the" );
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == weeknumber and monthnumber. <c>Visual Data Webservices");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" =================================================================");
   Serial.println("]#");
   Serial.flush();
} //Header

void Footer()
{
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" =================================================================");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == Logfile; Total of " + strLineCount + " lines written per sensor");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" =================================================================");
   Serial.println("]#");
   Serial.print("#S|VDWLOGFILE|[");
   Serial.print(" == EndOfFile");
   Serial.println("]#");
} //Footer

//read a string from the serial and store it in an array
//you must supply the array variable - return if timeOut ms passes before the sting is read
void readSerialString (char *strArray,long timeOut)
{
   long startTime=millis();
   int i;
   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
} // void readSerialString

/*
Sketch END
*/

After loading this sketch into your Arduino you can test the program by starting the " Serial Monitor"  (most right icon on the Arduino menu) to see what happens.