-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
332 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "MQTTClient.h" | ||
|
||
#define ADDRESS "tcp://192.168.3.200:1883" | ||
#define CLIENTID "ExampleClientPub" | ||
#define TOPIC "c/mqtt" | ||
#define PAYLOAD "Hello from C!" | ||
#define QOS 1 | ||
#define TIMEOUT 10000L | ||
|
||
// int main(int argc, char* argv[]) { | ||
// // MQTTClient declaration and other code for connecting, publishing, and disconnecting | ||
// } | ||
|
||
int main(int argc, char* argv[]) { | ||
MQTTClient client; | ||
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; | ||
MQTTClient_message pubmsg = MQTTClient_message_initializer; | ||
MQTTClient_deliveryToken token; | ||
int rc; | ||
|
||
// Initialize the client | ||
MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); | ||
conn_opts.keepAliveInterval = 20; | ||
conn_opts.cleansession = 1; | ||
|
||
// Connect to the MQTT broker | ||
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { | ||
printf("Failed to connect, return code %d\n", rc); | ||
exit(-1); | ||
} | ||
|
||
// Prepare and publish the message | ||
pubmsg.payload = PAYLOAD; | ||
pubmsg.payloadlen = strlen(PAYLOAD); | ||
pubmsg.qos = QOS; | ||
pubmsg.retained = 0; | ||
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token); | ||
printf("Waiting for up to %ld seconds for publication of %s\n" | ||
"on topic %s for client with ClientID: %s\n", | ||
TIMEOUT / 1000, PAYLOAD, TOPIC, CLIENTID); | ||
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT); | ||
printf("Message with delivery token %d delivered\n", token); | ||
|
||
// Disconnect | ||
MQTTClient_disconnect(client, 10000); | ||
MQTTClient_destroy(&client); | ||
return rc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
```sh | ||
sudo apt-get update | ||
#sudo apt-get install libpaho-mqtt-dev | ||
sudo apt-get install build-essential git cmake | ||
|
||
|
||
|
||
gcc -o mqtt_publish mqtt_publish.c -lpaho-mqtt3c | ||
./mqtt_publish | ||
#gcc -o mqtt_publish_server mqtt_publish_server.c -lpaho-mqtt3c | ||
#./mqtt_publish_server | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
version: '3' | ||
services: | ||
node-red: | ||
image: nodered/node-red | ||
ports: | ||
- "1880:1880" | ||
volumes: | ||
- myNodeREDdata:/data | ||
depends_on: | ||
- mqtt-broker | ||
|
||
mqtt-broker: | ||
image: emqx/emqx:5.0.0 | ||
ports: | ||
- "1883:1883" | ||
- "8083:8083" | ||
- "8084:8084" | ||
- "8883:8883" | ||
- "18083:18083" | ||
|
||
volumes: | ||
myNodeREDdata: | ||
|
||
#sudo docker-compose up -d | ||
|
||
#docker run -it -p 1880:1880 -v myNodeREDdata:/data --name mynodered nodered/node-red | ||
|
||
################################# | ||
# version: '3' | ||
# services: | ||
# node-red: | ||
# image: nodered/node-red | ||
# ports: | ||
# - "1880:1880" | ||
# environment: | ||
# - TZ=UTC # Optional: Set timezone if needed | ||
# volumes: | ||
# - ./data:/data # Optional: Mount a volume to persist data | ||
|
||
# version: '3' | ||
# services: | ||
# node-red: | ||
# image: nodered/node-red | ||
# ports: | ||
# - "1880:1880" | ||
# environment: | ||
# - TZ=UTC # Optional: Set timezone if needed | ||
# volumes: | ||
# - ./data:/data # Optional: Mount a volume to persist data | ||
|
||
# mqtt-broker: | ||
# image: emqx/emqx | ||
# ports: | ||
# - "1883:1883" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import time | ||
import random | ||
import math | ||
import paho.mqtt.publish as publish | ||
|
||
# MQTT Broker (EMQX) details | ||
#broker_address = "broker.emqx.io" #local broker | ||
broker_address = "192.168.3.200" #local network broker | ||
port = 1883 | ||
topic = "python/mqtt" | ||
|
||
while True: | ||
# Generate a random value based on normal distribution | ||
mean = 25 # Mean of the distribution | ||
std_dev = 10 # Standard deviation of the distribution | ||
value = random.normalvariate(mean, std_dev) | ||
value = max(0, min(50, value)) # Ensure value is within [0, 50] range | ||
|
||
# Message to publish | ||
message = str(value) | ||
|
||
# Publish the message | ||
publish.single(topic, message, hostname=broker_address, port=port) | ||
|
||
print(f"Message Published: {message}") | ||
|
||
# Wait for 1 second | ||
time.sleep(0.1) | ||
|
||
#python3 Python_push_distribution.py |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.