I am attempting to combine sketch 1 and sketch 2 into a single sketch 3. I am having difficulty getting past void setup. Sketch 3 compiles and uploads fine but wifi crashes when it runs.
sketch1:
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
const char* ssid = "netis_2_4G";
const char* password = "xxxxx";
// ThingSpeak information
char thingSpeakAddress[] = "api.thingspeak.com";
unsigned long channelID = xxxx;
char* readAPIKey = "xxxx";
char* writeAPIKey = "xxxxxx";
const unsigned long postingInterval = 120L * 1000L;
unsigned int dataFieldOne = 1; // Field to write temperature data
unsigned int dataFieldTwo = 2; // Field to write temperature data
unsigned int dataFieldThree = 3; // Field to write elapsed time data
unsigned int aField = 4; //Field to hold first constant of the thermistor calibration
unsigned int bField = 5; //Field to hold second constant of the thermistor calibration
unsigned int cField = 6; //Field to hold third constant of the thermistor calibration
// Global variables
// These constants are device specific. You will need to get them from the manufacturer or determine them yourself.
float aConst = 0.001129148;
float bConst = 0.000234125;
float cConst = 0.0000000876741;
unsigned long lastConnectionTime = 0;
long lastUpdateTime = 0;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Start");
connectWiFi();
// Read the constants at startup.
aConst = readTSData( channelID, aField );
bConst = readTSData( channelID, bField );
cConst = readTSData( channelID, cField );
}
void loop() {
// Only update if posting time is exceeded
if (millis() - lastUpdateTime >= postingInterval) {
float fahrenheitTemperature, celsiusTemperature;
lastUpdateTime = millis();
float readValue = analogRead(A0);
float logR = log( 10000 * ( 1024 / readValue - 1 )); // Separate the calculation for simplicity and debugging
celsiusTemperature = 1 / ( aConst + bConst * logR + cConst * pow(logR,3) ) - 273.15; // Calculate the temperature in Celsius
fahrenheitTemperature = celsiusTemperature * 9 / 5 + 32;
Serial.println("ADC = " + String( readValue )+ " Temp = "+String( fahrenheitTemperature ));
write2TSData( channelID , dataFieldOne , celsiusTemperature , dataFieldTwo , fahrenheitTemperature , dataFieldThree , millis() ); // Write the temperature in F, C, and time since starting.
}
}
int connectWiFi(){
WiFi.begin(ssid, password );
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Error connecting to WiFi");
while (true) {
delay(1);
}
}
ThingSpeak.begin( client );
}
float readTSData( long TSChannel,unsigned int TSField ){
float data = ThingSpeak.readFloatField( TSChannel, TSField, readAPIKey );
Serial.println( " Data read from ThingSpeak: " + String( data, 9 ) );
return data;
}
// Use this function if you want to write a single field
int writeTSData( long TSChannel, unsigned int TSField, float data ){
int writeSuccess = ThingSpeak.writeField( TSChannel, TSField, data, writeAPIKey ); // Write the data to the channel
if ( writeSuccess ){
Serial.println( String(data) + " written to Thingspeak." );
}
return writeSuccess;
}
//use this function if you want multiple fields simultaneously
int write2TSData( long TSChannel, unsigned int TSField1, float field1Data, unsigned int TSField2, long field2Data, unsigned int TSField3, long field3Data ){
ThingSpeak.setField( TSField1, field1Data );
ThingSpeak.setField( TSField2, field2Data );
ThingSpeak.setField( TSField3, field3Data );
int writeSuccess = ThingSpeak.writeFields( TSChannel, writeAPIKey );
return writeSuccess;
}
Sketch 2
#include <math.h>
int pinOut = 8;
double Thermistor(int RawADC) {
double Temp;
Temp = log(10000.0*((1024.0/RawADC-1)));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15;
Temp = (Temp * 9.0)/ 5.0 + 32.0;
return Temp;
}
void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT);
}
void loop() {
int val;
double temp;
val=analogRead(0);
temp=Thermistor(val);
Serial.print("Temperature = ");
Serial.print(temp);
Serial.println(" F");
if (temp >= 150){
digitalWrite(pinOut, LOW);
}
else {
digitalWrite(pinOut, HIGH);
}
delay(500);
}
Adding the following to sketch 3 is what seems to crash wifi: (pinMode(8, OUTPUT);)
I have been adding code in increments so I can be aware of what may break things.
Here is what I have for sketch 3 so far: (see attachment)
sketch3.txt (4.09 KB)