- Fri Sep 26, 2014 5:04 am
#175339
Hello all,
I'm trying to post some magnetometer data on data.sparkfun with the Yun and running into an issue:
I can get the example sketch https://learn.sparkfun.com/tutorials/pu ... arduino-yn of posting data with the Yun running just fine, so I know that the Yun can see the internet with no issues. When I try to post data from my sketch (below), I get pages and pages of garbled response. If I comment out the section of code that shows the response I can grab the curl command that is generated. That works fine when run from the command line on my computer, so the syntax isn’t wrong either. All baud rates are set correctly as well. Any ideas?
curl command generated on Arduino:
curl --header "Phant-Private-Key: XRR8oNoxjdH4W1nB84qn" --data "MagNorth=-29062.67" --data "MagEast=34957.33" --data "MagUp=76166.67" http://data.sparkfun.com/input/WGGbqQqxZRSKmXLEnKoL
I'm trying to post some magnetometer data on data.sparkfun with the Yun and running into an issue:
I can get the example sketch https://learn.sparkfun.com/tutorials/pu ... arduino-yn of posting data with the Yun running just fine, so I know that the Yun can see the internet with no issues. When I try to post data from my sketch (below), I get pages and pages of garbled response. If I comment out the section of code that shows the response I can grab the curl command that is generated. That works fine when run from the command line on my computer, so the syntax isn’t wrong either. All baud rates are set correctly as well. Any ideas?
curl command generated on Arduino:
curl --header "Phant-Private-Key: XRR8oNoxjdH4W1nB84qn" --data "MagNorth=-29062.67" --data "MagEast=34957.33" --data "MagUp=76166.67" http://data.sparkfun.com/input/WGGbqQqxZRSKmXLEnKoL
Code: Select all
#include <Wire.h>
#include <Process.h>
// Constants for Magnetometer
#define MAG_ADDR 0x0E //7-bit address for the MAG3110, doesn't change
#define NUMSAMPLES 5
#define NUMAVERAGES 5
// Phant Configuration
//Public URL: https://data.sparkfun.com/streams/WGGbqQqxZRSKmXLEnKoL
// URL to phant server
String phantURL = "http://data.sparkfun.com/input/";
// Public key:
String publicKey = "WGGbqQqxZRSKmXLEnKoL";
// Private key
String privateKey = "XRR8oNoxjdH4W1nB84qn";
// Data Fields to Stream
const int NUM_FIELDS = 3;
// Set Field Names
String fieldName[NUM_FIELDS] = {"MagNorth", "MagEast", "MagUp"};
// Array to store data for each field
String fieldData[NUM_FIELDS];
float avg_x;
float avg_y;
float avg_z;
float lta_x;
float lta_y;
float lta_z;
uint8_t i;
uint8_t j;
void setup(void)
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200); // start serial for output
config(); // turn the MAG3110 on
}
void loop(void)
{
lta_x = 0;
lta_y = 0;
lta_z = 0;
for (i=0; i<NUMAVERAGES; i++) {
avg_x = 0;
avg_y = 0;
avg_z = 0;
for (j=0; j<NUMSAMPLES; j++) {
avg_x += read_x();
avg_y += read_y();
avg_z += read_z();
}
avg_x /= NUMSAMPLES;
avg_y /= NUMSAMPLES;
avg_z /= NUMSAMPLES;
lta_x += avg_x;
lta_y += avg_y;
lta_z += avg_z;
}
lta_x /= NUMAVERAGES;
lta_y /= NUMAVERAGES;
lta_z /= NUMAVERAGES;
lta_x /= 0.03;
lta_y /= 0.03;
lta_z /= 0.03;
fieldData[0] = String(lta_y);
fieldData[1] = String(lta_x);
fieldData[2] = String(lta_z);
// Post Data
Serial.println("Posting Data!");
Serial.println(fieldData[0]);
Serial.println(fieldData[1]);
Serial.println(fieldData[2]);
delay(1000);
postData(); // the postData() function does all the work,
// see below.
delay(5000);
}
void config(void)
{
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x11); // cntrl register2
Wire.write(0x80); // write 0x80, enable auto resets
Wire.endTransmission(); // stop transmitting
delay(15);
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x10); // cntrl register1
Wire.write(0x19); // turn on over sampling and make active mode
Wire.endTransmission(); // stop transmitting
}
int read_x(void)
{
int xl, xh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x01); // x MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may write less than requested
{
xh = Wire.read(); // read the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x02); // x LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may write less than requested
{
xl = Wire.read(); // read the byte
}
int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
return xout;
}
int read_y(void)
{
int yl, yh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x03); // y MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may write less than requested
{
yh = Wire.read(); // read the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x04); // y LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may write less than requested
{
yl = Wire.read(); // read the byte
}
int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
return yout;
}
int read_z(void)
{
int zl, zh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x05); // z MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may write less than requested
{
zh = Wire.read(); // read the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x06); // z LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may write less than requested
{
zl = Wire.read(); // read the byte
}
int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
return zout;
}
void postData()
{
Process phant; // Used to send command to Shell, and view response
String curlCmd; // Where we'll put our curl command
String curlData[NUM_FIELDS]; // temp variables to store curl data
// Construct curl data fields
// Should look like: --data "fieldName=fieldData"
for (int i=0; i<NUM_FIELDS; i++)
{
curlData[i] = "--data \"" + fieldName[i] + "=" + fieldData[i] + "\" ";
}
// Construct the curl command:
curlCmd = "curl ";
curlCmd += "--header "; // Put our private key in the header.
curlCmd += "\"Phant-Private-Key: "; // Tells our server the key is coming
curlCmd += privateKey;
curlCmd += "\" "; // Enclose the entire header with quotes.
for (int i=0; i<NUM_FIELDS; i++)
curlCmd += curlData[i]; // Add our data fields to the command
curlCmd += phantURL + publicKey; // Add the server URL, including public key
// Send the curl command:
Serial.print("Sending command: ");
Serial.println(curlCmd); // Print command for debug
phant.runShellCommand(curlCmd); // Send command through Shell
// Read out the response:
Serial.print("Response: ");
// Use the phant process to read in any response from Linux:
while (phant.available())
{
char c = phant.read();
Serial.write(c);
}
}