// LCD i2c (Four Pin LCD sreen) // use pins 20 and 21 on MEGA #include #include #include // get new LCD library from: https://bitbucket.org/fmalpartida/new-liquidcrystal // DHT22 #include dht DHT; #define DHT22_PIN 6 // WiFi #define SSID "BTHub4-S5TP" #define PASS "e8525df9b5" // the ESP8266 should flash a blue LED when the Arduino flashes the TX on its board. // if the ESP8266 doesn't connect to WiFi unplug/plug the RST pin which goes to 3.3V //LCD #define I2C_ADDR 0x3F // <<----- Add your address here. Find it using I2C Scanner: http://arduino.cc/playground/Main/I2cScanner #define BACKLIGHT_PIN 3 #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); // Solid State Relay #Ch1 - Channels are mislabelled on the input. That's helpful. #define ssr1 53 int state = HIGH; // starting state is off // LEDS int power = 43; int red = 47; int blue = 39; void setup() { Serial.begin(115200); // dht22 Serial2.begin(9600); // wifi pinMode(ssr1, OUTPUT); // SSR pinMode(power, OUTPUT); // LED pinMode(red, OUTPUT); // LED pinMode(blue, OUTPUT); // LED digitalWrite(ssr1, state); // start with SSR off digitalWrite(power, HIGH); // power on LED lcd.begin (16,2); // LCD is 16x2 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); // Switch on the backlight lcd.setBacklight(HIGH); lcd.home(); // go home lcd.print("Screen OK"); // print to screen delay(2000); Serial.println("Type,\tstatus,\tHum. (%),\tTemp. ('C)"); //WiFi setup Serial2.println("AT"); Serial.println("AT sent - checking..."); lcd.clear(); lcd.home (); lcd.print("AT sent."); delay(1000); lcd.setCursor(0,2); lcd.print("Checking... "); delay(3000); char okcheck[]="OK"; if(Serial2.find(okcheck)){ Serial.println("OK, found. Connecting"); lcd.clear(); lcd.home (); lcd.print("OK. Found"); lcd.setCursor(0,2); lcd.print("Connecting... "); connectWiFi(); Serial.println("Yay! Should be connected now."); lcd.clear(); lcd.home (); lcd.print("Connected!"); } else{ Serial.println("NOT CONNECTED TO WIFI"); lcd.clear(); lcd.home (); lcd.print("NOT CONNECTED"); } } //========================================= void loop() { // read from DHT22 TempHum(); // data for Thingspeak: String SendTempLevel = String((float)DHT.temperature, 0); String SendHumLevel = String((float)DHT.humidity, 0); // send to Thingspeak updateTemp(SendTempLevel, SendHumLevel); // temperature check if (DHT.temperature > 24 ) { state=HIGH; // switch heat mat on digitalWrite(red, LOW); // heating on LED digitalWrite(blue, HIGH); // heating off LED } if (DHT.temperature < 24) { state=LOW; // switch heat mat off digitalWrite(red, HIGH); // heating on LED digitalWrite(blue, LOW); // heating off LED } // set SSR accoding to temperature digitalWrite(ssr1, state); delay(18000); // 18sec delay to not hammer ThingSpeak } //=========================================== void TempHum(){ // READ DATA Serial.print("DHT22, \t"); int chk = DHT.read22(DHT22_PIN); switch (chk) { case DHTLIB_OK: Serial.print("OK,\t"); break; case DHTLIB_ERROR_CHECKSUM: Serial.print("Checksum error,\t"); break; case DHTLIB_ERROR_TIMEOUT: Serial.print("Time out error,\t"); break; default: Serial.print("Unknown error,\t"); break; } // DISPLAY DATA TO SERIAL Serial.print(DHT.humidity, 1); Serial.print(",\t\t"); Serial.println(DHT.temperature, 1); // DISPLAY DATA TO LCD lcd.clear(); lcd.home (); lcd.print("Temp: "); lcd.setCursor(5,0); lcd.print(DHT.temperature, 0); lcd.setCursor(8,0); lcd.print("Hum: "); lcd.setCursor(12,0); lcd.print(DHT.humidity, 0); } //========================================== void updateTemp(String SendTempLevel, String SendHumLevel){ String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += "184.106.153.149"; // api.thingspeak.com cmd += "\",80"; Serial2.println(cmd); char errorcheck[]="Error"; if(Serial2.find(errorcheck)){ Serial.println("AT+CIPSTART error"); return; } // prepare GET string String getStr = "GET /update?api_key="; getStr += "8KS0CVMQ12A7D817"; getStr += "&field1="; getStr += String(SendTempLevel); getStr += "&field2="; getStr += String(SendHumLevel); getStr += "\r\n\r\n"; // prepare to send data cmd = "AT+CIPSEND="; cmd += String(getStr.length()); Serial2.println(cmd); // send data char greaterthanchar[]=">"; if(Serial2.find(greaterthanchar)){ Serial2.print(getStr); Serial.println("Yay! String should be sent."); lcd.setCursor(0,2); lcd.print("String sent!"); }else{ Serial2.println("AT+CIPCLOSE"); Serial.println("Oops! - AT+CIPCLOSE"); // seems to not send first time, but OK afterwards lcd.setCursor(0,2); lcd.print("String NOT sent!"); } } //=============================== boolean connectWiFi(){ Serial2.println("AT+CWMODE=1"); delay(2000); String cmd="AT+CWJAP=\""; // add SSID and password cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\""; // send string Serial2.println(cmd); delay(5000); // was the login accepted? char okcheck[]="OK"; if(Serial2.find(okcheck)){ Serial.println("Login accepted"); return true; }else{ Serial.println("Login not accepted."); return false; } }