Skip to content

Turning my computer on and off with Espruino Pico

Turning my computer on and off with Espruino Pico

WIZnet

Published February 21, 2023

Original author: http://forum.espruino.com/profiles/698/Original source (new tab)

Turning my computer on and off with Espruino  Pico

Components

Hardware components

Project description

DETAILS

 

project consists of an Espruino Pico, Wiznet5500io and a 2 Relay Module. I am using node-red and mqtt on my raspberry pi 3 to communicate with the Pico. I am using these parts to turn on and shutdown my computer. I mainly use my computer as a media server which is located downstairs in my office. My TV is located upstairs. I normally shut my computer off when it’s not in use so I can save on my electric bill. Instead of walking downstairs to turn on/off my computer I figured I can comfortably do that from my cell phone via from my couch 🙂

The pico controls the relay. The relay is connected to my motherboard shutdown header. The relay is triggered for a second to short the header to gnd when it receives the mqtt msg of “shutdown” or if the button on top of the pico is pressed. Triggering the relay will turn my computer on or shutdown my computer if its all ready on.

 

Guides that I followed:

  1. http://www.espruino.com/WIZnet
  2. http://www.espruino.com/Home+Automation

Todo:

  1. Create a cleaner fritzing sketch
  2. Add some gauges in node-red to receive feedback
  3. Create a PCB
    1. var turnedOFF = 1;
    2. var isConnected = false;
    3.  
    4. var MQTT_HOST = "192.168.0.22";
    5. var PATH = "/mydevice/";
    6. var mqtt;
    7. var eth;
    8.  
    9. function mqttMessage(pub) {
    10. console.log("pub: " + pub.message);
    11. console.log(
    12. "MQTT=> ",pub.topic,pub.message);
    13. if (pub.topic==PATH+"computer") {
    14. //var v = pub.message!=0;
    15. //console.log(v);
    16. if (pub.message == "shutdown") {
    17. trigger();
    18. mqtt.publish(PATH+"onoff/status", 1);
    19. }
    20. if (pub.message == "0") {
    21. mqtt.publish(PATH+"onoff/status", 0);
    22. }
    23. //digitalWrite(B3, v);
    24. }
    25. }
    26.  
    27. function mqttConnect() {
    28. console.log("mqttConnect");
    29. mqtt = require("MQTT").connect({
    30. host: MQTT_HOST,
    31. });
    32. mqtt.on('connected', function() {
    33. console.log("MQTT connected");
    34. // subscribe to wildcard for our name
    35. mqtt.subscribe(PATH+"#");
    36. });
    37. mqtt.on('publish', mqttMessage);
    38. mqtt.on('disconnected', function() {
    39. console.log("MQTT disconnected... reconnecting.");
    40. setTimeout(function() {
    41. mqtt.connect();
    42. }, 1000);
    43. });
    44. }
    45.  
    46. /*setInterval(function() {
    47. if (!mqtt) return;
    48. mqtt.publish(
    49. PATH+"cputemp",
    50. E.getTemperature());
    51. }, 2*60*1000);*/
    52.  
    53. function trigger() {
    54. turnedOFF = 1;
    55. digitalPulse(B3,0,1000);
    56. digitalWrite(LED1, 0);
    57. console.log("digitalWrite");
    58. }
    59.  
    60. setWatch(function(e) {
    61. digitalWrite(LED1, e.state);
    62. trigger();
    63. }, BTN, { repeat: true });
    64.  
    65. function onInit() {
    66. console.log("Connecting to Ethernet");
    67.  
    68. SPI2.setup({ mosi:B15, miso:B14, sck:B13 });
    69. eth = require("WIZnet").connect(SPI2, B10);
    70.  
    71. //console.log("Connect?: " + isConnected);
    72.  
    73. isConnected = eth.setIP();
    74. //console.log("Connect?: " + isConnected);
    75.  
    76. if (isConnected == true) {
    77. mqttConnect();
    78. }
    79. }
    80.  
    81. /*setWatch(function() {
    82. if (!mqtt) return;
    83. mqtt.publish(
    84. PATH+"buttonpress",
    85. 1);
    86. }, BTN, {edge:"rising",repeat:true,debounce:50});*/

1 Attachment

  • Untitled Sketch_bb.png
DOCUMENTS
Code
SketchTurning my computer on and off with Espruino  Pico
 
 
 

COMMENTS

You are logged in as mayuri | Log out
 
 
 
mayuri

 
 
  Subscribe  
 
 
 
POSTED BY
TAGS
Reusable S/W

Comments

Similar projects you might like

Comments