trying to send buffer data to node-red through mqtt

Zee_jim
Posts: 7
Joined: Wed Dec 05, 2018 9:07 am

trying to send buffer data to node-red through mqtt

Postby Zee_jim » Tue Dec 11, 2018 9:51 am

hello guys
been stuck on this for a week or so now.
am trying to get an audio sample from esp32 with and electric microphone(not boarded about quality yet).
the problem is i am trying to send the audio file to node-red by can send the full data cause i have a char size of (110) tried every other size above that and didn't get any output.

here's the code below.

Code: Select all

#include <PubSubClient.h>
#include <WiFi.h>
#include <WiFiClient.h>


const char* ssid = ""; 
const char* password = "";

const char* mqttServer = ""; 
const int mqttPort = 1883;

String str_payload;



WiFiClient espClient;
PubSubClient client(espClient);

void setup()
{
  Serial.begin(9600);
  WiFi.begin(ssid,password);
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
  client.setServer(mqttServer, mqttPort);
  while(!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if(client.connect("ESP8266Client" )) {
      Serial.println("connected");
    }else{
      Serial.print("failed state ");
      Serial.print(client.state());
      delay(2000);
    }
   }
vibrate();
 my_analog();
}


void loop()
{
}



void vibrate(){
  pinMode(2, OUTPUT);
  digitalWrite(2,HIGH);
  delay(2000);
  digitalWrite(2,LOW);
   my_record();
}


void my_record()
{
  for(int y=0;y<11;y++)
  {
    for(int i=0;i<800;i++)
    {
      str_payload += char(analogRead(35)/4);
//       Serial.println(str_payload.length());
    }
  my_print();
  }
}

void my_analog()
{
  if(digitalRead(2)==LOW)
  {
    my_record();
    
  }
}


void my_print()
{
  char buf[110];
//  String str_payload = String(str_payload);
  String (str_payload).toCharArray(buf,110);
  Serial.print("publishing");
  client.publish("test/esp8266",buf);
  str_payload="";
// Serial.println(str_topic);
// Serial.println(buf);
 Serial.println("finish");
}
thanks..
Last edited by Zee_jim on Wed Dec 12, 2018 9:43 am, edited 1 time in total.

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: trying to send buffer data to node-red through mqtt

Postby tommeyers » Tue Dec 11, 2018 12:23 pm

It is not clear what the problem is. What did you observe?

For more completer info:

Start MQTT Broker in verbose

Subscribe to all messages (#) with subscribe tool
IT Professional, Maker
Santiago, Dominican Republic

User avatar
ESP_Me-no-dev
Posts: 80
Joined: Mon Jan 04, 2016 6:30 pm

Re: trying to send buffer data to node-red through mqtt

Postby ESP_Me-no-dev » Tue Dec 11, 2018 12:25 pm

Why do you use String to collect byte data? Not all bytes are printable chars. Did you try to ask around in the PubSub repository?

Zee_jim
Posts: 7
Joined: Wed Dec 05, 2018 9:07 am

Re: trying to send buffer data to node-red through mqtt

Postby Zee_jim » Wed Dec 12, 2018 9:53 am

the problem is i cant save my audio buffer size of 1k into a char buffer( i don't know why), and the pubsubclient library wont let me send an audio buffer or string directly to node-red so i read the audio file as a string and then convert to char(what pubsubclient only send) but the char buffer does not give me more than [110] bytes if i give more i don't get any data at node-red but if i give 110 or less i get something (2 seconds audio) which is not enough for my application.
i want to know how (why) the char buff size doesn't get increased.

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: trying to send buffer data to node-red through mqtt

Postby tommeyers » Sat Dec 15, 2018 11:51 pm

So you are not using an mqtt broker. If so, install and use an mqtt broker.
IT Professional, Maker
Santiago, Dominican Republic

Who is online

Users browsing this forum: No registered users and 14 guests