I have an arduino uno that runs the code below minus the WiFi parts with the correct temperature readings (around 75°). However when I take the code and copy it to a sketch for the MKR 1010 board the readings from the Temp sensor read around 156°F. I use the 5V and GND pins to power the temp sensor from the MKR 1010 and plug the other wire from the temp sensor to A5 (I've tried also tried A0 and A1 all roughly the same outcome). I have even tried powering the temp sensor with the arduino uno but running the sensing wire to the MKR 1010. I'm new at this so I'm not sure if it is the hardware, does the MKR1010 have a different range for the analog pins? Do I need to multiple the Thermistor value by something other than 0.004882814? Any guidance would be helpful.
Could I connect the MKR 1010 board to the Uno board so I can read the value from the Uno board and then upload the value with the MKR 1010? Completely lost on how to do that, so I was trying the above instead.
#include <SPI.h>
#include <WiFiNINA.h>
//#include <WiFi.h>
#include <Time.h>
#include <TimeLib.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "localhost";
int port = 44321;
WiFiClient client;
const int ThermistorPin = A5; //USE Pin A0 on arduino board for data input change if Pin number changes
// Conversion constant converts analog reading, which varies from 0 to 1023 back to a voltage value from 0-5 volts
const double convertVolt = 0.004882814;
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
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA2 SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void loop() {
// read Temp sensor voltage and send to cloud database repeat every 1 second or 15 seconds?
const int readWaitTime = 15000; //5 seconds - value in milliseconds
double sensorValue = 0; // Clear previous sensor reading
double calculatedTemp = 0;
sensorValue = analogRead(ThermistorPin) * convertVolt; // Get reading from thermistor and convert back to a voltage
Serial.print("Direct Read: ");
Serial.println(analogRead(ThermistorPin));
Serial.print("Converted value to voltage: ");
Serial.println(sensorValue);
//Get Temperature from voltage
calculatedTemp = convertVoltageToTemp(sensorValue);
Serial.print("Calculated Temp - ");
Serial.println(calculatedTemp);
//Send sensor data to the web API
if (calculatedTemp > -40 && calculatedTemp < 257) { //lower and upper bound of thermistor
writeDb(calculatedTemp);
}
delay(readWaitTime);
}
double convertVoltageToTemp (double Voltage) { //0V to 5V range based on hardware
// Input voltage - output temperature for saving back to DB
// Convert from celsius to fahrenheit? F= 9/5*C+32
const double vOffset = 0.5;
const int degreeMultiplier = 100;
double degreesC = 0;
double degreesF = 0;
degreesC = (Voltage - vOffset) * degreeMultiplier;
degreesF = convertTempToFahrenheit(degreesC);
return degreesF;
// return degreesC; Comment out "degreesF = convertTemp..." and "return degreesF" to get temp in celsius
}
double convertTempToFahrenheit (double Celsius) {
const double DConstant = 1.8; //Math constant for conversion formula (9/5) * Celsius + 32
const int FConstant = 32; //Math constant for conversion formula
double Temp = 0;
Temp = DConstant * Celsius + FConstant;
return Temp;
}
void writeDb (double Temp) {
//write value to database - need timestamp and degree value
const int collectionPointId = 1; //Just picked the first collectionPointId since it is just for prototyping
String PostData = ""; //Clear PostData
time_t timestamp = now(); //Get current time
// Used Postman then clicked on "Code" to get the data portion of the request. Changed dropdown to C (LibCurl) example from Postman - curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\n\"collectionPointID\": 1,\n\"timestamp\": \"2018-10-09T1:09:01\",\n\"temperature\": 70.1\n}");
PostData = "{\n\"collectionPointID\": " ;
PostData += collectionPointId;
PostData += ",\n\"timestamp\": \"";
PostData += timestamp;
PostData += "\",\n\"temperature\": ";
PostData += Temp;
PostData += "\n}";
if (status != WL_CONNECTED) {
Serial.println("Lost WiFi connection");
while (true); //Stop program inifinite loop
}
else {
Serial.println("Connected to WiFi... Starting connection to the server...");
if (client.connect(server, port)) {
Serial.println("connected to the server");
// Make the request client.println prints the data to the server
client.println("POST /apiSonsorsAPI HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.print("content-type: ");
client.println("application/json");
client.print("Content-Length: ");
client.println(PostData.length());
client.println(); //closes header
client.println(PostData);
client.stop();
}
else{
Serial.println("not connected to server");
}
}
//Testing
Serial.print("timestamp: ");
Serial.println(timestamp);
Serial.print(" - temperature: ");
Serial.println(PostData);
}