Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.6 kB
5
Indexable
Never

#include <LiquidCrystal.h>

int seconds = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int red_led = 7;
int yellow_led = 8;
int green_led = 13;
int gas = A0;
int gas_data;
float value; int tmp = A0;

String gas_state;


String ssid     = "Simulator Wifi";  // SSID to connect to
String password = ""; // Our virtual wifi has no password 
String host     = "api.thingspeak.com"; // Open Weather Map API
const int httpPort   = 80;
String uri     = "/update?api_key=6LS3USTX91T2FKOO&field1=";

int setupESP8266(void) {

 Serial.begin(115200);
Serial.println("AT"); delay(10);
 if (!Serial.find("OK")) return 1;


 Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
delay(10); if (!Serial.find("OK")) return 2;

 // Open TCP connection to the host:
 Serial.println("AT+CIPSTART=\"TCP\",\"" + host + "\"," + httpPort);
delay(50); // Wait a little for the ESP to respond if
(!Serial.find("OK")); return 3;

 return 0;
}


void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(tmp,INPUT);

pinMode(red_led,OUTPUT);

pinMode(yellow_led,OUTPUT);
pinMode(green_led,OUTPUT);
pinMode(gas,INPUT);
  
setupESP8266();

}

void loop() {
value = analogRead(tmp)*0.004882814;
value = (value - 0.5) * 100.0;
lcd.setCursor(0,1); lcd.print("Tmp:");
lcd.print(value); delay(1000);
lcd.clear();

  
digitalWrite(red_led,LOW);
digitalWrite(yellow_led,LOW);
digitalWrite(green_led,LOW);
gas_data = analogRead(gas);
lcd.setCursor(00,00);
lcd.print("Gas:");
lcd.setCursor(5,00);
lcd.print(gas_data);

if(gas_data > 800){
digitalWrite(red_led,HIGH);
gas_state = "DANGER";

}
else if(gas_data > 700){
digitalWrite(yellow_led,HIGH);
gas_state = "WARNING";

}else {
digitalWrite(green_led,HIGH);
gas_state = "SAFE";
}

lcd.setCursor(00,00);
lcd.print("Gas:");
lcd.setCursor(5,00);
lcd.print(gas_data);
lcd.setCursor(00,1);
lcd.print(gas_state);

Serial.println(gas_data);
delay(1000);
lcd.clear();
  
  
  
String httpPacket = "GET " + uri + String(value) + "&field2=" + String(gas_data)+ "&field3=" + String(gas_state) + " HTTP/1.1\r\nHost: " + host +
"\r\n\r\n"; int length =
httpPacket.length();

  // Send our message length
  Serial.print("AT+CIPSEND=");
  Serial.println(length);
  delay(10); // Wait a little for the ESP to respond if (!Serial.find(">")) return -1;

  // Send our http request
  Serial.print(httpPacket);
  delay(10); // Wait a little for the ESP to respond
  if (!Serial.find("SEND OK\r\n")) return;
  
  delay(1024);
}