Skip to content

Instantly share code, notes, and snippets.

@bphermansson
bphermansson / progEsp.py
Last active February 4, 2017 15:22
Python script to set a Esp-12 in programming mode.
#!/usr/bin/env python
import RPi.GPIO as GPIO
from time import sleep
# Setup Gpio
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT) # Pin 3 (Gpio 2). To Esp Gpio0.
GPIO.setup(5, GPIO.OUT) # Pin 5 (Gpio 3). To Esp reset ("RST").
# Set D0 low
@bphermansson
bphermansson / ledtest.py
Created March 9, 2017 10:30
# Test three leds connected to Raspberry Pi Gpio.
#!/usr/bin/env python
# Test three leds connected to Raspberry Pi Gpio.
import RPi.GPIO as GPIO
from time import sleep
# Setup Gpio
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # Use actual pin numbers on the board
GPIO.setup(32, GPIO.OUT)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
@bphermansson
bphermansson / pwmpigpio.py
Created March 9, 2017 14:18
Python script for testing Raspberry Pi:s hardware PWM
#!/usr/bin/env python
import pigpio
from time import sleep
pi = pigpio.pi()
# Output is Broadcom Gpio12 (physical pin 32)
pi.hardware_PWM(12, 2000, 250000) # 2000Hz 75% dutycycle
sleep(1)
pi.hardware_PWM(12, 2000, 500000)
sleep(1)
#!/usr/bin/env python
import pigpio
from time import sleep
pi = pigpio.pi()
# Leds on Gpio 13, 19, 26
while 1:
# On Broadcom Gpio12 (physical pin 32) we have a analog gauge
pi.hardware_PWM(12, 2000, 250000)
@bphermansson
bphermansson / mqtt_pir.py
Created March 26, 2017 06:41
Uses a Raspberry Pi and a PIR (motion) sensor. Sends Mqtt messages when motion is detected.
#!/usr/bin/env python
from time import time, sleep
import datetime
import os # For running external commands
import RPi.GPIO as GPIO
import urlparse
import paho.mqtt.client as paho
# Time to wait (in seconds)
@bphermansson
bphermansson / washerMeter.py
Created April 16, 2017 10:50
Read power from a power meter and control a analog gauge and a RGB-led.
#!/usr/bin/env python
import urllib
import json
from time import sleep
import os
# Used to control Gpios
import pigpio
pi = pigpio.pi()
pi.set_mode(13, pigpio.OUTPUT) # GPIO as output Green led
#!/usr/bin/env python
#
# tvonoff.py
# This script uses a Pir sensor and a IR diode to turn on a tv when motion is detected.
# It uses time to only turn on tv at daytime.
# It also has a time out, if no motion is detected for a set period the tv is turned off.
# Another function is to send Mqtt messages when tv is turned on or off.
#
# The software irsend from Lirc is used to send codes.
# /usr/bin/irsend SEND_ONCE Samsung_BN59-00685A KEY_POWER
// https://stackoverflow.com/questions/41531412/nodemcu-auto-join-open-wifi
#include <ESP8266WiFi.h>
/* Serial Baud Rate */
#define SERIAL_BAUD 9600
/* Delay paramter for connection. */
#define WIFI_DELAY 500
/* Max SSID octets. */
#define MAX_SSID_LEN 32
@bphermansson
bphermansson / pirmqtt_v2.py
Last active September 21, 2021 10:14
Python script to send Mqtt message when Gpio on Raspberry Pi changes, for example triggered by a motion sensor
#!/usr/bin/python
# Read a Pir-sensor and send a Mqtt message when motion detected
# Uses edge detection to limit the rate of Mqtt-messages
import paho.mqtt.client as paho
import time
import urlparse
import RPi.GPIO as GPIO
import datetime
# Mqtt