Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Simple demo for feeding some random data to Pachube.
- // 2011-07-08 <jc@wippler.nl>
- //
- // License: GPLv2
- // Handle returning code and reset ethernet module if needed
- // 2013-10-22 hneiraf@gmail.com
- #include <EtherCard.h>
- #include <ArduinoJson.h>
- // change these settings to match your own setup
- #define FEED "000"
- #define APIKEY "xxx"
- // ethernet interface mac address, must be unique on the LAN
- static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
- const char website[] PROGMEM = "ticket-project.herokuapp.com";
- byte Ethernet::buffer[350];
- uint32_t timer;
- Stash stash;
- byte session;
- //timing variable
- int res = 0;
- char qrcode[] = "minhapirocaƩenormeeeuodeiooarduino";
- void setup () {
- Serial.begin(57600);
- Serial.println("\n[Xively example]");
- //Initialize Ethernet
- initialize_ethernet();
- }
- void loop () {
- //if correct answer is not received then re-initialize ethernet module
- if (res > 220){
- initialize_ethernet();
- }
- res = res + 1;
- ether.packetLoop(ether.packetReceive());
- //200 res = 10 seconds (50ms each res)
- if (res == 200) {
- // generate two fake values as payload - by using a separate stash,
- // we can determine the size of the generated message ahead of time
- byte sd = stash.create();
- stash.print("qr_code:");
- stash.println(qrcode);
- stash.save();
- //Display data to be sent
- Serial.println(qrcode);
- // generate the header with payload - note that the stash size is used,
- // and that a "stash descriptor" is passed in as argument using "$H"
- Stash::prepare(PSTR("POST $F/api/tickets HTTP/1.1" "\r\n"
- "Host: $F" "\r\n"
- "qr_code: $F"
- "Content-Length: $D" "\r\n"
- "\r\n"
- "$H"),
- website, website, qrcode, stash.size(), sd);
- // send the packet - this also releases all stash buffers once done
- session = ether.tcpSend();
- }
- const char* reply = ether.tcpReply(session);
- if (reply != 0) {
- res = 0;
- Serial.println(reply);
- }
- delay(50);
- }
- void initialize_ethernet(void){
- for(;;){ // keep trying until you succeed
- //Reinitialize ethernet module
- pinMode(5, OUTPUT);
- Serial.println("Reseting Ethernet...");
- digitalWrite(5, LOW);
- delay(1000);
- digitalWrite(5, HIGH);
- delay(500);
- // Change 'SS' to your Slave Select pin, if you arn't using the default pin
- if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0){
- Serial.println( "Failed to access Ethernet controller");
- continue;
- }
- if (!ether.dhcpSetup()){
- Serial.println("DHCP failed");
- continue;
- }
- ether.printIp("IP: ", ether.myip);
- ether.printIp("GW: ", ether.gwip);
- ether.printIp("DNS: ", ether.dnsip);
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
- ether.printIp("SRV: ", ether.hisip);
- //reset init value
- res = 0;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement