SEND TEMPERATURE DATA ON SERVER USING IoT

Table of Contents

In the previous tutorial you learnt about how you can control LED or any other device from the server. In this section you will learn how you can send data from device to server, in this way you can do both way communication. In this section we will send temperature data on server using IoT concept connected over analog pin A0 of NodeMCU.

CIRCUIT DIAGRAM

SEND TEMPERATURE DATA ON SERVER USING IoT

PROGRAMMING CODE

#include<ESP8266WiFi.h>
#include<ESP8266HTTPClient.h>
HTTPClient http;

const char* WIFINAME = "YOUR_SSID";
const char* PASS = "PASSWORD";

int led = D0;

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println("");
  pinMode(led, OUTPUT);
  Serial.print("Connecting To ");
  Serial.println(WIFINAME);

  WiFi.begin(WIFINAME, PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http; /////////////////http client to read data for LED//////////////////////////////
    http.begin("http://iot-dashboard.shoolinlabs.com/1/bulb.txt");
    int httpCode = http.GET();

    // List of HTTP status code: https://pijaeducation.com/list-of-http-status-codes/
    if (httpCode > 0) {
      String payload = http.getString();
      Serial.println(payload);

      if (payload == "1") {
        digitalWrite(led, LOW);
        Serial.println("LED ON");
      }
      else {
        digitalWrite(led, HIGH);
        Serial.println("LED OFF");
      }
      int raw_value = analogRead(A0);
      int cal = 0.01 * 1023 / 3.3;
      int temp = raw_value / cal;
      Serial.println(temp);
      String stringTempratureURL = "http://iot-dashboard.shoolinlabs.com/1/?temprature=";
      stringTempratureURL = stringTempratureURL + "Temperature=" + temp;
      http.begin(stringTempratureURL);
      int httpCode = http.GET();
    }
    http.end();
  }
  delay(100);
}

OUTPUT

Here you can see we can control led, When we click Turn On & Turn Off button a message is also shown and also below them some random value from analog pin A0 are shown.