Home › Forums › Mayfly Data Logger › Connecting to the Internet › Reply To: Connecting to the Internet
2018-04-09 at 2:58 PM
#12223
Here’s the code running on the UnoEthernet/Xbee shown above, but with the URL and IP addresses removed. I based this all from some examples I found about 6 years ago, so there’s probably better ways to do this nowadays.
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
/* Feb 18, 2013 With Xbee radio attached to pins 2 and 3, and ethernet shield connected to Arduino (or use Uno Ethernet), this sketch takes any captured string and sends it to a URL of a server running a php script that captures the data and writes it to a mySQL database */ #include <SPI.h> #include <Ethernet.h> #include <SoftwareSerial.h> SoftwareSerial xbeeSerial(2, 3); // RX, TX String readString = ""; //temporary string to store the incoming xbee data boolean stringComplete = false; // whether the string is complete byte mac[] = { 0x22, 0x22, 0x55, 0x55, 0x99, 0x99 }; // put the unique MAC address of your Uno ethernet board here IPAddress server(192, 555, 999, 999); // IP address of the name server for where your php script resides EthernetClient client; void setup() { // start serial port: Serial.begin(57600); xbeeSerial.begin(2400); //The Xbee 900mhz module has trouble capturing faster than 2400baud with the UnoEthernet //so be sure to program the Xbee module's baud rate beforehand using XCTU Serial.println("sends entire captured string (with full url) to whatever URL is captured"); Serial.println("Also resests the ethernet connection if it gets disconnected at some point"); // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // DHCP failed, so use a fixed IP address: } } void loop() // run over and over { //delay(100); if (stringComplete) { Serial.print("CapturedString: "); Serial.print(readString); //delay(1000); sendData(readString); //goes to URL and posts the data // clear the string: Serial.println("Clearing string..."); readString = ""; stringComplete = false; } if (xbeeSerial.available() >0) { char c = xbeeSerial.read(); //gets one byte from serial buffer //if((c>='0' and c<='9') || c==' ') //{ //Serial.print(c); readString += c; //makes the string readString //} if (c == '\n') { stringComplete = true; } //Serial.write(xbeeSerial.read()); prints each character if you really want to see each one as it comes in } } void sendData(String thisData) { // if there's a successful connection: Serial.println("connecting to server..."); if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: client.print("GET http://www.somewebsite.com/demo.php?"); //place your complete URL address here client.println(thisData); client.println(" HTTP/1.0"); client.println("Host: http://www.somewebsite.com"); client.println(); client.stop(); Serial.println("Data sent to the website"); delay(100); } else { Serial.println("internet connection failed"); delay(1000); //reset the Arduino module to hopefully fix the dropped internet connection client.stop(); delay(1000); Ethernet.begin(mac); delay(1000); } } //end sendData |