Advertisement
nikson1988

Lib

May 21st, 2022 (edited)
658
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef _TRIGGER_GOOGLESHEETS_H_
  2. #define _TRIGGER_GOOGLESHEETS_H_
  3.  
  4. #include <stdarg.h>
  5. #include <string.h>
  6.  
  7. void sheets_initialization();
  8. void Send_Data();
  9. void Google_Sheets_Init(char array_2d[10][10], String sheets_gas_id, int param_size);
  10.  
  11. String url;
  12.  
  13. char column_name[ ][10]={"x","y"};;
  14.  
  15. double random_values[100];
  16.  
  17. void float_to_string();
  18. const char* host = "script.google.com";
  19. const int httpsPort = 443;
  20.  
  21. //char Sensor_Values[10][10];
  22. char Sensor_Values[3][10];
  23. Sensor_Values[0]="abc";
  24. Sensor_Values[1]="def";
  25. Sensor_Values[2]="12345";
  26. const char* fingerprint = "46 B2 C3 44 9C 59 09 8B 01 B6 F8 BD 4C FB 00 74 91 2F EF F6";
  27. String GAS_ID;
  28. int Count;
  29.  
  30. void Google_Sheets_Init(char test[ ][10], String sheets_gas_id, int param_size)
  31. {
  32.     GAS_ID = sheets_gas_id;
  33.     Count = param_size;
  34.    
  35.     for (int i = 0; i < Count; i++)
  36.     {
  37.      for (int j = 0; j < 10; j++)
  38.      {
  39.         column_name[i][j] = test[i][j];
  40.      }
  41.     }
  42.    
  43.     #ifdef Debug_Serial_Mon
  44.        for(int i=0;i<Count;i++)
  45.        {
  46.             //Serial.print("column_name= ");
  47.             //Serial.println(column_name[i]);
  48.        }
  49.    #endif
  50. }
  51.  
  52. void Data_to_Sheets(int num, ...)
  53. {
  54.     va_list lst;
  55.     va_start(lst,num);
  56.    
  57.     for(int i=0;i<num;i++)
  58.     {
  59.         random_values[i]= va_arg(lst,double);
  60.     }
  61.     va_end(lst);   
  62.    
  63.     float_to_string();
  64.     Send_Data();
  65. }
  66.  
  67. void float_to_string()
  68. {
  69.   for(int j=0;j<Count;j++)
  70.   {
  71.     sprintf(Sensor_Values[j],"%.02f",random_values[j]);
  72.     #ifdef Debug_Serial_Mon
  73.         Serial.print("Sensor Values : ");
  74.         Serial.println(Sensor_Values[j]);
  75.     #endif
  76.   }
  77. }
  78. void Send_Data()
  79. {
  80.     sheets_initialization();
  81.    
  82.     String url = "/macros/s/" + GAS_ID + "/exec?";
  83.     int i=0;
  84.     while(i!=Count)
  85.     {
  86.         if(i==0)
  87.         {
  88.           url = url+column_name[i]+"="+Sensor_Values[i];
  89.           i++;
  90.         }
  91.         if(i==Count)
  92.           break;
  93.         url = url+"&"+column_name[i]+"="+Sensor_Values[i];
  94.         i++;    
  95.     }
  96.  
  97.   //Serial.print("requesting URL: ");
  98.   //Serial.println(url);
  99.  
  100.   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  101.          "Host: " + host + "\r\n" +
  102.          "User-Agent: BuildFailureDetectorESP8266\r\n" +
  103.          "Connection: close\r\n\r\n");
  104.  
  105.   #ifdef Debug_Serial_Mon
  106.   //Serial.println("request sent");
  107.   #endif
  108.   while (client.connected())
  109.   {
  110.       String line = client.readStringUntil('\n');
  111.       if (line == "\r")
  112.       {
  113.         #ifdef Debug_Serial_Mon  
  114.         //Serial.println("headers received");
  115.         #endif
  116.         break;
  117.       }
  118.   }
  119.   String line = client.readStringUntil('\n');
  120.  
  121.   if (line.startsWith("{\"state\":\"success\""))
  122.   {
  123.     #ifdef Debug_Serial_Mon
  124.     //Serial.println("esp8266/Arduino CI successfull!");
  125.     #endif
  126.   }
  127.   else
  128.   {
  129.     #ifdef Debug_Serial_Mon  
  130.     //Serial.println("esp8266/Arduino CI has failed");
  131.     #endif
  132.   }
  133.  
  134.   #ifdef Debug_Serial_Mon
  135.       //Serial.println("reply was:");
  136.       //Serial.println("==========");
  137.       //Serial.println(line);
  138.       //Serial.println("==========");
  139.       //Serial.println("closing connection");
  140.   #endif
  141.    
  142. }
  143. void sheets_initialization()
  144. {
  145.     client.setInsecure();
  146.  
  147.   #ifdef Debug_Serial_Mon
  148.       //Serial.print("connecting to ");
  149.       //Serial.println(host);
  150.   #endif
  151.  
  152.   if (!client.connect(host, httpsPort))
  153.   {
  154.     #ifdef Debug_Serial_Mon
  155.     //Serial.println("connection failed");
  156.     #endif
  157.     return;
  158.   }
  159.  
  160.   if (client.verify(fingerprint, host))
  161.   {
  162.     #ifdef Debug_Serial_Mon
  163.     //Serial.println("certificate matches");
  164.     #endif
  165.   }
  166.   else
  167.   {
  168.     #ifdef Debug_Serial_Mon
  169.     //Serial.println("certificate doesn't match");
  170.     #endif
  171.   }
  172.    
  173. }
  174. #endif
  175.  
Advertisement
RAW Paste Data Copied
Advertisement