Last active
February 6, 2021 09:17
-
-
Save jenschr/44511a6803abc2cc3b5dca02227e4d15 to your computer and use it in GitHub Desktop.
Simple example showcasing how to publish data from the HiGrow board to Thingspeak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Simple example showcasing how to publish data from the HiGrow board to Thingspeak | |
* For this to work, you'll need to create a file called "secrets.h" that holds the | |
* credentials you find on Thingspeak. These 4 lines are what is required in secrets.h | |
* | |
* #define SECRET_CH_ID 1234 | |
* #define SECRET_WRITE_APIKEY "KLLJNKLJHGI" | |
* #define SECRET_SSID "yourssid" | |
* #define SECRET_PASS "yourpasswd" | |
* | |
* It also holds the ID to your Thingspeak channel that should be setup to receive 4 fields. | |
* | |
* Details on the hardware and how to use it can be found on: | |
* http://flashgamer.com/blog/comments/higrow-esp32-moisture-and-temperature-sensor#capacitive | |
*/ | |
#include "ThingSpeak.h" | |
#include "secrets.h" | |
#include "DHT.h" | |
#include <WiFi.h> | |
#define DHTTYPE DHT11 | |
const int LEDPIN = 16; | |
const int DHTPIN = 22; | |
const int SOILPIN = 32; | |
unsigned long myChannelNumber = SECRET_CH_ID; | |
const char * myWriteAPIKey = SECRET_WRITE_APIKEY; | |
char ssid[] = SECRET_SSID; // your network SSID (name) | |
char pass[] = SECRET_PASS; // your network password | |
int keyIndex = 0; // your network key index number (needed only for WEP) | |
WiFiClient client; | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(115200); | |
pinMode(LEDPIN,OUTPUT); | |
delay(100); | |
WiFi.mode(WIFI_STA); | |
ThingSpeak.begin(client); | |
} | |
void loop() { | |
// Connect or reconnect to WiFi | |
if (WiFi.status() != WL_CONNECTED) { | |
digitalWrite(LEDPIN, LOW); | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(SECRET_SSID); | |
while (WiFi.status() != WL_CONNECTED) { | |
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network | |
Serial.print("."); | |
delay(5000); | |
} | |
Serial.print("\nConnected with IP "); | |
Serial.println(WiFi.localIP()); | |
digitalWrite(LEDPIN, HIGH); | |
} | |
int rssi = WiFi.RSSI(); | |
int waterlevel = analogRead(SOILPIN); | |
float humidity = dht.readHumidity(); | |
float temperature = dht.readTemperature(); | |
float hic = dht.computeHeatIndex(temperature, humidity, false); | |
ThingSpeak.setField(1, rssi); | |
ThingSpeak.setField(2, humidity); | |
ThingSpeak.setField(3, waterlevel); | |
ThingSpeak.setField(4, hic); | |
writeToThingSpeak(); | |
delay(20000); | |
} | |
void writeToThingSpeak() | |
{ | |
// Write value to Field 1 of a ThingSpeak Channel | |
int httpCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); | |
if (httpCode == 200) { | |
Serial.println("Wrote to ThingSpeak successfully"); | |
} else { | |
Serial.println("Problem writing to channel. HTTP error code " + String(httpCode)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment