Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ArduinoJson.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include "Esp.h"
- const char* ssid = "unoffice";
- const char* password = "15eb9a233199";
- const char* host = "energymonitor.xyz";
- String lineX="";
- //*Inicialização de vars para conexão
- HTTPClient http;
- WiFiClient client;
- //Arrays b_cell e t_cell
- int b_cell[24][4];
- int t_cell[2][4];
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- if(WiFi.status() != WL_CONNECTED) {
- connectWiFi();
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- if(WiFi.status() == WL_CONNECTED) {
- Serial.println(ESP.getFreeHeap());
- httpget();
- delay(3000);
- Serial.println(lineX);
- //JSON
- delay(3000);
- //WORKING: parseJson("{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}");
- //NOT WORKING (below):
- parseJson(lineX);
- delay(4000);
- Serial.println(b_cell[1][1]);
- Serial.println(ESP.getFreeHeap());
- delay(100);
- }
- }
- /*
- * connectWifi: Connect to WiFi and counterPanic
- */
- void connectWiFi(void){
- int counterPanic = 0;
- while (WiFi.status() != WL_CONNECTED) {
- WiFi.begin(ssid, password);
- delay(3000);
- counterPanic++;
- Serial.println("Counter Panic:");
- Serial.println(counterPanic);
- if(counterPanic>8){
- ESP.restart();
- }
- }
- }
- /*
- * httpget
- */
- void httpget(){
- int lineNo=0;
- const int httpPort = 80;
- String url = "/feedback-esp3/fbdata3.json";
- //String url = "/feedback-esp/fbdataino3.json";
- if(WiFi.status() != WL_CONNECTED) {
- connectWiFi();
- }
- Serial.println("WiFi connected");
- // Use WiFiClient class to create TCP connections
- if (!client.connect(host, httpPort)) {
- Serial.println("Connection to HTTP: FAILED!");
- client.stop();
- delay(1000);
- ESP.restart();
- }else{
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- delay(1000);
- Serial.println("Connection to HTTP: OK!");
- }
- // Read all the lines of the reply from server and print them to Serial
- while(client.available()){
- //lines[lineNo] = client.readStringUntil('\r');
- //lineNo=lineNo+1;
- lineX = client.readStringUntil('\n');
- //Serial.println(line);
- }
- Serial.println();
- Serial.println("closing connection");
- client.stop();
- }
- void parseJson(String lineX2){
- lineX2.trim();
- //Ajustar o valor do StaticJsonBuffer<SIZE> @ github: bblanchon/ArduinoJson/wiki/Memory-model
- StaticJsonBuffer<3000> jsonBuffer;
- JsonObject& root = jsonBuffer.parseObject(lineX2);
- if (!root.success()) {
- Serial.println("parseObject() failed");
- //return;
- }
- //String updated = root["updated"];
- //Serial.println(updated);
- for (int i=1;i<28;i++){
- String aux = "b_led"+String(i);
- Serial.println(aux);
- String datex = root["data"][0][aux];
- if (datex.length()>0){
- Serial.println(datex);
- mySplit(datex,i,0);
- delay(500);
- }
- }
- }
- /*
- * split & Organize b_cell & t_cell
- * id=0 => b_cell
- * id=1 => t_cell
- */
- void mySplit(String input, int index, int id){
- int sep1 = input.indexOf(',');
- int sep2 = input.indexOf(',');
- int sep3 = input.indexOf(',');
- int a = (input.substring(0,sep1)).toInt();
- int r = (input.substring(sep1+1,sep2)).toInt();
- int g = (input.substring(sep2+1,sep3)).toInt();
- int b = (input.substring(sep3+1)).toInt();
- if (id==0){
- b_cell[index][1]=a;
- b_cell[index][2]=r;
- b_cell[index][3]=g;
- b_cell[index][4]=b;
- }
- if (id==1){
- t_cell[index][1]=a;
- t_cell[index][2]=r;
- t_cell[index][3]=g;
- t_cell[index][4]=b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement