Advertisement
iaskiller

Untitled

Jun 16th, 2017
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. /**
  2.  * \file
  3.  *           ESP8266 RESTful example
  4.  * \author
  5.  *           BeeGee
  6.  */
  7.  
  8. #include <ELClient.h>
  9. #include <ELClientRest.h>
  10. #include <SoftwareSerial.h>
  11.  
  12. SoftwareSerial mySerial(2, 3); // RX, TX
  13.  
  14. //###########################################################
  15. // For ARDUINO UNO WIFI with I2C to serial chip!
  16. //###########################################################
  17. // Serial port to ESP8266
  18. // #include <SC16IS750.h>
  19. // SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AA);
  20.  
  21. // Initialize a connection to esp-link using the I2Cuart chip of the Arduino Uno WiFi board for
  22. // SLIP messages.
  23. // ELClient esp(&i2cuart);
  24.  
  25. //###########################################################
  26. // For boards using the hardware serial port!
  27. //###########################################################
  28. // Initialize a connection to esp-link using the normal hardware serial port both for
  29. // SLIP and for debug messages.
  30. //ELClient esp(&Serial, &Serial);
  31. ELClient esp(&mySerial, &mySerial);
  32.  
  33. // Initialize a REST client on the connection to esp-link
  34. ELClientRest rest(&esp);
  35.  
  36. boolean wifiConnected = false;
  37.  
  38. // Callback made from esp-link to notify of wifi status changes
  39. // Here we print something out and set a global flag
  40. void wifiCb(void *response) {
  41.     ELClientResponse *res = (ELClientResponse*)response;
  42.     if (res->argc() == 1) {
  43.         uint8_t status;
  44.         res->popArg(&status, 1);
  45.  
  46.         if(status == STATION_GOT_IP) {
  47.             Serial.println("WIFI CONNECTED");
  48.             wifiConnected = true;
  49.         } else {
  50.             Serial.print("WIFI NOT READY: ");
  51.             Serial.println(status);
  52.             wifiConnected = false;
  53.         }
  54.     }
  55. }
  56.  
  57. void setup() {
  58.     Serial.begin(9600);
  59.  mySerial.begin(9600);
  60. //###########################################################
  61. // For ARDUINO UNO WIFI with I2C to serial chip!
  62. //###########################################################
  63.     // i2cuart.begin(9600);
  64.    
  65.     Serial.println("");
  66.     Serial.println("EL-Client starting!");
  67.  
  68.     // Sync-up with esp-link, this is required at the start of any sketch and initializes the
  69.     // callbacks to the wifi status change callback. The callback gets called with the initial
  70.     // status right after Sync() below completes.
  71.     esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
  72.     bool ok;
  73.     do {
  74.         ok = esp.Sync();            // sync up with esp-link, blocks for up to 2 seconds
  75.         if (!ok) {
  76.             Serial.print("\nEL-Client sync failed! err: ");
  77.             Serial.println(ok);
  78.         }
  79.     } while(!ok);
  80.     Serial.println("EL-Client synced!");
  81.  
  82.     // Wait for WiFi to be connected.
  83. Serial.println("esp.GetWifiStatus()");
  84.     esp.GetWifiStatus();
  85.     ELClientPacket *packet;
  86.     Serial.println("Waiting for WiFi ");
  87.     if ((packet=esp.WaitReturn()) != NULL) {
  88.         Serial.print(".");
  89.         Serial.println(packet->value);
  90.     }
  91.     Serial.println("");
  92.  
  93.     // Set up the REST client to talk to api.thingspeak.com, this doesn't connect to that server,
  94.     // it just sets-up stuff on the esp-link side
  95.     int err = rest.begin("api.myurl.com");
  96.     if (err != 0) {
  97.         Serial.print("REST begin failed: ");
  98.         Serial.println(err);
  99.         while(1) ;
  100.     }
  101.     Serial.println("EL-REST ready");
  102. }
  103.  
  104. float solarValue = 99.5;
  105. // Change to your own API key
  106. char *api_key = "myapikey";
  107. // expand buffer size to your needs
  108. #define BUFLEN 266
  109.  
  110. void loop() {
  111.     // process any callbacks coming from esp_link
  112.     esp.Process();
  113.  
  114.     // if we're connected make an REST request
  115.     if(wifiConnected) {
  116.        
  117.         // Generate a fake value starting from 100 going up to 300
  118.         solarValue = solarValue + 0.5;
  119.         if (solarValue == 300) {
  120.             solarValue = 100;
  121.         }
  122.         String solarValString = String(solarValue);
  123.         const char *solarValChar = solarValString.c_str();
  124.        
  125.         // Reserve a buffer for sending the data
  126.         char path_data[BUFLEN];
  127.         // Copy the path and API key into the buffer
  128.         sprintf(path_data, "%s", "/?token=");
  129.         sprintf(path_data + strlen(path_data), "%s", api_key);
  130.        
  131.         // Copy the field number and value into the buffer
  132.         // If you have more than one field to update,
  133.         // repeat and change field1 to field2, field3, ...
  134.  
  135.         sprintf(path_data + strlen(path_data), "%s", "&time=");
  136.     sprintf(path_data + strlen(path_data), "%d", millis());
  137.        
  138.         // Send PUT request to thingspeak.com
  139.         rest.post(path_data,"");   
  140.     //rest.post("/?token=eaaeebace112754929d81074576e9e1b&time=12345","");  
  141.  
  142.         // Reserve a buffer for the response from Thingspeak
  143.         char response[BUFLEN];
  144.         // Clear the buffer
  145.         memset(response, 0, BUFLEN);
  146.         // Wait for response from Thingspeak
  147.         uint16_t code = rest.waitResponse(response, BUFLEN-1);
  148.         // Check the response from Thingspeak
  149.         if(code == HTTP_STATUS_OK){
  150.             Serial.println("Thingspeak: POST successful:");
  151.             Serial.print("Response: ");
  152.             Serial.println(response);
  153.         } else {
  154.             Serial.print("Thingspeak: POST failed with error ");
  155.             Serial.println(code);
  156.             Serial.print("Response: ");
  157.             Serial.println(response);
  158.         }
  159.         // Send next data in 2 seconds
  160.         delay(2000);
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement