#include #include #include #include #include #include #include // PETSCAN // // VER 1.5 // // Designed to send pet chip data to an HTTPS (secure site) // Change all locations in sketch that have your website URL // Enter your site's Certificate as needed. WebServer Server; AutoConnect Portal(Server); const char* myserver = "www.yoursite.com"; // Remote Server URL const char* wifi = "no"; #define NEOPIX 19 // Neopixel Pin #define VIBRATE 21 // Haptic Motor Pin #define ONBOARD_LED 2 byte Myarray[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; String datarx; //Received data as string NeoPixelBus strip(2, NEOPIX); HardwareSerial MySerial(1); // Certificate from HTTPS website where chip data will be sent. const char* test_root_ca= \ "-----BEGIN CERTIFICATE-----\n" \ "MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" \ "DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" \ "PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" \ "AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" \ "rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" \ "OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" \ "xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n" \ "aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" \ "HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" \ "SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" \ "AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" \ "R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" \ "Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" \ "-----END CERTIFICATE-----\n"; // You can use x.509 client certificates if you want //const char* test_client_key = ""; //to verify the client //const char* test_client_cert = ""; //to verify the client WiFiClientSecure client; void rootPage() { // If at any time, a browser on the same LAN accesses the IP for the ESP32, // it will run this function and display HTML served from this ESP32. char content[] = "Hello, This is a webpage served from the ESP32"; Server.send(200, "text/plain", content); } void setup() { // This resets all the neopixels to an off state strip.Begin(); strip.Show(); delay(1000); Serial.begin(115200); Serial.println(); Server.on("/", rootPage); strip.SetPixelColor(0, RgbColor(15, 0, 0)); strip.Show(); delay(200); if (Portal.begin()) { Serial.println("HTTP server:" + WiFi.localIP().toString()); wifi = "yes"; } MySerial.begin(9600, SERIAL_8N1, 16, 17); // Setup Serial for the Chip Reader Board pinMode(ONBOARD_LED, OUTPUT); // Set the output for on the onboard LED (if used). pinMode(VIBRATE, OUTPUT); // Set the output for the Haptic Vibration Motor. } void loop() { Portal.handleClient(); if(wifi == "yes"){ strip.SetPixelColor(0, RgbColor(0, 0, 15)); strip.Show(); delay(200); } else{ strip.SetPixelColor(0, RgbColor(15, 0, 0)); strip.Show(); delay(200); } int flag = 0; int count = 0; String hexx = ""; // This "while" section will execute if a Pet Chip is scanned and responds with serial data. while (MySerial.available() > 0) { strip.SetPixelColor(1, RgbColor(60, 60, 60)); strip.Show(); delay(50); byte byteFromSerial = MySerial.read(); if (byteFromSerial < 0x10) { hexx = hexx + "0"; } hexx = hexx + String(byteFromSerial, HEX); hexx = hexx + "."; Myarray[count] = byteFromSerial; count++; flag = 1; if(count < 6){ digitalWrite(VIBRATE, HIGH); } else{ digitalWrite(VIBRATE, LOW); } } if (flag == 1) { // Scan was successful, so process the information and send it to the remote API for database processing. strip.SetPixelColor(1, RgbColor(60, 30, 0)); strip.Show(); delay(100); digitalWrite(VIBRATE, LOW); unsigned long manuf = (Myarray[4] << 8) | Myarray[5]; unsigned long id = (Myarray[6] << 32) | (Myarray[7] << 24) | (Myarray[8] << 16) | (Myarray[9] << 8) | Myarray[10]; String brand = String(manuf); String idx = String(id); String build = "https://www.yoursite.com/petchip/?api_key=tU84-s8ns-l9Rh-70js-HH75-Rt27&hex=" + hexx + "&brand=" + brand + "&id=" + idx; // JSON build (if you wish to use that instead of a URL GET) // String build = "{\"api_key\": \"tU84-s8ns-l9Rh-70js-HH75-Rt27\", \"hex\": \"" + hexx + "\", \"brand\": \"" + brand + "\", \"id\": \"" + idx + "\"}"; Serial.println(build); Serial.println(" "); client.setCACert(test_root_ca); //client.setCertificate(test_client_key); // for client verification //client.setPrivateKey(test_client_cert); // for client verification // Checking connection to server... if (!client.connect(myserver, 443)){ Serial.println("Connection failed!"); strip.SetPixelColor(1, RgbColor(20, 0, 0)); // RED strip.Show(); delay(100); digitalWrite(VIBRATE, LOW); } else { // Make a HTTPS request (In this case, using GET instead of JSON) ... client.println("GET " + build + " HTTP/1.1"); client.println("Host: www.yoursite.com"); client.println("Connection: close"); client.println(); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { Serial.println("data received"); break; } } while (client.available()) { datarx += client.readStringUntil('\n'); } // Process Status returned from website // IV = Invalid Key // VS = Verified (previous id) and Saved. // NS = New id and Saved. if(datarx == "IV"){ strip.SetPixelColor(1, RgbColor(20, 0, 0)); // RED strip.Show(); delay(100); } if(datarx == "VS"){ strip.SetPixelColor(1, RgbColor(0, 20, 0)); // GREEN strip.Show(); delay(100); } if(datarx == "NS"){ strip.SetPixelColor(1, RgbColor(20, 0, 20)); // PURPLE strip.Show(); delay(100); } datarx = ""; client.stop(); } // connected to server delay(3000); strip.SetPixelColor(1, RgbColor(0, 0, 0)); strip.Show(); delay(100); } // if flag=1 (tag was scanned) digitalWrite(VIBRATE, LOW); }