Close

ESPduino or Neopixels memory leak?

A project log for A danceable notification cube

A motion sensing colorful cube that keeps track on Twitter, all for creating awareness about IoT through art.

jakob-andrnJakob Andrén 07/19/2015 at 23:211 Comment

So after several days trying diffrenet approaches for communication beetween server and ESP8266 we finally got the last bit working on the project (have some back logging to do here at hackaday.io), MQTT over a ESP8266 to a Teensy 3.1. The resulting speed is quite remarkable:

Small edit: is anyone able to see the video?

But it didn't work when I tried to combine it with my earlier MPU6050 and Neopixel code... Im now down to a mix of only ESPduino example code for a MQTT client (https://github.com/tuanpmt/espduino) and the Neopixel library from Adafruit. And from what I can see I have a memory problem and I had liked to get your input on that. So some examples:

First serial output from a fully functional run, only about 20 pixels here, last is the data received which changes the output colour:

KNӒ��ے�mode : sta(18:fe:34:a0:f1:eb)
add if0
�=����)��b�ARDUINO: setup mqtt client
ARDUINO: setup mqtt lwt
ARDUINO: setup wifi
ARDUINO: system started
scandone
add 0
aid 5
pm open phy2,type:2 0 0
cnt 

connected with #SSID#, channel 7
dhcp client start...
ip:192.168.1.103,mask:255.255.255.0,gw:192.168.1.1
WIFI CONNECTED
Connected
Received: topic=cube
data=01028028000150015020

And a faulty run that isn't able to connect to the MQTT server, get this or just nothing after "Arduino: system started" when rising the number of pixels. I guess that the ESP get the same gibberish and thats whats messes it up:

Ӓ��ے�mode : sta(18:fe:34:a0:f1:eb)
add if0
����=����b�ARDUINO: setup mqtt client
ARDUINO: setup mqtt lwt
ARDUINO: setup wifi
ARDUINO: system started
scandone
add 0
aid 5
 open ph,type:2 
cnt 
connectewith [Incorrect SSID]channel 
dhcp clnt start.
ip:192.168.103,mask:5.255.250,gw:1928.1.1
ARDUINO: Invalid CRC

So first thing that is visible is the missing characters in the serial communication, which what I could find is a sign of out of memory. But I run a Teensy 3.1 and both codes are supposed to work on Arduino Uno, I have done tests with the Neopixel library successfully on Uno, so it suprices me that it stops working after adding just about 80 pixels more. About 240 bytes of the 64 kbytes that the Teensy 3.1 has. The Teensy LC, which has 8 kbytes of RAM, also works if I reduce the number of LEDs down to 5.

Does anyone have any ideas about a more exakt reason or solution to this?

EDIT: It works together with FastLED, hmm. But FastLED is approx 5 times slower than the Adafruit library so I had prefered to have Adafruit up and running.

#include <Adafruit_NeoPixel.h>
#include <espduino.h>
#include <mqtt.h>

#define NEO_PIN           6
#define NEO_NUM           20

ESP esp(&Serial2, &Serial, 15);  // CHpd port
MQTT mqtt(&esp);
boolean wifiConnected = false;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEO_NUM, NEO_PIN, NEO_GRB + NEO_KHZ800);
uint32_t c = strip.Color(0,0,0); // Start with black


void setup() {
  strip.begin();
  strip.show();
  
  pinMode(16, OUTPUT);    // Turn on the Neopixel voltageregulator
  digitalWrite(16, HIGH);
  
  Serial2.begin(19200);
  Serial.begin(115200);
  //while(!Serial);

  esp.enable();
  delay(500);
  esp.reset();
  delay(500);
  while (!esp.ready());

  Serial.println(F("ARDUINO: setup mqtt client"));

  // Begin MQTT - settings in string, from example
  if (!mqtt.begin("DVES_duino", "admin", "Isb_C4OGD4c3", 120, 1)) {
    
    Serial.println(F("ARDUINO: fail to setup mqtt"));
    while (1);
    }


  Serial.println(F("ARDUINO: setup mqtt lwt"));
  mqtt.lwt("/lwt", "offline", 0, 0); //or mqtt.lwt("/lwt", "offline");

  /*setup mqtt events */
  mqtt.connectedCb.attach(&mqttConnected);
  mqtt.disconnectedCb.attach(&mqttDisconnected);
  mqtt.publishedCb.attach(&mqttPublished);
  mqtt.dataCb.attach(&mqttData);

  /*setup wifi*/
  Serial.println(F("ARDUINO: setup wifi"));
  esp.wifiCb.attach(&wifiCb);

  esp.wifiConnect("#SSID#", "###");

  Serial.println(F("ARDUINO: system started"));
}
// END SETUP


void loop() {
  unsigned long T = micros();

  // Check if there is a new message!
  esp.process();

  /*
  if (micros() - T > 10) {    // If there was a message, tell us the timing and result.
    Serial.print(micros() - T); Serial.print(F("us, "));
    Serial.println(c);
  }
  */

  for(int i=0;i<strip.numPixels();i++){
      strip.setPixelColor(i, c);
      }
  strip.show();
  
  if (wifiConnected) {
    }
}


/// Functions

void wifiCb(void* response)
{
  uint32_t status;
  RESPONSE res(response);

  if (res.getArgc() == 1) {
    res.popArgs((uint8_t*)&status, 4);
    if (status == STATION_GOT_IP) {
      Serial.println(F("WIFI CONNECTED"));
      mqtt.connect("#IP#", 1883, false);
      wifiConnected = true;
      //or mqtt.connect("host", 1883); /*without security ssl*/
    } else {
      wifiConnected = false;
      mqtt.disconnect();
    }

  }
}

void mqttConnected(void* response)
{
  Serial.println(F("Connected"));
  mqtt.subscribe("cube");
  mqtt.publish("tweets", "Hello mqtt from Teensy");

}


void mqttDisconnected(void* response)
{

}


void mqttData(void* response)
{
  RESPONSE res(response);

  Serial.print(F("Received: topic="));
  String topic = res.popString();
  Serial.println(topic);

  Serial.print(F("data="));
  String json = res.popString();
  Serial.println(json);

  c = strip.Color(json.substring(2,5).toInt(), json.substring(5,8).toInt(), json.substring(8,11).toInt());

}


void mqttPublished(void* response)
{
}

Discussions

Jasmine Brackett wrote 07/20/2015 at 19:26 point

I can see the video.

  Are you sure? yes | no