Rise of AI with ChatGPT
Tasbir Hossain
Tech Entrepreneur | Electronics Engineer | Expert in Digital Security, IT Equipment, Solution & Hardware Design | Driving Ideas to Innovation for a Safer World | Tech Dreamer
The AI is a very small word with an unimaginable impact on every part of our life. AI stands for Artificial Intelligence, for those who needs to know. AI is also a big buzz word now a days. Very few of us truly understand the power of AI and the possibilities. The word AI is simple yet have a very big impact on technology thus every part of our life. AI is currently involved in many sector i.e. Technology, Transport, Medical etc and many many more. AI is reshaping almost every part of the technology and development world. AI is improving road safety by autonomous car, improving diagnosis of general patient, fast tracking lots of research based development in multiple folds. Above are very few of the examples on top of my head. There are millions of use cases of AI.
Today I just wanted to point to an Open AI's ChatGPT, how awesome it has become already. ChatGPT is a chat based AI Bot who can answer many thing humanly and almost accurately. I have asked #chatgpt to "tell me about ChatGPT" ;). Here is the reply...
" ChatGPT is a variant of the GPT (Generative Pre-trained Transformer) language model that has been fine-tuned for the task of chatbot response generation. ChatGPT is able to generate human-like responses to user input in a conversation, and can be used to build chatbots that can hold engaging and coherent conversations with users.
The GPT model is a type of transformer-based language model developed by OpenAI that has been trained on a large dataset of human-generated text. It is designed to be able to generate human-like text by predicting the next word in a sequence based on the context of the words that come before it. By fine-tuning the GPT model on a specific task, such as chatbot response generation, it can be made more effective at generating relevant and coherent responses to user input.
There are a number of ways in which ChatGPT can be used, including building chatbots for customer service, personal assistants, and conversational AI applications. It can also be used to generate responses in chatbot systems that are designed for entertainment or social interactions."
Very recently ChatGBT opens its door to us to check its capabilities and i did so. Here is a conversation with the ChatGPT...
Me: 1. create an input pin, 2. attach interrupt to the pin, 3. create a wifi client, 4. create a MQTT connection. 5. Subscribe to topic "Zicore/SubData". now in the main loop keep MQTT connection alive. wait for any MQTT data. if there is any MQTT data then print it out to console. if there is any interrupt publish "interrupted" to the MQTT Topic "Zicore/PubData". write the code for Arduino
ChatGPT: Here is an example of how you can implement the steps you outlined in your question using Arduino:
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqttServer = "YOUR_MQTT_SERVER";
const int mqttPort = 1883;
const char* mqttUser = "YOUR_MQTT_USERNAME";
const char* mqttPassword = "YOUR_MQTT_PASSWORD";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
? // put your setup code here, to run once:
? pinMode(inputPin, INPUT); // set pin as input
? // initialize WiFi connection
? Serial.begin(115200);
? WiFi.begin(ssid, password);
? while (WiFi.status() != WL_CONNECTED) {
? ? delay(500);
? ? Serial.println("Connecting to WiFi..");
? }
? Serial.println("Connected to the WiFi network");
? // initialize MQTT client
? client.setServer(mqttServer, mqttPort);
? while (!client.connect("arduinoClient", mqttUser, mqttPassword)) {
? ? Serial.println("Connecting to MQTT server...");
? ? delay(500);
? }
? Serial.println("Connected to MQTT server");
? // subscribe to the "Zicore/SubData" topic
? client.subscribe("Zicore/SubData");
}
void loop() {
? // put your main code here, to run repeatedly:
? client.loop(); // keep the MQTT connection alive
? // check for incoming MQTT messages
? if (client.available()) {
? ? String message = client.readStringUntil('\n');
? ? Serial.println("Received message: " + message);
? }
? // check for interrupts on the input pin
? val = digitalRead(inputPin);
? if (val == HIGH) {
? ? // publish "interrupted" to the "Zicore/PubData" topic
? ? client.publish("Zicore/PubData", "interrupted");
? }
}
This code sets up an input pin, attaches an interrupt to it, establishes a WiFi connection and a MQTT connection, subscribes to the "Zicore/SubData" topic, and waits for any MQTT data or interrupts. If there is any MQTT data, it is printed out to the console. If there is an interrupt on the input pin, the code publishes the message "interrupted" to the "Zicore/PubData" topic.
Me: you forgot to attach interrupt
ChatGPT: You are correct, I apologize for the oversight. Here is the corrected code with the interrupt attached:
#inlcude <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqttServer = "YOUR_MQTT_SERVER";
const int mqttPort = 1883;
const char* mqttUser = "YOUR_MQTT_USERNAME";
const char* mqttPassword = "YOUR_MQTT_PASSWORD";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
? // put your setup code here, to run once:
? pinMode(inputPin, INPUT); // set pin as input
? // attach interrupt to the input pin
? attachInterrupt(digitalPinToInterrupt(inputPin), publishInterrupt, CHANGE);
? // initialize WiFi connection
? Serial.begin(115200);
? WiFi.begin(ssid, password);
? while (WiFi.status() != WL_CONNECTED) {
? ? delay(500);
? ? Serial.println("Connecting to WiFi..");
? }
? Serial.println("Connected to the WiFi network");
? // initialize MQTT client
? client.setServer(mqttServer, mqttPort);
? while (!client.connect("arduinoClient", mqttUser, mqttPassword)) {
? ? Serial.println("Connecting to MQTT server...");
? ? delay(500);
? }
? Serial.println("Connected to MQTT server");
? // subscribe to the "Zicore/SubData" topic
? client.subscribe("Zicore/SubData");
}
void loop() {
? // put your main code here, to run repeatedly:
? client.loop(); // keep the MQTT connection alive
? // check for incoming MQTT messages
? if (client.available()) {
? ? String message = client.readStringUntil('\n');
? ? Serial.println("Received message: " + message);
? }
}
// interrupt callback function
void publishInterrupt() {
? // publish "interrupted" to the "Zicore/PubData" topic
? client.publish("Zicore/PubData", "interrupted");
}
In this version of the code, I added a function called publishInterrupt that gets called whenever there is an interrupt on the input pin. This function simply publishes the message "interrupted" to the "Zicore/PubData" topic.
Me: great work
领英推荐
ChatGPT: Thank you! I'm glad that I could help. Let me know if you have any other questions or if there is anything else I can do to assist you.
Me: now can you add heartbeat LED with the code
ChatGPT: Certainly! Here is an example of how you can add a heartbeat LED to the code I provided earlier:
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqttServer = "YOUR_MQTT_SERVER";
const int mqttPort = 1883;
const char* mqttUser = "YOUR_MQTT_USERNAME";
const char* mqttPassword = "YOUR_MQTT_PASSWORD";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int ledPin = 13; // choose the pin for the LED
void setup() {
? // put your setup code here, to run once:
? pinMode(inputPin, INPUT); // set pin as input
? pinMode(ledPin, OUTPUT); // set pin as output
? // attach interrupt to the input pin
? attachInterrupt(digitalPinToInterrupt(inputPin), publishInterrupt, CHANGE);
? // initialize WiFi connection
? Serial.begin(115200);
? WiFi.begin(ssid, password);
? while (WiFi.status() != WL_CONNECTED) {
? ? delay(500);
? ? Serial.println("Connecting to WiFi..");
? }
? Serial.println("Connected to the WiFi network");
? // initialize MQTT client
? client.setServer(mqttServer, mqttPort);
? while (!client.connect("arduinoClient", mqttUser, mqttPassword)) {
? ? Serial.println("Connecting to MQTT server...");
? ? delay(500);
? }
? Serial.println("Connected to MQTT server");
? // subscribe to the "Zicore/SubData" topic
? client.subscribe("Zicore/SubData");
}
void loop() {
? // put your main code here, to run repeatedly:
? client.loop(); // keep the MQTT connection alive
? // check for incoming MQTT messages
? if (client.available()) {
? ? String message = client.readStringUntil('\n');
? ? Serial.println("Received message: " + message);
? }
? // toggle the LED on and off every 500 milliseconds
? digitalWrite(ledPin, HIGH);
? delay(250);
? digitalWrite(ledPin, LOW);
? delay(250);
}
// interrupt callback function
void publishInterrupt() {
? // publish "interrupted" to the "Zicore/PubData" topic
? client.publish("Zicore/PubData", "interrupted");
}
This code sets up an output pin for the LED and toggles it on and off every 500 milliseconds in the main loop. This creates a "heartbeat" effect, where the LED turns on and off at regular intervals. Let me know if you have any questions or if you'd like further clarification on how this works.
Me: great work. now please re write the code with non blocking heart beat LED code
ChatGPT: Certainly! Here is an example of how you can implement a non-blocking heartbeat LED using Arduino:
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqttServer = "YOUR_MQTT_SERVER";
const int mqttPort = 1883;
const char* mqttUser = "YOUR_MQTT_USERNAME";
const char* mqttPassword = "YOUR_MQTT_PASSWORD";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int ledPin = 13; // choose the pin for the LED
unsigned long previousMillis = 0; // stores the last time the LED was updated
const long interval = 500; // interval at which to toggle the LED (in milliseconds)
bool ledState = LOW; // stores the current state of the LED
void setup() {
? // put your setup code here, to run once:
? pinMode(inputPin, INPUT); // set pin as input
? pinMode(ledPin, OUTPUT); // set pin as output
? // attach interrupt to the input pin
? attachInterrupt(digitalPinToInterrupt(inputPin), publishInterrupt, CHANGE);
? // initialize WiFi connection
? Serial.begin(115200);
? WiFi.begin(ssid, password);
? while (WiFi.status() != WL_CONNECTED) {
? ? delay(500);
? ? Serial.println("Connecting to WiFi..");
? }
? Serial.println("Connected to the WiFi network");
? // initialize MQTT client
? client.setServer(mqttServer, mqttPort);
? while (!client.connect("arduinoClient", mqttUser, mqttPassword)) {
? ? Serial.println("Connecting to MQTT server...");
? ? delay(500);
? }
? Serial.println("Connected to MQTT server");
? // subscribe to the "Zicore/SubData" topic
? client.subscribe("Zicore/SubData");
}
void loop() {
? // put your main code here, to run repeatedly:
? client.loop(); // keep the MQTT connection alive
? // check for incoming MQTT messages
? if (client.available()) {
? ? String message = client.readStringUntil('\n');
? ? Serial.println("Received message: " + message);
? }
? // update the LED state if the specified interval has passed
? unsigned long currentMillis = millis();
? if (currentMillis - previousMillis >= interval) {
? ? // toggle the LED state
? ? ledState = !ledState;
? ? digitalWrite(ledPin, ledState);
? ? // update the previous time
? ? previousMillis = currentMillis;
? }
}
// interrupt callback function
void publishInterrupt() {
? // publish "interrupted" to the "Zicore/PubData" topic
? client.publish("Zicore/PubData", "interrupted");
}
In this version of the code
(End of Conversation with ChatGPT)
Now if you see the Code is almost correct with the right syntax and proper helper comment (a little change needed to work properly depending on the library used). I was so blown away by the conversation with the #chatgpt . It felt like I have been talking to another human on the other side. He was listening and giving me the right code as I have asked for. And you can see the conversation was so fluid, you cannot differentiate with other human conversation. Even when I told #chatgpt that "you forgot to attach interrupt", it replayed with apology which was so humanly. And still all the coding snippets are almost correct. You can say that there are better ways of doing the same coding, yes of-course I agree too. But every problem has different ways to solve. coding practices are different from person to person. But the code ChatGPT delivered is very standard practice.
The Future: I believe that with the #chatgpt it's been again proven that AI is the next generation tool to go ahead. In near future we will not do many things the way we do now a days. Many current practices will be changed and will be simplified. With these simplified process our productivity will be greatly increased. Looking forward to the new ways of doing things. Big changes are inevitable. AI will be used in the every parts of our life and we need to use it wisely.
Thanks for reading so far.