IoT Device Code Examples

Arduino Code (WiFi ESP)
#include <WiFiEsp.h>
#include 

SoftwareSerial esp(2, 3); // RX, TX

const char* ssid = "YOUR-SSID";
const char* password = "YOUR-PASSWD";
const char* host = "getyourprojectdone.in";
const int httpPort = 80;
const char* api_key = "YOUR-API-KEY";

void setup() {
  Serial.begin(9600);
  esp.begin(9600);
  delay(2000);
  
  sendCommand("AT", "OK", 2000);
  sendCommand("AT+CWMODE=1", "OK", 1000);
  
  String cmd = "AT+CWJAP=\"" + String(ssid) + "\",\"" + String(password) + "\"";
  sendCommand(cmd.c_str(), "WIFI CONNECTED", 10000);
  
  Serial.println("WiFi connected!");
}

void loop() {
   digitalWrite(LED_BUILTIN, LOW);
  delay(1000);

  // Read and convert to logical LED state (1 = ON)
  int ledState = digitalRead(LED_BUILTIN) == LOW ? 1 : 0;
  sendData(ledState);

  // Toggle LED OFF
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);

  ledState = digitalRead(LED_BUILTIN) == LOW ? 1 : 0;
  sendData(ledState);
}    // simulate ON
  float temp = 28.3;      // simulate temperature
  int distance = 120;     // simulate distance

  String json = "{";
  json += "\"api_key\":\"" + String(api_key) + "\",";
  json += "\"devices\":[";
  json += "{";
  json += "\"device_name\":\"YOUR-DEVICE-NAME(eg-led)\",";
  json += "\"sensors\":[{\"type\":\"ledlight\",\"value\":\"" + String(ledState) + "\",\"unit\":\"w\"}]";
  json += "},";
  json += "{";
  json += "\"device_name\":\"YOUR-DEVICE-NAME(eg-temp)\",";
  json += "\"sensors\":[{\"type\":\"temperature\",\"value\":\"" + String(temp) + "\",\"unit\":\"C\"}]";
  json += "},";
  json += "{";
  json += "\"device_name\":\"YOUR-DEVICE-NAME(eg-ultra)\",";
  json += "\"sensors\":[{\"type\":\"distance\",\"value\":\"" + String(distance) + "\",\"unit\":\"cm\"}]";
  json += "}";
  json += "]}";

  String postRequest =
    "POST /iot_platform/api/insert_data.php HTTP/1.1\r\n"
    "Host: getyourprojectdone.in\r\n"
    "Content-Type: application/json\r\n"
    "Content-Length: " + String(json.length()) + "\r\n"
    "Connection: close\r\n\r\n" +
    json;

  sendCommand("AT+CIPMUX=0", "OK", 1000);
  String connectCmd = "AT+CIPSTART=\"TCP\",\"" + String(host) + "\"," + String(httpPort);
  if (sendCommand(connectCmd.c_str(), "CONNECT", 5000)) {
    String sendLen = "AT+CIPSEND=" + String(postRequest.length());
    if (sendCommand(sendLen.c_str(), ">", 2000)) {
      esp.print(postRequest);
      Serial.println("Data sent:");
      Serial.println(json);
    }
  }

  delay(10000); // Wait 10s
}

bool sendCommand(const char* cmd, const char* ack, int timeout) {
  esp.println(cmd);
  long int time = millis();
  String response = "";

  while ((time + timeout) > millis()) {
    while (esp.available()) {
      char c = esp.read();
      response += c;
    }
    if (response.indexOf(ack) != -1) {
      Serial.println("Command OK: " + String(cmd));
      return true;
    }
  }
  Serial.println("Command FAIL: " + String(cmd));
  return false;
}

NodeMCU ESP8266 Code

         
            #include 
#include 

const char* ssid = "YOUR-SSID";
const char* password = "YOUR-PASSWD";

const char* host = "getyourprojectdone.in";
const int httpsPort = 443;

const char* api_key = "YOUR-API-KEY";

WiFiClientSecure client;

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);

  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");

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

  client.setInsecure(); // Skip SSL validation (for testing)
}

void loop() {
  // --- Simulate or read actual values ---
  int ledState = digitalRead(LED_BUILTIN); // Or manually manage state
  float temperature = 28.3; // Simulated temperature (replace with sensor reading)
  int distance = 120;       // Simulated ultrasonic value (replace with actual reading)

  // Turn LED ON/OFF
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);

  // --- Build JSON ---
  String json = "{";
  json += "\"api_key\":\"" + String(api_key) + "\",";
  json += "\"devices\":[";
  
  // LED Device
  json += "{";
  json += "\"device_name\":\"YOUR-DIVECE-NAME(eg-led)\",";
  json += "\"sensors\":[{\"type\":\"ledlight\",\"value\":\"" + String(ledState) + "\",\"unit\":\"w\"}]";
  json += "},";

  // Temperature Device
  json += "{";
  json += "\"device_name\":\"YOUR-DIVECE-NAME(eg-temp\",";
  json += "\"sensors\":[{\"type\":\"temperature\",\"value\":\"" + String(temperature) + "\",\"unit\":\"C\"}]";
  json += "},";

  // Ultrasonic Device
  json += "{";
  json += "\"device_name\":\"YOUR-DIVECE-NAME(eg-ultra)\",";
  json += "\"sensors\":[{\"type\":\"distance\",\"value\":\"" + String(distance) + "\",\"unit\":\"cm\"}]";
  json += "}";

  json += "]}";

  // --- POST Request ---
  if (!client.connect(host, httpsPort)) {
    Serial.println("Connection to server failed");
    return;
  }

  String request =
    String("POST ") + "/iot_platform/api/insert_data.php HTTP/1.1\r\n" +
    "Host: " + host + "\r\n" +
    "Content-Type: application/json\r\n" +
    "Content-Length: " + json.length() + "\r\n" +
    "Connection: close\r\n\r\n" +
    json;

  client.print(request);
  Serial.println("Data sent:");
  Serial.println(json);

  while (client.connected() || client.available()) {
    if (client.available()) {
      String line = client.readStringUntil('\n');
      Serial.println(line);
    }
  }

  client.stop();

  delay(10000); // Send every 10 seconds
}

Raspberry Pi Pico W (MicroPython + HTTP)
import network
import urequests
import utime
import json

WIFI_SSID = "Your-ssid"
WIFI_PASSWORD = "Your-pass"
API_KEY = "Your-api-key"
API_URL = "https://getyourprojectdone.in/iot_platform/api/insert_data.php"

def connect_to_wifi(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    while not wlan.isconnected():
        utime.sleep(1)
    print("Connected:", wlan.ifconfig())
    return wlan

def build_payload(temp):
    return {
        "api_key": API_KEY,
        "devices": [
            {
                "device_name": "temperature1",
                "sensors": [
                    {"type": "temperature", "value": temp, "unit": "C"}
                ]
            },
            {
                "device_name": "humidity_sensor",
                "sensors": [
                    {"type": "humidity", "value": 60.2, "unit": "%"}
                ]
            }
        ]
    }

def send_data(payload):
    headers = {"Content-Type": "application/json"}
    try:
        response = urequests.post(API_URL, data=json.dumps(payload), headers=headers)
        print("Server Response:", response.text)
        response.close()
    except Exception as e:
        print("Error:", e)

if __name__ == "__main__":
    wlan = connect_to_wifi(WIFI_SSID, WIFI_PASSWORD)
    while True:
        temperature = 25.3
        payload = build_payload(temperature)
        send_data(payload)
        utime.sleep(10)