c_cpp 与arduino联系

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 与arduino联系相关的知识,希望对你有一定的参考价值。

const net = require("net")
const server = net.createServer(onClientConnected)
const socket_api = require('socket.io-client')('http://192.168.1.10:3000')

// listen
server.listen(8888, '0.0.0.0', function() {
  console.log('server listening on %j', server.address())
})
// arduino ip setting
let ardinoIpAdress = "192.168.1.71"

/*---------------------------------------------------------------------------*/
/*-------------------- Get Data From TCP Client -----------------------------*/
/*---------------------------------------------------------------------------*/

function onClientConnected(socket) {
  // remote adress and port of arduino
  let remoteAddress = socket.remoteAddress + ':' + socket.remotePort

  console.log('new client connected: %s', remoteAddress)

  // get data from arduino
  socket.on('data', function(data) {
    console.log(JSON.parse(data.toString()))
  })
  socket.on('close',  function () {
    console.log('connection from %s closed', remoteAddress)
  })
  socket.on('error', function (err) {
    console.log('Connection %s error: %s', remoteAddress, err.message)
  })
}

/*---------------------------------------------------------------------------*/
/*------------------------ Send data to Arduino -----------------------------*/
/*---------------------------------------------------------------------------*/

// API socket listening
socket_api.on('history', function(data){
   await ArduinoControl({ port: 6060, host: ardinoIpAdress.pagedetection_left },"order")
})

// control arduino RFID sensor
function ArduinoControl(adress, order){
  return new Promise((resolve, reject) => {
    let client =  net.createConnection(adress, () => {
      client.write(order)
      client.destroy()
      resolve()
    })
  })
  
 client.on('data', (data) => {
   console.log('data from arduino:', data.toString());
   client.end()
 })
 client.on('error', () => {
   client.end()
 })
}
#include <Ethernet.h>

// client setting
EthernetClient tcpClient;
// server setting
EthernetServer tcpServer = EthernetServer(6060);

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFA, 0x1F };
byte ip[] = { 192, 168, 1, 55 };

// server configuration
byte server[] = { 192, 168, 1, 11 };

void setup()
{
  // client active
  Ethernet.begin(mac, ip);
  // server active
  tcpServer.begin();
  // node js server connect
  connectTcpClient();
}

void loop(){
  // client 
  statusTcpClient();
  tcpClient.flush();
}

void readTCPserverData(){
  String json = "";

  EthernetClient data = tcpServer.available();
  if (data.available() > 0) {
    int dataLength = data.available();

    for (int i = 0; i < dataLength; i++){
      json+= (char)data.read();
    }

    Serial.println(json);

    tcpServer.print("{\"status\": \"success\"}");
  }
}


void connectTcpClient(){
  //8888 is nodejs TCP server port number
  if (tcpClient.connect(server, 8888)) {
    Serial.println("connected");
  } else {
    Serial.println("connection failed");
  }
}

void statusTcpClient (){
  //Stop client and reconnect
  if (!tcpClient.connected()) {
   Serial.println("disconnecting.");
   tcpClient.stop();
   delay(500);
   connectTcpClient();
  }
}

以上是关于c_cpp 与arduino联系的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 与Pebble通信的Arduino代码

c_cpp 改进的firmata协议,适用于使用ttyATH0串口接口与arduino端通信的arduino YUN linux端。

c_cpp 使用Arduino作为直接USB转串口转换器!在Arduino Duemilanove上测试过。可能是3.3V TTL但是可以与之通信

c_cpp 使用Arduino作为直接USB转串口转换器!在Arduino Duemilanove上测试过。可能是3.3V TTL但是可以与之通信

c_cpp Arduino.ino

c_cpp Arduino的