0

I'm trying to send data from my ESP8266 to a NodeJS TCP server, the connection and basic charakters (48-90) are working fine, but if I use others like ASCII-Code (0,1,2,3...) I don't get any usefull data out of it. So what I was thinking about that there are some character which aren't allowed to send over TCP/IP.

By the way, I'm using this to write to the server, and I work with the standard WiFi Client library

char arr[255]
for(int i = 0; i < 255; i++){
 arr[i] = (char)i;
}
client.print(arr);

Nodejs File:

// Load the TCP Library
net = require('net');
var mysql = require('mysql');
// Keep track of the chat clients
var clients = [];
var uploadCounter = 0;
initMySQLConnection();
// Start a TCP Server
net.createServer(function (socket) {
 // Identify this client
 socket.name = socket.remoteAddress + ":" + socket.remotePort
 // Put this new client in the list
 clients.push(socket);
 // Send a nice welcome message and announce
 socket.write("Welcome " + socket.name + "\n");
 broadcast(socket.name + " connected\n", socket);
 // Handle incoming messages from clients.
 socket.on('data', function (data) {
 broadcast(">> " + data, socket);
 uploadData(data);
 });
 // Remove the client from the list when it leaves
 socket.on('end', function () {
 clients.splice(clients.indexOf(socket), 1);
 broadcast(socket.name + " disconnected.\n\n");
 });
 // Send a message to all clients
 function broadcast(message, sender) {
 clients.forEach(function (client) {
 // Don't want to send it to sender
 if (client === sender) return;
 //client.write(message);
 });
 // Log it to the server output too
 process.stdout.write(message)
 }
}).listen(10101);
// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 10101\n");
var connection;
function initMySQLConnection() {
 connection = mysql.createConnection({
 host: "host",
 user: "user",
 password: "pass",
 database: "database"
 });
 connection.connect(function (err) { 
 if (err) { 
 console.log('error when connecting to db:', err);
 } 
 }); 
 connection.on('error', function (err) {
 console.log('db error', err);
 if (err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
 // lost due to either server restart, or a
 } else { // connnection idle timeout (the wait_timeout
 throw err; // server variable configures this)
 }
 });
 setInterval(function () {
 connection.query('SELECT 1');
 }, 60000);
}
function uploadData(data) {
 data = String(data);
 console.log("Package Length: " + data.length);
 console.log("GOT ASCII CODE: ");
 for (var i = 0; i < 255; i++) {
 console.log(data.charCodeAt(i));
 }
 //upload Code
}
asked Jan 24, 2019 at 7:20
1
  • what does this mean? ... I don't get any usefull data out of it ................ what are you expecting for ASCII-Code (0,1,2,3...)? Commented Jan 24, 2019 at 7:22

1 Answer 1

0

The print function takes an array of characters as zero terminated c-string. It prints until a byte with value 0. You have zero at arr[0] so it is an empty string.

To send bytes, write function is used. The print functions convert parameters to text or expect text as zero terminated char array.

In your case it should be client.write(arr, length); where length is the number of bytes you want to send from the buffer arr.

answered Jan 24, 2019 at 8:04
6
  • now I get values from 0-127 but all above is 65533. so the error seems to be in my nodejs file, i will add it above Commented Jan 24, 2019 at 9:33
  • ASCII code are only until 127. why do you read it as String and chars? Commented Jan 24, 2019 at 10:07
  • Doesnt the extended ascii code go until 255? Commented Jan 24, 2019 at 10:14
  • tell it to data.charCodeAt(i) :-) Commented Jan 24, 2019 at 10:21
  • So you mean without the data = String(data)? Commented Jan 24, 2019 at 10:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.