Advertisement
pregomal

Untitled

Jun 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. // Simple demo for feeding some random data to Pachube.
  2. // 2011-07-08 <jc@wippler.nl>
  3. //
  4. // License: GPLv2
  5.  
  6. // Handle returning code and reset ethernet module if needed
  7. // 2013-10-22 hneiraf@gmail.com
  8.  
  9. #include <EtherCard.h>
  10. #include <ArduinoJson.h>
  11.  
  12. // change these settings to match your own setup
  13. #define FEED "000"
  14. #define APIKEY "xxx"
  15.  
  16. // ethernet interface mac address, must be unique on the LAN
  17. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
  18.  
  19. const char website[] PROGMEM = "ticket-project.herokuapp.com";
  20.  
  21. byte Ethernet::buffer[350];
  22. uint32_t timer;
  23. Stash stash;
  24. byte session;
  25.  
  26. //timing variable
  27. int res = 0;
  28.  
  29. char qrcode[] = "minhapirocaƩenormeeeuodeiooarduino";
  30.  
  31. void setup () {
  32. Serial.begin(57600);
  33. Serial.println("\n[Xively example]");
  34.  
  35. //Initialize Ethernet
  36. initialize_ethernet();
  37. }
  38.  
  39.  
  40. void loop () {
  41.  
  42. //if correct answer is not received then re-initialize ethernet module
  43. if (res > 220){
  44. initialize_ethernet();
  45. }
  46.  
  47. res = res + 1;
  48.  
  49. ether.packetLoop(ether.packetReceive());
  50.  
  51. //200 res = 10 seconds (50ms each res)
  52. if (res == 200) {
  53.  
  54. // generate two fake values as payload - by using a separate stash,
  55. // we can determine the size of the generated message ahead of time
  56. byte sd = stash.create();
  57. stash.print("qr_code:");
  58. stash.println(qrcode);
  59. stash.save();
  60.  
  61. //Display data to be sent
  62. Serial.println(qrcode);
  63.  
  64.  
  65. // generate the header with payload - note that the stash size is used,
  66. // and that a "stash descriptor" is passed in as argument using "$H"
  67. Stash::prepare(PSTR("POST $F/api/tickets HTTP/1.1" "\r\n"
  68. "Host: $F" "\r\n"
  69. "qr_code: $F"
  70. "Content-Length: $D" "\r\n"
  71. "\r\n"
  72. "$H"),
  73. website, website, qrcode, stash.size(), sd);
  74.  
  75. // send the packet - this also releases all stash buffers once done
  76. session = ether.tcpSend();
  77. }
  78.  
  79. const char* reply = ether.tcpReply(session);
  80.  
  81. if (reply != 0) {
  82. res = 0;
  83. Serial.println(reply);
  84. }
  85. delay(50);
  86. }
  87.  
  88.  
  89.  
  90. void initialize_ethernet(void){
  91. for(;;){ // keep trying until you succeed
  92. //Reinitialize ethernet module
  93. pinMode(5, OUTPUT);
  94. Serial.println("Reseting Ethernet...");
  95. digitalWrite(5, LOW);
  96. delay(1000);
  97. digitalWrite(5, HIGH);
  98. delay(500);
  99.  
  100. // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  101. if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0){
  102. Serial.println( "Failed to access Ethernet controller");
  103. continue;
  104. }
  105.  
  106. if (!ether.dhcpSetup()){
  107. Serial.println("DHCP failed");
  108. continue;
  109. }
  110.  
  111. ether.printIp("IP: ", ether.myip);
  112. ether.printIp("GW: ", ether.gwip);
  113. ether.printIp("DNS: ", ether.dnsip);
  114.  
  115. if (!ether.dnsLookup(website))
  116. Serial.println("DNS failed");
  117.  
  118. ether.printIp("SRV: ", ether.hisip);
  119.  
  120. //reset init value
  121. res = 0;
  122. break;
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement