Status

    21:e November 2024

Arkiv

Den 29:e Oktober - 2023

Dagens projekt.


Coppans Blogg



Coppans Blogg



Coppans Blogg


// All the code is "public domain", and free to use for everyone.
// Please mention me if you find this usefull.
//
// All kod är fri för alla att använda.
// Om du finner den användbar så nämn vart du fick den ifrån.
//
// www.coppan.se

float VERSION = 2.0;

#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#define BLUE A1 // Blue LED
#define RED A2 // Red LED
#define HEATER_PIN A0 // Waterheater ON/OFF

#define KwH 1000 // pulses per kwh.
#define Pulse_Threshold 25000 // Min. time between pulses (ms).
#define Watt_Counter_Time 10000 // post data time (ms).
#define Watt_Time 36 // Magical number.
#define Restart_Counter 20 // Software reset after this amount of errors.

IPAddress ip(192, 168, x, x);
IPAddress gateway(192, 168, x, x);
IPAddress subnet(255, 255, 255, 0);
IPAddress dnServer(192, 168, x, x);
char server[] = "cp.se";

int Pulse = 0, Watt = 0, ErrorCounter = 0;
unsigned long LastPulse = 0UL, PulseLastTime = 0UL, WattLastTime = 0UL;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
delay(2000); // Dont know.

lcd.begin();
delay(1000); // One second is an iternity.

lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Starting ");
lcd.print(VERSION);

Wire.begin();
delay(1000); // One second is an iternity.

pinMode(HEATER_PIN, INPUT);
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(HEATER_PIN, LOW);

Ethernet.begin(mac, ip, dnServer, gateway, subnet);
delay(1000); // One second is an iternity.

if (client.connect(server, 80))
{
client.println("GET /cp/start.php?data=1 HTTP/1.0");
client.println("Host: cp.se");
client.println("");
delay(5);
client.stop();
}
else
{
lcd.setCursor(0,0);
lcd.print("No Network!");
delay(5000);
lcd.clear();

ErrorCounter++;
}

attachInterrupt(1, Blink, CHANGE); // Attach interupt #1.

lcd.clear();
}

void loop()
{
unsigned long CurrentTime = millis();

if (CurrentTime - WattLastTime >= Watt_Counter_Time)
{
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("WATT = ");
lcd.print(Watt * 10);

lcd.setCursor(0,1);

if (digitalRead(HEATER_PIN) == 0) lcd.print("Heater ON");
else lcd.print("Heater OFF");

WattLastTime = CurrentTime;
Watt = (Pulse - LastPulse) * Watt_Time;
LastPulse = Pulse;

if (client.connect(server, 80))
{
digitalWrite(BLUE, LOW);

ErrorCounter = 0;

client.print("GET /cp/watt.php?data=");
client.print(Watt);
client.print(",");

if (digitalRead(HEATER_PIN) == 0) client.print("1");
else client.print("0");

client.println(" HTTP/1.0");
client.println("Host: cp.se");
client.println("");
delay(5);
client.stop();
}
else
{
lcd.setCursor(0,1);
lcd.print("No Network! (");
lcd.print(Restart_Counter - ErrorCounter);
lcd.print(")");

digitalWrite(BLUE, HIGH);

ErrorCounter++;
if (ErrorCounter >= Restart_Counter) Software_Reset();
}
}

if (Pulse >= KwH)
{
Pulse = 0;

if (client.connect(server, 80))
{
digitalWrite(BLUE, LOW);

client.println("GET /cp/kwh.php HTTP/1.0");
client.println("Host: cp.se");
client.println("");
delay(5);
client.stop();
}
else digitalWrite(BLUE, HIGH);
}
}

void Blink()
{
unsigned long PulseCurrentTime = micros();

if (PulseCurrentTime - PulseLastTime >= Pulse_Threshold)
{
digitalWrite(RED, HIGH);

PulseLastTime = PulseCurrentTime;
Pulse++;
}
else digitalWrite(RED, LOW);
}

void Software_Reset()
{
asm volatile ("jmp 0");
}

Innehållet som publiceras här omfattas av grundlagsskydd. Detta inkluderar inte kommentarsfälten. Du som kommenterar är helt ansvarig för det du skriver.

Kommenterarer på "Dagens projekt."

A CkWare Production. Copyright 1996 - 2024. NightMode - Where Do You Wanne Sleep Tonight?

Sidan tog 0.55 sekunder att skapa.

Du kommer INTE från en mobil enhet. Du använder SSL
Du använder IPV4.