W3vina.COM Free Wordpress Themes Joomla Templates Best Wordpress Themes Premium Wordpress Themes Top Best Wordpress Themes 2012
syntax highlighter

syntax highlighter

jan 01

Hello world written in php withouth the [ php ] tag:


<?

?>

<?
echo «Hello world!»;
?>

with the [ php ] and [/ php ] tags:

<?
echo "Hello world!";
?>

Some large block with C code

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
#include <Ethernet.h>
#define rxPin 2
#define txPin 3
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress utesensor = { 0x28, 0x7E, 0xF9, 0x5E, 0x2, 0x0, 0x0, 0x42 }; // loddet på 3.5mm
// DeviceAddress innetemp = { 0x28, 0xF0, 0x12, 0x5F, 0x02, 0x0 , 0x0, 0x9E }; //  på hylla
DeviceAddress innesensor = { 0x28,0x2A,0xDE,0x5E,0x02,0x00,0x00,0x19 }; // I BODEN.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte gw[] = {10,0,0,101};
byte server[] = { 88,87,42,5 }; // gmx.no
byte subnet[] = { 255, 255, 255, 0 };

SoftwareSerial lcdSerial =  SoftwareSerial(rxPin, txPin);
int ledPin = 6;
int avgwatt = 5;
int switchPin = 7;
const int numReadings = 20;
unsigned long period_start;
unsigned long period_length = 57000;
unsigned long period_total;
unsigned long period_stop;
unsigned long run = 0;
int period_average;
int period_counter;
float itemp;
float utemp;

void setup()
{
  Serial.begin(9600);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  lcdSerial.begin(9600);
  sensors.begin();
  sensors.setResolution(innesensor, 12);
  sensors.setResolution(utesensor, 12);
}
void loop()
{
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcdcls();
  period_total = 0;
  period_counter = 0;
  period_start = millis();
  period_stop = period_start + period_length;
  if (run > 0) {
     lcdgoTo(0);
     lcdSerial.print(avgwatt);
     lcdSerial.print(" Current.  ");
     lcdgoTo(17);
     lcdSerial.print(period_average);
     lcdSerial.print(" Average.  ");

      }
      else {        // First run.. write no data.
     lcdgoTo(0);
     lcdSerial.print("No data.");
     lcdgoTo(17);
     lcdSerial.print("No data.");

      }

  while( millis() <period_stop) {
      lcdgoTo(0);
      avgwatt = getAvgWatt();   // returns average watt
      Serial.print("Snitt er: ");  // for debugging
      Serial.println(avgwatt);
      lcdSerial.print(avgwatt);
      lcdSerial.print(" Current.  ");
      // timedAction.check(); // check if its time to do the Senddata()
      period_total += avgwatt;
      period_counter += 1;

  }    // Etter at period- er utløpt. Dvs 1min. Vi regner ut avg og skriver til Linje2 på lcd.
   lcdcls();
   period_average = 0;
   period_average = period_total / period_counter;
   lcdgoTo(17);
   lcdSerial.print(period_average);
   lcdSerial.print(" Average.  ");
   sensors.requestTemperatures(); // Send the command to get temperatures
   delay(750);
   itemp = getTemp(innesensor);
   utemp = getTemp(utesensor);

   sendWatt();
   run +=1;
}

void sendWatt(){
  Ethernet.begin(mac, ip, gw, subnet);
  Client client(server, 80);
//   delay(1000);
  Serial.println();
  Serial.println("Initiates connection...");
  Serial.println("Connecting...");
  if (client.connect()) {
    Serial.println("Connected!");
    client.print("GET http://gmx.no/strom/regusage.php?watt=");
    client.print(period_average);
    client.print("&pw=--------&temp1=");
    client.print(itemp);
    client.print("&temp2=");
    client.print(utemp);
//    client.print(sensors.getTempC(innetemp));
    client.println(" HTTP/1.1");
    client.println("Host: www.gmx.no");
    client.println();
 //   delay(1000);             // Lar webserver rekke å returnere data før vi prøve å lese det ut.
  //  while(client.available()) {
  //    char c = client.read();
   //   Serial.print(c);
   //   }
    } else {
   Serial.println("Connection failed");
  }
   //stopper client
   client.stop();
    while(client.status() != 0) {
    delay(5);
    }

}

