Re: New build, work in progress.
Posted: Wed Sep 23, 2020 7:04 am
About a week left. I think I finally made a dent in the gnats problem, it's gnat funny how many there were.
A friendly LED grow light community.
https://ledgardener.com/forum/
Code: Select all
//for WIFI
#include <ESP8266WiFi.h>
const char* ssid = "teehee"; //Enter SSID
const char* password = "poopoo"; //Enter Password
IPAddress staticIP(192,168,2,50);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,0,0);
//for i2c
#include <Wire.h>
//for MQTT
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
const char* mqtt_server = "192.168.2.1";
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
char tempString[8];
//for soil moisture
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);
//for am2315
#include <Adafruit_AM2315.h>
Adafruit_AM2315 am2315;
//this device's name
const char* espName = "espOne";
//loop delay ms
const int interval = 5000;
void wifi_setup() {
WiFi.persistent( false );
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet);
WiFi.setOutputPower(10.0); //0 to 20.5
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
}
void i2c_setup()
{
Wire.begin();
ads.begin();
if (! am2315.begin()) {
Serial.println("am2315 not found, check wiring & pullups!");
while (1);
}
}
void mqtt_connect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(espName)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "espOne Online");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void mqtt_setup()
{
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
mqtt_connect();
}
void read_ads()
{
int16_t adc0, adc1, adc2, adc3;
adc0 = ads.readADC_SingleEnded(0);
adc1 = ads.readADC_SingleEnded(1);
adc2 = ads.readADC_SingleEnded(2);
adc3 = ads.readADC_SingleEnded(3);
dtostrf(adc0, 4, 1, tempString);
client.publish("soil/moisture/0", tempString);
dtostrf(adc1, 4, 1, tempString);
client.publish("soil/moisture/1", tempString);
dtostrf(adc2, 4, 1, tempString);
client.publish("soil/moisture/2", tempString);
dtostrf(adc3, 4, 1, tempString);
client.publish("soil/moisture/3", tempString);
}
void read_am2315()
{
float temperature, humidity;
if (! am2315.readTemperatureAndHumidity(&temperature, &humidity)) {
Serial.println("Failed to read data from AM2315");
return;
}
Serial.print("Temp *C: "); Serial.println(temperature);
Serial.print("Hum %: "); Serial.println(humidity);
dtostrf(temperature, 4, 1, tempString);
client.publish("canopy/temperature", tempString);
dtostrf(humidity, 4, 1, tempString);
client.publish("canopy/humidity", tempString);
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
//this is where we received a message and need to reply?
}
void setup()
{
Serial.begin(115200);
wifi_setup();
i2c_setup();
mqtt_setup();
}
void loop() {
unsigned long lastMsg = 0;
unsigned long now = 0;
if (WiFi.status() != WL_CONNECTED) {
wifi_setup();
}
if (!client.connected()) {
mqtt_connect();
}
now = millis();
if (now - lastMsg > interval) {
lastMsg = now;
read_ads();
read_am2315();
}
client.loop();
}