write a program to perform logical opertion using embedded c
#include <stdio.h>
int main() {
int num1 = 10; // Binary: 1010
int num2 = 6; // Binary: 0110
printf("Bitwise AND: %d\n", a & b;); // Expected output: 2 (Binary: 0010)
printf("Bitwise OR: %d\n", a | b); // Expected output: 14 (Binary: 1110)
printf("Bitwise XOR: %d\n, a ^ b); // Expected output: 12 (Binary: 1100)
printf("Bitwise NOT of num1: %d\n",~a); // Expected output: -11 (Binary:
11111111111111111111111111110101)
return 0;
}
write a program to compare two number in 8051 using simulator
ORG 0x0000
MOV A, #25h
MOV B, #3Ah
CJNE A, B, NOT_EQUAL
SETB P1.0
SJMP END_PROGRAM
NOT_EQUAL:
CLR P1.0
END_PROGRAM:
SJMP $
END
write a program to communicate between IoT and Bluetooth
C++:
// Arduino code
#include <SoftwareSerial.h>
// Define Bluetooth module connections
#define BT_RX_PIN 2
#define BT_TX_PIN 3
SoftwareSerial bluetoothSerial(BT_RX_PIN, BT_TX_PIN); // RX, TX
void setup() {
Serial.begin(9600); // Initialize serial communication with computer
bluetoothSerial.begin(9600); // Initialize serial communication with Bluetooth module
}
void loop() {
// Check if data is available on Bluetooth module
if (bluetoothSerial.available()) {
char receivedChar = bluetoothSerial.read(); // Read the incoming byte from Bluetooth
Serial.print("Received from Bluetooth: ");
Serial.println(receivedChar); // Print the received character to Serial Monitor
}
// Check if data is available on Serial Monitor
if (Serial.available()) {
char dataToSend = Serial.read(); // Read the incoming byte from Serial Monitor
bluetoothSerial.print(dataToSend); // Send the data to Bluetooth module
}
}
Python:
import paho.mqtt.client as mqtt
import bluetooth
# MQTT Broker details
broker_address = "mqtt.example.com"
broker_port = 1883
mqtt_topic = "bluetooth_data"
# Bluetooth device details
bluetooth_device_address = "XX:XX:XX:XX:XX:XX" # Replace with your Bluetooth device
address
bluetooth_port = 1
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker")
else:
print("Failed to connect, return code %d\n", rc)
def on_message(client, userdata, message):
print("Received message: ", str(message.payload.decode("utf-8")))
def bluetooth_receive():
try:
bluetooth_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
bluetooth_socket.connect((bluetooth_device_address, bluetooth_port))
print("Connected to Bluetooth device")
while True:
data = bluetooth_socket.recv(1024)
if len(data) > 0:
print("Received data from Bluetooth:", data.decode("utf-8"))
mqtt_client.publish(mqtt_topic, data.decode("utf-8"))
except KeyboardInterrupt:
print("Keyboard interrupt detected, closing connections")
except Exception as e:
print("Error:", str(e))
finally:
bluetooth_socket.close()
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect(broker_address, broker_port)
bluetooth_receive()
write a program to communicate between zigbee and IOT
#include <XBee.h>
#include <PubSubClient.h>
#include <Ethernet.h> // If using Ethernet for MQTT
// Zigbee parameters
#define PAN_ID 0xABCD // Example PAN ID
#define MY_ADDRESS 0x1234 // Example device address
#define DEST_ADDR 0x5678 // Example destination address
#define RX_PIN 2 // Pin for RX (receive) on the Zigbee module
#define TX_PIN 3 // Pin for TX (transmit) on the Zigbee module
// MQTT parameters
const char* mqtt_server = "mqtt.example.com"; // MQTT broker address
const int mqtt_port = 1883; // MQTT broker port
const char* mqtt_username = "your_username"; // MQTT username
const char* mqtt_password = "your_password"; // MQTT password
const char* mqtt_topic = "zigbee_data"; // MQTT topic
XBee xbee = XBee();
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x40C3F7A5); // Example 64-bit address
of the Zigbee Coordinator
ZBRxResponse rx = ZBRxResponse();
uint8_t payload[100];
int payloadLength;
EthernetClient ethClient; // If using Ethernet for MQTT
PubSubClient client(ethClient); // If using Ethernet for MQTT
void setup() {
Serial.begin(9600);
xbee.begin(Serial);
// Connect to MQTT broker
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
reconnect();
}
void loop() {
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
xbee.getResponse().getZBRxResponse(rx);
payloadLength = rx.getDataLength();
for (int i = 0; i < payloadLength; i++) {
payload[i] = rx.getData(i);
}
client.publish(mqtt_topic, payload, payloadLength); // Publish Zigbee data to MQTT topic
}
}
client.loop();
}
void callback(char* topic, byte* message, unsigned int length) {
// Handle MQTT messages if needed
}
void reconnect() {
while (!client.connected()) {
if (client.connect("arduinoClient", mqtt_username, mqtt_password)) {
client.subscribe(mqtt_topic); // Subscribe to MQTT topic
} else {
delay(5000);
}
}
}
write a program to communicate between IoT and GSM
#include <SoftwareSerial.h>
// Define GSM module connections
#define GSM_TX_PIN 2
#define GSM_RX_PIN 3
// Define IoT parameters
const char* mqtt_server = "mqtt.example.com"; // MQTT broker address
const int mqtt_port = 1883; // MQTT broker port
const char* mqtt_username = "your_username"; // MQTT username
const char* mqtt_password = "your_password"; // MQTT password
const char* mqtt_topic = "gsm_data"; // MQTT topic
SoftwareSerial gsmSerial(GSM_TX_PIN, GSM_RX_PIN); // RX, TX
void setup() {
Serial.begin(9600);
gsmSerial.begin(9600);
delay(1000);
sendATCommand("AT"); // Check if the GSM module is responding
sendATCommand("AT+CMGF=1"); // Set SMS text mode
}
void loop() {
// Read SMS from GSM module
String message = readSMS();
// Publish received SMS to MQTT topic
if (message.length() > 0) {
publishToMQTT(message);
}
delay(5000); // Adjust delay as needed
}
String readSMS() {
String message = "";
sendATCommand("AT+CMGL=\"REC UNREAD\""); // Read unread SMS
while (gsmSerial.available()) {
char c = gsmSerial.read();
message += c;
}
return message;
}
void publishToMQTT(String data) {
if (!data.startsWith("+CMGL:")) {
Serial.println("Received SMS: " + data);
// Connect to MQTT broker
// Publish SMS data to MQTT topic
// Example:
// mqttClient.publish(mqtt_topic, data.c_str());
}
}
void sendATCommand(String command) {
gsmSerial.println(command);
delay(500);
while (gsmSerial.available()) {
Serial.write(gsmSerial.read());
}
}