Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.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. //Arrays b_cell e t_cell
  18. int b_cell[24][4];
  19. int t_cell[2][4];
  20.  
  21.  
  22. void setup() {
  23. Serial.begin(115200);
  24. Serial.println();
  25. Serial.print("Connecting to ");
  26. Serial.println(ssid);
  27.  
  28.  
  29. WiFi.mode(WIFI_STA);
  30. if(WiFi.status() != WL_CONNECTED) {
  31. connectWiFi();
  32. }
  33.  
  34. Serial.println("");
  35. Serial.println("WiFi connected");
  36. Serial.println("IP address: ");
  37. Serial.println(WiFi.localIP());
  38.  
  39.  
  40.  
  41.  
  42.  
  43. }
  44.  
  45. void loop() {
  46. if(WiFi.status() == WL_CONNECTED) {
  47. Serial.println(ESP.getFreeHeap());
  48. httpget();
  49. delay(3000);
  50. Serial.println(lineX);
  51. //JSON
  52. delay(3000);
  53. //WORKING: parseJson("{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}");
  54. //NOT WORKING (below):
  55. parseJson(lineX);
  56. delay(4000);
  57. Serial.println(b_cell[1][1]);
  58. Serial.println(ESP.getFreeHeap());
  59. delay(100);
  60. }
  61.  
  62. }
  63.  
  64.  
  65. /*
  66. * connectWifi: Connect to WiFi and counterPanic
  67. */
  68.  
  69. void connectWiFi(void){
  70. int counterPanic = 0;
  71. while (WiFi.status() != WL_CONNECTED) {
  72. WiFi.begin(ssid, password);
  73. delay(3000);
  74. counterPanic++;
  75. Serial.println("Counter Panic:");
  76. Serial.println(counterPanic);
  77. if(counterPanic>8){
  78. ESP.restart();
  79. }
  80. }
  81.  
  82. }
  83.  
  84. /*
  85. * httpget
  86. */
  87. void httpget(){
  88.  
  89. int lineNo=0;
  90. const int httpPort = 80;
  91. String url = "/feedback-esp3/fbdata3.json";
  92. //String url = "/feedback-esp/fbdataino3.json";
  93. if(WiFi.status() != WL_CONNECTED) {
  94. connectWiFi();
  95. }
  96. Serial.println("WiFi connected");
  97.  
  98.  
  99. // Use WiFiClient class to create TCP connections
  100. if (!client.connect(host, httpPort)) {
  101. Serial.println("Connection to HTTP: FAILED!");
  102. client.stop();
  103. delay(1000);
  104. ESP.restart();
  105. }else{
  106. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  107. "Host: " + host + "\r\n" +
  108. "Connection: close\r\n\r\n");
  109. delay(1000);
  110. Serial.println("Connection to HTTP: OK!");
  111. }
  112.  
  113. // Read all the lines of the reply from server and print them to Serial
  114. while(client.available()){
  115. //lines[lineNo] = client.readStringUntil('\r');
  116. //lineNo=lineNo+1;
  117. lineX = client.readStringUntil('\n');
  118. //Serial.println(line);
  119. }
  120. Serial.println();
  121. Serial.println("closing connection");
  122. client.stop();
  123.  
  124.  
  125. }
  126.  
  127.  
  128. void parseJson(String lineX2){
  129. lineX2.trim();
  130. //Ajustar o valor do StaticJsonBuffer<SIZE> @ github: bblanchon/ArduinoJson/wiki/Memory-model
  131. StaticJsonBuffer<3000> jsonBuffer;
  132. JsonObject& root = jsonBuffer.parseObject(lineX2);
  133.  
  134. if (!root.success()) {
  135. Serial.println("parseObject() failed");
  136. //return;
  137. }
  138.  
  139. //String updated = root["updated"];
  140. //Serial.println(updated);
  141. for (int i=1;i<28;i++){
  142. String aux = "b_led"+String(i);
  143. Serial.println(aux);
  144. String datex = root["data"][0][aux];
  145.  
  146. if (datex.length()>0){
  147. Serial.println(datex);
  148. mySplit(datex,i,0);
  149.  
  150.  
  151. delay(500);
  152. }
  153.  
  154.  
  155. }
  156.  
  157.  
  158. }
  159.  
  160.  
  161.  
  162. /*
  163. * split & Organize b_cell & t_cell
  164. * id=0 => b_cell
  165. * id=1 => t_cell
  166. */
  167.  
  168. void mySplit(String input, int index, int id){
  169. int sep1 = input.indexOf(',');
  170. int sep2 = input.indexOf(',');
  171. int sep3 = input.indexOf(',');
  172.  
  173. int a = (input.substring(0,sep1)).toInt();
  174. int r = (input.substring(sep1+1,sep2)).toInt();
  175. int g = (input.substring(sep2+1,sep3)).toInt();
  176. int b = (input.substring(sep3+1)).toInt();
  177.  
  178. if (id==0){
  179. b_cell[index][1]=a;
  180. b_cell[index][2]=r;
  181. b_cell[index][3]=g;
  182. b_cell[index][4]=b;
  183. }
  184.  
  185. if (id==1){
  186. t_cell[index][1]=a;
  187. t_cell[index][2]=r;
  188. t_cell[index][3]=g;
  189. t_cell[index][4]=b;
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement