delish
This commit is contained in:
parent
0152ac91cb
commit
c79e6e82ea
130
examples/mqtt_cc3k/cc3000helper.cpp
Normal file
130
examples/mqtt_cc3k/cc3000helper.cpp
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
#include <Adafruit_Watchdog.h>
|
||||||
|
#include <Adafruit_CC3000.h>
|
||||||
|
#include <ccspi.h>
|
||||||
|
#include <SPI.h>
|
||||||
|
#include "mqtt_config.h"
|
||||||
|
|
||||||
|
#define STATICIP 1
|
||||||
|
|
||||||
|
uint16_t checkFirmwareVersion(void);
|
||||||
|
bool displayConnectionDetails(void);
|
||||||
|
|
||||||
|
extern Adafruit_CC3000 cc3000;
|
||||||
|
|
||||||
|
boolean CC3000connect(void) {
|
||||||
|
Watchdog.reset();
|
||||||
|
|
||||||
|
// Check for compatible firmware
|
||||||
|
if (checkFirmwareVersion() < 0x113) halt("Wrong firmware version!");
|
||||||
|
|
||||||
|
// Delete any old connection data on the module
|
||||||
|
Serial.println(F("\nDeleting old connection profiles"));
|
||||||
|
if (!cc3000.deleteProfiles()) halt("Failed!");
|
||||||
|
|
||||||
|
#ifdef STATICIP
|
||||||
|
Serial.println(F("Setting static IP"));
|
||||||
|
uint32_t ipAddress = cc3000.IP2U32(10, 0, 1, 19);
|
||||||
|
uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0);
|
||||||
|
uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1);
|
||||||
|
uint32_t dns = cc3000.IP2U32(8, 8, 4, 4);
|
||||||
|
|
||||||
|
if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) {
|
||||||
|
Serial.println(F("Failed to set static IP!"));
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Attempt to connect to an access point
|
||||||
|
Serial.print(F("\nAttempting to connect to "));
|
||||||
|
Serial.print(WLAN_SSID); Serial.print(F("..."));
|
||||||
|
|
||||||
|
Watchdog.disable();
|
||||||
|
// try 3 times
|
||||||
|
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY, 3)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Watchdog.enable(8000);
|
||||||
|
Serial.println(F("Connected!"));
|
||||||
|
|
||||||
|
uint8_t retries;
|
||||||
|
#ifndef STATICIP
|
||||||
|
/* Wait for DHCP to complete */
|
||||||
|
Serial.println(F("Requesting DHCP"));
|
||||||
|
retries = 10;
|
||||||
|
while (!cc3000.checkDHCP())
|
||||||
|
{
|
||||||
|
Watchdog.reset();
|
||||||
|
delay(1000);
|
||||||
|
retries--;
|
||||||
|
if (!retries) return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
/* Display the IP address DNS, Gateway, etc. */
|
||||||
|
retries = 10;
|
||||||
|
while (! displayConnectionDetails()) {
|
||||||
|
Watchdog.reset();
|
||||||
|
delay(1000);
|
||||||
|
retries--;
|
||||||
|
if (!retries) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Watchdog.reset();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Tries to read the CC3000's internal firmware patch ID
|
||||||
|
*/
|
||||||
|
/**************************************************************************/
|
||||||
|
uint16_t checkFirmwareVersion(void)
|
||||||
|
{
|
||||||
|
uint8_t major, minor;
|
||||||
|
uint16_t version;
|
||||||
|
|
||||||
|
if(!cc3000.getFirmwareVersion(&major, &minor))
|
||||||
|
{
|
||||||
|
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
|
||||||
|
version = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print(F("Firmware V. : "));
|
||||||
|
Serial.print(major); Serial.print(F(".")); Serial.println(minor);
|
||||||
|
version = major; version <<= 8; version |= minor;
|
||||||
|
}
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Tries to read the IP address and other connection details
|
||||||
|
*/
|
||||||
|
/**************************************************************************/
|
||||||
|
bool displayConnectionDetails(void)
|
||||||
|
{
|
||||||
|
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
|
||||||
|
|
||||||
|
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
|
||||||
|
{
|
||||||
|
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
|
||||||
|
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
|
||||||
|
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
|
||||||
|
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
|
||||||
|
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
|
||||||
|
Serial.println();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
102
examples/mqtt_cc3k/mqtt_cc3k.ino
Normal file
102
examples/mqtt_cc3k/mqtt_cc3k.ino
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
#include <Adafruit_Watchdog.h>
|
||||||
|
#include <Adafruit_CC3000.h>
|
||||||
|
#include <ccspi.h>
|
||||||
|
#include <SPI.h>
|
||||||
|
#include "utility/debug.h"
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
#include "Adafruit_MQTT.h"
|
||||||
|
#include "Adafruit_MQTT_CC3000.h"
|
||||||
|
#include "mqtt_config.h"
|
||||||
|
|
||||||
|
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2); // you can change this clock speed
|
||||||
|
|
||||||
|
Adafruit_MQTT_CC3000 mqtt(&cc3000, AIO_SERVER, AIO_SERVERPORT, AIO_CLIENTNAME, AIO_KEY, AIO_PASSWORD);
|
||||||
|
|
||||||
|
const char PHOTOCELL_FEED[] PROGMEM = "api/feeds/photocell/data/send.json";
|
||||||
|
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
||||||
|
|
||||||
|
const char ONOFF_FEED[] PROGMEM = "api/feeds/onoff/data/receive.json";
|
||||||
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
Serial.println(F("Adafruit MQTT demo"));
|
||||||
|
|
||||||
|
Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC);
|
||||||
|
|
||||||
|
// Initialise the CC3000 module
|
||||||
|
Serial.print(F("\nInit the CC3000..."));
|
||||||
|
if (!cc3000.begin())
|
||||||
|
halt("Failed");
|
||||||
|
|
||||||
|
mqtt.subscribe(&onoffbutton);
|
||||||
|
|
||||||
|
while (! CC3000connect()) {
|
||||||
|
Serial.println(F("Retrying WiFi"));
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println(F("Connected to WiFi!"));
|
||||||
|
//////////////////////////////
|
||||||
|
Serial.println(F("Connecting to MQTT..."));
|
||||||
|
int8_t ret;
|
||||||
|
while ((ret = mqtt.connect()) != 0) {
|
||||||
|
switch (ret) {
|
||||||
|
case 1: Serial.println(F("Wrong protocol")); break;
|
||||||
|
case 2: Serial.println(F("ID rejected")); break;
|
||||||
|
case 3: Serial.println(F("Server unavail")); break;
|
||||||
|
case 4: Serial.println(F("Bad user/pass")); break;
|
||||||
|
case 5: Serial.println(F("Not authed")); break;
|
||||||
|
case 6: Serial.println(F("Failed to subscribe")); break;
|
||||||
|
default: {
|
||||||
|
Serial.println(F("Connection failed"));
|
||||||
|
CC3000connect(); // y0w, lets connect to wifi again
|
||||||
|
return; // restart the loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.println(F("Retrying MQTT connection"));
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
|
//////////////////////////////
|
||||||
|
|
||||||
|
Serial.println(F("MQTT Connected!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t x=0;
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
Adafruit_MQTT_Subscribe *subscription;
|
||||||
|
|
||||||
|
Watchdog.reset();
|
||||||
|
|
||||||
|
// Try to ping the MQTT server
|
||||||
|
/*
|
||||||
|
if (! mqtt.ping(3) ) {
|
||||||
|
// MQTT pings failed, lets reconnect
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// this is our 'wait for incoming subscription packets' busy subloop
|
||||||
|
while (subscription = mqtt.readSubscription(1000)) {
|
||||||
|
if (subscription == &onoffbutton) {
|
||||||
|
Serial.print(F("Got: "));
|
||||||
|
Serial.println((char *)onoffbutton.lastread);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now we can publish stuff!
|
||||||
|
|
||||||
|
Serial.print(F("\nSending photocell val "));
|
||||||
|
Serial.print(x);
|
||||||
|
Serial.print("...");
|
||||||
|
if (! photocell.publish(x++)) {
|
||||||
|
Serial.println(F("Failed"));
|
||||||
|
} else {
|
||||||
|
Serial.println(F("OK!"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
42
examples/mqtt_cc3k/mqtt_config.h
Normal file
42
examples/mqtt_cc3k/mqtt_config.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#define IDLE_TIMEOUT_MS 5000
|
||||||
|
|
||||||
|
/*************************** CC3000 Pins ***********************************/
|
||||||
|
|
||||||
|
// These are the interrupt and control pins
|
||||||
|
#define ADAFRUIT_CC3000_IRQ 2 // MUST be an interrupt pin!
|
||||||
|
// These can be any two pins
|
||||||
|
#define ADAFRUIT_CC3000_VBAT 8
|
||||||
|
#define ADAFRUIT_CC3000_CS 10
|
||||||
|
// Use hardware SPI for the remaining pins
|
||||||
|
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
|
||||||
|
|
||||||
|
/************************* WiFi Access Point *********************************/
|
||||||
|
|
||||||
|
#define WLAN_SSID "" // cannot be longer than 32 characters!
|
||||||
|
#define WLAN_PASS ""
|
||||||
|
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
|
||||||
|
#define WLAN_SECURITY WLAN_SEC_WPA2
|
||||||
|
|
||||||
|
/************************* Adafruit.io Setup *********************************/
|
||||||
|
|
||||||
|
const char AIO_SERVER[] PROGMEM = "io.adafruit.com";
|
||||||
|
#define AIO_SERVERPORT 1883
|
||||||
|
const char AIO_CLIENTNAME[] PROGMEM = "huzzah";
|
||||||
|
const char AIO_KEY[] PROGMEM = "";
|
||||||
|
const char AIO_PASSWORD[] PROGMEM = "";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const char DBG_SERVER[] PROGMEM = "apt.adafruit.com";
|
||||||
|
#define DBG_SERVERPORT 1883
|
||||||
|
const char DBG_CLIENTNAME[] PROGMEM = "huzzah";
|
||||||
|
const char DBG_KEY[] PROGMEM = "";
|
||||||
|
const char DBG_PASSWORD[] PROGMEM = "";
|
||||||
|
|
||||||
|
|
||||||
|
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||||
|
|
||||||
|
|
||||||
|
#define println_P(s) { for (uint8_t i=0; i<strlen_P(s); i++) { Serial.write(pgm_read_byte(s+i)); } }
|
||||||
|
boolean CC3000connect(void);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user