I have made the code for a humidity and temperature controller that are supposed to send data to ThingSpeak as well.
Here is how the code looks like:
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 0 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Uncomment to get temperatures in fahrenheit
//#define USE_FAHRENHEIT
#define WARMRELAYPIN 2
#define COOLRELAYPIN 3
#define HUMIDRELAYPIN 4
#define DRYRELAYPIN 5
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
#include "ThingSpeak.h"
// ***********************************************************************************************************
// This example selects the correct library to use based on the board selected under the Tools menu in the IDE.
// Yun, Ethernet shield, WiFi101 shield, esp8266, ESP32, and MKR1000 are all supported.
// With Yun, the default is that you're using the Ethernet connection.
// If you're using a wi-fi 101 or ethernet shield (http://www.arduino.cc/en/Main/ArduinoWiFiShield), uncomment the corresponding line below
// ***********************************************************************************************************
#include <ESP8266WiFi.h>
const char ssid[] = "TAH";
const char pass[] = "28.Januar1951";
unsigned long ChannelNumber = 557790;
char WriteAPIkey[] = "A71VMEADZDSF5ISQ";
char ReadAPIKey[] = "JQ05PV694PGR4L3A";
void setup() {
Serial.begin(9600);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("DHTxx test!");
pinMode(WARMRELAYPIN, OUTPUT);
pinMode(COOLRELAYPIN, OUTPUT);
pinMode(HUMIDRELAYPIN, OUTPUT);
pinMode(DRYRELAYPIN, OUTPUT);
digitalWrite(WARMRELAYPIN, HIGH);
digitalWrite(COOLRELAYPIN, HIGH);
digitalWrite(HUMIDRELAYPIN, HIGH);
digitalWrite(DRYRELAYPIN, HIGH);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(1000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
//Now send the data to ThingSpeak
ThingSpeak.setField(1,t);
ThingSpeak.setField(2,h);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
delay(500);
if (t <= 12.00){
digitalWrite(WARMRELAYPIN, LOW);
} else {
digitalWrite(WARMRELAYPIN, HIGH);
}
if (t >= 13.00){
digitalWrite(COOLRELAYPIN, LOW);
} else {
digitalWrite(COOLRELAYPIN, HIGH);
}
if (h < 72){
digitalWrite(HUMIDRELAYPIN, LOW);
} else {
digitalWrite(HUMIDRELAYPIN, HIGH);
}
if (h > 73){
digitalWrite(DRYRELAYPIN, LOW);
} else {
digitalWrite(DRYRELAYPIN, HIGH);
}
delay(1000);
ThingSpeak.writeFields(ChannelNumber, WriteAPIkey);
}
When running the code, however, I get this:
SDK:2.2.1(cfd48f3)/Core:2.4.2/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1-13-g163bb82)/BearSSL:6d1cefc
Connecting to
TAH
scandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 3
cnt
.
connected with TAH, channel 13
dhcp client start...
ip:192.168.1.137,mask:255.255.255.0,gw:192.168.1.1
.
WiFi connected
DHTxx test!
Humidity: 50.20 % Temperature: 24.10 *C 75.38 *F Heat index: 23.88 *C 74.98 *F
Fatal exception 28(LoadProhibitedCause):
epc1=0x40202a99, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
Exception (28):
epc1=0x40202a99 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: cont
sp: 3ffffcc0 end: 3fffffd0 offset: 01a0
>>>stack>>>
3ffffe60: 0000005c 00000028 0000005d 00000077
3ffffe70: 33323130 37363534 62613938 3ffeeafc
3ffffe80: 0000002c 3fffff2c 3ffeedd8 3ffeedd8
3ffffe90: 00000020 00000000 0000002c 401009c1
3ffffea0: 3ffffee0 00000001 3fffff2c 40204e11
3ffffeb0: 00000002 3fffff38 3fffff2c 3ffeeb50
3ffffec0: 00000020 3fffff20 3fffff20 40204d93
3ffffed0: 3fff0244 0000001f 3fffff20 3ffeeb50
3ffffee0: 3ffe84dc 3fffff20 3ffeeadc 40202c8c
3ffffef0: 00000009 00000000 3fffff20 40204ec4
3fffff00: 3ffe8b44 3fffff38 3ffeeadc 40204eec
3fffff10: 00000009 00000000 3ffeeadc 402032a1
3fffff20: 3fff01d4 0000001f 0000001f 00000000
3fffff30: 00000000 00000000 00000000 00000000
3fffff40: 00000000 00000000 00000000 00000000
3fffff50: 3fff0244 0000001f 0000001f 3ffeecf0
3fffff60: 000882de 3ffe84dc 402051f8 3fffefb0
3fffff70: 40201792 000003e8 000003e8 4296c28f
3fffff80: 41c0cccd 4248cccd 3ffeeadc 402034fd
3fffff90: 41bf023c 4295f46d 00000000 401069e9
3fffffa0: 3ffe8b6c 00000003 3ffeebdc 3ffeec9c
3fffffb0: 3fffdad0 00000000 3ffeec94 40205284
3fffffc0: feefeffe feefeffe 3ffe85f4 40100a49
<<<stack<<<
⸮⸮⸮K⸮
When I comment on line "ThingSpeak.writeFields(ChannelNumber, WriteAPIkey);", the issue will disappear, but will not upload to ThingSpeak. How do I work around this?