Hi!
I have the following code that I would like to use with my Mega and a CC3000 wifi shield to transmit MQTT messages.
The problem is that my MQTT client cannot connect with the server. I have a broker running in my computer(server) and I have tested it and it works fine - sending and receiving messages. The problem appears after Im connected with my server, I am not able to receive any messages in the Subscription Topic.
if (!client.connected()) {
client = cc3000.connectTCP(server.ip, 1883);
Serial.println("Server connected");
}
if(client.connected()) {
if (mqttclient.connect("home")) {
mqttclient.publish("home","home");
Serial.println("Home connected");
}
}
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <PubSubClient.h>
#define aref_voltage 3.3
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
#define WLAN_SSID "xxxxxxxxxx"
#define WLAN_PASS "xxxxxxxxxx"
#define WLAN_SECURITY WLAN_SEC_WPA2
Adafruit_CC3000_Client client;
//TMP36
int tempPin = 1;
int tempReading;
// We're going to set our broker IP and union it to something we can use
union ArrayToIp {
byte array[4];
uint32_t ip;
};
ArrayToIp server = {
192,168,2,101 };
PubSubClient mqttclient(server.ip, 1883, callback, client, cc3000);
void callback (char* topic, byte* payload, unsigned int length) {
}
void setup(void)
{
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));
analogReference(EXTERNAL);
Serial.println(F("\nInitialising the CC3000 ..."));
if (!cc3000.begin()) {
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
for(;;);
}
Serial.println(F("\nDeleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
/* Attempt to connect to an access point */
char *ssid = WLAN_SSID; /* Max 32 chars */
Serial.print(F("\nAttempting to connect to "));
Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode! */
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP()) {
delay(100); // ToDo: Insert a DHCP timeout!
}
// connect to the broker
if (!client.connected()) {
client = cc3000.connectTCP(server.ip, 1883);
Serial.println("Server connected");
}
// did that last thing work? sweet, let's do something
if(client.connected()) {
if (mqttclient.connect("home")) {
mqttclient.publish("home","home");
Serial.println("Home connected");
}
}
}
void loop(void) {
float temperatureReading = analogRead(tempPin);
float voltage = temperatureReading * aref_voltage;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
// cheap tricks I tell you
char stringTemperatureF[10];
dtostrf(temperatureF, 0, 3, stringTemperatureF);
// are we still connected?
if (!client.connected()) {
client = cc3000.connectTCP(server.ip, 1883);
Serial.println("Server connected");
if(client.connected()) {
if (mqttclient.connect("client")) {
mqttclient.publish("home","home");
Serial.println("Home connected");
}
}
}
else {
// yep, publish that test
mqttclient.publish("home", stringTemperatureF);
}
Serial.println(stringTemperatureF);
delay(5000);
}
EDIT
Code works fine. Tested it with another WiFi shield. Obviously the one before was faulty and can't connect.