teasense/teasense.ino

142 lines
4.0 KiB
Arduino
Raw Permalink Normal View History

2023-12-15 01:16:41 +00:00
/*
* teasense.ino - Arduino code for the tea sensor
* Copyright (C) 2023 William Moore
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Coded for the Arduino Uno WiFi rev. 2
// Uses a standard PT100 RTD and the Adafruit MAX 31865 interface.
#include <SPI.h>
#include <WiFiNINA.h>
#include <Adafruit_MAX31865.h>
// Create a file called config.h that contains the following:
// char ssid[] = "SSID";
// char pass[] = "Password";
#include "config.h"
// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31865 thermo = Adafruit_MAX31865(10, 11, 12, 13);
#define RREF 430.0
// The 'nominal' 0-degrees-C resistance of the sensor
// 100.0 for PT100, 1000.0 for PT1000
#define RNOMINAL 100.0
int status = WL_IDLE_STATUS; //connection status
const int port = 80;
WiFiServer server(port);
void setup() {
Serial.begin(9600);
thermo.begin(MAX31865_3WIRE); // set to 2WIRE or 4WIRE as necessary
Serial.println("Welcome to teasense!");
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.setHostname("teasensor");
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
IPAddress gw = WiFi.gatewayIP();
Serial.print("Gateway IP: ");
Serial.println(gw);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void enable_WiFi() {
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}
Serial.println(fv);
}
void connect_WiFi() {
// attempt to connect to Wifi network:
// while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.disconnect();
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(5000);
// }
}
int resetCounter = 0;
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
if (client.connected()) {
Serial.println("Reading data from sensor");
String postData = "{\"temperature\": ";
postData += thermo.temperature(RNOMINAL, RREF);
postData += "}";
// Serial.println(postData);
String httpResponse = "HTTP/1.1 200 OK\r\n";
httpResponse += "Content-type: application/json\r\n";
httpResponse += "Connection: close\r\n\r\n";
httpResponse += postData;
client.print(httpResponse);
Serial.println("HTTP Data Written");
}
delay(1);
Serial.println("Data written");
client.stop();
}
}