|
Ex33 private msg quote post Address this user | |
Okay so im just gonna try make this nice and short.
So im new here not sure if im doing things correctly, but I tired to stream data by using the code found in the Arduino Quick Start guide and what i got in the serial monitor is in the picture in this link https://drive.google.com/open?id=0B-5__H_OGsN1ajZJaTRQNE5FVDQ
The code i used is as followed
#include <SPI.h>
#include <Ethernet.h>
// Local Network Settings
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0x60, 0xA3 }; // Change this!!! Must be unique on local network.
// Look for a sticker on the back of your Ethernet shield.
// GroveStreams Settings
char gsApiKey[] = "MY_API_KEY"; //Change This!!!
char gsComponentName[] = "Pressure"; //Optionally change. Set this to give your component a name when it initially registers.
char gsDomain[] = "grovestreams.com"; //Don't change. The GroveStreams domain.
char gsComponentTemplateId[] = "Pressure Readings"; //Don't change. Tells GS what template to use when the feed initially arrives and a new component needs to be created.
//GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to.
//Don't change these unless you edit your GroveStreams component definition and change the stream IDs to match these.
char gsStreamId1[] = "Pre1";
char gsStreamId2[] = "Pre2";
char gsStreamId3[] = "Pre3";
// Other Settings
const unsigned long updateFrequency = 60000UL; // Update frequency in milliseconds (20000 = 20 seconds). Change this to change your sample frequency.
char samples[35]; // Change this buffer size only if you increase or decrease the size of samples being uploaded.
char myIPAddress[20]; //Don't Change. Set below from DHCP. Needed by GroveStreams to verify that a device is not uploading more than once every 10s.
char myMac[20]; //Don't Change. Set below from the above mac variable. The readable Mac is used by GS to determine which component the
// feeds are uploading into. It must match an existing GroveStreams component's ID
unsigned long lastSuccessfulUploadTime = 0; //Don't change. Used to determine if samples need to be uploaded.
int failedCounter = 0; //Don't change. Used for Internet Connection Reset logic
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
// Update sensor data to GroveStreams
if(millis() - lastSuccessfulUploadTime > updateFrequency)
{
updateGroveStreams();
}
}
void updateGroveStreams()
{
//Assemble the url that is used to pass the temperature readings to GroveStreams and call it
unsigned long connectAttemptTime = millis();
if (client.connect(gsDomain, 80))
{
//You may need to increase the size of urlBuf if any other char array sizes have increased
char urlBuf[200]; //Original is 175
sprintf(urlBuf, "PUT /api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
gsComponentTemplateId, myMac, gsComponentName, gsApiKey, getSamples());
//Uncomment the next three lines for debugging purposes
Serial.println(urlBuf); // Make into comment if casues problem------------------------------------------------------
Serial.print(F("urlBuf length = "));
Serial.println(strlen(urlBuf));
client.println(urlBuf); //Send the url with temp readings in one println(..) to decrease the chance of dropped packets
client.print(F("Host: "));
client.println();
client.println(F("Connection: close"));
client.print(F("X-Forwarded-For: ")); //Include this line and the next line if you have more than one device uploading behind
client.println(myIPAddress); // your outward facing router (avoids the GS 10 second upload rule)
client.println(F("Content-Type: application/json"));
client.println();
if (client.connected())
{
//Begin Report Response
while(!client.available())
{
delay(1);
}
while(client.available())
{
char c = client.read();
Serial.print(c);
}
//End Report Response
//Client is now disconnected; stop it to cleannup.
client.stop();
lastSuccessfulUploadTime = connectAttemptTime;
failedCounter = 0;
}
else
{
handleConnectionFailure();
}
}
else
{
handleConnectionFailure();
}
}
void handleConnectionFailure() { // wat is this dont touch
//Connection failed. Increase failed counter
failedCounter++;
Serial.print(F("Connection to GroveStreams Failed "));
Serial.print(failedCounter);
Serial.println(F(" times"));
delay(1000);
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 )
{
//Too many failures. Restart Ethernet.
startEthernet();
}
}
void startEthernet()//dont touch this also----------------------------------------------
{
//Start or restart the Ethernet connection.
client.stop();
Serial.println(F("Connecting Arduino to network..."));
Serial.println();
//Wait for the connection to finish stopping
delay(2000);
//Connect to the network and obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println(F("DHCP Failed, reset your Arduino and try again"));
Serial.println();
}
else
{
Serial.println(F("Arduino connected to network using DHCP"));
//Set the mac and ip variables so that they can be used during sensor uploads later
Serial.print(F(" MAC: "));
Serial.println(getMacReadable());
Serial.print(F(" IP address: "));
Serial.println(getIpReadable(Ethernet.localIP()));
Serial.println();
}
}
char* getMacReadable()//dont touch this--------------------------------------------------
{
//Convert the mac address to a readable string
sprintf(myMac, "%02x:%02x:%02x:%02x:%02x:%02x�", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return myMac;
}
char* getIpReadable(IPAddress ipAddress)// dont touch this---------------------------------------------
{
//Convert the ip address to a readable string
unsigned char octet[4] = {0,0,0,0};
for (int i=0; i<4; i++)
{
octet[i] = ( ipAddress >> (i*8) ) & 0xFF;
}
sprintf(myIPAddress, "%d.%d.%d.%d�",octet[0],octet[1],octet[2],octet[3]);
return myIPAddress;
}
char* getSamples() // This needs to be fixed-----------------------------------------------------------------------------------------------------------------------------
{
//Get the temperature analog reading and convert it to a string
float voltage, pressure;
int val;
val = random(0,1023);
voltage = ( val * 0.004882814);
pressure = (voltage - 0.5) * 100.0;
char pressured[15] = {0}; //Initialize buffer to nulls
dtostrf(pressure, 12, 3,trim( pressured)); //Convert float to string
//Assemble the samples into URL parameters which are seperated with the "&" character
//
// Example: &s1=25.684&s2=78.231
sprintf(samples, "&%s=%s", gsStreamId1, trim(pressured));
//Remove &%s=%s&%s=%s and gsStreamId2 to last trim(pressured)b
return samples;
}
char* trim(char* input) //dont touch------------------------------------------
{
//Trim leading and ending spaces
int i,j;
char *output=input;
for (i = 0, j = 0; i<strlen(input); i++,j++)
{
if (input[i]!=' ')
output[j]=input[i];
else
j--;
}
output[j]=0;
return output;
}
The original code can be found in the Arduino quick start for tempreature thing on the website.(i edited it for my needs)
Okay so the problem is i cant resolve this 401/error thing as seen in the picture so could someone help please?
Extra info i can think of is that Im using an arduino mega 2560 with a stacked internet shield(this has no assigned mac address). And i did change my api key to the grovestreams one(i didnt leave it as in the code i shown) but not sure if its the thing causing the problem.
Many thanks in advance! |
8 years ago |
Post 1 IP
flag post |