int getAvgWatt(){

  int watt = 0;
  int index = 0;
  float average= 0;
  float total = 0;
  int old_val = 0;
  unsigned long current_time = 0;
  unsigned long last_time = 0;
  float delayarray[numReadings];
  float pulsedelay = 0;
  last_time = millis();
  int val = LOW;
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  {
     delayarray[thisReading] = 0;
  }
   while (index < numReadings){

       val = (digitalRead(switchPin));              // Reads value  high/low and saves it
       if ((val == HIGH) && (old_val == LOW)) {     // We have light, lets read the time, and value.
       current_time = millis();                         // Current time
       pulsedelay = current_time - last_time;      	 // Delay since last pulse
       pulsedelay = pulsedelay / 1000;         		// I want seconds, not ms
       digitalWrite(ledPin, HIGH);                  // Blinks the LED
       delayarray[index] = pulsedelay;              // Add the value to the array
       old_val = val;        // Sets old_val high
       Serial.println(pulsedelay);					// THIS IS THE ONE THAT KEEPS IT FROM FREEZING

       last_time = current_time;                    // for the next whileloop-cycle to know the next delay.
       index +=1;

   }
   else {
       digitalWrite(ledPin, LOW); // No light detected
       old_val = val;
       }
  }

  for (int i = 1; i< numReadings;i++){    //Skip the first value in the array, since millis() from the start is off.
     total = total + delayarray[i];
   		}
     average = total / (numReadings-1); // Minus 1 since we skipped the first value in the array.

     watt = 360 / average; // 360 / average delay gives us current powerconsumption in watts.
     total = 0;  // for the next cycle
     average= 0; // for the next cycle

     // Empty out the array. Not sure if this is needed.
     // for (int y = 0; y < numReadings; y++) {
     //delayarray[y] = 0;
     //}
     return watt;
}

//SerialLCD Functions
void selectLineOne(){  //puts the cursor at line 0 char 0.
   lcdSerial.print(0xFE, BYTE);   //command flag
   lcdSerial.print(128, BYTE);    //position
}
void selectLineTwo(){  //puts the cursor at line 2 char 0.
   lcdSerial.print(0xFE, BYTE);   //command flag
   lcdSerial.print(192, BYTE);    //position
}

void lcdcls(){
  lcdgoTo(0);
  lcdSerial.print("                                ");
}

void clearLCD(){
   lcdSerial.print(0xFE, BYTE);   //command flag
   lcdSerial.print(0x01, BYTE);   //clear command.
}
void backlightOn(){  //turns on the backlight
    lcdSerial.print(0x7C, BYTE);   //command flag for backlight stuff
    lcdSerial.print(157, BYTE);    //light level.
}
void backlightOff(){  //turns off the backlight
    lcdSerial.print(0x7C, BYTE);   //command flag for backlight stuff
    lcdSerial.print(128, BYTE);     //light level for off.
}
void lcdgoTo(int position) { //position = line 1: 0-19, line 2: 20-39, etc, 79+ defaults back to 0
if (position<16){
  lcdSerial.print(0xFE, BYTE);   //command flag
  lcdSerial.print((position+128), BYTE);    //position
  }
  else if (position<32)
   {
   lcdSerial.print(0xFE, BYTE);   //command flag
   lcdSerial.print((position+47+128), BYTE);    //position
}
else { lcdgoTo(0); }
}

// Henter temp fra gitt sensor.
float getTemp(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  return tempC;
}

Leave a Reply

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *

Du kan bruke disse HTML-kodene og -egenskapene: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>