Since my actual Terrarium Controller got serious problems with the usb power strip, I bought a raspberry pi and a Ninja Pi Crust to update the old System.

The old System contains of the following Elements:
– 1Wire Temperature Sensor
– USB Power Strip (sispm)
– WebInterface
– Many Shell Scripts to switch outlets on/off
– Big Crontab…

The new System should consist of the following Elements:
– 1Wire Temperature Senso
– Power Strip based on some very cheap replaceable components like arduino, releay modules
– reworked Terrarium Conroll Scripts based on Perl without big crontab…
– Fancy webinterface with graphs and buttons
– Maybe controlling some more items 😉

As power strip I used a 4 Port relay in combination with some “Aufputz” Power sockets and a Arduino Pro mini.

outlet_1
I connected the four relay Ports to the Arduino, disabled the Auto reset and uploaded the following Code:

 
//#define DEBUG

int CH1 = 9;
int CH2 = 12; 
int CH3 = 11; 
int CH4 = 10; 

String Input;
int ch1Value = 0;
int ch2Value = 0;
int ch3Value = 0;
int ch4Value = 0;
int ChSelected = 0;
int ChValue = 0;

void setup()
{
  pinMode(CH1, OUTPUT);      // sets the digital pin as output
  digitalWrite(CH1, HIGH); 
  pinMode(CH2, OUTPUT);      // sets the digital pin as output
  digitalWrite(CH2, HIGH);
  pinMode(CH3, OUTPUT);      // sets the digital pin as output
  digitalWrite(CH3, HIGH);  
  pinMode(CH4, OUTPUT);      // sets the digital pin as output
  digitalWrite(CH4, HIGH); 
        
       
     
    
  Serial.begin(9600);
  #ifdef DEBUG
    Serial.println("DEBUG!!!");
    Serial.println("Started");
  #endif
}

void loop()
{
  while (Serial.available()) 
  {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) 
    {
      char c = Serial.read();  //gets one byte from serial buffer
      if(c  != ';')
      {
        Input += c; //makes the string readString
      }
      else
      {
        ParseInput();
      }

    } 
  }

}

void ParseInput()
{
  //10;
  //Define single Channels
  if (Input.length() == 2) 
  {
#ifdef DEBUG
    Serial.print("Got: ");
    Serial.println(Input); 
#endif
ChSelected = Input.substring(0,1).toInt();
ChValue = Input.substring(1,2).toInt();
#ifdef DEBUG
    Serial.print("Selected Channel: ");
    Serial.println(ChSelected); 
    Serial.print("Value: ");
    Serial.println(ChValue); 
#endif
// Set:1,1
Serial.print("Set:");
    switch (ChSelected) 
    {
      case 1:
        #ifdef DEBUG
        Serial.print("Case 1 ");
        #endif      
        digitalWrite(CH1, !ChValue);
        ch1Value = ChValue;
        Serial.print("1,");
        Serial.print(ChValue);
      break;
      case 2:
        #ifdef DEBUG
        Serial.print("Case 2 ");
        #endif 
        digitalWrite(CH2, !ChValue);
        ch2Value = ChValue;
        Serial.print("2,");
        Serial.print(ChValue);
      break;
      case 3:
        #ifdef DEBUG
        Serial.print("Case 3 ");
        #endif 
        digitalWrite(CH3, !ChValue);
        ch3Value = ChValue;
        Serial.print("3,");
        Serial.print(ChValue);   
      break;
      case 4:
        #ifdef DEBUG
        Serial.print("Case 4 ");
        #endif 
        digitalWrite(CH4, !ChValue);
        ch4Value = ChValue;
        Serial.print("4,");
        Serial.print(ChValue);  
      break;
    }
    Serial.println(";");
     Input="";
  } 
  //1; 
  //Status for Channel
  else if (Input.length() == 1)
  {
    ChSelected=Input.toInt();
    #ifdef DEBUG
    Serial.print("Got status request for: ");
    Serial.print(ChSelected); 
    #endif
    //Status:1,1
    Serial.print("Status:");
    switch (ChSelected) 
    {
      case 1:
        #ifdef DEBUG
        Serial.print("Case 1 ");
        #endif
        Serial.print("1,");      
        Serial.print(!digitalRead(CH1));
      break;
      case 2:
        #ifdef DEBUG
        Serial.print("Case 2 ");
        #endif
        Serial.print("2,");    
        Serial.print(!digitalRead(CH2));
      break;
      case 3:
        #ifdef DEBUG
        Serial.print("Case 3 ");
        #endif
        Serial.print("3,");    
        Serial.print(!digitalRead(CH3));
      break;
      case 4:
        #ifdef DEBUG
        Serial.print("Case 4 ");
        #endif 
        Serial.print("4,");   
        Serial.print(!digitalRead(CH4));
      break;
    }
    Serial.println(";");
    Input="";
    
  }
  //10,20,30,41;
  //Define all Channels
  else if (Input.length() == 11)
  {
    #ifdef DEBUG
    Serial.print("Got; ");
    Serial.println(Input);
    #endif
    ch1Value=Input.substring(1,2).toInt();
    ch2Value=Input.substring(4,5).toInt();
    ch3Value=Input.substring(7,8).toInt();
    ch4Value=Input.substring(10,11).toInt();

    #ifdef DEBUG
    Serial.print("ch1Value: ");
    Serial.println(ch1Value);
    Serial.print("ch2Value: ");
    Serial.println(ch2Value);
    Serial.print("ch3Value: ");
    Serial.println(ch3Value);
    Serial.print("ch4Value: ");
    Serial.println(ch4Value);
    #endif    
    digitalWrite(CH1, !ch1Value); 
    digitalWrite(CH2, !ch2Value);   
    digitalWrite(CH3, !ch3Value);   
    digitalWrite(CH4, !ch4Value); 
    Input="";
  }
 Input="";
}

The next step was to connect a 5V FTDI Adapter to the Arduino and place everything in the “Aufputz” case.
Now you have a usb controlled outlet 😉

Switch outlet on/off:
Command:$Outlet,$State(0,1);
Returns: Set:$Outlet,$Status
Example:

 
1,0;
Set:1,0
1,1;
Set:1,1

Get Status of Outlet:
Command:$Outlet
Returns: Status:$Outlet,$Status
Example:

 
1;
Status:1,1
Advertisements