Last active
November 15, 2020 18:28
-
-
Save bbharris/53f4351af704867b4df0 to your computer and use it in GitHub Desktop.
Load Cell Example Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2014 Electric Imp | |
// This file is licensed under the MIT License | |
// http://opensource.org/licenses/MIT | |
const PLOTLY_USER = "XXXXX"; | |
const PLOTLY_KEY = "XXXXX"; | |
// Name your plot | |
const PLOTLY_FILENAME = "Compensated Weight vs Temp Graph 3kg"; | |
// How much historical data to show in hours (autorange must be disabled) | |
const GRAPH_WINDOW = 24; | |
firstRun <- true; | |
query <- { | |
un = PLOTLY_USER, | |
key = PLOTLY_KEY, | |
origin = "plot", | |
platform = "electricimp", | |
kwargs = http.jsonencode({ | |
filename = PLOTLY_FILENAME | |
fileopt = "extend" | |
world_readable = "true" | |
layout = { | |
title = "Electric Imp Weight Logger 1kg" | |
xaxis = { | |
title = "Date" | |
type = "date" | |
autorange=true | |
//range = [format("%i000", time() - (GRAPH_WINDOW * 3600)), format("%i000", time())] | |
} | |
yaxis = { | |
title = "Weight (g)", | |
side = "left", | |
autorange=true, | |
} | |
yaxis2 = { | |
title = "Temperature (°C)", | |
side = "right", | |
overlaying="y" | |
autorange=true, | |
} | |
} | |
}) | |
} | |
function updatePlotly(table) { | |
local t = time().tostring()+"000"; | |
query.args <- "["+ //{\"x\": "+t+", \"y\": "+table.weight+", \"name\": \"Weight\"},"+ | |
" {\"x\": "+t+", \"y\": "+table.comp+", \"name\": \"Compensated Weight\"},"+ | |
" {\"x\": "+t+", \"y\": "+table.temp+", \"name\": \"Temp\", \"yaxis\": \"y2\"}]"; | |
local query_encoded = http.urlencode(query); | |
//server.log(query_encoded); | |
local request = http.post("https://plot.ly/clientresp", {}, query_encoded); | |
local response = request.sendsync(); | |
local reply = http.jsondecode(response.body); | |
// Display any responses we get from Plotly | |
if (reply["message"] != "") { | |
//server.log(reply["message"]); | |
} | |
if (reply["warning"] != "") { | |
server.log(reply["warning"]); | |
} | |
if (reply["error"] != "") { | |
server.log(reply["error"]); | |
} | |
if (firstRun) { | |
server.log("Graph available at " + reply.url); | |
firstRun = false; | |
} | |
} | |
// When we get data from the device, format it and send it to Plotly | |
device.on("data", function(data) { | |
updatePlotly(data); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2014 Electric Imp | |
// This file is licensed under the MIT License | |
// http://opensource.org/licenses/MIT | |
const PULSE = "\x7F"; | |
const ZERO_OFFSET = 161150; //Measured emperically | |
const GAIN = 1301.48; //LSBs/Gram Measured emperically | |
spi <- hardware.spi257; | |
data <- hardware.pin1; | |
ntc <- hardware.pin2; | |
w_sum <- 0.0; | |
t_sum <- 0.0; | |
cnt <- 0; | |
// Heavily compressed NTC formula | |
// for better explanation see: | |
// https://github.com/electricimp/reference/tree/master/hardware/thermistor | |
function read_temp() { | |
local vdda = hardware.voltage(); | |
local v_therm = ntc.read() * (vdda / 65535.0); | |
return (298.15 * 3380.0) / | |
(3380.0 - 298.15 * math.log(10000.0 / ((vdda - v_therm) * | |
(10000.0 / v_therm)))) - 273.15; | |
} | |
function read_weight(){ | |
//This is bit banged so we | |
//bind calls to local variables | |
//to speed things up. | |
//Takes about 2.2mS to do the read | |
local sw = spi.write.bindenv(spi); | |
local dr = data.read.bindenv(data); | |
local val = 0; | |
for(local i = 24; i >= 0; i--){ | |
sw(PULSE); | |
val += dr() ? math.pow(2, i) : 0; | |
} | |
return (val-ZERO_OFFSET)/GAIN; //Value in grams | |
} | |
function sample(){ | |
if( ! data.read() ){ | |
data.configure(DIGITAL_IN); | |
w_sum += read_weight(); | |
t_sum += read_temp(); | |
cnt++; | |
data.configure(DIGITAL_IN, sample); | |
}else{ | |
server.log("Data not ready"); | |
} | |
} | |
function round(value, precision){ | |
local n = math.pow(10, precision); | |
return (n * 1.0 * value).tointeger() / 100.0; | |
} | |
function loop(){ | |
imp.wakeup(30, loop); | |
if(cnt == 0){ | |
return | |
} | |
local weight = round(w_sum / cnt, 2); | |
local temp = round(t_sum / cnt, 2); | |
local comp = round(weight + 1.2*(temp - 25), 2); | |
//server.log("Weight: "+weight); | |
server.log("Compensated: "+comp); | |
server.log("Temp: "+temp); | |
agent.send("data", {weight = weight, temp = temp, comp = comp}); | |
w_sum = 0.0; | |
t_sum = 0.0; | |
cnt = 0; | |
} | |
//This makes the clock pulse time 1.865us | |
spi.configure(SIMPLEX_TX, 3750); | |
data.configure(DIGITAL_IN, sample); | |
ntc.configure(ANALOG_IN); | |
loop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The HX711 outputs in two's compliment, so we need to account for this in the
read_weight
function: