-1

Initially I was sending sensor data to NodeJS using socketIO and this worked perfectly. I had to add a function to control an RGB LED using this. This also works but I'm unable to perform these two functions simultaneously.

I have tried to use two different ports on my Arduino. One to send data and another to control the LED but one overrides the other.

Question: Is there a way around this ? Is it practical to use two different ports?

arduino sketch

if (client.connect(server, 4000)) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

  pinMode(pin, OUTPUT);
}

void loop()
{

  //if you are connected and data is available
  if (client.available()) {
    char c = client.read();
   // Serial.print("Message is :  ");
    Serial.print(c);

    // '1' was received, return '1' for HIGH
    if (c == '1') {
      pinVal = HIGH;
    //  client.print("ON");
    } 
    else if (c == '0') {
      pinVal = LOW;
    //  client.print("OFF"); 
    }

    //turn led pin to new position
    digitalWrite(pin, pinVal);

  }

int chk = DHT.read11(DHT11_PIN);
  int t = DHT.temperature;
  int h = DHT.humidity;

          // send data to the server
        sendData(t, h); 

  Serial.print("Temperature = ");
  Serial.print(t);
  Serial.print( "," );
  Serial.print("Humidity = ");
  Serial.print(h);
  Serial.print("\n");
   delay(6000);

}

void sendData(int temperature, int humidity) 
{
  if(!client.connected())
  {
    if (client.connect(server, 3000)) {

      Serial.println("Sending data...");
      // send the HTTP PUT request:
      client.print("GET /weatherserver/");
      client.print(temperature);
      client.print("/");      
      client.print(humidity);
      client.print("/"); 

      client.println(" HTTP/1.1");
      client.println("Host: localhost");
      client.println("User-Agent: arduino-barom");
      client.println("Connection: close");
      client.println();
      client.stop();
    } else {
       Serial.println("Could not connect.");
       client.stop();
    }
  }
  delay(6000);
}
0

I'm not sure what you are doing, but you seem to be using two different protocols on two different ports. So does you server side code open two ports? and does it accept the two different protocols?

If I was you I would change the RGB control to use REST (like you are doing with the DHT) So you would send something like GET /RGB/<id>/<state>.

I'm not sure how your temperature is arriving at the server since you have sent a GET command rather than a PUT command (this could be the bug you are looking for).

  • Thanks for responding. This is how i'm sending my temperature to my server. However you're right I am using two different protocols on two different ports. My server accepts two ports perfectly. But my Arduino doesn't initiate both connections. From my server side i'm sending my commands to the LED using a TCP protocol. Do you think if I change this I can achieve my goal? Do know if any documentation that shows how I can implement this? – Ekom Jan 16 '17 at 15:20
  • I'm trying to implement you solution you suggested. I really can't find anything to help. Would you be kind to show me a link that could guide me? – Ekom Jan 16 '17 at 23:17
  • This page explains what to do, it has all the code you want, but it is not doing exactly what you want, it should help you understand though. learn.adafruit.com/… I don't know if you can open two sockets, but even if you can I don't think the temperature will work because you are using GET, you should use PUT, you'll understand why when you read the link. – Code Gorilla Jan 17 '17 at 8:28
  • Thanks Matt! I believe I mentioned in my post that I'm using NodeJS. However your solution isn't related to the NodeJS platform. That being said, I thank you for taking your time to help. I was able to find a better solution. – Ekom Jan 17 '17 at 18:30
0

I implemented a better solution differentiating the client as EthernetClient clientA & EthernetClient clientB

Client A handles TCP port and Client B handles HTTP GET Request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.