Last active
September 28, 2020 15:52
-
-
Save xxlukas42/5f30cf80131b9e6d7e8824eae13d170a to your computer and use it in GitHub Desktop.
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
#include <SPI.h> | |
#include <ESP8266WiFi.h> | |
#include <Wire.h> | |
#include <SFE_MicroOLED.h> | |
#define PIN_RESET 255 // | |
#define DC_JUMPER 0 // I2C Addres: 0 - 0x3C, 1 - 0x3D | |
MicroOLED oled(PIN_RESET, DC_JUMPER); | |
// Wifi and ThingSpeak settings | |
const char* ssid = "YourSSID"; | |
char* password = "YourPassword"; | |
const char* server = "api.thingspeak.com"; | |
const char* api_key = "YourApiKey"; | |
// Measurement interval (seconds) | |
const int sleepTimeS = 10; // 10 secs | |
#define LED D4 | |
int moisture, light, i; | |
WiFiClient client; | |
void printFormattedFloat(float x, uint8_t precision) { | |
char buffer[10]; | |
dtostrf(x, 7, precision, buffer); | |
Serial.print(buffer); | |
} | |
void postChirp(int moisture, int light){ | |
if (client.connect(server,80)) { | |
Serial.println("Connect to ThingSpeak - OK"); | |
String dataToThingSpeak = ""; | |
dataToThingSpeak+="GET /update?api_key="; | |
dataToThingSpeak+=api_key; | |
dataToThingSpeak+="&field1="; | |
dataToThingSpeak+=String(moisture); | |
dataToThingSpeak+="&field2="; | |
dataToThingSpeak+=String(light); | |
dataToThingSpeak+=" HTTP/1.1\r\nHost: a.c.d\r\nConnection: close\r\n\r\n"; | |
dataToThingSpeak+=""; | |
client.print(dataToThingSpeak); | |
int timeout = millis() + 5000; | |
while (client.available() == 0) { | |
if (timeout - millis() < 0) { | |
Serial.println("Error: Client Timeout!"); | |
client.stop(); | |
return; | |
} | |
} | |
} | |
} | |
void writeI2CRegister8bit(int addr, int value) { | |
Wire.beginTransmission(addr); | |
Wire.write(value); | |
Wire.endTransmission(); | |
} | |
unsigned int readI2CRegister16bit(int addr, int reg) { | |
Wire.beginTransmission(addr); | |
Wire.write(reg); | |
Wire.endTransmission(); | |
delay(1100); | |
Wire.requestFrom(addr, 2); | |
unsigned int t = Wire.read() << 8; | |
t = t | Wire.read(); | |
return t; | |
} | |
// Setup wire and serial | |
void setup(){ | |
Wire.begin(); | |
Serial.begin(9600); | |
pinMode(LED, OUTPUT); | |
delay(10); | |
oled.begin(); | |
oled.clear(PAGE); | |
oled.clear(ALL); | |
oled.setCursor(0, 0); | |
oled.print("WLAN: "); | |
oled.display(); | |
Serial.println("Connecting to wifi..."); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED){ | |
// Blink LED when connecting to wifi | |
digitalWrite(LED, LOW); | |
oled.setCursor(30, 0); | |
oled.print("ASSOC"); | |
oled.display(); | |
delay(250); | |
digitalWrite(LED, HIGH); | |
oled.setCursor(30, 0); | |
oled.print(" "); | |
oled.display(); | |
delay(250); | |
} | |
oled.setCursor(30, 0); | |
oled.print(" "); | |
oled.setCursor(30, 0); | |
oled.print("OK"); | |
oled.display(); | |
Serial.println("WiFi connected"); | |
oled.display(); | |
oled.setCursor(0, 10); | |
oled.print("SWITCH NOW"); | |
oled.display(); | |
while(i < 10){ | |
writeI2CRegister8bit(0x20, 3); | |
light = readI2CRegister16bit(0x20, 4); | |
oled.setCursor(0, 20); | |
oled.print(light); | |
oled.display(); | |
i++; | |
delay(100); | |
} | |
oled.clear(PAGE); | |
oled.clear(ALL); | |
} | |
// main loop | |
void loop(){ | |
Serial.print("Moisture: "); | |
moisture = readI2CRegister16bit(0x20, 0); | |
Serial.println(moisture); //read capacitance register | |
writeI2CRegister8bit(0x20, 3); //request light measurement | |
delay(10000); //this can take a while | |
Serial.print("Light: "); | |
light = readI2CRegister16bit(0x20, 4); | |
Serial.println(light); //read light register | |
Serial.println("============="); | |
oled.clear(PAGE); | |
oled.clear(ALL); | |
oled.setCursor(0, 10); | |
oled.print("MOI: "); | |
oled.print(moisture); | |
oled.setCursor(0, 20); | |
oled.print("LUM: "); | |
oled.print(light); | |
oled.display(); | |
delay(38900); | |
postChirp(moisture, light); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment