Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
LarsBergqvist / measurecputemp.py
Created February 1, 2017 18:15
Measure the CPU temperature on a Raspberry Pi and publish the data with MQTT
#!/usr/bin/env python3
import paho.mqtt.publish as publish
from subprocess import check_output
from re import findall
def get_temp():
temp = check_output(["vcgencmd","measure_temp"]).decode("UTF-8")
return(findall("\d+\.\d+",temp)[0])
def publish_message(topic, message):
@LarsBergqvist
LarsBergqvist / printcputemp.py
Created February 1, 2017 18:13
Print the CPU temperature on a Raspberry Pi
#!/usr/bin/env python3
from subprocess import check_output
from re import findall
def get_temp():
temp = check_output(["vcgencmd","measure_temp"]).decode("UTF-8")
return(findall("\d+\.\d+",temp)[0])
print(get_temp())
@LarsBergqvist
LarsBergqvist / bot.py
Last active December 15, 2016 07:34
A MicroPython car bot
"""
A bot with servos for an Adafruit Feather Huzzah
Requires the Adafruit PWM servo library:
https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
Uses a FeatherWing servo board attached with i2C
"""
import machine
import servo
import utime
@LarsBergqvist
LarsBergqvist / servo_wheels.py
Last active December 15, 2016 14:31
servo_wheels.py
def stop():
"""Stops the wheels"""
servos.position(left_wheel, degrees=90)
servos.position(right_wheel, degrees=90)
def fw_step():
"""Moves the bot forward one step"""
servos.position(left_wheel, degrees=90 + wheel_speed)
servos.position(right_wheel, degrees=90 - wheel_speed + drift)
utime.sleep(straight_length)
@LarsBergqvist
LarsBergqvist / execute_sequence.py
Last active December 15, 2016 14:31
Call functions according to mappings between characters and objects' functions
action_methods = {
'F' : wheels.fw_step,
'B' : wheels.back_step,
'L' : wheels.turn_left,
'R' : wheels.turn_right,
'S' : wheels.stop,
'U' : hammer.up,
'D' : hammer.down,
'O' : claws.open,
'C' : claws.close
@LarsBergqvist
LarsBergqvist / mqtt_oled.py
Last active December 3, 2016 20:52
mqtt_oled.py
from umqtt.simple import MQTTClient
import utime
from machine import Pin
import oled_message_display
import wifi_connection
last_messages = {}
def mqtt_message_received(topic, msg):
# add or update the latest stored message for this topic
@LarsBergqvist
LarsBergqvist / ssd1306_library_with_text.py
Created December 3, 2016 20:00
Example on text output on a 128x32 SSD1306 OLED display
import machine
i2c = machine.I2C(machine.Pin(5), machine.Pin(4))
import ssd1306
oled = ssd1306.SSD1306_I2C(128, 32, i2c)
oled.fill(0)
oled.text("First line", 0, 0)
oled.text("Second line", 0, 10)
oled.text("Third line", 0, 20)
oled.show()
@LarsBergqvist
LarsBergqvist / umqtt_subscription.py
Last active December 3, 2016 18:17
umqtt example in MicroPython
from umqtt.simple import MQTTClient
import utime
c = MQTTClient("umqtt_client", "192.168.1.16",port=1883)
c.set_callback(mqtt_message_received)
c.connect()
c.subscribe("Home/#")
while True:
c.check_msg()
@LarsBergqvist
LarsBergqvist / wifi_connection.py
Created December 3, 2016 17:56
Basic network setup with ESP8266 and MicroPython
import network
import utime
def setup_wifi():
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
# Replace with your SSID and WiFi password
sta_if.connect('SSID', 'PASSWORD')
while not sta_if.isconnected():
utime.sleep(1)
@LarsBergqvist
LarsBergqvist / crontab.sh
Last active November 3, 2016 12:24
crontab file for automatically turning remote controlled outlets on/off
# Turn on outdoor lights in the afternoon
00 17 * * * curl -i -H "Content-Type: application/json" -X PUT -d '{"state":"on"}' http://localhost:5000/Outlets/api/outlets/2/1
# Turn off outdoor lights in the morning
00 08 * * * curl -i -H "Content-Type: application/json" -X PUT -d '{"state":"off"}' http://localhost:5000/Outlets/api/outlets/2/1
# Turn on downstairs window lights in the afternoon
30 19 * * * curl -i -H "Content-Type: application/json" -X PUT -d '{"state":"on"}' http://localhost:5000/Outlets/api/outlets/1/2
# Turn on upstairs window lights in the evening