Charles van't Westeinde
Published © GPL3+

Control remote power outlets with ESP8266 (NodeMCU)

Home automation combining cheap remote power outlets with the flexibility of the ESP8266.

BeginnerProtip2 hours265
Control remote power outlets with ESP8266 (NodeMCU)

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
set of powerpoints with remote control
The remote control should have an EV1527 encoder or similar. See story and schematics.
×1
General Purpose Transistor PNP
General Purpose Transistor PNP
×4
Through Hole Resistor, 39 kohm
Through Hole Resistor, 39 kohm
×1

Story

Read more

Schematics

EV1527 OTP encoder datasheet

remote powerpoint control schematic

Code

powerpoint_control.ino

Arduino
Functions to turn individual powerpoints on/off.
The main loop repeatedly turns 5 powerpoints on/off.
/*******************************************************************************
*
*	Turn remote powerpoints on/off
*
*******************************************************************************/



// Defining available LED's here, because the value of LED_BUILTIN has been known change with updates.
#define LED_1			16		// D0, WAKE
#define LED_2			2		// D4, TXD1


// The pins connected to the remote powerpoint board.
#define	LOAD_0		14	// D5	// Meaning of all the LOAD pins is inverted!!
#define LOAD_1      12	// D6	// A signal is transmitted when at least one pin is LOW
#define LOAD_2      13	// D7
#define LOAD_ON     2	// D4	// Inverted: "0" turns the selected load on.


// Lookup array for the control pins, Least Significant first.
// For easy conversion from power-point number to pins.
#define nLoadPins		3
static const uint8_t	aLoadPins[nLoadPins] =  {LOAD_0, LOAD_1, LOAD_2};


// An array of powerpoint numbers. Note that in my first set of powerpoints they were numbered
// 1-5, while the second set had a gap at 3. Maybe makes the board simpler....

#define nPowerPoints			5			// Number of remote powerpoints
static const uint8_t	aCodes[nPowerPoints] = { 1, 2, 4, 5, 6 };

//////////////////////////////////// transmit /////////////////////////////////////

static void activatePowerPoint( const uint8_t i )
	// transmits the code for a specific remote powerpoint.
	// shifts through <code's> bits, and sets LOAD_X accordingly.
{
	uint8_t code = aCodes[i];
	for( uint8_t i = 0; i<nLoadPins; i++)
	{
		 digitalWrite(aLoadPins[i], !(code % 2) );
		 code = (code >> 1);
	}
}

static void endTransmission( void )
	// Clears all bits on the controller, ending the transmission.
{
  digitalWrite(LOAD_ON, HIGH);
  digitalWrite(LOAD_0, HIGH);
  digitalWrite(LOAD_1, HIGH);
  digitalWrite(LOAD_2, HIGH);
}


static void transmit( const int i, const bool bOn )
	// Transmit a signal to turn powerpoint <i> on/off.
{
	digitalWrite(LED_1, LOW);	// Just to show something is happening
		
	// duration of a transmission (25 msec works ok, doubled just in case)
	const uint8_t   xmitDuration = 50;

	Serial.print("transmitting ");
	Serial.print( bOn );
	Serial.print(" to powerpoint ");
	Serial.println(i);

	//	Start transmission
	activatePowerPoint( i );
	digitalWrite(LOAD_ON, !bOn); // inverted
	
    delay(xmitDuration);	// Yielding delay
	
	endTransmission();
	
	digitalWrite(LED_1, HIGH); 		
}

//////////////////////////////////// setup /////////////////////////////////////



void setup(void) {
	pinMode(LED_1, OUTPUT);
	pinMode(LOAD_ON, OUTPUT);
	pinMode(LOAD_0, OUTPUT);
	pinMode(LOAD_1, OUTPUT);
	pinMode(LOAD_2, OUTPUT);

	Serial.begin(115200);         // Start the Serial communication to send messages to the computer

	Serial.println();
	Serial.println();
	Serial.println();
	Serial.println();

	for (uint8_t t = 4; t > 0; t--) {
		Serial.printf("[SERIAL] WAIT %d...\n", t);
		Serial.flush();
		delay(1000);
	}
}

///////////////////////////// Main Loop //////////////////////////////////////

void loop(void) {
	digitalWrite(LED_1, LOW); 	
		
	// turn all powerpoints on
	for( int ipp = 0; ipp < nPowerPoints; ipp++ )
	{
		transmit( ipp, HIGH );
		delay(500);
	}
	
	delay(1000);
	
	// turn all powerpoints off (in reverse order)
	for( int ipp = nPowerPoints-1; ipp >= 0; ipp-- )
	{
		transmit( ipp, LOW );
		delay(500);
	}
	
	delay(1000);
}

Credits

Charles van't Westeinde
4 projects • 1 follower
Electronics engineer by education, software engineer by trade. Combining the two is my idea of fun.

Comments