Saturday 3 June 2017

sending data from node.js webserver to arduino

I'm trying to send data from node.js webserver to arduino using serialport. for example, when I send value from webserver, arduino takes the value and if temperature is lower than the value, led is on or if temperature is higher than the value, led is off. I successfully send data to arduino but led is not working. This is my arduino code and node.js code.

...
        function openPort() {
              var temp = temperature; 
                console.log('port open');
                  console.log('baud rate: ' + myPort.options.baudRate);

            function sendData() {

            //myPort.write(temp.toString());
        for(var i=0; i<temp.length; i++) {
          myPort.write(new Buffer(temp[i], 'ascii'), function(err, results) {
            });

          }

            console.log('Sending ' + temp + ' out the serial port');
        }
        setTimeout(sendData, 500);


       }
...



float temperature;
float temp1;
float temp2;
float lm35Pin1 = A0;
float lm35Pin2 = A1;
int led = 13;
String inData = "";

void setup()  
{
  pinMode(led, OUTPUT);
  analogReference(INTERNAL);
  Serial.begin(9600);
}

void loop()  
{
  temp1 = analogRead(lm35Pin1);
  temp2 = analogRead(lm35Pin2);
  temperature = ((temp1 / 9.31) + (temp2 / 9.31)) /2 ;

  while (Serial.available() > 0) {
    long received = Serial.parseInt();
    inData.concat(received);
  }

  if(temperature <= inData.toInt()) {
    digitalWrite(led, HIGH);
  }
  if(temperature > inData.toInt()) {
    digitalWrite(led, LOW);
  }

  Serial.println(temperature);
  Serial.println(inData);
  inData = "";
  delay(3000);
}



via Byunghun Jang

No comments:

Post a Comment