Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <MKRNB.h>
  2. #include <Base64.h>
  3.  
  4. #define PINNUMBER ""
  5.  
  6. NBSSLClient client;
  7. GPRS gprs;
  8. NB nbAccess;
  9.  
  10. // modem verification object
  11. NBModem modem;
  12.  
  13. char server[] = "https://firehive.azurewebsites.net";
  14. char path[] = "/sensordata";
  15. int port = 443; // port 443 is the default for HTTPS
  16.  
  17. String iccidVar = "";
  18. String tidVar = "2020-01-20T00:00:00";
  19. float tempVar1 = 24.00;
  20. float tempVar2 = 12.00;
  21.  
  22. String username = "firefly";
  23. String password = "0#92)FaG_S!(jkisf#5SD";
  24.  
  25. char toEncode[] = "firefly:0#92)FaG_S!(jkisf#5SD";
  26. int inputLen = sizeof(toEncode);
  27.  
  28. String data = "{\"id\":\"" + iccidVar + "\",\"t\":\"" + tidVar + "\",\"t1\":" + tempVar1 + ",\"t2\":" + tempVar2 + "}";
  29.  
  30. void setup(){
  31. Serial.begin(9600);
  32. modem.begin();
  33. Serial.println("Starting Arduino web client.");
  34. boolean connected = true;
  35.  
  36. while(!connected) {
  37. if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
  38. (gprs.attachGPRS() == GPRS_READY)) {
  39. connected = true;
  40. } else {
  41. Serial.println("Not connected");
  42. delay(1000);
  43. }
  44. }
  45. Serial.println("NB initialized");
  46. createBase64();
  47. }
  48.  
  49. char* createBase64(){
  50. int encodedLen = base64_enc_len(inputLen);
  51. char encoded[encodedLen];
  52. base64_encode(encoded, toEncode, inputLen);
  53. Serial.print("Base64 Encoded = ");
  54. Serial.println(encoded);
  55. return encoded;
  56. }
  57.  
  58. void findICCD(){
  59. if(iccidVar == "") {
  60. iccidVar = modem.getICCID();
  61. Serial.print("ICCID = ");
  62. Serial.println(iccidVar);
  63. }
  64. }
  65.  
  66. void postData(){
  67. Serial.println("Connecting to server...");
  68. if (client.connect(server, port)){
  69. Serial.println("connected");
  70. client.print("POST");
  71. client.print(path);
  72. client.println(" HTTP/1.1");
  73. client.print("Host: ");
  74. client.println(server);
  75. client.print("Authorization: ");
  76. client.println("Basic ");
  77. client.print(auth);
  78. client.println("Connection: close");
  79. client.println("Content-Type: application/json");
  80. client.print("Content-Length: ");
  81. client.println(data.length()); // TODO?
  82. client.println(data);
  83. Serial.println("Disconnecting from server...");
  84. client.stop();
  85. }
  86. else{
  87. Serial.println("unable to connect");
  88. client.stop();
  89. }
  90. }
  91.  
  92. void loop(){
  93. findICCD();
  94. postData();
  95. }
  96.  
  97.  
  98.  
  99. /*
  100.  
  101. void loop(){
  102. if (client.available()) {
  103. postData();
  104. }
  105. if (!client.available() && !client.connected()) {
  106. Serial.println();
  107. Serial.println("disconnecting.");
  108. client.stop();
  109. }
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement