Hi, new user here, I am trying to connect my Arduino Uno with ESP8266 Wifi Shield to the Cayenne Cloud, with no success so far.
Followed the tutorials, got the code running, but I am getting this error over and over:
“[4135259] Network connect failed
AT+CIPSTART=1,“TCP”,“mqtt.mydevices.com”,1883”,
basically it cannot connect to the Cayenne cloud.
I think it’s something related to the internet connection, but tested with both my fixed fiber internet connection and my 4G mobile network through Wifi hotspot, with different ISPs, and I get the same result.
Pinging “mqtt.mydevices.com” result in “Request timed out” 100% packet loss all the time.
Is the Cayenne cloud down ?, is the 1883 port blocked (but on both connections) ?
Already used this exact configuration with Blynk and it worked only after I used the IP address of the cloud instead of the hostname, and it also used port 80.
Is it possible I can give the Cayenne Library the IP address of “mqtt.mydevices.com” ?
Any help would be much appreciated !
arduino uno and esp as shield is not the most stable way for connection for this code. you may want to try with arduino mega instead.
you can get it using
$ host mqtt.mydevices.com
mqtt.mydevices.com has address 34.236.59.34
mqtt.mydevices.com has address 34.206.242.200
it is good to troubleshoot, but not long term fix. IP addresses change over time,
Thanks for your answer.
It may not be the most stable connection, but being on the hardware serial and with very new AT firmware on the ESP it should work. And it worked fine with Blynk, like I said.
So, I try to debug this issue, and connected just the ESP to my laptop and started giving it AT commands.
It turns out it’s some sort of bug in the ESP AT firmware related to the DNS resolution.
It cannot resolve the DNS query for the host “mqtt.mydevices.com”, but it works perfectly with the direct IP address. You can check the log below:
ready
WIFI CONNECTED
WIFI GOT IP
AT+GMR
AT version:1.6.2.0(Apr 13 2018 11:10:59)
SDK version:2.2.1(6ab97e9)
compile time:Jun 7 2018 19:34:26
Bin version(Wroom 02):1.6.2
OK
AT+CIPSTART=1,“TCP”,“34.206.242.200”,1883
Link type ERROR
ERROR
AT+CIPSTART=1,"TCP","34.236.59.34",1883
Link type ERROR
ERROR
AT+CIPSTART=1,"TCP","mqtt.mydevices.com",1883
Link type ERROR
ERROR
AT+CIPMUX=1
OK
AT+CIPSTART=1,"TCP","mqtt.mydevices.com",1883
DNS Fail
ERROR
AT+CIPSTART=1,"TCP","34.236.59.34",1883
1,CONNECT
OK
1,CLOSED
So now my question is: how can I change the hostname of the MQTT server with the IP address
when starting Cayenne:
“Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);”
Is there a version of the begin function with the server as a parameter ?
OK, so managed to get past the DNS issue by replacing the host with the IP address in the CayenneDefines.h. Now the web dashboard recognized my Arduino Uno device, and I added
a widgeton channel 0 to show the running time and a photoresistor on channel 1 and analog pin 0.
But the device keeps going “offline” every couple of seconds and even if the data is sent from my device (see the logs below), no data is received in the web dashboard.
Any advice ?
“AT
ATE0
AT+CIPMUX=1
AT+CWMODE?
AT+CWJAP=“xxx”,“xxx”
[6484] Connected to WiFi
[6486] Connecting to 34.236.59.34:1883
AT+CIPSTART=1,“TCP”,“34.236.59.34”,1883
AT+CIPSEND=1,40
172a4b4d4/cmd/+7875] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key
AT+CIPSEND=1,40
[12892] Publish: topic 6, channel 65534, value ATmega328P, subkey , key
AT+CIPSEND=1,40
[17911] Publish: topic 7, channel 65534, value 16000000, subkey , key
AT+CIPSEND=1,40
[27728] Publish: topic 8, channel 65534, value ESP8266Shield, subkey , key
[27798] Publish: topic 1, channel 1, value 530, subkey , key
AT+CIPSEND=1,40
[32815] Publish: topic 1, channel 0, value 32815, subkey , key
AT+CIPSEND=1,40
[37831] Connection ok
[42802] Publish: topic 1, channel 1, value 531, subkey , key
AT+CIPSEND=1,40”
Can you try commenting all serial print lines. It is not the best practise to use one single hardware serial for both communication and serial printing.
Yes, the serial print line were just for debugging purposes, I never leave them upon deployment.
Turn out the problem was in my Arduino code, I had some channels mixed up, or they did not correspond to the widgets on the web dashboard, this is why I think the device was disconnecting very often and not receiving data.
Re-wrote all my channel functions and not it works correctly with a photoresistor, a termistor and a button controlled LED.
I now have 4 questions:
- Is it possible to change the MQTT server hostname from the Arduino code, so I can use the IP address ?
- When adding a widget I can only select Virtual from the Connectivity field, why aren’t there the Analog and Digital options ?
- It seems the sensors are sending data every 15 seconds. Where is this configured?, can I change this value ? Or add some sort of timer or trigger for sending the data from the device to the cloud ?
- When using a Line Chart, is it possible to define a threshold for the data, so that it generates an ON/OFF chart, OS when the data is above and OFF when the data is below ?
Thank you!
i guess you solved this by changing it in CayenneDefines.h.
virtual channel are MQTT channels used to send data between device and cayenne server. There are not related to analog or digital connective. but then you can use the data received from this channel to do whatever you want.
have a look at this topic Sending MQTT messages within rate limits
i did not understand this
Blockquote
//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
// WiFi network info.
char ssid = “aaa”;
char wifiPassword = “aaa”;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “aaa”;
char password = “aaa”;
char clientID = “aaa”;
// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial
ESP8266 wifi(&EspSerial);
void setup()
{
//Serial.begin(9600);
//delay(10);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(10);
Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}
void loop()
{
Cayenne.loop();
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
//CAYENNE_OUT_DEFAULT() //THIS DOES NOT WORK!!!
CAYENNE_OUT(0) //THIS WORKS!!!
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
Blockquote
OK, so I’m still figuring out how to time the sending of my messages to the cloud.
I read the article you suggested “Sending MQTT messages within rate limits”,
but basically none of the examples provided there work for me.
I’ve narrowed it down to this: if I use only “CAYENNE_OUT(n)” functions with virtualWrite inside
it works perfectly, I get the messages in the cloud every 15 seconds.
But if I use any other kind of methods to send the messages, like “CAYENNE_OUT_DEFAULT()”,
of the if millis code in the loop, or a timer function, or the if state changed code in the loop, then I never get any messages in the cloud, and the device disconnects after about 50 seconds then reconnects after 30 seconds continually. In the code above, If I change just just the one line of code, from “CAYENNE_OUT(0)” to “CAYENNE_OUT_DEFAULT()” then it does not work anymore.
I’ve tried to debug it, and when it does not work in the console I see about 5 commands “AT+CIPSEND=1,40” one after another without anything in between,
but with the working code every command like that is followed my a message to the cloud.
Any advice ? Thank you.
it should work with CAYENNE_OUT_DEFAULT
, i dont see any reason not to work. can you add a debug line inside the CAYENNE_OUT_DEFAULT
function to verify that the function is called or not.
Used exactly the code in my previous post, added the debug message “Inside ” in “CAYENNE_OUT/_DEFAULT” functions, and saved the serial log from both versions: “CAYENNE_OUT(0)” - working and “CAYENNE_OUT_DEFAULT()” - not working.putty_not_working.log (3.2 KB) putty_working.log (3.2 KB)
Can you take a look at this log output ?
In the working version there are some additional messages (lines 20,21) that are missing in the not working version. Also when not working I get disconnected from the server, line 46, which is not happening in the other one. In the not working version I never receive any message on the server.
Thanks.
i tried to look into what was causing the issue but i was not able to reproduce it at my end. can you continue using only CAYENNE_OUT(0)
and keep away from CAYENNE_OUT_DEFAULT
for time being.
I’ve done a little bit of digging and debugging and what I saw was, that when it’s not working, the issue isn’t the sending of the regular data, but as you can see from the comparison of the putty logs,
the initial sending of the device information to the server is somehow wrong, so the device is not registering correctly to the cloud, and then it makes sense to go offline and not receive data.
On the serial, after the “Connected” message, this message should follow:
“Publish: topic 4, channel 65534, value Arduino Uno, subkey , key
AT+CIPSEND=1,40
1j]v1/f065f240-1d9f-11ea-b73d-1be39589cAT+CIPSEND=1,40
6b2/things/f57e5070-2050-11ea-a38a-d5717AT+CIPSEND=1,28
2a4b4d4/sys/modelArduino Uno”, but instead, only this appears “Publish: topic 4, channel 65534, value Arduino Uno, subkey , key
AT+CIPSEND=1,40”, it’s missing the MTTQ authentication information.
And this happens for all the information from the “publishDeviceInfo” function that is called from
“Cayenne.begin()”, and every time.
Any advice ? Thanks!
well, as mentioned in the sketch this code is unstable for arduino uno and i guess that is what is producing this error. You may want to change over to an arduino mega to use the extra hardware serial
I am encountering a similar problem to the original issue raised in the earlier posts on this topic:
- Running code on ESP8266 based device (Wemos D1 R2)
- Connection to Cayenne works fine on home wifi network (normal terrestrial fibre internet).
- Connection to Cayenne fails when going via LTE 4G router (which is how I want to connect the device for remote deployment). This appears to be the same problem described above where the DNS can’t resolve the IP for mqtt.mydevices.com.
- Device connects fine to wifi, and have confirmed internet connection is ok from LTE router from computer.
- Serial log shows following:
[hostByName] request IP for: mqtt.mydevices.com
[hostByName] Host: mqtt.mydevices.com IP: 255.255.255.255
pm open,type:2 0
- If I hardcode the IP address for mqtt.mydevices.com into the CayenneDefines.h file (as described above) then it works. But I don’t want to run with this long term as also discussed in thread above.
Any ideas?
Thanks,
Josh.
Typical, after having tried a lot of different things, it occurred to me straight after posting this that maybe the dns server settings in the LTE router were causing the problem. So I changed them (to the google ones) and voila, it works. Leaving the post here in case anyone else has the same problem.
Thanks to myself for solving.
Josh.