Files
Arduino/ESP8266_BasicHttpClient/ESP8266_BasicHttpClient.ino
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

175 lines
5.3 KiB
C++
Executable File

/**
* BasicHTTPClient.ino
*
* Created on: 24.05.2015
*
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial
// Time to sleep (in seconds):
const int sleepTimeS = 10;
ESP8266WiFiMulti WiFiMulti;
void setup() {
USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("Livebox-37cc", "8A6060920A8A86896F770F2C47");
}
void loop() {
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {
//
// HTTPClient http;
//
// USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
// http.begin("http://192.168.0.10/test.html"); //HTTP
// http.begin("http://192.168.0.10:8080/json.htm?type=devices&rid=290"); // + String(idRadiateurInDomoticz)
getHttp("http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx=288&nvalue=123&svalue=330");
delay(1000);
getHttp("http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx=127&nvalue=123&svalue=300");
delay(1000);
getHttp("http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx=196&nvalue=12&svalue=129");
delay(1000);
getHttp("http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx=197&nvalue=13&svalue=100");
// http.begin("http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx=288&nvalue=123&svalue=330");
// //http://192.168.0.10:8080/json.htm?type=devices&rid=290
// USE_SERIAL.print("[HTTP] GET...\n");
// // start connection and send HTTP header
// int httpCode = http.GET();
//
// // httpCode will be negative on error
// if(httpCode > 0) {
// // HTTP header has been send and Server response header has been handled
// USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
//
// // file found at server
// if(httpCode == HTTP_CODE_OK) {
// String reponse = http.getString();
// USE_SERIAL.println(reponse.length());
// int p = reponse.indexOf('Status');
// int p2 = reponse.indexOf('StrParam1');
//
// String id = reponse.substring(p, p2);
//
// USE_SERIAL.println(p);
// USE_SERIAL.println(p2);
// USE_SERIAL.println(reponse);
// // String value = reponse.substring(p + 1, reponse.length() -1);
// }
// } else {
// USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
// }
// http.end();
}
// delay(10000);
// Sleep
Serial.println("ESP8266 in sleep mode");
//ESP.deepSleep(sleepTimeS * 1000000, WAKE_RFCAL);
delay(60000);
}
void getHttp(String url) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
http.begin(url);
//http://192.168.0.10:8080/json.htm?type=devices&rid=290
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String reponse = http.getString();
USE_SERIAL.println(reponse.length());
int p = reponse.indexOf('Status');
int p2 = reponse.indexOf('StrParam1');
String id = reponse.substring(p, p2);
USE_SERIAL.println(p);
USE_SERIAL.println(p2);
USE_SERIAL.println(reponse);
// String value = reponse.substring(p + 1, reponse.length() -1);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
//--------------------------------------------------------------------------------------------------
// Read current supply voltage
//--------------------------------------------------------------------------------------------------
long readVcc() {
bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
long result;
// Read 1.1V reference against Vcc
#if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328
#endif
// ADCSRB = 0;
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate Vcc in mV
// ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
// analogReference(DEFAULT);
return result; // Vcc in millivolts
}