ChatGPT, the future for programmers?

From a chatGPT site. Isn’t it impressive? I haven’t tried it but it looks about right.

Question: blynk code: turn a led on using esp8266
Answer: Here is an example of code in C++ for turning on a LED using the Blynk library:

#include <BlynkSimpleEsp8266.h>

const int LED_PIN = 2;  // GPIO pin for the LED

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

BLYNK_WRITE(V0) {
  int ledValue = param.asInt();  // Get the LED state from the app
  digitalWrite(LED_PIN, ledValue);  // Set the LED state
}

void loop() {
  Blynk.run();
}

In this code, V0 is a virtual pin in the Blynk app that you can use to control the state of the LED. The BLYNK_WRITE macro is triggered when the virtual pin value changes in the app, and the digitalWrite function is used to turn the LED on or off based on the value received from the app.

1 Like

Not that impressive when you consider that it won’t work!

Pete.

3 Likes

It uses old code and old examples. So we’ll have to wait while it will index the latest code.

In general, yeah - ChatGPT is very powerful. I’m already using co-pilot (based on GPT 3 model) in my daily work and I must say - it’s pretty cool. It saves ~10-20% of the coding time in my case. It’s great with simple/small methods/tasks (literally it writes the code for you) and during refactorings. It won’t replace the programmers in the near future, but it’s a big step forward :wink: .

3 Likes

ChatGPT wrote the following code and it works perfectly

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""

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

// Blynk and Wi-Fi credentials
char auth[] = ""; 
char ssid[] = "";                         
char password[] = "";                  

// MQ2 sensor pin (analog)
int mq2Pin = 34; 

// Calibration variables
int baselineValue = 0;  
int targetBaseline = 200; 
int offset = 0;         

// Timer variables
unsigned long previousMillis = 0;
const unsigned long interval = 2000; 

void setup()
{
  // Start serial communication for debugging
  Serial.begin(115200);

  // Connect to Wi-Fi and Blynk
  Blynk.begin(auth, ssid, password);

  // Initialize MQ2 sensor pin as input
  pinMode(mq2Pin, INPUT);

  // Calibration process
  Serial.println("Calibrating sensor...");
  baselineValue = calibrateSensor();
  offset = baselineValue - targetBaseline; 
  Serial.print("Baseline value: ");
  Serial.println(baselineValue);
  Serial.print("Offset: ");
  Serial.println(offset);
  Serial.println("Calibration complete.");
}

void loop()
{
  // Run the Blynk process to handle communication
  Blynk.run();

  // Check if the interval has passed
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis; 

    // Read the analog value from MQ2 sensor
    int mq2Value = analogRead(mq2Pin);

    // Adjust the value using the offset
    int calibratedValue = mq2Value - offset;

    // Print sensor values to Serial Monitor for debugging
    Serial.print("Raw MQ2 Sensor Value: ");
    Serial.println(mq2Value);
    Serial.print("Calibrated Sensor Value: ");
    Serial.println(calibratedValue);

    // Send the calibrated sensor value to Blynk Virtual Pin V0 
    Blynk.virtualWrite(V0, calibratedValue);

    // Send the same value to Virtual Pin V1 for the graph widget
    Blynk.virtualWrite(V1, calibratedValue);

    // Check if the gas level exceeds 700 and trigger an event
    if (calibratedValue > 1000)
    {
      Serial.println("Gas level too high! Sending alert...");
      Blynk.logEvent("gas_level", "Gas level exceeds safe limit!");
    }
  }
}

// Function to calibrate the MQ2 sensor
int calibrateSensor()
{
  int total = 0;      
  const int samples = 100; 

  for (int i = 0; i < samples; i++)
  {
    int value = analogRead(mq2Pin);
    total += value;
    delay(50); 
  }

  // Calculate the average baseline value
  return total / samples;
}

@Bateleur Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Hmmm, I guess you’ve not done much testing with that code tgen.

First of all, it uses a millis() comparison instead of BlynkTimer, which isn’t a good idea.
Secondly, it would attempt to do a Blynk.logEvent (presumably to trigger a notification) once every two seconds for the whole period when the gas reading is >1000.
That means that after 10 seconds of the gas reading being >1000 you’ll have used-up your daily allowance of events if you have a free plan, or if you have a Pro plan that limit will be reached in 200 seconds.

Far better to have a “notification_sent” flag so that multiple events aren’t logged for the same high level of gas situation.

