How to connect my arduino to node js?

I am new in arduino and node js so I have really weak understanding with this

All I need is to send the smokeLevel data to node js because I am going to create an app for this like a gauge. As you notice that I have blynk there, it is working but I want to practice on how to create my own app like blynk. Thank you for you help

here is my code in arduino correct me if I am wrong all I need is like node js

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <ArduinoHttpClient.h>  // Include HTTP client for Node.js communication

#define BLYNK_TEMPLATE_ID "TMPL6DU_J0N9D"
#define BLYNK_TEMPLATE_NAME "Smoke and Gas Sensor"
#define BLYNK_AUTH_TOKEN "2EC_053aiQl9OMFxGAe90HCtZridzUhn"

#include <BlynkSimpleWifi.h>

#define SCREEN_WIDTH 128  
#define SCREEN_HEIGHT 64  

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define mq135Pin A0    
#define redLEDPin 7    
#define greenLEDPin 8  
#define buzzerPin 10  

const int smokeThreshold = 400;       
const long alertInterval = 3100;    
unsigned long previousMillis = 0;

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "ZTE_2.4G_R5fjan";  // Wifi name
char pass[] = "23WQCdNv";         // Wifi password

char serverAddress[] = "192.168.1.15";  // Node.js server address
int port = 3000;                       // Port of the server

BlynkTimer timer;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

void setup() {
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  Serial.begin(115200);
  // Start I2C communication
  Wire.begin();

  // Initialize OLED Display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {  
    Serial.println("SSD1306 OLED not found!");
    while (1); 
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(10, 10);
  display.print("Connecting to WiFi...");
  display.display();

  Blynk.begin(auth, ssid, pass);

  // Connect to WiFi
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  display.clearDisplay();
  display.setCursor(10, 10);
  display.print("WiFi Connected!");
  display.display();
  delay(2000);
}

void loop() {
  Blynk.run();
  unsigned long currentMillis = millis();

  // Read MQ-135 sensor
  int smokeLevel = analogRead(mq135Pin);
  Serial.print("Smoke Level: ");
  Serial.println(smokeLevel);

  // DATASTREAMS 
  Blynk.virtualWrite(V0, smokeLevel);

  // Update OLED
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(10, 10);
  display.print("Smoke Level: ");
  display.print(smokeLevel);

  if (smokeLevel > smokeThreshold) {
    display.setCursor(10, 30);
    display.print("SMOKE ALERT! ");
    display.setCursor(10, 50);
    display.print("POSSIBLE FIRE!");

       // Blynk.logEvent({EVENT_CODE}, {MESSAGE})
    Blynk.logEvent("smoke_and_gas_sensor", "🚨 ALERT: Smoke/Gas Detected! 🚨\n\n"
    "Warning! Smoke or gas has been detected in your house. Please check immediately and take necessary precautions. If needed, evacuate and contact emergency services.");


    // Sending the smoke data to Node.js server
    sendSmokeData(smokeLevel);

    Serial.println("SMOKE ALERT! ");
    digitalWrite(redLEDPin, HIGH);   
    digitalWrite(greenLEDPin, LOW);  

    if (currentMillis - previousMillis >= alertInterval) {
      digitalWrite(buzzerPin, HIGH); 
      delay(500);                    
      digitalWrite(buzzerPin, LOW);  
      previousMillis = currentMillis;
    }
  } else {
    display.setCursor(10, 30);
    display.print("Safe Environment");

    Serial.println("Safe Environment");

    digitalWrite(redLEDPin, LOW);    
    digitalWrite(greenLEDPin, HIGH); 
    digitalWrite(buzzerPin, LOW);    
  }

  Blynk.run();
  timer.run();
  display.display();
  delay(500);
}

// Function to send smoke level data to Node.js server
void sendSmokeData(int smokeLevel) {
  Serial.println("Sending smoke data to Node.js server...");
  
  String url = "/smoke_alert";  // Define the endpoint
  String jsonData = "{\"smokeLevel\": " + String(smokeLevel) + "}";  // Convert smoke level to JSON

  client.beginRequest();
  client.post(url);
  client.sendHeader("Content-Type", "application/json");
  client.sendHeader("Content-Length", jsonData.length());
  client.sendHeader("Connection", "close");
  client.beginBody();
  client.print(jsonData);  // Send the JSON data

  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);
}

Topic moved. Please do not post in "Uncategorised"; see the sticky topics in Uncategorized - Arduino Forum.

1 Like

Welcome to the forum.

If you would like to read how to handle POST requests in node.js, here is an article that may help to get you started.

1 Like

Thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.