Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 21 of 21

Thread: Adafruit.io MQTT

  1. #1
    Junior Member
    Join Date
    Feb 2015
    Posts
    3

    Adafruit.io MQTT

    Hello, I am trying to use this tutorial with teensy 3.1


    The code will work with my teensy?


    Thanks.

  2. #2
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,915
    The MQTT part will be fine, but you don't say how you are getting your Teensy to connect to Adafruit IO?

    There will be some minor hurdles there matching up a wifi or ethernet module to work with with the examples but if you can get internet connectivity to work the MQTT should be fine.

  3. #3
    Junior Member
    Join Date
    Feb 2015
    Posts
    3
    I'm using CC3000, but I cant use the Adafruit_MQTT in the tutorial.

  4. #4
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,915
    Is in you can't compile the library, or you can't get the CC3000 to work for anything?

    Before trying to get MQTT to work would suggest getting a handle on basic network connectivity first.

  5. #5
    Junior Member
    Join Date
    Feb 2015
    Posts
    3
    The CC3000 is working, but can't compile the Adafruit MQTT example without errors.

    Resuming:

    Setup-> teensy 3.1 + CC3000 wifi breakout board + io.adafruit.com account
    Testing-> Adafruit MQTT Library (CC3000) -> needs the Adafruit SleepyDog Arduino Library


    When I try to verify/compile the mqtt_cc3k (CC3000) example from Adafruit_MQTT_Library the IDE give me errors:

    Error compiling for board Teensy 3.2 / 3.1.
    Code:
    Arduino: 1.6.8 (Windows 10), TD: 1.28-beta1, Board: "Teensy 3.2 / 3.1, Serial, 96 MHz optimized (overclock), US English"
    
    Build options changed, rebuilding all
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:14:4: error: expected constructor, destructor, or type conversion before '(' token
    
     ISR(WDT_vect)
    
        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp: In member function 'int WatchdogAVR::enable(int)':
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:25:21: error: 'wdt_enable' was not declared in this scope
    
         wdt_enable(_wdto);
    
                         ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp: In member function 'void WatchdogAVR::reset()':
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:31:15: error: 'wdt_reset' was not declared in this scope
    
         wdt_reset();
    
                   ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp: In member function 'void WatchdogAVR::disable()':
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:36:17: error: 'wdt_disable' was not declared in this scope
    
         wdt_disable();
    
                     ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp: In member function 'int WatchdogAVR::sleep(int)':
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:46:5: error: 'uint8_t' was not declared in this scope
    
         uint8_t wdps = ((sleepWDTO & 0x08 ? 1 : 0) << WDP3) |
    
         ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:46:13: error: expected ';' before 'wdps'
    
         uint8_t wdps = ((sleepWDTO & 0x08 ? 1 : 0) << WDP3) |
    
                 ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:52:9: error: 'cli' was not declared in this scope
    
         cli();
    
             ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:54:5: error: 'MCUSR' was not declared in this scope
    
         MCUSR &= ~(1<<WDRF);
    
         ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:54:19: error: 'WDRF' was not declared in this scope
    
         MCUSR &= ~(1<<WDRF);
    
                       ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:59:5: error: 'WDTCSR' was not declared in this scope
    
         WDTCSR |= (1<<WDCE) | (1<<WDE);  // Set WDCE and WDE to enable changes.
    
         ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:59:19: error: 'WDCE' was not declared in this scope
    
         WDTCSR |= (1<<WDCE) | (1<<WDE);  // Set WDCE and WDE to enable changes.
    
                       ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:59:31: error: 'WDE' was not declared in this scope
    
         WDTCSR |= (1<<WDCE) | (1<<WDE);  // Set WDCE and WDE to enable changes.
    
                                   ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:60:14: error: 'wdps' was not declared in this scope
    
         WDTCSR = wdps;                   // Set the prescaler bit values.
    
                  ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:61:19: error: 'WDIE' was not declared in this scope
    
         WDTCSR |= (1<<WDIE);             // Enable only watchdog interrupts.
    
                       ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:63:9: error: 'sei' was not declared in this scope
    
         sei();
    
             ^
    
    In file included from %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:8:0:
    
    C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/avr/sleep.h:36:23: error: expected primary-expression before 'asm'
    
     #define sleep_cpu()  (asm("wfi"))
    
                           ^
    
    C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/avr/sleep.h:38:23: note: in expansion of macro 'sleep_cpu'
    
     #define sleep_mode()  sleep_cpu()
    
                           ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:67:5: note: in expansion of macro 'sleep_mode'
    
         sleep_mode();
    
         ^
    
    C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/avr/sleep.h:36:23: error: expected ')' before 'asm'
    
     #define sleep_cpu()  (asm("wfi"))
    
                           ^
    
    C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3/avr/sleep.h:38:23: note: in expansion of macro 'sleep_cpu'
    
     #define sleep_mode()  sleep_cpu()
    
                           ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:67:5: note: in expansion of macro 'sleep_mode'
    
         sleep_mode();
    
         ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:77:25: error: 'wdt_enable' was not declared in this scope
    
             wdt_enable(_wdto);
    
                             ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp: In member function 'void WatchdogAVR::_setPeriod(int, int&, int&)':
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:89:20: error: 'WDTO_8S' was not declared in this scope
    
             wdto     = WDTO_8S;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:93:20: error: 'WDTO_4S' was not declared in this scope
    
             wdto     = WDTO_4S;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:97:20: error: 'WDTO_2S' was not declared in this scope
    
             wdto     = WDTO_2S;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:101:20: error: 'WDTO_1S' was not declared in this scope
    
             wdto     = WDTO_1S;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:105:20: error: 'WDTO_500MS' was not declared in this scope
    
             wdto     = WDTO_500MS;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:109:20: error: 'WDTO_250MS' was not declared in this scope
    
             wdto     = WDTO_250MS;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:113:20: error: 'WDTO_120MS' was not declared in this scope
    
             wdto     = WDTO_120MS;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:117:20: error: 'WDTO_60MS' was not declared in this scope
    
             wdto     = WDTO_60MS;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:121:20: error: 'WDTO_30MS' was not declared in this scope
    
             wdto     = WDTO_30MS;
    
                        ^
    
    %UserProfile%\Documents\Arduino\libraries\Adafruit_SleepyDog_Library\utility\WatchdogAVR.cpp:125:20: error: 'WDTO_15MS' was not declared in this scope
    
             wdto     = WDTO_15MS;
    
                        ^
    
    Error compiling for board Teensy 3.2 / 3.1.
    
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    Last edited by J83R; 04-06-2016 at 09:03 PM.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,073
    I've ported Adafruit_SleepyDog to Teensy. You can get it here:

    https://github.com/PaulStoffregen/Adafruit_SleepyDog

    Just install this version, and your code will compile.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,073
    Adafruit has merged the code, so you can get this working by just getting the latest from Adafruit's library.

    Edit: oh, and you'll need to remove an AVR define... which I removed here. Maybe wait for 1.28 release (within the next day or two)
    Last edited by PaulStoffregen; 04-07-2016 at 08:47 PM.

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,333
    Nice, I have been wanting to see good example for MQTT for my ESP8266 on Teensy - so double win here as AdaFruit shows it working on Huzzah!

    Good to see Teensy sleep and watchdog too - slight bug in README.md - doesn't note the fixes to include Teensy

    Is there a way to wake a Teensy early from Watchdog.sleep(); with a pin interrupt prepared in advance? That would be great to see if so.

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,073
    So far I haven't implemented the sleep function in Adafruit_SleepyDog. I only did the watchdog.

  10. #10
    Senior Member adrian's Avatar
    Join Date
    Oct 2015
    Location
    Wellington, NZ
    Posts
    504
    Has anyone installed an mtqq broker on the teensy 3.x?

  11. #11
    Has anyone implemented something similar using the esp8266? I'm looking to use adafruit or aws to do mqtt and I'd like to setup the teensy to work over WiFi for internet connection..

    I was thinking of using a particle and passing the payload to the teensy but it would be nice if I could do it all in one..

  12. #12
    Junior Member
    Join Date
    Mar 2017
    Posts
    12
    I had a good success with the following solution: https://github.com/mrizvic/nodemcu-uart2mqtt

    I am using it to connect to a private MQTT server, but I don't see why it won't work with any other.

  13. #13
    Thanks danshtr
    What WiFi module and library did you use for connectivity WiFi wise?

  14. #14
    Junior Member
    Join Date
    Mar 2017
    Posts
    12
    I have used the above library with ESP8266. You would need to follow the instructions on that page. Mainly:

    * Load nodemcu on the 8266.
    * Edit the init.lua file
    * Upload the init.lua file to node MCU
    * Connect the 8266 RX and TX to Teensy's RX and TX.
    * Use and modify the example code from that page.

    Notice that I have mentioned that I am using this setup to connect to a privately running MQTT. I don't use Adafruit's MQTT server.

    HTH

  15. #15
    Thanks very much I really appreciate you taking the time.. Sorry I'm totally new to all of this and reading like crazy!

    I really hope I can pull this project off haha hoping to do some screen work with a bit of video and a images and then some mqtt to get some servos moving!

  16. #16
    Junior Member
    Join Date
    Mar 2017
    Posts
    12
    If you don't need gauntleted delivery (not that MQTT is perfect), maybe RF24L01 will fit your needs. It is way way easier.

    https://arduino-info.wikispaces.com/...1-2.4GHz-HowTo

  17. #17
    Unfortunately mqtt is a project requirement

  18. #18
    Senior Member
    Join Date
    Mar 2013
    Posts
    651
    Quote Originally Posted by PaulStoffregen View Post
    I only did the watchdog.
    Man, every time I did a search for Teensy 3 watchdog I only ever found the few posts about manually doing it. Adding this to my list of libraries to use.

    Paul, I need to send you some beer

  19. #19
    Junior Member
    Join Date
    Mar 2017
    Posts
    12
    An update: while working, the NodeMCU solution was very hard to use/modify.

    I have set to a new solution, which is much stable and do not require to flash anything on the ESP8266:

    Amazing library that uses AT commands to emulate a "client" library: https://github.com/bportaluri/WiFiEsp
    A simple MQTT library that works for me: https://github.com/knolleary/pubsubclient


    HTH

  20. #20
    Junior Member
    Join Date
    May 2015
    Posts
    8
    Quote Originally Posted by danshtr View Post
    An update: while working, the NodeMCU solution was very hard to use/modify.

    I have set to a new solution, which is much stable and do not require to flash anything on the ESP8266:

    Amazing library that uses AT commands to emulate a "client" library: https://github.com/bportaluri/WiFiEsp
    A simple MQTT library that works for me: https://github.com/knolleary/pubsubclient


    HTH
    hello Do you have any basic code to share - with both libraries mentioned above thanks in advance Charles

  21. #21
    Junior Member
    Join Date
    Mar 2017
    Posts
    12
    This code wasn't intended to more than a prototype, that worked, so I left it as is....

    The code is waiting for messages from nRF24L01 and post them to MQTT server.

    The most relevant piece of code that you might be interested in is:

    Code:
    WiFiEspClient net;
    PubSubClient client(net);
    For the rest, just follow the examples on the PubSubClient library.

    Hope it will help you.

    Code:
    // undefine this if WIFI settings are hardcoded into nodemcu lua script
    #include <RF24.h>
    #include <SPI.h>
    #include "WiFiEsp.h"
    #include "WiFiEspClient.h"
    #include <PubSubClient.h>
    
    // WIFI settings to pass towards esp
    char SSIDNAME[] =  "myssid";
    char SSIDPASS[] =  "mypassword";
    
    // baudrate towards computer (USB) and towards esp
    #define USB_BAUDRATE  19200
    #define ESP_BAUDRATE  19200
    
    int status = WL_IDLE_STATUS; //
    WiFiEspClient net;
    PubSubClient client(net);
    
    void messageReceived(char *topic, char *payload, unsigned int length) {
      Serial.print("incoming: ");
      Serial.print(topic);
      Serial.print(" - ");
      Serial.print(payload);
      Serial.println();
    }
    
    
    // periodically send data to esp (in milliseconds)
    unsigned long interval = 1000;
    
    
    // general consts
    const uint8_t PIN_ESP_ENABLE =  6;
    // stop unless you want to modify the code :)
    
    int x = 0;
    
    
    
    static char sreg = 0;
    
    static unsigned long previousMillis = 0;
    static unsigned long currentMillis = 0;
    
    static boolean connected = 0;
    // positions
    const uint8_t POS_BASE           = 1;
    const uint8_t POS_WORKING_GREEN  = 2;
    const uint8_t POS_WORKING_YELLOW = 3;
    const uint8_t POS_WORKING_RED    = 4;
    const uint8_t POS_LEARNING       = 5;
    const uint8_t POS_OTHER          = 6;
    const uint8_t POS_NADA           = 7;
    uint8_t curPos=POS_NADA;
    
    //radio
    RF24 myRadio (14, 16);
    byte addresses[][6] = {"1Cube"};
    const uint32_t RF_MAGIC_NUMBER        = 136768103;
    const uint8_t RF_CHANNEL              = 119;
    const uint16_t RF_MILLIS_PACE         = 2000;
    const uint8_t RF_MISSING_BITS_COUTNER = 20;
    typedef struct  {
        uint32_t magic = RF_MAGIC_NUMBER;
        uint8_t pos;
    } tRFData;
    tRFData rfData;
    unsigned long prevRF = 0;
    uint8_t missingBits = RF_MISSING_BITS_COUTNER;
    
    void printPosName(uint8_t pos){
        const char *posNames[8];
        posNames[0] = "NONE";
        posNames[POS_BASE] = "BASE";
        posNames[POS_WORKING_GREEN] = "GREEN";
        posNames[POS_WORKING_YELLOW] = "YELLOW";
        posNames[POS_WORKING_RED] = "RED";
        posNames[POS_LEARNING] = "LEARNING";
        posNames[POS_OTHER] = "OTHER";
        posNames[POS_NADA] = "NADA";
        Serial.print(posNames[pos]);
    }
    
    
    void setup() {
        // Serial = USB (console)
        // Serial3 = tx3/rx3 - esp
        Serial.begin(USB_BAUDRATE);
        Serial3.begin(ESP_BAUDRATE);
        // wait for serial hardware to finish setup
        while (!Serial3);
    
        pinMode(PIN_ESP_ENABLE, OUTPUT);
        digitalWrite(PIN_ESP_ENABLE,LOW);
        delay(500);
        digitalWrite(PIN_ESP_ENABLE,HIGH);
        delay(1000);
    
        WiFi.init(&Serial3);
    
    
        if (WiFi.status() == WL_NO_SHIELD) {
             Serial.println("WiFi shield not present");
             // don't continue
             while (true);
        }
    
        while ( status != WL_CONNECTED) {
            Serial.print("Attempting to connect to WPA SSID: ");
            Serial.println(SSIDNAME);
            // Connect to WPA/WPA2 network
            status = WiFi.begin(SSIDNAME, SSIDPASS);
        }
        client.setServer("myMQTTServerAddress", 1883);
    
        Serial.println("You're connected to the network");
        printWifiData();
        printCurrentNet();
        // if not connected and stuff then restart and init
        Serial.flush();
        myRadio.begin();
        myRadio.setChannel(RF_CHANNEL);
        myRadio.setPALevel(RF24_PA_HIGH);
        myRadio.openReadingPipe(0, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
        myRadio.startListening();
        Serial.println("After RF init");
        MQTT_connect();
    }
    
    void MQTT_connect() {
      Serial.print("checking wifi...");
      while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(1000);
      }
    
      Serial.print("\nconnecting...");
      while (!client.connect("taskCubeMQTTz")) {
        Serial.print(".");
        delay(1000);
      }
    
      Serial.println("\nconnected!");
      // client.unsubscribe("/example");
    }
    
    void printWifiData()
    {
      // print your WiFi shield's IP address
      IPAddress ip = WiFi.localIP();
      Serial.print("IP Address: ");
      Serial.println(ip);
    
      // print your MAC address
      byte mac[6];
      WiFi.macAddress(mac);
      char buf[20];
      sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
      Serial.print("MAC address: ");
      Serial.println(buf);
    }
    void printCurrentNet()
    {
      // print the SSID of the network you're attached to
      Serial.print("SSID: ");
      Serial.println(WiFi.SSID());
    
      // print the MAC address of the router you're attached to
      byte bssid[6];
      WiFi.BSSID(bssid);
      char buf[20];
      sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
      Serial.print("BSSID: ");
      Serial.println(buf);
    
      // print the received signal strength
      long rssi = WiFi.RSSI();
      Serial.print("Signal strength (RSSI): ");
      Serial.println(rssi);
    }
    void loop() {
        
        if(!client.connected()) {
            MQTT_connect();
        }
        currentMillis = millis();
    
        // RF24
        if (currentMillis - prevRF >=  RF_MILLIS_PACE) {
            if (myRadio.available()) {
                myRadio.read(&rfData, sizeof(rfData));
                if (rfData.magic == RF_MAGIC_NUMBER) {
                    printPosName(rfData.pos);
                    curPos=rfData.pos;
                    Serial.println(' ');
    
                } else {
                    Serial.println("Error in receiving");
                }
                //read rest of buffer
                while(myRadio.available()) {
                    myRadio.read(&rfData, sizeof(rfData));
                }
                missingBits = RF_MISSING_BITS_COUTNER;
            } else {
                if (missingBits == 0) {
                    curPos = POS_NADA;
                } else {
                    missingBits--;
                }
            }
            //convert int to char
            char buf[5];
            String(curPos).toCharArray(buf,5);
            client.publish("taskCube/status",buf);
            prevRF = currentMillis;
        }
    
    }
    Last edited by danshtr; 04-17-2017 at 04:16 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •