Akshayan Sinha
Published © CC BY-NC-SA

Thinger. io | IoT Platform Series - 9 🚀

Monitor your HTTP Device on Thinger. io Platform. Easy Documentation to start in seconds.

BeginnerProtip2 hours241
Thinger. io | IoT Platform Series - 9 🚀

Things used in this project

Hardware components

Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1

Software apps and online services

Thinger.io Platform
Thinger.io Platform

Story

Read more

Code

Thinger.io using HTTPClient & ArduinoJson Library

Arduino
#include "WiFi.h"
#include "HTTPClient.h"
#include <ArduinoJson.h>
 
const char* ssid = " ";
const char* password = " ";

//Your Domain name with URL path or IP address with path
String serverName = "https://backend.thinger.io/v3/users/<yourusername/devices/<devicename>/callback/data";
const char* token = "Bearer <token>";

char out[128];

void setup() {

  pinMode(2,OUTPUT);

  Serial.begin(115200);
 
  WiFi.begin(ssid, password); 
 
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(2, HIGH);
    delay(500);
    digitalWrite(2, LOW);
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
  digitalWrite(2, HIGH);

 
}
 
void loop() {
 
 if(WiFi.status()== WL_CONNECTED){
 
   HTTPClient http;
   
   String serverPath = serverName;
      
   // Your Domain name with URL path or IP address with path
   http.begin(serverPath.c_str());
   http.addHeader("Content-Type", "application/json"); 
   http.addHeader("Authorization", token);            

   DynamicJsonDocument doc(200); // <- 200 bytes in the heap

   doc["temp"] = random(24,39);
   doc["hum"] = random(40,59);
   doc["alt"] = random(1024,1300);
  
   serializeJson(doc, Serial);
  
   serializeJson(doc, out);
   Serial.println(out);
   
   int httpResponseCode = http.POST(out);
 
   if(httpResponseCode>0){
 
    String response = http.getString();   

    digitalWrite(2, 0);
    delay(100);
    digitalWrite(2, 1);
    delay(100);
    digitalWrite(2, 0);
    delay(100);
    digitalWrite(2, 1);
    Serial.println(httpResponseCode);
    Serial.println(response);          
 
   }else{
 
    Serial.print("Error on sending PUT Request: ");
    Serial.println(httpResponseCode);
 
   }
 
   http.end();
 
 }else{
    Serial.println("Error in WiFi connection");
 }
 
  delay(8000);
}  

Credits

Akshayan Sinha
28 projects • 28 followers
Robotics Software Engineer with a makermind.
Contact

Comments

Please log in or sign up to comment.