Pete.

Thank you for the advice, i will try to correct it. The reason i use chatgpt is due to the fact that i am useless at coding, more so than chatgpt. :face_with_spiral_eyes:

Here is the updated code. I will do more testing as the project progresses.

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""

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

// Blynk and Wi-Fi credentials
char auth[] = "";
char ssid[] = "";
char password[] = "";

// MQ2 sensor pin (analog)
int mq2Pin = 34;

// LED pins
int greenLED = 25;  // Green LED pin
int yellowLED = 26; // Yellow LED pin
int redLED = 27;    // Red LED pin

// Calibration variables
int baselineValue = 0;
int targetBaseline = 200;
int offset = 0;

// Notification flag
bool notification_sent = false;

// BlynkTimer instance
BlynkTimer timer;

// Function prototypes
void checkGasLevel();

// Setup function
void setup()
{
  // Start serial communication for debugging
  Serial.begin(115200);

  // Connect to Wi-Fi and Blynk
  Blynk.begin(auth, ssid, password);

  // Initialize MQ2 sensor pin as input
  pinMode(mq2Pin, INPUT);

  // Initialize LED pins as output
  pinMode(greenLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(redLED, OUTPUT);

  // Turn off all LEDs initially
  digitalWrite(greenLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(redLED, LOW);

  // Calibration process
  Serial.println("Calibrating sensor...");
  baselineValue = calibrateSensor();
  offset = baselineValue - targetBaseline;
  Serial.print("Baseline value: ");
  Serial.println(baselineValue);
  Serial.print("Offset: ");
  Serial.println(offset);
  Serial.println("Calibration complete.");

  // Set up a timer to check gas levels every 2 seconds
  timer.setInterval(2000L, checkGasLevel);
}

// Loop function
void loop()
{
  // Run the Blynk process to handle communication
  Blynk.run();

  // Run the timer
  timer.run();
}

// Function to check gas levels and send alerts
void checkGasLevel()
{
  // Read the analog value from MQ2 sensor
  int mq2Value = analogRead(mq2Pin);

  // Adjust the value using the offset
  int calibratedValue = mq2Value - offset;

  // Print sensor values to Serial Monitor for debugging
  Serial.print("Raw MQ2 Sensor Value: ");
  Serial.println(mq2Value);
  Serial.print("Calibrated Sensor Value: ");
  Serial.println(calibratedValue);

  // Send the calibrated sensor value to Blynk Virtual Pin V0
  Blynk.virtualWrite(V0, calibratedValue);

  // Send the same value to Virtual Pin V1 for the graph widget
  Blynk.virtualWrite(V1, calibratedValue);

  // Check if the gas level exceeds 1000
  if (calibratedValue > 1000)
  {
    // If notification hasn't been sent, send it now
    if (!notification_sent)
    {
      Serial.println("Gas level too high! Sending alert...");
      Blynk.logEvent("gas_level", "Gas level exceeds safe limit!");
      notification_sent = true; // Set the flag to prevent duplicate notifications
    }
  }
  else
  {
    // Reset the notification flag when the gas level drops below the threshold
    notification_sent = false;
  }

  // Set gauge color and control LEDs based on calibratedValue
  String color;
  if (calibratedValue < 500)
  {
    color = "#00FF00"; // Green
    digitalWrite(greenLED, HIGH);
    digitalWrite(yellowLED, LOW);
    digitalWrite(redLED, LOW);
  }
  else if (calibratedValue <= 1000)
  {
    color = "#FFFF00"; // Yellow
    digitalWrite(greenLED, LOW);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(redLED, LOW);
  }
  else
  {
    color = "#FF0000"; // Red
    digitalWrite(greenLED, LOW);
    digitalWrite(yellowLED, LOW);
    digitalWrite(redLED, HIGH);
  }

  // Dynamically change gauge color in Blynk
  Blynk.setProperty(V0, "color", color);
}

// Function to calibrate the MQ2 sensor
int calibrateSensor()
{
  int total = 0;
  const int samples = 100;

  for (int i = 0; i < samples; i++)
  {
    int value = analogRead(mq2Pin);
    total += value;
    delay(50);
  }

  // Calculate the average baseline value
  return total / samples;
}

If you have a project that you want assistance with, rather than discussing the merits of ChatGPT, then I’d suggest you start a new topic.

Pete.

1 Like