Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "EmonLib.h"
- #include <LiquidCrystal.h>
- #include "WiFiEsp.h"
- #include <DHT.h>
- EnergyMonitor emon1;
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- //Hálózati Feszültség
- int rede = 224.0;
- //SCT PIN
- int pino_sct = 1;
- // DHT CONFIGURE
- #define DHTPIN 10 // what digital pin we're connected to
- #define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
- DHT dht(DHTPIN, DHTTYPE);
- float temp,humidity=0;
- // Emulate Serial1 on pins 6/7 if not present
- #ifndef HAVE_HWSERIAL1
- #include "SoftwareSerial.h"
- SoftwareSerial Serial1(8, 9); // RX, TX
- #endif
- //SoftwareSerial Serial1(8, 9); // RX, TX
- char ssid[] = "WifiSID"; // your network SSID (name)
- char pass[] = "Pass"; // your network password
- int status = WL_IDLE_STATUS; // the Wifi radio's status
- // Initialize the Ethernet client object
- char server[] = "emoncms.org";
- const char* nodeId = "2";
- const char* privateKey = "123123123das123";
- WiFiEspClient client;
- // SAJ�T ID�ZÍT�
- unsigned long previousMillis = 0;
- const long interval = 10000;
- void startLcd(){
- lcd.begin(16,2);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Booting Module..");
- lcd.setCursor(0,1);
- lcd.print("v1.0 2016");
- }
- void printMainLcd(){
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("A:");
- lcd.setCursor(8,0);
- lcd.print("W:");
- lcd.setCursor(0,1);
- lcd.print("IP:");
- lcd.setCursor(8,1) ;
- lcd.print("T:");
- }
- void initCTSensors(){
- //Pino, calibracao - Cur Const= Tekercsszám/Ellenállás 2000/94 = 21.
- emon1.current(pino_sct, 21);
- }
- void checkWifi(){
- // attempt to connect to WiFi network
- while ( status != WL_CONNECTED) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
- // Connect to WPA/WPA2 network
- status = WiFi.begin(ssid, pass);
- }
- }
- void initWifi(){
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
- lcd.setCursor(0,1);
- lcd.print("ERROR: NO ESP");
- // don't continue
- while (true);
- }
- checkWifi();
- // you're connected now, so print out the data
- Serial.println("You're connected to the network");
- lcd.setCursor(0,1);
- lcd.print("ESP Connected ");
- delay(3000);
- printWifiStatus();
- }
- void measureTempHumidity(){
- float h = dht.readHumidity();
- // Read temperature as Celsius (the default)
- float t = dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- float f = dht.readTemperature(true);
- // Check if any reads failed and exit early (to try again).
- if (isnan(h) || isnan(t) || isnan(f)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- temp =t;
- humidity=h;
- Serial.print("Humidity: ");
- Serial.print(h);
- Serial.print(" %\t");
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.println(" *C ");
- }
- void printWifiStatus()
- {
- // print the SSID of the network you're attached to
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
- // print your WiFi shield's IP address
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
- // print the received signal strength
- long rssi = WiFi.RSSI();
- Serial.print("Signal strength (RSSI):");
- Serial.print(rssi);
- Serial.println(" dBm");
- }
- void setup()
- {
- startLcd();
- lcd.setCursor(0,1);
- lcd.print("Init HW Serial");
- Serial.begin(115200);
- lcd.setCursor(0,1);
- lcd.print("Init SW Serial");
- Serial1.begin(9600);
- // initialize ESP module
- //if(Serial1.available()){
- lcd.setCursor(0,1);
- lcd.print("Init ESP Module");
- WiFi.init(&Serial1);
- initWifi();
- lcd.setCursor(0,1);
- lcd.print("Init CT Sensors");
- initCTSensors();
- printMainLcd();
- }
- void loop()
- {
- //Calcula a corrente
- double Irms = emon1.calcIrms(1480)-0.01;
- //Mostra o valor da corrente
- Serial.print("Aramerosseg : ");
- Serial.print(Irms); // Irms
- Serial.print(" Amper");
- lcd.setCursor(2,0);
- lcd.print(Irms);
- //emon1.serialprint();
- //Calcula e mostra o valor da potencia
- Serial.print(" Teljesitmenyfelvetel : ");
- Serial.print(Irms*rede);
- Serial.println(" Watt");
- lcd.setCursor(10,0);
- lcd.print(" ");
- lcd.setCursor(10,0);
- lcd.print(Irms*rede,1);
- IPAddress ip = WiFi.localIP();
- lcd.setCursor(0,1);
- lcd.print("IP:");
- lcd.print(ip[3]);
- lcd.setCursor(10,1);
- lcd.print(temp);
- unsigned long currentMillis = millis();
- delay(1);
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- measureTempHumidity();
- checkWifi();
- sendData(Irms);
- Serial.println("---Idozitett futtatas---");
- }
- delay(1000);
- }
- void sendData(double Irms){
- lcd.setCursor(14,1);
- lcd.print("->");
- client.stop();
- if (client.connect(server, 80)) {
- Serial.println("Connected to server");
- lcd.setCursor(0,1);
- lcd.print("Connected server");
- delay(2000);
- }
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print(client.status());
- delay(5000);
- String json="";
- json += "{amper:";
- json += Irms;
- json += ",watt:";
- json += Irms*rede;
- json += ",temp:";
- json += temp;
- json += ",humidity:";
- json += humidity;
- json += "}";
- String url = "/input/post.json?node=";
- url += nodeId;
- url += "&apikey=";
- url += privateKey;
- url += "&json=";
- url += json;
- Serial.println("Requesting URL: ");
- Serial.println(url);
- // This will send the request to the server
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + server + "\r\n" +
- "Connection: close\r\n\r\n");
- delay(10);
- lcd.setCursor(14,1);
- lcd.print(" ");
- }
Advertisement
Add Comment
Please, Sign In to add comment