Created
June 28, 2020 11:52
-
-
Save jonathanrd/e22310fbb1f40cbfe8ed46bae39e2090 to your computer and use it in GitHub Desktop.
ESP32 code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> //Wifi library | |
#include <HTTPClient.h> // Library for HTTPS GET request | |
#include "mbedtls/md.h" | |
#include "esp_wpa2.h" //WPA2 library for Eduroam Connection | |
#define EAP_IDENTITY "USERNAME" | |
#define EAP_PASSWORD "PASSWORD" | |
const char* ssid = "eduroam"; | |
long chipid; // Variable for unique chip ID | |
int counter; // Variable for WiFi timeout | |
// Include the libraries for the ext temp sensor | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
// GPIO where the DS18B20 is connected to | |
const int oneWireBus = 15; | |
// Setup a oneWire instance to communicate with any OneWire devices | |
OneWire oneWire(oneWireBus); | |
// Pass our oneWire reference to Dallas Temperature sensor | |
DallasTemperature sensors(&oneWire); | |
extern "C" { // Init function to read internal ESP32 temp | |
uint8_t temprature_sens_read(); | |
} | |
// Root CA pub used on server | |
const char* root_ca= \ | |
"-----BEGIN CERTIFICATE-----\n" \ | |
"MILDSjCCAjKgAwIBAgIQRK+waNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" \ | |
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \ | |
"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" \ | |
"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" \ | |
"Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n" \ | |
"AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" \ | |
"rz5Iy2Xu/xAxD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" \ | |
"OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" \ | |
"xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqcxh49ZLOMxt+/yUFw\n" \ | |
"7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n" \ | |
"aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" \ | |
"HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" \ | |
"SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" \ | |
"ikugdB/OEIKcdBodfpga3csTS7MgPOSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n" \ | |
"AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" \ | |
"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" \ | |
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZAL+T0yjWW06XyxV3bqxbYo\n" \ | |
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CPTUQ\n" \ | |
"-----END CERTIFICATE-----"; | |
void setup() { | |
sensors.begin(); | |
// Get unique device ID | |
chipid = ESP.getEfuseMac(); | |
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection | |
WiFi.mode(WIFI_STA); //init wifi mode | |
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity | |
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same | |
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password | |
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default | |
esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function | |
WiFi.begin(ssid); //connect to wifi | |
// Was the connnection successful? | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
counter++; | |
if(counter>=60){ //after 30 seconds timeout - reset board | |
ESP.restart(); | |
} | |
} | |
// Yes, we're connected! | |
} | |
void loop() { | |
// Are we still connected to WiFi? | |
if (WiFi.status() == WL_CONNECTED) { | |
// Yes, reset counter. | |
counter = 0; | |
}else if (WiFi.status() != WL_CONNECTED) { | |
// No! Let's restart the connection.. | |
WiFi.begin(ssid); | |
} | |
// If we had to restart the connection, wait until it's ready.. | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
counter++; | |
if(counter>=60){ //30 seconds timeout - reset board | |
ESP.restart(); | |
} | |
} | |
// Let's create the payload | |
const char* request; | |
request ="https://server.com/api.php?id=%08X&value=%.2f"; | |
char bigBuf[100] = ""; | |
// Get internal temperature | |
uint8_t temp_farenheit= temprature_sens_read(); | |
//convert farenheit to celcius | |
float temp = ( temp_farenheit - 32 ) / 1.8; | |
int tempint = round(temp); | |
// Get external sensor data | |
float temperatureC = -100.00; | |
while (temperatureC < -99.00){ | |
sensors.requestTemperatures(); | |
temperatureC = sensors.getTempCByIndex(0); | |
Serial.println(temperatureC); | |
//temperatureC = 20.01; | |
} | |
sprintf_P(bigBuf, request, chipid, temperatureC); | |
Serial.println(bigBuf); | |
// Init the HTTP connection variable | |
HTTPClient http; | |
// Setup the Useragent string using the chipid | |
const char* useragent; | |
useragent = "JD-ESP-[%08X]"; | |
char completeuseragent[100] = ""; | |
sprintf_P(completeuseragent, useragent, chipid); | |
http.setUserAgent( completeuseragent ) ; | |
// Begin the HTTP connection | |
http.begin(bigBuf, root_ca); | |
// Make the GET request and return the response code | |
int httpCode = http.GET(); | |
if (httpCode > 0) { | |
fastblink(); | |
String payload = http.getString(); | |
} | |
http.end(); //Free the resources | |
delay(10000); | |
} | |
void fastblink(){ | |
digitalWrite(13, HIGH); | |
delay(50); | |
digitalWrite(13, LOW); | |
delay(50); | |
digitalWrite(13, HIGH); | |
delay(50); | |
digitalWrite(13, LOW); | |
delay(50); | |
digitalWrite(13, HIGH); | |
delay(50); | |
digitalWrite(13, LOW); | |
delay(50); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment