Link Ads

More search type here...

February 2, 2023

IOT arduino logging data to web server

To log data from an Arduino IoT device to a web server, you can follow these general steps:

  1. Connect the Arduino to the internet, either through Wi-Fi or Ethernet.
  2. Use a library such as the HTTPClient library to make HTTP requests to the server.
  3. Write code to format the data you want to log into a JSON or CSV string.
  4. Send the data as a POST request to a server-side script (e.g. PHP, Python, Node.js) that can receive and store the data in a database.
  5. On the server side, use a database management system such as MySQL or MongoDB to store the data.
  6. Optionally, you can also retrieve the data from the database and display it on a web page.
Here is an example of sending data from an Arduino to a server:

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid     = "your_SSID";
const char* password = "your_PASSWORD";
const char* serverName = "your_SERVER_IP_ADDRESS";

void setup() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  HTTPClient http;
  http.begin("http://" + String(serverName) + "/data");
  http.addHeader("Content-Type", "application/json");
  int httpResponseCode = http.POST("{\"value\":\"" + String(analogRead(A0)) + "\"}");
  String response = http.getString();
  Serial.println(httpResponseCode);
  Serial.println(response);
  http.end();
  delay(10000);
}

Here some example to temperature logging by LM35 temperature sensor:


Here is an example of how you could log temperature data from an LM35 temperature sensor connected to an Arduino to a web server:

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid     = "your_SSID";
const char* password = "your_PASSWORD";
const char* serverName = "your_SERVER_IP_ADDRESS";
const int temperaturePin = A0;

void setup() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  pinMode(temperaturePin, INPUT);
}

void loop() {
  int reading = analogRead(temperaturePin);
  float voltage = reading * 5.0 / 1024;
  float temperature = (voltage - 0.5) * 100;

  HTTPClient http;
  http.begin("http://" + String(serverName) + "/data");
  http.addHeader("Content-Type", "application/json");
  int httpResponseCode = http.POST("{\"temperature\":\"" + String(temperature) + "\"}");
  String response = http.getString();
  Serial.println(httpResponseCode);
  Serial.println(response);
  http.end();
  delay(10000);
}

This code connects to a Wi-Fi network using the WiFi.begin method and waits until the connection is established. The pinMode method is used to set the pin connected to the LM35 to INPUT mode. In the loop function, the code reads the analog value from the LM35, converts it to a temperature value, and then sends a POST request to the server containing the temperature data in JSON format. The server's response is printed to the serial monitor for debugging purposes.


Here is an example of how you could log temperature data from an LM35 temperature sensor connected to an Arduino to the Blynk cloud platform:



#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "your_BLYNK_AUTH_TOKEN";
char ssid[] = "your_SSID";
char pass[] = "your_PASSWORD";

const int temperaturePin = A0;

void setup() {
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  Blynk.begin(auth, ssid, pass);
  pinMode(temperaturePin, INPUT);
}

void loop() {
  int reading = analogRead(temperaturePin);
  float voltage = reading * 5.0 / 1024;
  float temperature = (voltage - 0.5) * 100;

  Blynk.virtualWrite(V0, temperature);
  delay(1000);
}


This code connects to a Wi-Fi network using the WiFi.begin method and waits until the connection is established. The Blynk.begin method is used to initialize the Blynk library with the auth token and the Wi-Fi credentials. The pinMode method is used to set the pin connected to the LM35 to INPUT mode. In the loop function, the code reads the analog value from the LM35, converts it to a temperature value, and then sends the temperature data to Blynk's virtual pin 0 using the Blynk.virtualWrite method. The data can be displayed in a Blynk app using various widgets such as a gauge or a chart.






January 19, 2023

Arduino circuit for digital clock and Coding

An Arduino circuit for a digital clock can be created using an Arduino board, a 16x2 LCD display, a real-time clock module, and a few passive components such as resistors and a capacitor. The real-time clock module, such as the DS1307, provides the timekeeping functionality, while the LCD display is used to display the current time. The Arduino board is used to control the LCD and interface with the real-time clock module. A simple circuit diagram could be:


Arduino ------------------------- LCD
|
|
R1,R2
|
|
DS1307






Arduino oding for clock display
Here is an example of Arduino code for displaying the current time on an LCD using a real-time clock module:

Copy code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>

RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  lcd.begin();
  lcd.backlight();
  rtc.begin();
  if (!rtc.isrunning()) {
    lcd.print("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  DateTime now = rtc.now();
  lcd.setCursor(0, 0);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  lcd.setCursor(0, 1);
  lcd.print(now.day(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.year(), DEC);
  delay(1000);
}
 

This code uses the Wire and RTClib libraries to interface with the real-time clock module. The LiquidCrystal_I2C library is used to control the LCD display. The code first checks if the real-time clock is running, and sets the time if it is not. Then, in the loop, it continuously updates the LCD display with the current time and date retrieved from the real-time clock.

Note: in the code above, you can see that the I2C address of the LCD is 0x27, if it's different for you, you need to change that to the correct one.






KALMAN FILTER IN ARDUINO without Header file kalman.h

This is a basic Kalman filter that uses a constant measurement noise and process noise. You can adjust these values to fine-tune the filter for your specific application. The filter takes a measurement (z_measured) and uses it to estimate the real value (z_real). The estimated value is then updated with each new measurement.

#define MEASUREMENT_NOISE 0.1
#define PROCESS_NOISE 0.05

double x_est_last = 0;
double P_last = 0;
double K;
double P;
double x_temp_est;
double x_est;
double z_measured = 0; // the measured value
double z_real = 0; // the real value

void KalmanFilter()
{
    x_temp_est = x_est_last;
    P = P_last + PROCESS_NOISE;
    K = P / (P + MEASUREMENT_NOISE);
    x_est = x_temp_est + K * (z_measured - x_temp_est);
    P = (1 - K) * P;
    x_est_last = x_est;
    P_last = P;
}