Basically can I use one Arduino Sketch to create 4 components each with 2 streams in it? Been trying for quite sometime now cant seem to find a way to make it happen.
So far what I managed to get is 1 out of the 4 component streams i need with the following code
#include <SPI.h>
#include <Ethernet.h>
int logValue1=0;
int logValue2=0;
int logValue3=0;
int a,b,c;
void handleConnectionFailure();
void startEthernet();
char* getMacReadable();
char* getIpReadable(IPAddress ipAddress);
char* getSamples1();
char* getSamples2();
char* getSamples3();
char* getSamples4();
char* trim(char* input);
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0x60, 0xA3 };
char gsApiKey[] = "API_KEY";
char gsComponentName1[] = "Station1";
char gsComponentName2[] = "Station2";
char gsComponentName3[] = "Station3";
char gsComponentName4[] = "Station4";
char gsDomain[] = "grovestreams.com";
char gsComponentTemplateId1[] = "Station1";
char gsComponentTemplateId2[] = "Station2";
char gsComponentTemplateId3[] = "Station3";
char gsComponentTemplateId4[] = "Station4";
char gsStreamId1[] = "set1";
char gsStreamId2[] = "set2";
char gsStreamId3[] = "set3";
char gsStreamId4[] = "set4";
char gsStreamId5[] = "set5";
char gsStreamId6[] = "set6";
char gsStreamId7[] = "set7";
char gsStreamId8[] = "set8";
const unsigned long updateFrequency = 60000UL;
char samples[35];
char myIPAddress[20];
char myMac[20];
unsigned long lastSuccessfulUploadTime = 0;
int failedCounter = 0;
EthernetClient client;
void setup()
{
pinMode (13 ,OUTPUT); //Red
pinMode (12 ,OUTPUT); //Yellow
pinMode (11 ,OUTPUT); //Green
pinMode (9 ,OUTPUT); //Red
pinMode (8 ,OUTPUT); //Yellow
pinMode (7 ,OUTPUT); //Green
//pinMode (6, INPUT); // For switch
pinMode(A13, INPUT); //analog
pinMode(A12, INPUT); //analog
pinMode(A11, INPUT); //analog
Serial.begin(9600);
startEthernet();
}
void loop()
{
logValue1= analogRead(A13);
logValue2= analogRead(A12);
a = logValue1;
b = logValue2;
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("proxy.tp.edu.sg", 80))
{
char urlBuf[175];
sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
gsComponentTemplateId1, myMac, gsComponentName1, gsApiKey, getSamples1());
sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
gsComponentTemplateId2, myMac, gsComponentName2, gsApiKey, getSamples2());
sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
gsComponentTemplateId3, myMac, gsComponentName3, gsApiKey, getSamples3());
sprintf(urlBuf, "PUT http://www.grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
gsComponentTemplateId4, myMac, gsComponentName4, gsApiKey, getSamples4());
Serial.println(urlBuf);
//Serial.print(F("urlBuf length = ");
//Serial.println(strlen(urlBuf));
client.println(urlBuf);
client.print(F("Host:"));
client.println();
client.println(F("Connection: close"));
client.print(F("X-Forwarded-For: "));
client.println(myIPAddress);
client.println(F("Content-Type: application/json"));
client.println();
if (client.connected())
{
while(!client.available())
{
delay(1);
}
while(client.available())
{
char c = client.read();
Serial.print(c);
}
client.stop();
lastSuccessfulUploadTime = connectAttemptTime;
failedCounter = 0;
}
else
{
handleConnectionFailure();
}
}
else
{
handleConnectionFailure();
}
}
void handleConnectionFailure() {
failedCounter++;
Serial.print(F("Connection to GroveStreams Failed "));
Serial.print(failedCounter);
Serial.println(F(" times"));
delay(1000);
if (failedCounter > 3 )
{
startEthernet();
}
}
void startEthernet()
{
client.stop();
Serial.println(F("Connecting Arduino to network..."));
Serial.println();
delay(2000);
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"));
Serial.print(F(" MAC: "));
Serial.println(getMacReadable());
Serial.print(F(" IP address: "));
Serial.println(getIpReadable(Ethernet.localIP()));
Serial.println();
}
}
char* getMacReadable()
{
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)
{
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* getSamples1()
{
float PCV1,MFCV1;
int val;
PCV1 = logValue1;
MFCV1 = logValue2;
char PCa[15] = {0};
dtostrf(PCV1, 12, 3,trim(PCa));
char MFCa[15] = {0};
dtostrf(MFCV1, 12, 3,trim(MFCa));
sprintf(samples, "&%s=%s&%s=%s", gsStreamId1, trim(PCa),gsStreamId2, trim(MFCa));
return samples;
}
char* getSamples2()
{
float PCV2,MFCV2;
int val;
PCV2 = logValue1;
MFCV2 = logValue2;
char PCb[15] = {0};
dtostrf(PCV2, 12, 3,trim(PCb));
char MFCb[15] = {0};
dtostrf(MFCV2, 12, 3,trim(MFCb));
sprintf(samples, "&%s=%s&%s=%s", gsStreamId3, trim(PCb),gsStreamId4, trim(MFCb)); // change to set 3, 4, 5, 6, 7, 8
return samples;
}
char* getSamples3()
{
float PCV3,MFCV3;
int val;
PCV3 = logValue1;
MFCV3 = logValue2;
char PCc[15] = {0};
dtostrf(PCV3, 12, 3,trim(PCc));
char MFCc[15] = {0};
dtostrf(MFCV3, 12, 3,trim(MFCc));
sprintf(samples, "&%s=%s&%s=%s", gsStreamId5, trim(PCc),gsStreamId6, trim(MFCc)); // change to set 3, 4, 5, 6, 7, 8
return samples;
}
char* getSamples4() //change no 2 ,3 ,4
{
float PCV4,MFCV4;
int val;
PCV4 = logValue1;
MFCV4 = logValue2;
char PCd[15] = {0};
dtostrf(PCV4, 12, 3,trim(PCd));
char MFCd[15] = {0};
dtostrf(MFCV4, 12, 3,trim(MFCd));
sprintf(samples, "&%s=%s&%s=%s", gsStreamId7, trim(PCd),gsStreamId8, trim(MFCd));
return samples;
}
char* trim(char* input)
{
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;
}
This was edited from the code found in a arduino quick start tutorial found on the site. |