Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266HTTPClient.h>
  4. #include "Esp.h"
  5.  
  6. const char* ssid = "unoffice";
  7. const char* password = "15eb9a233199";
  8.  
  9. const char* host = "energymonitor.xyz";
  10. String lineX="";
  11.  
  12.  
  13. //*Inicialização de vars para conexão
  14. HTTPClient http;
  15. WiFiClient client;
  16.  
  17.  
  18. void setup() {
  19. Serial.begin(115200);
  20. Serial.println();
  21.  
  22.  
  23. Serial.print("Connecting to ");
  24. Serial.println(ssid);
  25.  
  26.  
  27. WiFi.mode(WIFI_STA);
  28. if(WiFi.status() != WL_CONNECTED) {
  29. connectWiFi();
  30. }
  31.  
  32. Serial.println("");
  33. Serial.println("WiFi connected");
  34. Serial.println("IP address: ");
  35. Serial.println(WiFi.localIP());
  36. }
  37.  
  38. void loop() {
  39. if(WiFi.status() != WL_CONNECTED) {
  40. connectWiFi();
  41. }
  42. if(WiFi.status() == WL_CONNECTED) {
  43. httpget();
  44. delay(3000);
  45. Serial.println(lineX);
  46. //JSON
  47. delay(3000);
  48. //WORKING: parseJson("{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}");
  49. //NOT WORKING (below):
  50. parseJson(lineX);
  51.  
  52.  
  53. }
  54.  
  55. }
  56.  
  57.  
  58. /*
  59. * connectWifi: Connect to WiFi and counterPanic
  60. */
  61.  
  62. void connectWiFi(void){
  63. int counterPanic = 0;
  64. while (WiFi.status() != WL_CONNECTED) {
  65. WiFi.begin(ssid, password);
  66. delay(3000);
  67. counterPanic++;
  68. Serial.println("Counter Panic:");
  69. Serial.println(counterPanic);
  70. if(counterPanic>8){
  71. ESP.restart();
  72. }
  73. }
  74.  
  75. }
  76.  
  77. /*
  78. * httpget
  79. */
  80. void httpget(){
  81.  
  82. int lineNo=0;
  83. const int httpPort = 80;
  84. //String url = "/feedback-esp3/fbdata3.json";
  85. String url = "/feedback-esp/fbdataino.json";
  86. if(WiFi.status() != WL_CONNECTED) {
  87. connectWiFi();
  88. }
  89. Serial.println("WiFi connected");
  90.  
  91.  
  92. // Use WiFiClient class to create TCP connections
  93. if (!client.connect(host, httpPort)) {
  94. Serial.println("Connection to HTTP: FAILED!");
  95. client.stop();
  96. delay(1000);
  97. ESP.restart();
  98. }else{
  99. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  100. "Host: " + host + "\r\n" +
  101. "Connection: close\r\n\r\n");
  102. delay(1000);
  103. Serial.println("Connection to HTTP: OK!");
  104. }
  105.  
  106. // Read all the lines of the reply from server and print them to Serial
  107. while(client.available()){
  108. //lines[lineNo] = client.readStringUntil('\r');
  109. //lineNo=lineNo+1;
  110. lineX = client.readStringUntil('\n');
  111. //Serial.println(line);
  112. }
  113. Serial.println();
  114. Serial.println("closing connection");
  115. client.stop();
  116.  
  117.  
  118. }
  119.  
  120.  
  121. void parseJson(String lineX2){
  122. StaticJsonBuffer<500> jsonBuffer;
  123. JsonObject& root = jsonBuffer.parseObject(lineX2);
  124.  
  125. if (!root.success()) {
  126. Serial.println("parseObject() failed");
  127. //return;
  128. }
  129.  
  130. const char* sensor = root["sensor"];
  131. long time = root["time"];
  132. double latitude = root["data"][0];
  133. double longitude = root["data"][1];
  134.  
  135. Serial.println(sensor);
  136. Serial.println(time);
  137. Serial.println(latitude, 6);
  138. Serial.println(longitude, 6);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement