Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
LarsBergqvist / subscriber_adafruit_proxy.py
Last active March 1, 2018 06:05
An MQTT subscriber that routes all incoming messages to Adafruit IO
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import datetime
import time
from Adafruit_IO import MQTTClient
# Set to your Adafruit IO key and username
ADAFRUIT_IO_KEY = 'XXX'
ADAFRUIT_IO_USERNAME = 'XXX'
@LarsBergqvist
LarsBergqvist / charts_server.py
Last active July 12, 2016 06:15
A flask server for IoT chart data
#!/usr/bin/env python3
from flask import Flask, render_template, jsonify, request
from datetime import datetime, timedelta
import sys
import data_fake
import data_mongodb
app = Flask(__name__)
def get_labels_and_values_for_topic(topic_name, numdays):
from datetime import datetime, timedelta
from repository_base import Repository
class FakeRepository(Repository):
def get_data(self,topic_name, numdays):
today=datetime.today()
# Create some test data
# Three values from yesterday and three values from today
import pymongo
from datetime import datetime, timedelta
from repository_base import Repository
class MongoDBRepository(Repository):
def get_data(self,topic_name, numdays):
mongoClient=pymongo.MongoClient()
db=mongoClient.SensorData
yesterday=datetime.today() - timedelta(numdays)
cursor = db.home_data.find({"topic":topic_name,"time":{"$gte":yesterday}}).sort("time",pymongo.ASCENDING)
from datetime import datetime, timedelta
class Repository:
"""Abstract base class for the repositories.
The sub classes should implement the get_data method."""
def date_formatted(date):
return date.strftime('%d %b %H:%M')
def get_data(self,topic_name, numdays):
raise NotImplementedError( "Should have implemented this" )
<!DOCTYPE html>
<html ng-app="app" lang="en">
<head>
<meta charset="utf-8" />
<title>Sensor data</title>
<script src="/static/node_modules/angular/angular.js"></script>
<script src="/static/node_modules/chart.js/dist/Chart.min.js"></script>
<script src="/static/node_modules/angular-chart.js/dist/angular-chart.min.js"></script>
<script src="/static/app.js"></script>
</head>
var myApp = angular.module('app', ["chart.js"])
.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('[[').endSymbol(']]');
});
myApp.controller("ChartCtrl", function ($scope,$http) {
$scope.numdaysChanged = function() {
requestNewData();
@LarsBergqvist
LarsBergqvist / door_open_detector.ino
Created July 25, 2016 08:12
Digispark USB board sketch for sending an 433MHz RF signal when a door is opened
// A sketch for a DigiSpark USB device that detects when a door is opened
// The door should have a reed switch with NC (normally closed) connected to the Vin wire of the device
// When the door is opened, the reed switch is closed and the device is started
// A 32-nit message is then sent via a 433 MHz signal that can be picked up by an appropriate receiver
// (a Raspberry Pi in my case).
// When the door is closed, the reed switch is opened and the device is shut down
//
// Depends on the RCSwitch library https://github.com/sui77/rc-switch
// and the eeprom-library
//
@LarsBergqvist
LarsBergqvist / receiver_publish_mqtt.py
Created July 25, 2016 08:37
A Python scrip that publishes an MQTT message when receiving a radio signal with a specific code
# Uses pi_switch from https://github.com/lexruee/pi-switch-python
# See pi_switch readme for details on setup
from pi_switch import RCSwitchReceiver
import time
import paho.mqtt.client as mqtt
receiver = RCSwitchReceiver()
receiver.enableReceive(2)
@LarsBergqvist
LarsBergqvist / flask_no_authorization.py
Last active September 3, 2016 19:41
Flask with no authorization
@app.route('/api/secret')
def api_secret():
return jsonify({'message':get_secret_message()})
@app.route('/')
def index():
return render_template('index.html', message=get_secret_message())