Commit 75bb0870 authored by louis.colclough's avatar louis.colclough

Add new file

parents
#include <math.h>
#include <Wire.h>
#include "rgb_lcd.h"
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
#include "ThingSpeak.h"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
unsigned long myChannelNumber = 2504682;
const char * myWriteAPIKey = "SNN8Y9Q55L68MOY5";
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char topic[] = "st_john_iot_temperature_value_topic";
rgb_lcd lcd;
const int colorR = 0;
const int colorG = 0;
const int colorB = 255;
const int B = 4275;
const int R0 = 100000;
const int pinTempSensor = A0;
const long interval = 8000;
unsigned long previousMillis = 0;
int count = 0;
#if defined(ARDUINO_ARCH_AVR)
#define debug Serial
#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
#define debug SerialUSB
#else
#define debug Serial
#endif
void setup() {
// put your setup code here, to run once:
//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
}
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
ThingSpeak.begin(wifiClient);
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
Serial.begin(9600);
pinMode(6, OUTPUT);
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
}
void loop() {
// put your main code here, to run repeatedly:
int a = analogRead(pinTempSensor);
float R = 1023.0/a-1.0;
R = R0*R;
float temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15; // convert to temperature via datasheet
ThingSpeak.setField(2, temperature);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (temperature >= 29){
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
delay(1000);
}
else {digitalWrite(6,LOW);}
lcd.setCursor(0, 1);
lcd.print(temperature);
delay(100);
delay(100);
// call poll() regularly to allow the library to send MQTT keep alive which
// avoids being disconnected by the broker
mqttClient.poll();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.println(temperature);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print(temperature);
mqttClient.endMessage();
Serial.println();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment