arduino button can't work send message to telegram use python based arduino uno

Hi all, i'm student, and still progress do my project for my task exam. I hope kindly anyone can help me because i'm still beginner for programming. I build project about smarthome arduino can control with telegram and i use python programming for connection from arduino to telegram.

and i want ask about read component (pushbutton) from python to telegram :

If pushbutton pressed the lamp is turn ON, arduino will send the information to python and telegram that "pushbutton pressed, and the lamp turn ON"

python :

#!/usr/bin/python
import telepot, time, serial
ser = serial.Serial('COM10', 9600, timeout=1)

   

def handle(msg):

   userName = msg['from']['first_name']+" "+msg['from']['last_name']

   content_type, chat_type, chat_id = telepot.glance2(msg)
   buffer=ser.read()
   
   if (content_type == 'text'):
      command = msg['text']
      print ('Got command: %s' % command)

      if  '/hello' in command:
         bot.sendMessage(chat_id, "Assalamu'alaikum "+userName+", ..?")

      if '/lampu1_1' in command:
         ser.write(b'A')
         bot.sendMessage(chat_id, "Lampu1 ON")

      if '/lampu1_0' in command:
         ser.write(b'B')
         bot.sendMessage(chat_id, "Lampu1 OFF")
            
      if '/lampu2_1' in command:
         ser.write(b'C')
         bot.sendMessage(chat_id, "Lampu2 ON")
         
      if '/lampu2_0' in command:
         ser.write(b'D')
         bot.sendMessage(chat_id, "Lampu2 OFF")
         
   elif(content_type == 'text'):
      command = msg['text']
      print ('Got command: %s' %command)
      
      if buffer=="H" :
         bot.sendMessage(chat_id, "pushbutton pressed, the lamp turn ON")
   
      if buffer=="M" :
         bot.sendMessage(chat_id, "pushbutton unpress, the lamp turn OFF")
         
# Create a bot using the token given by BotFather
bot = telepot.Bot('201023475:AAF_Ym_l662EayJpG8hWG-rMMQKbETW_4Yw')

# Add handle function to be called every received message.

bot.notifyOnMessage(handle)
#bot.notifyOnMessage(tekan)

# Wait for new messages
while 1:
   time.sleep(20)

and script for pushbutton *python :

   elif(content_type == 'text'):
      command = msg['text']
      print ('Got command: %s' %command)
      
      if buffer=="H" :
         bot.sendMessage(chat_id, "pushbutton pressed, the lamp turn ON")
   
      if buffer=="M" :
         bot.sendMessage(chat_id, "pushbutton unpress, the lamp turn OFF")

===========================================================

Arduino Code :

const int ledPin = 12; // the pin that the LED is attached to
unsigned int incomingByte;      // a variable to read incoming serial data into
int button=10;
int val=0;
unsigned int outgoingByte;
int LED_state=LOW;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  if(digitalRead(button)==LOW)
  {
  if(LED_state==LOW){
    digitalWrite(ledPin,LOW);
    LED_state=HIGH;
    Serial.write("H");
  }
  else{
  digitalWrite(ledPin,HIGH);
  LED_state=LOW;
  Serial.write("M");
  }
  }
  delay(300);

    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'D') {
      digitalWrite(ledPin, LOW);
      Serial.print("Lampu Mati");
      Serial.print("\n");
    }
    // if it's an L (ASCII 76) turn off the LED:
    else if (incomingByte == 'C') {
      digitalWrite(ledPin, HIGH);
      Serial.print("Lampu Hidup");
      Serial.print("\n");
    }
    }

code pushbutton in arduino code :

if(digitalRead(button)==LOW)
  {
  if(LED_state==LOW){
    digitalWrite(ledPin,LOW);
    LED_state=HIGH;
    Serial.write("H");
  }
  else{
  digitalWrite(ledPin,HIGH);
  LED_state=LOW;
  Serial.write("M");
  }
  }
  delay(300);

========================================================================

I hope anyone can help me, please

the problem : Pushbutton if pressed can't work send message to telegram

I already try use seral monitor in arduino, pushbutton working and the lamp is turn on and show the command in serial monitor in arduino

But with telegram can't work

This Python - Arduino demo may be helpful.

Your description of your problem is not clear.

Does your Python program show that it has properly received the message from the Arduino?

Using delay() in your Arduino program is not a good idea because the Arduino cannot do anything else during the delay() period. Use millis() to manage timing as illustrated in Several Things at a Time

Your system for reading data in the Arduino is not robust. Look at the examples in Serial Input Basics. They may be a little different from the code in the Python demo because they were written more recently.

...R

Could you give me one example code about pushbutton in python sir ?

thanks

rifqidp: Could you give me one example code about pushbutton in python sir ?

This is a Forum for Arduino programming, not Python programming.

Have you studied the example I already gave you?

I thought you wanted the push-button to be connected to your Arduino and for the Arduino to send a message to your Python program when the button is pressed? Am I wrong?

...R

yes sir.

i wanted the push button to be connected to my Arduino and for the Arduino to send a message to my Python program when the button is pressed, sir

rifqidp: i wanted the push button to be connected to my Arduino and for the Arduino to send a message to my Python program when the button is pressed, sir

Then let me repeat my earlier question

Have you studied the example I already gave you?

...R