-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
blockchain_integration/pi_network/pi_network_university/device_code/iot_device_code.ino
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,48 @@ | ||
// iot_device_code.ino | ||
|
||
#include <WiFi.h> | ||
#include <PubSubClient.h> | ||
|
||
// WiFi credentials | ||
const char* ssid = "your_wifi_ssid"; | ||
const char* password = "your_wifi_password"; | ||
|
||
// MQTT broker credentials | ||
const char* mqttServer = "your_mqtt_broker_url"; | ||
const char* mqttTopic = "your_mqtt_topic"; | ||
const char* mqttUsername = "your_mqtt_username"; | ||
const char* mqttPassword = "your_mqtt_password"; | ||
|
||
WiFiClient espClient; | ||
PubSubClient client(espClient); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
// Connect to WiFi | ||
WiFi.begin(ssid, password); | ||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(1000); | ||
Serial.println("Connecting to WiFi..."); | ||
} | ||
Serial.println("Connected to WiFi"); | ||
Serial.println("Initializing MQTT client..."); | ||
|
||
// Connect to MQTT broker | ||
client.setServer(mqttServer, 1883); | ||
client.connect(mqttUsername, mqttPassword); | ||
} | ||
|
||
void loop() { | ||
// Read sensor data (e.g. temperature, humidity, etc.) | ||
int sensorValue = analogRead(A0); | ||
|
||
// Convert sensor data to string | ||
String sensorData = String(sensorValue); | ||
|
||
// Publish sensor data to MQTT topic | ||
client.publish(mqttTopic, sensorData.c_str()); | ||
|
||
// Wait for 1 minute before publishing again | ||
delay(60000); | ||
} |