New project code named: teensquitto

Status
Not open for further replies.
DeQueing Handler! Queue size: x in debug means handler data was queued, and it's being unqueued
PROCESS?: 175 175 5 0 12 2 0 0 0 0 0 11 0 0 0 0 0 0 0 11 116 101 101 110 115 113 117 105 116 116 111 47 104 101 108 108 111 32 119 111 114 108 100 111
^------ mqtt packet for handler, which topic and payload is included, is the data debug of the handler capture from deque

Code:
****************************
Publish received.
 topic: teensquitto/ payload: hello world
 qos: 2 dup: 0
 retain: 0 len: 11 index: 0 total: 11

^-- the resulting print data from the user's sketch handler function, which was "asynchronosly queued" ? :D
 
Heres another video of jumping the ESP's RX line to an external uart interface for debugging, ESP (left(black putty)), TEENSY (right(serial monitor)), displaying Unicast and Broadcast packets being captured while mqtt callbacks are firing for subscription publishes which are being flooded in a tight loop, ending with a capture of the sketch showing the async callback for marvin's mqtt library which formats the printout for the serial monitor

https://www.youtube.com/watch?v=VTF_c4Sbjbo

this is the sketch currently running, still lots of cleaning up to do, all the tests keep adding bloat :D

Code:
#include <teensquitto.h>
#include <IPAddress.h>

teensquitto amber = teensquitto("amber", &Serial1, "192.168.2.122:1883", "ESP02", "tony");
teensquitto mqtt = teensquitto("MQTT", &Serial1);
teensquitto client = teensquitto("WiFiClient", &Serial1);
teensquitto sclient = teensquitto("WiFiClientSecure", &Serial1);
teensquitto Udp = teensquitto("WiFiUdp", &Serial1);
teensquitto eep = teensquitto("EEPROM", &Serial1);
teensquitto webSocketClient = teensquitto("WSC", &Serial1);
teensquitto webSocketServer = teensquitto("WSS", &Serial1);


void setup() {
  Serial.begin(1);
  Serial1.begin(2000000);
  Serial2.begin(921600);
  delay(2000);

  ESP.onDetect([]() {
    IPAddress ip(192, 168, 2, 168);
    IPAddress gate(192, 168, 2, 1);
    IPAddress sub(255, 255, 255, 0);
    WiFi.mode(WIFI_STA);
    WiFi.begin("Tony", "password");
    WiFi.waitForConnectResult();
    WiFi.config(ip, gate, sub);
    // setup MQTT handlers
    mqtt.onConnect(onMqttConnect);
    mqtt.onDisconnect(onMqttDisconnect);
    mqtt.onSubscribe(onMqttSubscribe);
    mqtt.onUnsubscribe(onMqttUnsubscribe);
    mqtt.onMessage(onMqttMessage);
    mqtt.onPublish(onMqttPublish);
    WiFi.onStationModeGotIP(onWifiConnect);
    WiFi.onStationModeDisconnected(onWifiDisconnect);
    // setup MQTT connection
    IPAddress _server = { 192, 168, 2, 122 };
    mqtt.setKeepAlive(8);
    mqtt.setMaxTopicLength(128);
    mqtt.setClientId("Tony");
    mqtt.setCredentials("ESP02", "tony");
    mqtt.setServer(_server, 1883);
    mqtt.connect();

    webSocketClient.onEvent(webSocketEventClient);
    webSocketServer.onEvent(webSocketEventServer);

    webSocketServer.begin(); webSocketServer.setAuthorization("tony", "teensy");
    webSocketClient.disconnect();
    //  webSocketClient.begin("192.168.2.55", 43110, "/");
    // webSocketClient.setAuthorization("tony", "teensy");
    webSocketClient.setReconnectInterval(5000);

    if (  udp.listen(1234) ) {
      udp.onPacket([](AsyncUDPPacket packet) {
        Serial.print("UDP Packet Type: ");
        Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
        Serial.print(", From: ");
        Serial.print(packet.remoteIP());
        Serial.print(":");
        Serial.print(packet.remotePort());
        Serial.print(", To: ");
        Serial.print(packet.localIP());
        Serial.print(":");
        Serial.print(packet.localPort());
        Serial.print(", Length: ");
        Serial.print(packet.length());
        Serial.print(", Data: ");
        Serial.write(packet.data(), packet.length());
        Serial.println();
        // reply to the client
        packet.printf("Got %u bytes of data", packet.length());
      });
    }


  }); // end of onDetect callback

}

void webSocketEventServer(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  switch (type) {
    case WStype_DISCONNECTED:
      Serial.printf("[%u] Disconnected!\n", num);
      break;
    case WStype_CONNECTED:
      {
        Serial.println("connected server");
        //                IPAddress ip = webSocketServer.remoteIP(num);
        //                Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);

        // send message to client
        //  webSocketServer.sendTXT(num, "Connected");
      }
      break;
    case WStype_TEXT:
      Serial.printf("[%u] get Text: %s\n", num, payload);

      // send message to client
      // webSocket.sendTXT(num, "message here");

      // send data to all connected clients
      // webSocket.broadcastTXT("message here");
      break;
    case WStype_BIN:
      Serial.printf("[%u] get binary length: %u\n", num, length);
      //      hexdump(payload, length);

      // send message to client
      // webSocket.sendBIN(num, payload, length);
      break;
  }
}
void webSocketEventClient(WStype_t type, uint8_t * payload, size_t length) {
  switch (type) {
    case WStype_DISCONNECTED:
      Serial.printf("[WSc] Disconnected!\n");
      break;
    case WStype_CONNECTED: {
        Serial.printf("[WSc] Connected to url: %s\n", payload);

        // send message to server when Connected
        //  webSocketClient.sendTXT("Connected");
      }
      break;
    case WStype_TEXT:
      Serial.printf("[WSc] get text: %s\n", payload);

      // send message to server
      // webSocket.sendTXT("message here");
      break;
    case WStype_BIN:
      Serial.printf("[WSc] get binary length: %u\n", length);
      //  hexdump(payload, length);

      // send data to server
      // webSocket.sendBIN(payload, length);
      break;
  }
}



void onMqttConnect(bool sessionPresent) {
  mqtt.subscribe("teensquitto/#", 2);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
}
void onMqttUnsubscribe(uint16_t packetId) {
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  Serial.println("****************************");
  Serial.println("Publish received.");
  Serial.print(" topic: "); Serial.print(topic);
  Serial.print(" payload: ");
  for ( uint32_t i = 0; i < len; i++ ) Serial.print(payload[i]); Serial.println();
  Serial.print(" qos: "); Serial.print(properties.qos); Serial.print(" dup: "); Serial.println(properties.dup);
  Serial.print(" retain: "); Serial.print(properties.retain);
  Serial.print(" len: "); Serial.print(len); Serial.print(" index: "); Serial.print(index); Serial.print(" total: "); Serial.println(total);
  Serial.println(""); Serial.println("");
}
void onMqttPublish(uint16_t packetId) {
  Serial.print("Packet ID: "); Serial.println(packetId);
}

void onWifiConnect(const WiFiEventStationModeGotIP& event) {
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
  Serial.println("--------DISCONNECTED!!---------------");
  Serial.println(event.ssid);
  Serial.println(event.bssid[0]);
  Serial.println(event.bssid[1]);
  Serial.println(event.bssid[2]);
  Serial.println(event.bssid[3]);
  Serial.println(event.bssid[4]);
  Serial.println(event.bssid[5]);
  Serial.println(event.reason);
  Serial.println("-----------------------------------");
}
const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";
const uint8_t fingerprintb[] = { 0xCF, 0x05, 0x98, 0x89, 0xCA, 0xFF, 0x8E, 0xD8, 0x5E, 0x5C, 0xE0, 0xC2, 0xE4, 0xF7, 0xE6, 0xC3, 0xC7, 0x50, 0xDD, 0x5C };
void loop() {

  //  delay(100); Serial.print("MILLIS(): "); Serial.println(millis());

  static uint32_t timer = millis();
  if ( millis() - timer > 50 ) {
    timer = millis();

    static bool toggle_led; toggle_led = !toggle_led; pinMode(13, OUTPUT); digitalWrite(13, toggle_led);

    static bool conn_udp = 1;
    webSocketClient.sendTXT("Hello Server!, from Tony!");
    //       if ( conn_udp && udp.listen(1234) ) {
    //      webSocketServer.begin(); webSocketServer.setAuthorization("tony", "teensy");
    //      webSocketClient.disconnect();
    //      webSocketClient.begin("192.168.2.55", 43110, "/");
    //      webSocketClient.setAuthorization("tony", "teensy");
    //      webSocketClient.setReconnectInterval(5000);
    //      conn_udp = 0;
    //      udp.onPacket([](AsyncUDPPacket packet) {
    //        Serial.print("UDP Packet Type: ");
    //        Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
    //        Serial.print(", From: ");
    //        Serial.print(packet.remoteIP());
    //        Serial.print(":");
    //        Serial.print(packet.remotePort());
    //        Serial.print(", To: ");
    //        Serial.print(packet.localIP());
    //        Serial.print(":");
    //        Serial.print(packet.localPort());
    //        Serial.print(", Length: ");
    //        Serial.print(packet.length());
    //        Serial.print(", Data: ");
    //        Serial.write(packet.data(), packet.length());
    //        Serial.println();
    //        // reply to the client
    //        packet.printf("Got %u bytes of data", packet.length());
    //      });
    //    }

    mqtt.publish("teensquitto/", 2, 1, "hello world"); // WiFi.scanNetworksAsync(hndl, 1);
  }
  WiFi.events();
}

void serialEvent1() {
  WiFi.events();
}

void hndl(int networksFound) {
  Serial.print("Networks Found: "); Serial.println(networksFound);
}


void serialEvent() {
  static bool _DTR = Serial.dtr();
  if ( _DTR != Serial.dtr() ) {
    uint8_t ESP_RESET = 24, ESP_GPIO0 = 25;
    static uint32_t _baud = 1;
    _DTR = Serial.dtr();
    if ( !_DTR ) {
      delay(100); while ( Serial.available() > 0 ) Serial.read();
    }
    else {
      if ( _baud != Serial.baud() ) {
        _baud = Serial.baud(); pinMode(0, INPUT_DISABLE); Serial1.setRX(27); Serial1.begin(_baud);
      }
      delay(40); if ( Serial.peek() != 0xC0 ) return;
      pinMode(ESP_RESET, OUTPUT); digitalWriteFast(ESP_RESET, 0);
      pinMode(ESP_GPIO0, OUTPUT); digitalWriteFast(ESP_GPIO0, 0);
      while ( Serial.available() > 0 ) Serial.read();
      uint32_t _pulse = millis(), _count = 0, _program_timeout = millis();
      while (1) {
        if ( _baud != Serial.baud() ) {
          _baud = Serial.baud(); Serial1.begin(_baud);
        }
        digitalWriteFast(ESP_RESET, !Serial.dtr());
        if ( millis() - _program_timeout > 12000 ) {
          pinMode(ESP_GPIO0, INPUT_DISABLE); digitalWriteFast(ESP_RESET, 0); delay(100); pinMode(ESP_RESET, INPUT_DISABLE);
          Serial1.setRX(0); pinMode(27, INPUT_DISABLE); Serial1.begin(2000000); return;
        }
        while ( Serial.available() > 0 ) {
          _pulse = millis(); _count++;
          if ( _count > 3500 ) {
            _program_timeout = millis(); pinMode(ESP_GPIO0, INPUT_DISABLE);
            if ( millis() - _pulse > 500 ) {
              pinMode(ESP_RESET, INPUT_DISABLE); ((*(volatile uint32_t *)0xE000ED0C) = (0x5FA0004));
            }
          }
          Serial1.write(Serial.read());
        }
        if ( Serial1.available() > 0 ) Serial.write(Serial1.read());
        if ( _count > 3500 ) {
          _program_timeout = millis(); pinMode(ESP_GPIO0, INPUT_DISABLE);
          if ( millis() - _pulse > 500 ) {
            pinMode(ESP_RESET, INPUT_DISABLE); ((*(volatile uint32_t *)0xE000ED0C) = (0x5FA0004));
          }
        }
      }
    }
  }
}

this next video shows me holding the ESP module in reset, where the library detects this state and shuts off all functions being called to allow loop() to run at top speed. blinking led on teensy shows what happens during the disconnection while a function is being used in a detected state till timeout, led resumes it's loop() cycle afterwards while library remains offline, all available functions in library remain in a return state with no functionality until the ESP comes back, at which point, the lamba ESP.onDetect([](){}); function re-registers all setup() calls for the ESP while the host library re-enables itself, resuming course :)

https://www.youtube.com/watch?v=ezgRw6VxmhA

and no, there is no RTS/CTS being used at these speeds :)
 
Last edited:
Hi tonton81
Was scanning through the posts and watching the video. Looks really really interesting.

I do have a couple of questions This thread and your SPI_MSTranfer thread are giving me all kinds of ideas.

I am assuming this would work with a 3.2 and a ESP8266 (have a breakout board with a mega footprint with a 8266 builtin and a couple of other interfaces, (2) could this work in conjunction with the SPI_MSTransfer library??? Think if would just don't know about transfer rates = nice to do things at the desk but even more interesting to put it on a rover and run it around the yard.

Thanks
Mike
 
yea, if you looked back a few posts back in my other thread
i was running async UDP with async mqtt while using the new SPI_MSTransfer all same time

library supports all ESP core updates and library updates as I made it as transparent as possible
You can even access SPIFFs from teensy, to read/write logging files :)
 
Ok. Picked up a huzzah feather 8266 and getting ready to start this up again and would you believe it - I forgot the connections that have to be made between the teensy and 8266? Help :) Mind too much on SPI lately
 
I seem to remember RX0 was for debugging????? By the way having problem getting UDP working with HUZZAH - 99% sure its me and some firewall blocking it. Argh - nothing is ever easy is it////
 
RX debugging on ESP is not influenced by the incoming buffer, the TX1 port is dedicated for sending
so in teensquitto, you can have RX on esp8266 as both debug and receiving

for testing UDP, you can use PacketSender windows application
for ESPAsyncUDP you need the lambda callback from original example, for the internal ESP udp you must use it same as you would on the ESP

AsyncUDP doesnt require a constructor it uses same object as original example, ESP core udp does

dont forget you must be running the github version of esp8266 core, not the one that comes from the IDE
 
Running the same sketch that had running before on the other 8266 I had. When I go to my router I don't see it. :(

Yep I am using packetsender and using the constructor. Yep I am using the GitHub version... :) Going to do some debugging now.
 
check wifi credentials on teensy?

don't forget to flash the ESP with the proper baud in the sketch (3Megabaud?) and make sure teensy matches it

Teensy side:
events() is mandatory for syncing to ESP and communication in both loop() and SerialEvent1()
 
"(3Megabaud?)" = it really has been awhile - its set at 2megabaud :)
wifi cred - first thing I checked
Got events all over the place - yep in loop and serialevent1


Wifiscan does work :)
 
good, your wifiscan copy will most likely be the one upped to github since it has the timeout corrections

since wifiscan works its pretty much good, just need follow the core’s udp example or AsyncUDP example
 
Yeah going to go back and double check your sample - but I had run these sketches before and they worked so its driving me nuts. I also hooked up TX0 to RX2 on the teensy so I am getting to see addition info - right now waiting for a connection:
Code:
�*** MQTT disconnected ***
ESP was restarted.
Serial2 Event: 
scandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3
�Serial2 Event: 
MQTT Connection: 0

�Serial2 Event: 
MQTT Connection: 0

�Serial2 Event: 
MQTT Connection: 0

�Serial2 Event: 
pm open,type:2 0
MQTT Connection: 0

�Serial2 Event: 
MQTT Connection: 0

Just getting MQTT Conn - 0 now. I was getting a connection before - but I have seen this type of delay before :)
 
for mqtt, marvin’s library has been updated quite a bit, you might wanna update that before flashing ESP, usually because the new core 2.4/2.5? breaks things

make sure mosquitto is working in background with credentials updated as well
theres no code
involved for function transparency, all your overloads are sent to the ESP and ran in the original function

The original MQTT testing, was known after only a first connection it would work, consecutive/dynamic not, i think they fixed that later, but usually rebooting the ESP fixed that, and the new ESP.detect lambda on teensy would re-register your ESP settings when the ESP was ever restarted
 
Last edited:
"marvin’s library has been updated quite a bit," - ok will update it but right now I am trying to use UDP.

EDIT: I am getting Now listening at IP 192.168.1.159, UDP port 3333 but I don't see 159 in my router as connected so something is not right.
 
If udp works on the ESP side, it should work from teensy side using same identical code
did you load the constructor?
Code:
teensquitto Udp = teensquitto("WiFiUdp", &Serial1);

case sensitive

These were the last used constructors when I had it setup:
Code:
teensquitto amber = teensquitto("amber", &Serial1, "192.168.2.122:1883", "ESP02", "tony");
teensquitto mqtt = teensquitto("MQTT", &Serial1);
teensquitto client = teensquitto("WiFiClient", &Serial1);
teensquitto sclient = teensquitto("WiFiClientSecure", &Serial1);
teensquitto Udp = teensquitto("WiFiUdp", &Serial1);
teensquitto eep = teensquitto("EEPROM", &Serial1);
teensquitto webSocketClient = teensquitto("WSC", &Serial1);
teensquitto webSocketServer = teensquitto("WSS", &Serial1);
 
Yeah - the only difference is that I am using Serial3 - teensquitto Udp = teensquitto("WiFiUdp", &Serial3); --- unless you have Serial1 hardcoded in the ESP Code? which I doubt because Serial3 worked with Wifiscan..... Going for diner and going to take a break for now.


EDIT: I still use WiFi.events(); even for Udp
 
Serial3 with 1 byte FIFO hardware buffer might be a challenge at 3 MBaud. The interrupts for data will be continuous ... every 2.67 uS? and if missed will be lost data. The #1 and #2 8 byte FIFO will cut that by at least 4.
 
I moved it off 1 because I wanted to put the GPS on it - will move it to 2 and give it another try.
 
Tried serial1, 2 and 3 on the T3.5 and just does want to work, so I went back to my original config(Link node d1 and T3.2). I am now at the point of at least I pretty sure I am connected:
Code:
Now listening at IP 192.168.1.159, UDP port 3333
Serial2 Event: 
MQTT Connection: 0
scandone
MQTT Connection: 0

�Serial2 Event: 
MQTT Connection: 0

�Serial2 Event: 
MQTT Connection: 0

�Serial2 Event: 
pm open,type:2 0
MQTT Connection: 0

�Serial2 Event: 
MQTT Connection: 0

�Serial2 Event: 
MQTT Connection: 0

�Serial2 Event: 
MQTT Connection: 0

�Serial2 Event: 
state: 5 -> 2 (fc0)
rm 0
pm close 7
MQTT Connection: 0
reconnect
state: 2 -> 0 (0)

�Serial2 Event: 
MQTT Connection: 0
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 5
cnt 

connected with Cy
bSerial2 Event: 
erPalin, channel 1
ip:192.168.1.159,mask:255.255.255.0,gw:192.168.1.1
MQTT Connection: 0

Still can't see anything transferring. I do see the little blue light on the esp8266 flashing. Well tomorrows another day
 
I quickly connected an ESP8266 to S1 of T3.6
Code:
EA EA 23 
EA EA 24 
EA EA 12 
EA EA 20 4 6 74 6F 6E 79 74 65 65 6E 73 79 
EA EA 4 
EA EA 9 0 0 13 88 
CE CE 1 4 D2 
CE CE 7 
MQTT Connection: 1
38 38 0 2 D 74 65 65 6E 73 71 75 69 74 74 6F 2F 23 
[COLOR="#FF0000"]MQTT Connection: 1
MQTT Connection: 1
MQTT Connection: 1[/COLOR]
A2 A2 54 0 0 
pm open,type:2 0
scandone
usl
MQTT Connection: 1
A2 A2 59 0 
A2 A2 61 0 
A2 A2 60 0 
A2 A2 59 1 
A2 A2 61 1 
A2 A2 60 1 
A2 A2 59 2 
A2 A2 61 2 
A2 A2 60 2 
A2 A2 59 3 
A2 A2 61 3 
A2 A2 60 3 
A2 A2 59 4 
A2 A2 61 4 
A2 A2 60 4 
A2 A2 59 5 
A2 A2 61 5 
A2 A2 60 5 
A2 A2 59 6 
A2 A2 61 6 
A2 A2 60 6 
A2 A2 59 7 
A2 A2 61 7 
A2 A2 60 7 
A2 A2 59 8 
A2 A2 61 8 
A2 A2 60 8 
A2 A2 59 9 
A2 A2 61 9 
A2 A2 60 9

WiFi scans are working, MQTT is connected

auto reconfigure ESP on reset is working like a charm
 
Status
Not open for further replies.
Back
Top