[SOLVED] Remote Power ON Computer (Wemos D1 mini + DHT22 +BMP280)

Hi, I have this code to remotely boot my computer.
Wemos power comes from the 5v computer. and I have connected to the relay by the (-) to the start button of the computer.
I also have in the D5 input of the Wemos, 3v that come from the Led of the computer when it is on.
The DHT measures the temperature inside the computer and the bmp280 measures the temperature outside.
In the code I have another led to know when the relay is active.

This I have done copying from other codes, so I need a little help with the response speed of the Led.

This code works well, except when the computer is suspended, since the LED starts to flash, but Blynk does not reflect (I think it’s because of the refresh rate)

This is the code:


//#define BLYNK_PRINT Serial
#include <ArduinoOTA.h> //OTA   
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <SimpleTimer.h>   //Good timer library 
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define DHTPIN D4      // Pin for DHT
#define Relay_1 D7     // Pin for Relay
#define computer D5    // + Led Input Computer

#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP280 bme;    // I2C D1 D2

int temp;
int humidity;

char auth[] = "xxxxxxxxxxxxxxxxx"; //Auth Token for project

// Select your pin with physical button
const int cmpPin = D5;
const int btnPin = D7;

// Blynk Led Widget
WidgetLED led0(V0); //register led to virtual pin 0
WidgetLED led1(V1); //register led to virtual pin 1

SimpleTimer timer;
String myHostname = "Computer";

void setup(){
  
  Serial.begin(115200);
  dht.begin();
  bme.begin();
  WiFi.hostname(myHostname);
  Blynk.begin(auth, "SSID", "PASSWORD");
  Serial.println("Blynk test!");

  Serial.println();
  ArduinoOTA.setHostname("Computer"); // OPTIONAL
  ArduinoOTA.begin();


  // Setup physical button pin (active low) 
  pinMode(btnPin, INPUT_PULLUP); 
  
//-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, LOW);
  pinMode(Relay_1, OUTPUT);
  pinMode(computer, INPUT);
  delay(4000); //Check that all relays are inactive at Reset

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Setup function to be called every 2 seconds
  timer.setInterval(2000, sendData);
  timer.setInterval(10L, buttonLedWidget); 
  timer.setInterval(10L, computerLedWidget);

}//--(end setup )---

  // V0 LED Widget represents the physical computer state
boolean cmpState = false;
void computerLedWidget()
{
  // Read button
  boolean isPressed = (digitalRead(cmpPin) == LOW);

  // If state has changed...
  if (isPressed != cmpState) {
    if (isPressed) {
      led0.off();
    } else { 
      led0.on(); 
    }
    cmpState = isPressed;
  }
}

  // V1 LED Widget represents the physical relay state
boolean btnState = false;
void buttonLedWidget()
{ 
  // Read relay
  boolean isPressed = (digitalRead(btnPin) == LOW);

  // If state has changed...
  if (isPressed != btnState) {
    if (isPressed) {
      led1.off();
    } else {
      led1.on();
    }
    btnState = isPressed;
  }
}

void sendData()
{
 float t = dht.readTemperature();
 float h = dht.readHumidity();
 float T = bme.readTemperature();
 float p = bme.readPressure();
  
  Blynk.virtualWrite(V2, t);
  Blynk.virtualWrite(V3, h);
  Blynk.virtualWrite(V4, T-1.6);
  Blynk.virtualWrite(V5, p/100);
}

void loop(){
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
  ArduinoOTA.handle();            //OTA
}

.


That sounds like a very strange way to describe how you’d connect up a PC correctly.
The power button on modern (ATX specification, introduced in 1995) PCs is a normally open momentary push button, which connects to two pins on the motherboard.
Any relay that you want to use to mimick the action of the front panel power switch will need to be wired in parallel with the current switch, so that momentarily pressing the existing physical switch, or momentarily activating the relay will connect the contacts on the motherboard together.

Pete.

Here I put the scheme. I feel the crosses, I hope it looks good.

Esquema

