|
nab96 private msg quote post Address this user | |
please help, we have a problem with the code. it keeps sending 400 bad request, and to mention that we are uploading ECG to GS and not temperature.
the code we used:
#include <SPI.h>
#include <ESP8266WiFi.h>
/******************************************** SAVING TIME FROM GS FEEDBACK ********************************************/
//---------------- Fill in your credentails ---------------------
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "pass"; // your network password
//------------------------------------------------------------------
// Initialize Arduino Ethernet Client
WiFiClient client;
char Time[35];
int H=8,H0,H1,M = 5,M0,M1,S,S0,S1; // to store the last time for update and to manage the clock
char c;
//byte mac[] = {
//0x00, 0x16, 0x3E, 0x27, 0x85, 0x4B };
//char* getSamples();
//char* getMacReadable();
//char* getIpReadable(IPAddress ipAddress);
// Local Network Settings
IPAddress ip;
// GroveStreams Settings
char gsApiKey[] = "8624f637-8e2a-35a7-9323-6434dad422ca";
char gsComponentName[] = "ECG";
char gsDomain[] = "grovestreams.com";
char gsComponentTemplateId[] = "ECG";
//GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to.
char gsStreamId1[] = "s1"; //TEMPERATURE
const unsigned long updateFrequency = 20000UL; // Update frequency in milliseconds (20000 = 20 seconds).
char samples[35]; // size of samples being uploaded.
char myIPAddress[20]; // Set below from DHCP. Needed by GroveStreams to verify that a device is not uploading more than once every 10s.
char myMac[20];//Set below from the above mac variable. The readable Mac is used by GS to determine which component the
byte mac[6]; // feeds are uploading into. It must match an existing GroveStreams component's ID
unsigned long lastSuccessfulUploadTime = 0; // Used to determine if samples need to be uploaded.
int failedCounter = 0;
int number = A0;
//--------------------setup-------------------
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
// initialize the serial communication:
Serial.begin(9600);
pinMode(D3, INPUT); // Setup for leads off detection LO +
pinMode(D4, INPUT); // Setup for leads off detection LO -
}
startWiFi();
}
//-----------Loop----------------
void loop()
{
ecg();
// Update sensor data to GroveStreams
if(millis() - lastSuccessfulUploadTime > updateFrequency)
{
updateGroveStreams();
}
}
//----------------------------ecg viod--------------
void ecg()
{
if((digitalRead(D3) == 1)||(digitalRead(D4) == 1)){
Serial.println('!');
//Serial.print("balale6"
}
else{
// send the value of analog input 0:
Serial.println(analogRead(A0));
//Serial.print("nooro"
}
//Wait for a bit to keep serial data from saturating
delay(1);
}
/********************************* I N E R N E T C O N N E C C T I O N & G R O V E S T R E A M S D A T A U P D A T E *********************************/
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[175];
sprintf(urlBuf, "PUT /api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s HTTP/1.1",
gsComponentTemplateId ,myMac , gsComponentName, gsApiKey, getSamples()); // URL SENT TO THE GS
Serial.println(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())
{
while(!client.available())
{
delay(1);
}
int i = 0;
int j = 0;
while(client.available())
{
char c = client.read();
Serial.print(c);
++ j; // counts the order of the recieved bytes
//Detect the word "Date" from GS's feedback post
if ( (j >= 18) & (j <53))
{
Time[i] = c;
++i;
if (i >35)
{i = 0;}
}
}
// Serial print the time that we saved after the print of the feedback post
H0 = String(Time[23]).toInt();// there is no direct way to convert char to int thas why we convert it first to string then use toInt() to convert it back to int
H1 = String(Time[24]).toInt();
H = (H0*10) + H1;
Serial.print(H0);
Serial.print(H1);
Serial.print(":");
M0 = String(Time[26]).toInt();
M1 = String(Time[27]).toInt();
M = (M0*10) + M1;
Serial.print(M0);
Serial.print(M1);
Serial.print(":");
S0 = String(Time[29]).toInt();
S1 = String(Time[30]).toInt();
S = (S0*10) + S1;
Serial.print(S0);
Serial.println(S1);
//End Report Response
//Client is now disconnected; stop it to cleannup.
client.stop();
lastSuccessfulUploadTime = connectAttemptTime;
failedCounter = 0;
}
else
{
handleConnectionFailure();
}
}
else
{
handleConnectionFailure();
}
}
void handleConnectionFailure() {
//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.
startWiFi();
}
}
//------------------wifi loop------------
long ConnectionPrev = 0;
long ConnectionWait = 2000;
void startWiFi()
{
unsigned long ConnectionTime = millis();
//Start or restart the Ethernet connection.
client.stop();
Serial.println(F("Connecting Arduino to network..."));
Serial.println();
//Wait for the connection to finish stopping
if(ConnectionTime - ConnectionPrev > ConnectionWait) {
ConnectionPrev = ConnectionTime;
//Connect to the network and obtain an IP address using DHCP
if
(WiFi.status() != WL_CONNECTED);
{(WiFi.begin(ssid, pass));
Serial.print("Attempting to connect to SSID: ");
Serial.println("Mena_BBE94E");
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(5000);
}
Serial.println("nConnected.");
Serial.println(F("Arduino connected to network using DHCP"));
}
}
else
//(WiFi.begin(mac) == 0 )
{
Serial.println(F("DHCP Failed, reset your Arduino and try again"));
Serial.println();
}
//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(WiFi.localIP()));
Serial.println();
}
char* getMacReadable()
{
WiFi.macAddress(mac);
//Convert the mac address to a readable string
sprintf(myMac, "%02x:%02x:%02x:%02x:%02x:%02x
|