Jump to content









Photo

Arduino Alarm System "Connection Failed"

arduino alarm connection failed

  • Please log in to reply
19 replies to this topic

#1 arojas

arojas

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 07 May 2014 - 10:46 PM

Hello,

I deployed the example about Arduino alarm system. I am getting a Connection Failed message. Here is the code:

 

/*
Carriots.com
Created 27 Dec 2012

This sketch sends streams to Carriots according to the values read by a LDR sensor

*/
#include <SPI.h>
#include <Ethernet.h>

const int ON = 1; // Constant to indicate that lights are on
const int OFF = 2; // Constant to indicate that lights are off
const String APIKEY = "My Key"; // Replace with your Carriots apikey
const String DEVICE = "MyID"; // Replace with the id_developer of your device

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { MyMac };
IPAddress ip(172,16,254,220); // Your IP Address
IPAddress server(82,223,244,60); // api.carriots.com IP Address

EthernetClient client; // Initialize the library instance

int ledPin = 7; // Led pin number
int LDRPin = 3; // LDR sensor pin number

int lights = OFF; // Current status
int newLights = OFF; // New status



// The setup routine runs once when you press reset
void setup() {

pinMode(ledPin, OUTPUT); // Initialize the digital pin as an output

Serial.begin(9600); // Start serial port
Serial.println(F("Starting"));

Ethernet.begin(mac,ip); // Start the Ethernet connection
delay(1000); // Give the Ethernet shield a second to initialize
}

// The loop routine runs over and over again forever
void loop() {

int val = analogRead(LDRPin); // Read the value from the sensor
Serial.println(val);

if (val > 990) { // This is the value limit between day or night with our LDR sensor. Maybe you need adjust this value.
newLights = OFF; // Now it's night. We have to turn on the LED
digitalWrite(ledPin, HIGH); // Turn the LED on (HIGH is the voltage level)
}
else {
newLights = ON; // Now it's day. We have to turn off the LED
digitalWrite(ledPin, LOW); // Turn the LED off by making the voltage LOW
}

if (lights != newLights) { // Check if we have a change in status
Serial.println(F("Send Stream"));
lights = newLights; // Status update and send stream
sendStream();
}

delay(500);

// If there's incoming data from the net connection, send it out the serial port
// This is for debugging purposes only
while (client.available()) {
char c = client.read();
Serial.print©;
}

if (!client.connected()) {
client.stop();
}
}

// Send stream to Carriots
void sendStream()
{
String txt = ""; // Text to send
if ( lights == OFF ) { // Alarm OFF
txt = "OFF";
} else { // Alarm ON
txt = "ON";
}
Serial.println(txt); // For debugging purpose only

if (client.connect(server, 80)) { // If there's a successful connection
Serial.println(F("connected"));
// Build the data field
String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":1356390000,\"data\":{\"light\":\""+txt+"\"}}";

// Make a HTTP request
client.println("POST /streams HTTP/1.1");
client.println("Host: api.carriots.com");
client.println("Accept: application/json");
client.println("User-Agent: Arduino-Carriots");
client.println("Content-Type: application/json");
client.print("carriots.apikey: ");
client.println(APIKEY);
client.print("Content-Length: ");
int thisLength = json.length();
client.println(thisLength);
client.println("Connection: close");
client.println();

client.println(json);
}
else {
// If you didn't get a connection to the server:
Serial.println(F("connection failed"));
}


}

Also I have a doubt how can this example work without a gateway? I am assigning my IP static. I am using an Arduino UNO Rev 3 and a Ethernet Shield.

 

Thanks in advance. 

 

Sorry for my english, its my second language.

 

Regards,

Adolfo



#2 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 09 May 2014 - 11:06 AM

Hello Adolfo

I'm testing your code and working properly.

"Connection Failed" usually occurs if you have not correctly specified the Mac address (make sure that the format is like {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}) or if your arduino does not have Internet connection.

Please check this and tell us the result.

If you prefer you can write in Spanish.

 

 

Enjoy Carriots!



#3 grupp26

grupp26

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 11 May 2014 - 03:52 PM

I have the same problem. I think there's something weird with Arduino. with some codes it works fine but if you change the codes in other places ie outside the function, the HTTP POST does not work. I also wonder what this bug may be due to?

 

 

 

 

HTTP/1.1 401 Unauthorized
Date: Sun, 11 May 2014 15:55:17 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 40
Connection: close
Server: TornadoServer/2.4

{ "response": "Not supported payload." }



#4 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 12 May 2014 - 07:32 AM

Hello grupp26

 

It seems that you are sending the body of the HTTP request with an error. Can you specify what you're sending in the payload?

 

 

Enjoy Carriots!



#5 grupp26

grupp26

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 May 2014 - 09:46 PM

I know what the problem is now , it seems like the Arduino UNO has not enough SRAM , UNO has 2kb . it takes a lot to work with strings and Carriots requires long strings .

text string that I tried to send was very long .

String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":\""+newTime+"\",\"data\":{\"light\":\""+txt+"\",\"WATT\":\""+StringA0+"\" , \ "Time \": \ "" + hour + " :" + minute + ":" + secound + "\ "} } ";

there are a couple of solutions, but the easiest is to buy an Arduino Mega 2560 which has 8kb SRAM . http://arduino.cc/en...noBoardMega2560

you can try to work in char instead of string so you can save some space in the SRAM .
redo all Serial.print ( "thing "); to Serial.print (F ( "thing" ) .; deta Heaven help mighty strap .

visit desa pages if you are experiencing problems sending long strings to Carriots with Arduino UNO .

http://www.gnu.org/s...l/pgmspace.html
http://www.arduino.c...ference/PROGMEM

I'll continue working on this to see if I can find a good solution .
now that I know what the problem is I do not know if I'm posting this in the correct place



#6 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 13 May 2014 - 11:22 AM

Hello grupp26



The Arduino boards and specially "Arduino Uno" model are perfect for prototyping, but certainly it has their limitations.

If your code is very large, you should optimize it to consume the fewest resources.

When Arduino reaches its memory limit, it sends an incomplete streams to Carriots and this is the reason why the error "Not supported payload" is returned.

Thanks for links!

#7 MorgKrig

MorgKrig

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 14 May 2014 - 10:29 AM

I use the Arduino DUE and the example code for the alert system. I modified the code to use the Arduino WiFi Shield.

Means @Carriots I can't see a Data Stream

The HTTP Request is the same as in the Tutorial

 

So what would be a possible solution?

 
EDIT: Same Problem as described:
 
Serial:
 
connected
POST /streams HTTP/1.1
Host: api.carriots.com
Accept: application/json
User-Agent: Arduino-Carriots
Content-Type: application/json
carriots.apikey: xxxxxxxxx
Content-Length: 96
Connection: close
{"protocol":"v2","device":"DataSensor@xxxxx","at":1356390000,"data":{"light":"OFF"}}
 
-------------------------------------
HTTP/1.1 401 Unauthorized
Date: Wed, 14 May 2014 11:14:14 GMT
Content-Type: application/vnd.carriots.apiv2+json
Content-Length: 40
Connection: keep-alive
Server: TornadoServer/3.2.1
 
{ "response": "Not supported payload." }


#8 grupp26

grupp26

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 19 May 2014 - 06:18 PM

what does the Json string look like when you print the on serial?

I'm 90% sure that it is something with the json string that causes trouble.
change the code to something like that I have written and see. hope that helps.

 

String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":1356390000,\"data\":{\"light\":\""+txt+"\"}}";

 

Serial.println(F("1.        "));

Serial.print("json");


// Make a HTTP request
client.println("POST /streams HTTP/1.1");
client.println("Host: api.carriots.com");
client.println("Accept: application/json");
client.println("User-Agent: Arduino-Carriots");
client.println("Content-Type: application/json");
client.print("carriots.apikey: ");
client.println(APIKEY);
client.print("Content-Length: ");
int thisLength = json.length();
client.println(thisLength);
client.println("Connection: close");
client.println();

client.println(json);

 

Serial.println(F("2.        "));

Serial.print("json");



#9 roguetrader

roguetrader

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 21 May 2014 - 10:13 PM

Also getting Connection failed on serial monitor.

 

The code fails here:

 

if (client.connect(server, 80)){ ....................

 

so doesn't get to build the json string

 

Its as if the ip address of the server is incorrect?? 82.223.244.60

 

 



#10 roguetrader

roguetrader

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 06 June 2014 - 01:54 PM

Connecting and streaming successfully with Hurl.

Still can't connect with Arduino. My code is the sample arduino alarm system code. The main answer was the MAC address, which I've had to create because there's no sticker on the shield.

Latest MAC address I've tried is

 

0xDE, 0xAD, 0xBE, 0x59, 0x18, 0x26

 

Thanks



#11 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 09 June 2014 - 07:43 AM

Hello roguetrader.

 

Sometimes works by assigning a random MAC address, but it is better to assign the Arduino shield MAC address.

To find the MAC address you can connect the Arduino to your network and use some tool to Network Analysis like NMAP (for computer) or FING (for mobile and very simple to use).

 

 

Hope it helps!



#12 roguetrader

roguetrader

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 14 July 2014 - 10:11 AM

I'm now using a Galileo with a mac address 984FEE0077A2 and I did get a connection followed by a 404 error. Then no connection. I then changed my api key to the read only streaming api key and got a connection followed by a 404 error. Since these two connections I can't get a connection again, almost like I'm being blocked.

 

Is it possible for you to check the logs for my account and see what the problem is. I'm so close!!

 

Thanks



#13 roguetrader2

roguetrader2

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 15 July 2014 - 02:15 PM

So, new account(roguetrader2) with new api keys and device.

New ethernet shield with mac address.

Copy and paste the alarm code from the tutorial and fill in api key, device, mac address and ip address.

Connect to my works network and change ip address accordingly.

No connection!!!!!!!!!

 

I've used xively successfully, but prefer the features in carriots. My company has 360 photographers who all work from home with cameras, laptops phones and cars(telemetry), this is the start of us using a platform for collecting data.

 

Can you help or should I just use Xively?

 

Thanks.



#14 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 15 July 2014 - 03:49 PM

Hi roguetrader.

 

Can you please post here your code? We will try to help you with that connection issue.

 

Are you using our Arduino UNO sketch with or without the library?

 

Thanks.


Alvaro Everlet

Carriots CTO


#15 roguetrader

roguetrader

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 16 July 2014 - 11:53 AM

Hi Alvaro
 
I'm using UNO sketch without the library.

/*
Carriots.com
Created 27 Dec 2012

This sketch sends streams to Carriots according to the values read by a LDR sensor

*/
#include <SPI.h>
#include <Ethernet.h>

const int ON = 1; // Constant to indicate that lights are on
const int OFF = 2; // Constant to indicate that lights are off
const String APIKEY = "bd06d4be616b232db5e8663fd58bf73a4f7f6696886a79ce9b9c9d4649a772ae"; // Replace with your Carriots apikey
const String DEVICE = "defaultDevice@roguetrader2.roguetrader2"; // Replace with the id_developer of your device

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x98, 0x4F, 0xEE, 0x00, 0x77, 0xA2 };
IPAddress ip(10,1,0,26); // Your IP Address
IPAddress server(82,223,244,60); // api.carriots.com IP Address

EthernetClient client; // Initialize the library instance

int ledPin = 7; // Led pin number
int LDRPin = 3; // LDR sensor pin number

int lights = OFF; // Current status
int newLights = OFF; // New status



// The setup routine runs once when you press reset
void setup() {

pinMode(ledPin, OUTPUT); // Initialize the digital pin as an output

Serial.begin(9600); // Start serial port
Serial.println(F("Starting"));

Ethernet.begin(mac,ip); // Start the Ethernet connection
delay(1000); // Give the Ethernet shield a second to initialize
}

// The loop routine runs over and over again forever
void loop() {

int val = analogRead(LDRPin); // Read the value from the sensor
Serial.println(val);

if (val > 990) { // This is the value limit between day or night with our LDR sensor. Maybe you need adjust this value.
newLights = OFF; // Now it's night. We have to turn on the LED
digitalWrite(ledPin, HIGH); // Turn the LED on (HIGH is the voltage level)
}
else {
newLights = ON; // Now it's day. We have to turn off the LED
digitalWrite(ledPin, LOW); // Turn the LED off by making the voltage LOW
}

if (lights != newLights) { // Check if we have a change in status
Serial.println(F("Send Stream"));
lights = newLights; // Status update and send stream
sendStream();
}

delay(500);

// If there's incoming data from the net connection, send it out the serial port
// This is for debugging purposes only
while (client.available()) {
char c = client.read();
Serial.print©;
}

if (!client.connected()) {
client.stop();
}
}

// Send stream to Carriots
void sendStream()
{
String txt = ""; // Text to send
if ( lights == OFF ) { // Alarm OFF
txt = "OFF";
} else { // Alarm ON
txt = "ON";
}
Serial.println(txt); // For debugging purpose only

if (client.connect(server, 80)) { // If there's a successful connection
Serial.println(F("connected"));
// Build the data field
String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":1356390000,\"data\":{\"light\":\""+txt+"\"}}";

// Make a HTTP request
client.println("POST /streams HTTP/1.1");
client.println("Host: api.carriots.com");
client.println("Accept: application/json");
client.println("User-Agent: Arduino-Carriots");
client.println("Content-Type: application/json");
client.print("carriots.apikey: ");
client.println(APIKEY);
client.print("Content-Length: ");
int thisLength = json.length();
client.println(thisLength);
client.println("Connection: close");
client.println();

client.println(json);
}
else {
// If you didn't get a connection to the server:
Serial.println(F("connection failed"));
}


}

#16 rebeburns

rebeburns

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 17 July 2014 - 09:00 AM

Hi rouguetrader2,

I tested your code exactly as you put it above. Only changing these lines:

byte mac[] = { 0x98, 0x4F, 0xEE, 0x00, 0x77, 0xA2 };
IPAddress ip(10,1,0,26); // Your IP Address

I was able to send streams to your account without any problems. This leads me to the conclusion that the problem must be the mac address (retrieved from the Ethernet Shield) or the IP Address.

I hope this helps.

#17 roguetrader

roguetrader

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 05 August 2014 - 01:28 PM

Finally got this to work by adding all my IP parameters so added dns, gateway and subnetmask to mac and ip.

byte mac[] = { 0x98, 0x4F, 0xEE, 0x00, 0x77, 0xA2 };
IPAddress ip(X,X,X,X); // Your IP Address
IPAddress dnsip(X,X,X,X);
IPAddress gateway(X,X,X,X);
IPAddress subnet(255,0,0,0);

and also changed

Ethernet.begin(mac,ip);

to

Ethernet.begin(mac,ip,dnsip,gateway,subnet);

This was needed on my BT home router as well as my works network.

Thanks for your help.

#18 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 06 August 2014 - 06:23 AM

Thanks for sharing, we are very pleased that you found the solution.



#19 hivescale

hivescale

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 01 February 2017 - 06:07 PM

Thankyou ROGUETRADER,

 

This is the same in France with FREEBOX

 

You have to apply the same patch.

You find the data with a :

 

cmd

ipconfig /all

 

serveur DNS =dnsip

passerelle = gateway

sous reseau = subnet

 

Best to all



#20 erios_arduino

erios_arduino

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 06 April 2017 - 05:30 AM

tanks roguetrader it made it work by specifying data. My router is a HG8245 huawei from telmex. Now i can go to sleep...







Also tagged with one or more of these keywords: arduino, alarm, connection, failed

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users