Skip to content

Commit

Permalink
initial code.
Browse files Browse the repository at this point in the history
Signed-off-by: Raquel <[email protected]>
  • Loading branch information
Raquel authored and Raquel committed Mar 19, 2022
1 parent 0139534 commit c85c0c3
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Created by https://www.toptal.com/developers/gitignore/api/c,c++,visualstudiocode,macos,vim
# Edit at https://www.toptal.com/developers/gitignore?templates=c,c++,visualstudiocode,macos,vim

src/arduino_secrets.h

### C ###
# Prerequisites
*.d
Expand Down
142 changes: 142 additions & 0 deletions src/src.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include "arduino_secrets.h"
#include <WiFiNINA.h>
#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <Arduino_MKRENV.h>

const int json_size = 1000

// Add WiFi connection information
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password

int WiFistatus = WL_IDLE_STATUS; // the Wifi radio's status



// MQTT objects
void callback(char* topic, byte* payload, unsigned int length);
WiFiClient wifiClient;
PubSubClient mqtt(MQTT_HOST, MQTT_PORT, callback, wifiClient);

// variables to hold data
StaticJsonDocument<json_size> jsonDoc;
JsonObject payload = jsonDoc.to<JsonObject>();
JsonObject status = payload.createNestedObject("d");
static char msg[json_size];

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] : ");

payload[length] = 0; // ensure valid content is zero terminated so can treat as c-string
Serial.println((char *)payload);
}




void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (!ENV.begin()) { //check correct initialisation of shield
Serial.println("Failed to initialize MKR ENV shield!");
while (1); //infinite loop, avoids loop to be executed
}
// attempt to connect to Wifi network:

while (WiFistatus != WL_CONNECTED) {
Serial.print("Attempting to connect to network: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
WiFistatus = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// you're connected now, so print out the data:
Serial.println("You're connected to the network");

Serial.println("----------------------------------------");
printData();
Serial.println("----------------------------------------");

// Connect to MQTT - IBM Watson IoT Platform
if (mqtt.connect(MQTT_DEVICEID, MQTT_USER, MQTT_TOKEN)) {
Serial.println("MQTT Connected");
mqtt.subscribe(MQTT_TOPIC_DISPLAY);

} else {
Serial.println("MQTT Failed to connect!");
}
}

void loop() {
mqtt.loop();
while (!mqtt.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqtt.connect(MQTT_DEVICEID, MQTT_USER, MQTT_TOKEN)) {
Serial.println("MQTT Connected");
mqtt.subscribe(MQTT_TOPIC_DISPLAY);
mqtt.loop();
} else {
Serial.println("MQTT Failed to connect!");
delay(5000);
}
}
float temp = ENV.readTemperature();
float hum = ENV.readHumidity();
float illu = ENV.readIlluminance();

// Check if any reads failed and exit early (to try again).
if (isnan(temp) || isnan(hum) || isnan(illu)) {
Serial.println("Failed to read data!");
} else {

// Send data to Watson IoT Platform
status["temp"] = temp;
status["hum"] = hum;
status["illu"] = illu;
Serial.println(msg);
if (!mqtt.publish(MQTT_TOPIC, msg)) {
Serial.println("MQTT Publish failed");
}
}

// Pause - but keep polling MQTT for incoming messages
for (int i = 0; i < 4; i++) {
mqtt.loop();
delay(1000);
}
}

void printData() {
Serial.println("Board Information:");
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

Serial.println();
Serial.println("Network Information:");
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);

byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}

0 comments on commit c85c0c3

Please sign in to comment.