Witam wszystkich,
Jestem nowy na forum. Mam problem z komunikacja miedzy arduino a serwerem w nodejs. Przekopałem dosłownie cały internet i nic. Sytyuacja ma się następująco: Podłączam serwer nodejs komendą node server.js i odpala się serwer. Potem wgrywam kod na płytkę- pokazuje się komunikat, 'connected'. I w tym momencie nic dalej mi nie działa. Tzn nie wiem jak przesłać informację z serwera do Arduino. Jak zamknę połączenie z serwerem to pokaze sie w monitorze portu szeregowego 'disconnected'. Poniżej moje kody. Co robię nie tak?
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Arduino's IP address, dependent on your network configuration
IPAddress ip(192,168,xxx,xxx);
// Node.js Server IP Address
IPAddress server(192,168,xxx,xxx);
// Create the Ethernet client
EthernetClient client;
void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// start the serial library:
Serial.begin(9600);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 3000)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
delay(1000);
setup();
}
}
void loop()
{
// if you are connected and data is available
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const server = require('http').createServer(app);
const io = require('socket.io').listen(server);
var cors = require('cors');
const PORT = 3000;
app.use(cors({credentials: true, origin: '*'}));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
server.listen(PORT, function () {
console.log('Server is running on Port: ' + PORT);
});
io.sockets.on('connection', function(socket) {
console.log('A new user connected');
socket.emit('info', {msg: 'The world is round.' });
});