1. Introduction
So in this project, we are going show live Instagram Account followers and following count. The workflow of the project is first we run a python script on a running system or a server. The python script fetches the following and follower count for the Instagram server through an HTTP request. Then the number is being sent to the bolt cloud using the Bolt API. The data is then sent to the particular Bolt module according to the provided Bolt ID. Then the data is sent to Arduino Board through UART and then the Arduino shows the count on the LCD screen. Here for the demo, I have used an LCD display but you can visualize the count as you want using the Arduino.
2. Setting up the Bolt SideSo as you might have got your Bold board and get it connected to the Bolt Cloud. Then you can proceed further. If you don't know-how to connect your Bolt board to the Bolt Cloud watch this starter video.
First Get the API key and the Bolt ID from the cloud.
- Connect your Bolt Device to the Bolt Cloud
- Open the cloud dashboard https://cloud.boltiot.com/
- In your Devices tap get your 8 Character Bolt ID.
- Then go to the API tab Generate a new key if you haven't created one. Then click on the copy button to copy the API key to the clipboard.
SettingUp Python Script.
Now SSH into your Server cuz the python script will be running on your server. If you don't have a server then you can use your own computer (Linux recommended).
Let's first install some python dependencies.
pip install boltiot
pip install requests
pip install json
Now create a python file as server.py and past the following code.
import requests
import json
import time
from boltiot import Bolt
bolt_api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
device_id = "BOLTXXXXXX"
insta_username = "anurag_3301"
mybolt = Bolt(bolt_api_key, device_id)
response = mybolt.serialBegin('9600')
print(response)
def get_insta_count(username):
url = 'https://www.instagram.com/' + username + "/?__a=1"
r = requests.get(url)
data = json.loads(r.text)
return {"followers":data["graphql"]["user"]["edge_followed_by"]["count"] ,
"following": data["graphql"]["user"]["edge_follow"]["count"]}
while True:
data = get_insta_count(insta_username)
followers = str(data["followers"])
following = str(data["following"])
send = followers + "$" + following
response = mybolt.serialWrite(send)
print(response)
time.sleep(60)
Code Explanation
- First, we will import the dependencies
import requests
import json
import time
from boltiot import Bolt
- Then comes the personal credential part. On first-line replace the string with your API Key, then on the second-line replace the sting with your Bolt ID, and then on the third like replace my Instagram username with yours.
bolt_api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
device_id = "BOLTXXXXXX"
insta_username = "anurag_3301"
- Then we will contact the Bolt could with the API credentials and tell the Bolt board to start UART communication with 9600 Baud rate. Then print the response for troubleshooting.
mybolt = Bolt(bolt_api_key, device_id)
response = mybolt.serialBegin('9600')
print(response)
- Then declare the get_insta_count function which takes the insta username as an argument and returns a dictionary with count data. First, we create the URL for the request and then load the response as a JSON instance and then return a dictionary with the followers and following count extracted from the JSON instance.
def get_insta_count(username):
url = 'https://www.instagram.com/' + username + "/?__a=1"
r = requests.get(url)
data = json.loads(r.text)
return {"followers":data["graphql"]["user"]["edge_followed_by"]["count"] ,
"following": data["graphql"]["user"]["edge_follow"]["count"]}
- Then we create the infinite loop where first we call the get_insta_count function and then extract the followers and following count from the dictionary and creating a string with the followers and following count separated by '$'. And then sending the data to Arduino through UART line using serialWrite function. Then printing the response for troubleshooting. And wait for a min.
while True:
data = get_insta_count(insta_username)
followers = str(data["followers"])
following = str(data["following"])
send = followers + "$" + following
response = mybolt.serialWrite(send)
print(response)
time.sleep(60)
Let's See The Arduino PartDo not connect anything to the arduino except the USB cable and upload this code.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
void setup(){
lcd.begin();
lcd.backlight();
lcd.print("");
Serial.begin(9600);
Serial.setTimeout(50);
}
void loop(){
String inString = "";
String str[2] = {"", ""};
if (Serial.available() > 0){
lcd.clear();
inString = Serial.readString();
int index = 0;
for (int i = 0; i < inString.length(); i++){
if (inString[i] == '$'){
index = 1;
continue;
}
str[index] += inString[i];
}
lcd.setCursor(0, 0);
lcd.print("Followers " + str[0]);
lcd.setCursor(0, 1);
lcd.print("Following " + str[1]);
}
}
- First, we will import the required libraries. Get the LiquidCrystal_I2C library here.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
- Now create a LiquidCrystal_I2C instance with the I2C address and the dimension of the display.
// Make sure to replace the I2C address with yours
LiquidCrystal_I2C lcd(0x3F, 16, 2);
- Then comes the setup function. Here we first begin the lcd and Serial for UART communication at 9600 baud rate.
void setup(){
lcd.begin();
lcd.backlight();
lcd.print("");
Serial.begin(9600);
Serial.setTimeout(50);
}
- Then comes the loop function. First, we declare a string where the data send by Bolt will be stored and a string array where the following and follower count will be stored after separation. Then we wait till there is some data send by Bolt. Then we clear the LCD and store the data send by Bolt. Then we separate the following and follower count and then print in the LCD display.
void loop(){
String inString = "";
String str[2] = {"", ""};
if (Serial.available() > 0){
lcd.clear();
inString = Serial.readString();
int index = 0;
for (int i = 0; i < inString.length(); i++){
if (inString[i] == '$'){
index = 1;
continue;
}
str[index] += inString[i];
}
lcd.setCursor(0, 0);
lcd.print("Followers " + str[0]);
lcd.setCursor(0, 1);
lcd.print("Following " + str[1]);
}
}
ConnectionsNow do the following connection between Arduino and I2C LCD adapter
- Arduino A5 --> SCL of adapter
- Arduino A4 --> SDA of adapter
- Arduino GND --> GND of adapter
- Arduino 5V --> VCC of adapter
Now do the following connection between Arduino and Bolt Board
- Arduino GND --> GND of Bolt
- Arduino TX --> RX of Bolt
- Arduino RX --> TX of Bolt
Comments