The relay input, by default is D1, but I modified it by D7. I do not remember why.
On my computer, USB power is always active.
The relay only active with Push button very little time.

When you say “I modified it”, do you mean that you cross-connected pin D1 on the Relay shield to pin D7, or that you cut the track that was connected to D1 and re-routed it to D7 ?

When you activate the relay, do you bear it clicking on?

Have you tried changing the duration of the pulse that you’re sending to the relay to something more than 100th of a second?

Pete.

Helps to understand the code you are pasting together… don’t just rely on others to troubleshoot :wink:

Yes, even at a 10ms polling rate there will be rare times when the reading rate and LED flash will occur at the needed exact same time.

If you insist on reading the LED this way (no limiting resistor between Wemos and PC??), then switch to a External GPIO Interrupt to read the pulsing LED signal.

Google for more info… e.g…

I cut the track that was connected to D1 and re-routed it to D7

I have modified similar relay modules made for the Wemos system, including one that was configurable for the pin I wanted :stuck_out_tongue:

To confirm, your issue was the LED or the relay??

I have already solved it, although it is not very good.

The problem was that I believed that in the suspended state, the computer turned on and off the Led with 0v and 3v. But really, the computer when suspended sends the led 3v and 5v.

There’s nothing like checking tensions with a multimeter. :hot_face::hot_face::hot_face:

Now, I have removed the power led from the computer in the blynk project that was in V0, and I have replaced it with a vertical level measuring in the analog input A0.

When the computer is off, it does not check level. When it is on, the computer marks half a bar and when it is suspended, marks the entire bar.

I pass you Captures and code of how it is. I know it could be better, but for me it is enough, since the important thing is to know if it is on or suspended, although I wait for your opinions to improve it.

Thanks

This is the code:


//#define BLYNK_PRINT Serial
#include <ArduinoOTA.h> //OTA   
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <SimpleTimer.h>   //Good timer library 
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define DHTPIN D4      // Pin for DHT
#define Relay_1 D7     // Pin for Relay


#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP280 bme;    // I2C D1 D2

int temp;
int humidity;

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; //Auth Token for project

// Select your pin with physical button
const int btnPin = D7;

// Blynk Led Widget
WidgetLED led1(V1); //register led to virtual pin 1

SimpleTimer timer;
String myHostname = "Computer";

void setup(){
  
  Serial.begin(115200);
  dht.begin();
  bme.begin();
  WiFi.hostname(myHostname);
  Blynk.begin(auth, "SSID", "PASSWORD");
  Serial.println("Blynk test!");
  Serial.println();
  ArduinoOTA.setHostname("Computer"); // OPTIONAL
  ArduinoOTA.begin();


  // Setup physical button pin (active low) 
  pinMode(btnPin, INPUT_PULLUP); 
  
//-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, LOW);
  pinMode(Relay_1, OUTPUT);

  delay(4000); //Check that all relays are inactive at Reset

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Setup function to be called every 2 seconds
  timer.setInterval(2000, sendData);
  timer.setInterval(10L, buttonLedWidget); 



}//--(end setup )---

  // V1 LED Widget represents the physical relay state
boolean btnState = false;
void buttonLedWidget()
{ 
  // Read relay
  boolean isPressed = (digitalRead(btnPin) == LOW);

  // If state has changed...
  if (isPressed != btnState) {
    if (isPressed) {
      led1.off();
    } else {
      led1.on();
    }
    btnState = isPressed;
  }
}



void sendData()
{
 float t = dht.readTemperature();
 float h = dht.readHumidity();
 float T = bme.readTemperature();
 float p = bme.readPressure();
  
  Blynk.virtualWrite(V2, t);
  Blynk.virtualWrite(V3, h);
  Blynk.virtualWrite(V4, T-1.6);
  Blynk.virtualWrite(V5, p/100);
}

void loop(){
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
  ArduinoOTA.handle();            //OTA
}


Esquema-Analogico

Attention changed the PowerLed input from D5 to A0

Screenshot-20181110-104657-Blynk

Computer ON

Screenshot-20181110-104721-Blynk

Suspend Computer

