Guest User

Untitled

a guest
Mar 1st, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. #include "EmonLib.h"
  2. #include <LiquidCrystal.h>
  3. #include "WiFiEsp.h"
  4. #include <DHT.h>
  5.  
  6. EnergyMonitor emon1;
  7. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  8.  
  9. //Hálózati Feszültség
  10. int rede = 224.0;
  11.  
  12. //SCT PIN
  13. int pino_sct = 1;
  14.  
  15. // DHT CONFIGURE
  16. #define DHTPIN 10 // what digital pin we're connected to
  17. #define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
  18. DHT dht(DHTPIN, DHTTYPE);
  19. float temp,humidity=0;
  20.  
  21. // Emulate Serial1 on pins 6/7 if not present
  22. #ifndef HAVE_HWSERIAL1
  23. #include "SoftwareSerial.h"
  24. SoftwareSerial Serial1(8, 9); // RX, TX
  25. #endif
  26. //SoftwareSerial Serial1(8, 9); // RX, TX
  27.  
  28. char ssid[] = "WifiSID"; // your network SSID (name)
  29. char pass[] = "Pass"; // your network password
  30. int status = WL_IDLE_STATUS; // the Wifi radio's status
  31.  
  32. // Initialize the Ethernet client object
  33.  
  34. char server[] = "emoncms.org";
  35. const char* nodeId = "2";
  36. const char* privateKey = "123123123das123";
  37. WiFiEspClient client;
  38.  
  39. // SAJ�T ID�ZÍT�
  40. unsigned long previousMillis = 0;
  41. const long interval = 10000;
  42.  
  43. void startLcd(){
  44. lcd.begin(16,2);
  45. lcd.clear();
  46. lcd.setCursor(0,0);
  47. lcd.print("Booting Module..");
  48. lcd.setCursor(0,1);
  49. lcd.print("v1.0 2016");
  50. }
  51. void printMainLcd(){
  52. lcd.clear();
  53. lcd.setCursor(0,0);
  54. lcd.print("A:");
  55. lcd.setCursor(8,0);
  56. lcd.print("W:");
  57. lcd.setCursor(0,1);
  58. lcd.print("IP:");
  59. lcd.setCursor(8,1) ;
  60. lcd.print("T:");
  61. }
  62.  
  63. void initCTSensors(){
  64. //Pino, calibracao - Cur Const= Tekercsszám/Ellenállás 2000/94 = 21.
  65. emon1.current(pino_sct, 21);
  66. }
  67. void checkWifi(){
  68. // attempt to connect to WiFi network
  69. while ( status != WL_CONNECTED) {
  70. Serial.print("Attempting to connect to WPA SSID: ");
  71. Serial.println(ssid);
  72. // Connect to WPA/WPA2 network
  73. status = WiFi.begin(ssid, pass);
  74. }
  75. }
  76.  
  77. void initWifi(){
  78. // check for the presence of the shield
  79. if (WiFi.status() == WL_NO_SHIELD) {
  80. Serial.println("WiFi shield not present");
  81. lcd.setCursor(0,1);
  82. lcd.print("ERROR: NO ESP");
  83. // don't continue
  84. while (true);
  85. }
  86.  
  87. checkWifi();
  88.  
  89. // you're connected now, so print out the data
  90. Serial.println("You're connected to the network");
  91. lcd.setCursor(0,1);
  92. lcd.print("ESP Connected ");
  93. delay(3000);
  94. printWifiStatus();
  95. }
  96.  
  97. void measureTempHumidity(){
  98. float h = dht.readHumidity();
  99. // Read temperature as Celsius (the default)
  100. float t = dht.readTemperature();
  101. // Read temperature as Fahrenheit (isFahrenheit = true)
  102. float f = dht.readTemperature(true);
  103.  
  104. // Check if any reads failed and exit early (to try again).
  105. if (isnan(h) || isnan(t) || isnan(f)) {
  106. Serial.println("Failed to read from DHT sensor!");
  107. return;
  108. }
  109. temp =t;
  110. humidity=h;
  111. Serial.print("Humidity: ");
  112. Serial.print(h);
  113. Serial.print(" %\t");
  114. Serial.print("Temperature: ");
  115. Serial.print(t);
  116. Serial.println(" *C ");
  117. }
  118. void printWifiStatus()
  119. {
  120. // print the SSID of the network you're attached to
  121. Serial.print("SSID: ");
  122. Serial.println(WiFi.SSID());
  123.  
  124. // print your WiFi shield's IP address
  125. IPAddress ip = WiFi.localIP();
  126. Serial.print("IP Address: ");
  127. Serial.println(ip);
  128.  
  129. // print the received signal strength
  130. long rssi = WiFi.RSSI();
  131. Serial.print("Signal strength (RSSI):");
  132. Serial.print(rssi);
  133. Serial.println(" dBm");
  134. }
  135.  
  136. void setup()
  137. {
  138. startLcd();
  139. lcd.setCursor(0,1);
  140. lcd.print("Init HW Serial");
  141. Serial.begin(115200);
  142. lcd.setCursor(0,1);
  143. lcd.print("Init SW Serial");
  144. Serial1.begin(9600);
  145.  
  146. // initialize ESP module
  147. //if(Serial1.available()){
  148. lcd.setCursor(0,1);
  149. lcd.print("Init ESP Module");
  150. WiFi.init(&Serial1);
  151. initWifi();
  152.  
  153. lcd.setCursor(0,1);
  154. lcd.print("Init CT Sensors");
  155. initCTSensors();
  156. printMainLcd();
  157. }
  158.  
  159. void loop()
  160. {
  161. //Calcula a corrente
  162. double Irms = emon1.calcIrms(1480)-0.01;
  163. //Mostra o valor da corrente
  164. Serial.print("Aramerosseg : ");
  165. Serial.print(Irms); // Irms
  166. Serial.print(" Amper");
  167. lcd.setCursor(2,0);
  168. lcd.print(Irms);
  169. //emon1.serialprint();
  170. //Calcula e mostra o valor da potencia
  171. Serial.print(" Teljesitmenyfelvetel : ");
  172. Serial.print(Irms*rede);
  173. Serial.println(" Watt");
  174. lcd.setCursor(10,0);
  175. lcd.print(" ");
  176. lcd.setCursor(10,0);
  177. lcd.print(Irms*rede,1);
  178.  
  179. IPAddress ip = WiFi.localIP();
  180. lcd.setCursor(0,1);
  181. lcd.print("IP:");
  182. lcd.print(ip[3]);
  183. lcd.setCursor(10,1);
  184. lcd.print(temp);
  185.  
  186. unsigned long currentMillis = millis();
  187. delay(1);
  188. if (currentMillis - previousMillis >= interval) {
  189. previousMillis = currentMillis;
  190. measureTempHumidity();
  191. checkWifi();
  192. sendData(Irms);
  193. Serial.println("---Idozitett futtatas---");
  194. }
  195.  
  196. delay(1000);
  197. }
  198.  
  199. void sendData(double Irms){
  200. lcd.setCursor(14,1);
  201. lcd.print("->");
  202. client.stop();
  203. if (client.connect(server, 80)) {
  204. Serial.println("Connected to server");
  205. lcd.setCursor(0,1);
  206. lcd.print("Connected server");
  207. delay(2000);
  208. }
  209. lcd.clear();
  210. lcd.setCursor(0,0);
  211. lcd.print(client.status());
  212. delay(5000);
  213.  
  214. String json="";
  215.  
  216. json += "{amper:";
  217. json += Irms;
  218. json += ",watt:";
  219. json += Irms*rede;
  220. json += ",temp:";
  221. json += temp;
  222. json += ",humidity:";
  223. json += humidity;
  224. json += "}";
  225.  
  226.  
  227. String url = "/input/post.json?node=";
  228. url += nodeId;
  229. url += "&apikey=";
  230. url += privateKey;
  231. url += "&json=";
  232. url += json;
  233.  
  234. Serial.println("Requesting URL: ");
  235. Serial.println(url);
  236.  
  237. // This will send the request to the server
  238. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  239. "Host: " + server + "\r\n" +
  240. "Connection: close\r\n\r\n");
  241. delay(10);
  242.  
  243. lcd.setCursor(14,1);
  244. lcd.print(" ");
  245.  
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment