Created
October 2, 2013 16:13
-
-
Save dansteingart/6796224 to your computer and use it in GitHub Desktop.
MAE 221/224 Lab Stuff
This file contains hidden or 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
| void setup(){ | |
| Serial.begin(57600); | |
| millis(); | |
| //analogReference(DEFAULT); | |
| } | |
| float vals[6]; | |
| void RMSAveraged(int time){ | |
| float n=0; | |
| for(int i=0;i<6;i++) vals[i]=0; | |
| int sleep = ceil(float(time)/50); | |
| for(int t=0;t<sleep;t++){ | |
| n++; | |
| for(int i=0;i<6;i++){ | |
| vals[i]=vals[i]+sq(analogRead(i)/1023.0*5); | |
| } | |
| delay(50); | |
| } | |
| if(n>0){ | |
| for(int i=0;i<6;i++){ | |
| vals[i]=sqrt(vals[i]/n); | |
| } | |
| } | |
| } | |
| void rawRead(){ | |
| for(int i=0;i<6;i++){ | |
| vals[i]=analogRead(i)/1023.0*5; | |
| } | |
| } | |
| void voltsToJSON(){ | |
| RMSAveraged(1000); | |
| //rawRead(); | |
| Serial.print("{\"A0\":"); | |
| Serial.print(((vals[0]))); | |
| Serial.print(",\"A1\":"); | |
| Serial.print(((vals[1]))); | |
| Serial.print(",\"A2\":"); | |
| Serial.print(((vals[2]))); | |
| Serial.print(",\"A3\":"); | |
| Serial.print(((vals[3]))); | |
| Serial.print(",\"A4\":"); | |
| Serial.print(((vals[4]))); | |
| Serial.print(",\"A5\":"); | |
| Serial.print(((vals[5]))); | |
| Serial.print("}"); | |
| Serial.println(""); | |
| } | |
| void loop(){ | |
| if(Serial.available()>0){ | |
| Serial.flush(); | |
| //voltsToJSON(); | |
| } | |
| voltsToJSON(); | |
| //delay(500); | |
| } | |
This file contains hidden or 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
| /* | |
| NodeForwader: an serial to http proxy driven by ghetto get calls | |
| requirements | |
| -- serialport -> npm install serialport | |
| -- express -> npm install express | |
| -- sleep -> npm install sleep | |
| to start: node nodeforwader.js [HTTP PORT] [SERIAL PORT] [BAUD] [BUFFER LENGTH] | |
| to read: http://[yourip]:[spec'd port]/read/ -> returns the last [BUFFER LENGTH] bytes from the serial port as a string | |
| to write: http://[yourip]:[spec'd port]/write/[YOUR STRING HERE] | |
| what will probably create farts/list of things to deal with later if I need to: | |
| - returning characters that html has issues with | |
| - spaces in the url | |
| */ | |
| parts = process.argv | |
| if (parts.length < 6) | |
| { | |
| console.log("usage: node nodeforwader.js [HTTP PORT] [SERIAL PORT] [BAUD] [BUFFER LENGTH]") | |
| process.exit(1); | |
| } | |
| else | |
| { | |
| console.log(parts); | |
| hp = parts[2] | |
| sp = parts[3] | |
| baud = parseInt(parts[4]) | |
| blen = parseInt(parts[5]) | |
| } | |
| var express = require('express'); | |
| var app = express(); | |
| app.listen(hp); | |
| var sleep = require("sleep").sleep | |
| var SerialPort = require("serialport").SerialPort ; | |
| var serialPort = new SerialPort( | |
| sp, | |
| { | |
| baudrate: baud | |
| } | |
| ); | |
| serialPort.on("open", function () { | |
| console.log('open'); | |
| }); | |
| //sleep for 5 seconds for arduino serialport purposes | |
| for (var i=0; i<5; i++ ) | |
| { | |
| console.log(i); | |
| sleep(1); | |
| } | |
| //On Data fill a circular buf of the specified length | |
| buf = "" | |
| serialPort.on('data', function(data) { | |
| buf += data | |
| if (buf.length > blen) buf = buf.substr(buf.length-blen,buf.length) | |
| }); | |
| //Write to serial port | |
| app.get('/write/*',function(req,res){ | |
| toSend = req.originalUrl.replace("/write/","") | |
| toSend = decodeURIComponent(toSend); | |
| serialPort.write(toSend) | |
| res.send(toSend) | |
| }); | |
| //read buffer | |
| app.get('/read/', function(req, res){ | |
| res.send(buf) | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment