Web based ATV Relay Control

Aus DL8RDS Wiki
Version vom 27. Dezember 2017, 03:31 Uhr von Dl8rds (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „== Scope == Out ATV relay DB0MHB is a microcontroller based system that can be controlled through DTMF tones, but is still lacking HAMNET support even though…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

1 Scope

Out ATV relay DB0MHB is a microcontroller based system that can be controlled through DTMF tones, but is still lacking HAMNET support even though we have HAMNET connectivity at our location.

In order to control it through a web page, we decided to upgrade it with pinouts from the DTMF panel. These pinouts will be translated in an ARDUINO Mega 1280 which has a sufficient number of GPIOs, and the Arduino is supposed to provide a serial interface to the relay.

The serial interface will be controlled again through a Raspberry Pi that does all the rest.

The most remarkable feature is a shell like interface that allows to invoke commands and provides a history function.

2 Useful Links

3 Arduino Code

#include "CommandLine.h"
#include <Wire.h>

#define SensorAdresse 0x48 // Basisadresse für ersten Temperatursensor
// Registerparameter fuer get_LM75_temperature
#define TEMP 0  // Temperaturregister anwählen

// LM75 Configuration Register Registeradresse: 1
// Bit 0: Stromsparmodus, bei 1 geht Temperatursensor in den Stromsparmodus (keine Messung, aber aktive Steuerung) Ausgang wird auch abgeschaltet
//                        bei 0 geht Temperatursensor aus dem Stromsparmodus (Messung) Ausgang wird wieder freigegeben  
// Bit 1: Interrupt Modus, bei 1 schaltet der Ausgang sowohl bei oberen als auch unteren Schwellwert ein, wird zurückgesetzt durch Auslesen des Registers
//                         bei 0 schaltet der Ausgang bei oberen Schaltpunkt ein und bei unteren aus (default 80°C / 75°C)
// Bit 2: OS-Pin bei 1 wird das Verhalten des Ausgangs invertiert, Ausgang ist eingeschalten innerhalb der Schwellwerte
//               bei 0 Ausgang schaltet bei Überschreiten der eingestellten Schwellwerte
// Bit 3 und 4: Wert 0-3, besagt wieviele Messzyklen abgewartet wird, bis Ausgang aktiv/inaktiv wird, wenn die Bedingung erfüllt ist (verhindert Flattern des Ausgangs)
// Bit 5-7 müssen 0 sein
// Byte: 7 6 5 4 3 2 1 0

// LM75 Temperatur auslesen. Device = 0-7, regx = TEMP, OBEN, UNTEN (Registerauswahl)  
double get_LM75_temperature(int device, int regx)
{
  int8_t msb;
  int8_t lsb;
  int8_t msb1;
  Wire.beginTransmission(SensorAdresse + device);
  Wire.write(regx);
  Wire.endTransmission();
  Wire.beginTransmission(SensorAdresse + device);
  Wire.requestFrom(SensorAdresse + device, 2);
  if (Wire.available()) {
     msb1 = Wire.read();
     msb = msb1 << 1; // Vorzeichenbit entfernen, verbliebener Wert ist nun doppelt so groß
     lsb = Wire.read();
  }
  // höchstes bit von lsb sagt aus, ob 0,5 Grad dazu addiert werden sollen
  lsb = (lsb & 0x80 ) >> 7; // nun ist lsb = 0 oder 1
  Wire.endTransmission();
  if (msb1 < 0x80) { // Positiver Wert?
    return double(msb + lsb)/2; // positiver Wert
  }  
  else {
    return double(msb + lsb)/2 - 128; // negativer Wert
  }  
}



// Keep track of the count, for the count command.
int count = 0;

// CommandLine instance.
CommandLine commandLine(Serial, "> ");

// Commands are simple structures that can be.
Command set     = Command("set", &handleSet);
Command temp    = Command("temp", &handleTemp);
Command state   = Command("state", &handleState);

/**
 * Setup serial port and add commands.
 */
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);

  // Pre-defined commands
  commandLine.add(set);
  commandLine.add(temp);
  commandLine.add(state);

  // On-the-fly commands -- instance is allocated dynamically
  commandLine.add("help", handleHelp);
}

/**
 * Read-eval-print-loop.
 */
void loop()
{
  commandLine.update();
}

/**
 * Handle the count command. The command has one additional argument that can be the integer to set the count to.
 *
 * @param tokens The rest of the input command.
 */
void handleSet(char* tokens)
{
  char* token = strtok(NULL, " ");

  if (token != NULL) {
    count = atoi(token);
  } else {
    count++;
  }

  Serial.println(count);
}

/**
 * Handle the temp command. The command has no argument.
 *
 * @param tokens The rest of the input command.
 */
void handleTemp(char* tokens)
{
  char dataString[7]; // gelesene Temperatur als String aufbereitet: (-xx)x.x
  double temp; // gelesene Temperatur als double
  
  temp = get_LM75_temperature(0, TEMP); //(Device)Wert vom 1. Temperatursensor lesen (0-7, je nach Jumperstellung am Board, 2. Parameter wie oben definiert)
  dtostrf(temp, 4, 1, dataString); //dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf); (standard avr-libc function)

  Serial.print("Temp: ");
  Serial.println(dataString);
}

/**
 * Handle the temp command. The command has no argument.
 *
 * @param tokens The rest of the input command.
 */
void handleState(char* tokens)
{
  char* token = strtok(NULL, " ");
  char* relstate = "inaktiv";
  if (token != NULL) {
    // retrieve relay status
  }

  Serial.print("Status: ");
  Serial.println(relstate);
}

/**
 * Print some help.
 *
 * @param tokens The rest of the input command.
 */
void handleHelp(char* tokens)
{
  Serial.println("Available commands: 'help', 'set', 'temp', 'state'.");
}


4 Raspberry Pi Code