Advertisement
tomasburancgi

sadsadasdas

Nov 10th, 2021
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <Wire.h>
  3. #include <SFE_MicroOLED.h>
  4. #include <ArduinoJson.h>
  5.  
  6. const char* ssid = "iainhendry";
  7. const char* pass = "iain061271";
  8. #define PIN_RESET 255 //
  9. #define DC_JUMPER 0 // I2C Addres: 0 - 0x3C, 1 - 0x3D
  10. String id;
  11. String value;
  12. String json;
  13.  
  14. MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C Example
  15.  
  16. WiFiClient client;
  17.  
  18. // delay between updates
  19. const unsigned long postingInterval = 60L * 1000L;
  20. unsigned long lastConnectionTime = 0;
  21.  
  22. void setup()
  23. {
  24. delay(100);
  25. Serial.begin(115200);
  26. Serial.println();
  27. Serial.println();
  28. Serial.print("Connecting to ");
  29. Serial.println(ssid);
  30. WiFi.begin(ssid, pass);
  31. while (WiFi.status() != WL_CONNECTED)
  32. {
  33. delay(500);
  34. Serial.print(".");
  35. }
  36. Serial.println("");
  37. Serial.println("WiFi connected");
  38. Serial.println("IP address: ");
  39. Serial.println(WiFi.localIP());
  40.  
  41. Serial.print("Connecting to ");
  42.  
  43. oled.begin();
  44. oled.clear(ALL);
  45. oled.setCursor(0,0);
  46. oled.display();
  47. oled.clear(PAGE);
  48. oled.clear(ALL);
  49. oled.print("Bitcoin price");
  50. oled.setCursor(0,1);
  51. oled.print("loading..");
  52. oled.display(); // Display what's in the buffer (splashscreen)
  53. delay(50);
  54.  
  55. }
  56.  
  57. int check_connect = 0;
  58.  
  59. void httpRequest()
  60. {
  61. client.stop();
  62.  
  63. // if there's a successful connection:
  64. if (client.connect("api.coindesk.com", 80))
  65. {
  66. Serial.println("connecting...");
  67. client.println("GET /v1/bpi/currentprice.json HTTP/1.1");
  68. client.println("Host: api.coindesk.com");
  69. client.println("User-Agent: ESP8266/1.1");
  70. client.println("Connection: close");
  71. client.println();
  72. lastConnectionTime = millis();
  73. }
  74. else
  75. {
  76. // if you couldn't make a connection:
  77. Serial.println("connection failed");
  78. }
  79. }
  80.  
  81. void loop()
  82. {
  83. int cnt;
  84.  
  85. if (cnt++ == 10000)
  86. {
  87. cnt = 0;
  88. if (check_connect++ == 50)
  89. {
  90. check_connect = 0;
  91. if (WiFi.status() != WL_CONNECTED)
  92. {
  93. }
  94. }
  95. }
  96.  
  97. if (millis() - lastConnectionTime > postingInterval)
  98. {
  99. httpRequest();
  100. unsigned int i = 0; //timeout counter
  101. int n = 1; // char counter
  102. char json[500] ="{";
  103.  
  104. while (!client.find("\"USD\":{")){}
  105.  
  106. while (i<20000)
  107. {
  108. if (client.available())
  109. {
  110. char c = client.read();
  111. json[n]=c;
  112. if (c=='}') break;
  113. n++;
  114. i=0;
  115. }
  116. i++;
  117. }
  118.  
  119. StaticJsonBuffer<500> jsonBuffer;
  120. JsonObject& root = jsonBuffer.parseObject(json);
  121.  
  122. String newjson = root["code"];
  123. String value = root["rate"];
  124. id = newjson.substring(9,12);
  125.  
  126. // value = newjson.substring(41,51);
  127. oled.display();
  128. oled.clear(PAGE); // Clear the display's internal memory
  129. oled.clear(ALL); // Clear the library's display buffer
  130. oled.setCursor(0,1);
  131. oled.print(value);
  132. oled.display();
  133.  
  134. id="";
  135. value="";
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement