Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 4 ■ COMPLEX FLOWS: NODE-RED

void doHttpGet()
{
// Read all incoming data (if any)
while (client.available())
{
char c = client.read();
Serial.write(c);
}


Serial.println();
Serial.println("-----------------------------------------------");
if (millis() - lastConnectionTime > postingInterval)
{
client.stop();


//Read sensor data


readSensorData();


// Prepare data or parameters that need to be posted to server
String requestData = "requestVar=" + String(lightValue);


Serial.println("[INFO] Connecting to Server");


// Check if a connection to server:port was made
if (client.connect(server, port))
{
Serial.println("[INFO] Server Connected - HTTP GET Started");


// Make HTTP GET request
client.println("GET /lightSensorTweet?" + requestData + " HTTP/1.1");
client.println("Host: " + String(server));
client.println("Connection: close");
client.println();


lastConnectionTime = millis();


Serial.println("[INFO] HTTP GET Completed");
}
else
{
// Connection to server:port failed
Serial.println("[ERROR] Connection failed");
}
}
}

Free download pdf