Screenshot-20181110-161603-Blynk

Vertical bar configuration

Edited, to put protection to A0

int PCLED;


timer.setInterval(1000L, PCpowerState); 


void PCpowerState() {
  PCLED = analogRead(A0);
  if (PCLED <= 100) {  // adjust accordingly
    // PC OFF
  } else if (PCLED >= 1000) {  // adjust accordingly
    // PC ON
  } else {
    // PC Standby
  }
}

BTW, A0 is only rated for 0-3v… not 5v :wink: if you don’t want to kill it, add in a 180K resistor in series.

Thank you very much, Gunner. I will take your recommendations seriously.

The first step is to put a resistance in A0 to protect it. Since I do not have a resistance of 180 K Ohm, I have put two in series of 100 K Ohm.

Now the average in A0 is: 0 Power Off, 590 Power On and 1018 is suspend Computer.

Later, I’ll try to adapt the code.

Really thank you very much.

2 Likes

I already have the code as I wanted.
Computer off -> LED Off
Computer On -> LED On
Computer Suspended -> LED blinking

Thank you very much for your help, Gunner.
Here is the code, and I use the LED again in V0


//#define BLYNK_PRINT Serial
#include <ArduinoOTA.h> //OTA   
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <SimpleTimer.h>   //Good timer library 
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define DHTPIN D4      // Pin for DHT
#define Relay_1 D7     // Pin for Relay

#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP280 bme;    // I2C D1 D2

int temp;
int humidity;
int PCLED;

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Auth Token for project

// Select your pin with physical button

const int btnPin = D7;

// Blynk Led Widget
WidgetLED led0(V0); //register led to virtual pin 0
WidgetLED led1(V1); //register led to virtual pin 1

SimpleTimer timer;
String myHostname = "Computer";

void setup(){
  
  Serial.begin(115200);
  dht.begin();
  bme.begin();
  WiFi.hostname(myHostname);
  Blynk.begin(auth, "SSID", "PASSWORD");
  Serial.println("Blynk test!");

  Serial.println();
  ArduinoOTA.setHostname("Computer"); // OPTIONAL
  ArduinoOTA.begin();


  // Setup physical button pin (active low) 
  pinMode(btnPin, INPUT_PULLUP); 
  
//-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, LOW);
  pinMode(Relay_1, OUTPUT);

  delay(4000); //Check that all relays are inactive at Reset

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  // Setup function to be called every 2 seconds
  timer.setInterval(2000, sendData);
  timer.setInterval(10L, buttonLedWidget); 
  timer.setInterval(1000L, PCpowerState);
  

}//--(end setup )---

  // V1 LED Widget represents the physical relay state
boolean btnState = false;
void buttonLedWidget()
{ 
  // Read relay
  boolean isPressed = (digitalRead(btnPin) == LOW);

  // If state has changed...
  if (isPressed != btnState) {
    if (isPressed) {
      led1.off();
    } else {
      led1.on();
    }
    btnState = isPressed;
  }
}

// V0 LED Widget represents the physical computer state in A0
void PCpowerState() {
  PCLED = analogRead(A0);
  if (PCLED <= 100) {  // adjust accordingly
    // PC OFF
    led0.off();
  } else if (PCLED >= 1000) {  // adjust accordingly
    // PC Suspend
    blinkLedWidget();
  } else {
    // PC ON
    led0.on();
  }
}

// V0 LED Widget is blinking
void blinkLedWidget()
{
  if (led0.getValue()) {
    led0.off();
  } else {
    led0.on();
  }
}

void sendData()
{
 float t = dht.readTemperature();
 float h = dht.readHumidity();
 float T = bme.readTemperature();
 float p = bme.readPressure();
  
  Blynk.virtualWrite(V2, t);
  Blynk.virtualWrite(V3, h);
  Blynk.virtualWrite(V4, T-1.6);
  Blynk.virtualWrite(V5, p/100);
}

void loop(){
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
  ArduinoOTA.handle();            //OTA
}

A post was split to a new topic: Need help with ESP8266, DHT222 and Blynk Bridge