Mechatronics LAB
Published © MPL-2.0

Automatically Send Tweets from Your Raspberry Pi Using Thing

You need to automatically send tweets from your Raspberry Pi. Today I show you, How to Sending Tweets Using ThingSpeak with Raspberry Pi. fo

BeginnerFull instructions provided1 hour1,225
Automatically Send Tweets from Your Raspberry Pi Using Thing

Things used in this project

Hardware components

raspberry pi 5
×1

Story

Read more

Schematics

How to Sending Tweets Using ThingSpeak with Raspberry Pi

Code

Code

Arduino
import time, os, urllib, urllib2

MAX_TEMP = 37.0
MIN_T_BETWEEN_WARNINGS = 60  # Minutes
BASE_URL = 'https://api.thingspeak.com/apps/thingtweet/1/statuses/update/'
KEY = '68LZC4LBMXLMMYDY'

def send_notification(temp):
    status = 'Hello Raspberry Pi getting hot. CPU temp=' + temp
    data = urllib.urlencode({'api_key': KEY, 'status': status})
    response = urllib2.urlopen(url=BASE_URL, data=data)
    print(response.read())

def cpu_temp():
    dev = os.popen('/opt/vc/bin/vcgencmd measure_temp')
    cpu_temp = dev.read()[5:-3]
    return cpu_temp

while True:
    temp = cpu_temp()
    print("CPU Temp (C): " + str(temp))
    if temp > MAX_TEMP:
        print("CPU TOO HOT!")
        send_notification(temp)
        print("No more notifications for: " + str(MIN_T_BETWEEN_WARNINGS) + " mins")
        time.sleep(MIN_T_BETWEEN_WARNINGS * 60)
    time.sleep(1)

Code

Python
import time
import urequests as requests
import machine

MAX_TEMP = 37.0
MIN_T_BETWEEN_WARNINGS = 60  # Minutes
BASE_URL = 'https://api.thingspeak.com/apps/thingtweet/1/statuses/update/'
KEY = 'YOUR_API_KEY'

def send_notification(temp):
    status = 'Hello Raspberry Pi getting hot. CPU temp=' + str(temp)
    data = {'api_key': KEY, 'status': status}
    response = requests.post(BASE_URL, json=data)
    print(response.text)

def cpu_temp():
    adc = machine.ADC(machine.Pin(34))  # ADC1 channel 6 (GPIO34)
    adc.width(machine.ADC.WIDTH_12BIT)
    adc.atten(machine.ADC.ATTN_11DB)
    raw_value = adc.read()
    voltage = raw_value / 4095.0 * 3.3  # Assuming 12-bit ADC and 3.3V reference
    temperature = (voltage - 0.5) * 100  # Assuming MCP9700 temperature sensor
    return temperature

while True:
    temp = cpu_temp()
    print("CPU Temp (C): " + str(temp))
    if temp > MAX_TEMP:
        print("CPU TOO HOT!")
        send_notification(temp)
        print("No more notifications for: " + str(MIN_T_BETWEEN_WARNINGS) + " mins")
        time.sleep(MIN_T_BETWEEN_WARNINGS * 60)
    time.sleep(1)

Credits

Mechatronics LAB
75 projects • 49 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .

Comments