I am using an ESP8266 Thing Dev board configured as an access point and server to store data from temperature sensors.
The goal is to access this data with an Android application, but for now, the client used is my computer running Chrome.
The sensors provide new temperatures every 250 ms, which are added to a string (JSON formatted).
When the client requests the data, the string is ended (closing JSON structure), and printed on the server.
The client should be able to access up to 2*50 values at a time. However, each time a request is send, either only 2 or 4 values are printed, either the page is not working.
My Arduino code, using ESP8266 and TMP102 libraries for arduino :
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Ticker.h>
#include "tmp102.h"
#define NB_MAX_TEMP 50
#define MAX_INPUT 20
const byte sensorAddress1 = 0x90; // ADD0 -> GND
const byte sensorAddress2 = 0x92; // ADD0 -> V+
tmp102 *thermometer1 = new tmp102(&Wire);
tmp102 *thermometer2 = new tmp102(&Wire);
// Timer
Ticker ticker;
bool flag = false;
// Wifi
const char WiFiAPPSK[] = "sparkfun"; // Wifi password
WiFiServer server(80);
int count = 0;
String s;
void setupWiFi()
{
WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "ThingDev-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "ThingDev-" + macID;
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i = 0; i < AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);
WiFi.softAP(AP_NameChar, WiFiAPPSK);
}
void timer_done()
{
flag = true;
}
void acquire()
{
int temp1 = 0, temp2 = 0;
if (thermometer1->readTemp(temp1))
{
// If we're reading pins, print out those values:
// first temp
s += String(temp1 * 0.0625);
}
s += ",";
if (thermometer2->readTemp(temp2))
{
// If we're reading pins, print out those values:
// second temp
s += String(temp2 * 0.0625);
}
return;
}
void setup()
{
Wire.begin(); // start the I2C library
Serial.begin(115200);
thermometer1->init(sensorAddress1);
thermometer2->init(sensorAddress2);
//Set default config.
thermometer1->writeConf(TMP102_DEFAULT_CONF);
thermometer2->writeConf(TMP102_DEFAULT_CONF);
setupWiFi();
server.begin();
// timer every 0.25s
ticker.attach(0.25, timer_done);
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
if (flag) {
flag = false;
int temp1 = 0, temp2 = 0;
if (count == 0) {
// prepare string
s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "{temp:[";
}
delay(1);
// add a measurement
s += "[";
acquire();
s += "],";
count += 1;
if (count == NB_MAX_TEMP) {
// Prevent the string to become infinite
count = 0;
s = s.substring(0, s.length() - 1);
s += "]};";
}
}
if (client.available() > 0) {
// Read the first line of the request
String req = client.readStringUntil('\r');
client.flush();
Serial.println(req);
if (req.indexOf("/read") != -1) {
// End the string
s.setCharAt(s.length() - 1, '\0');
s += "]};";
client.print(s);
Serial.println(s);
count = 0;
}
}
delay(1);
Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
And a sample of what I get on the Serial console (the JSON string is the same I read on the server page) :
GET /read HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/html
{temp:[[24.00,24.56],[24.00,24.56];
Client disconnected
GET /favicon.ico HTTP/1.1
Client disconnected
GET /read HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/html
{temp:[[24.00,24.56];
Client disconnected
What am I doing wrong ? Am I using a blocking function ? Are the /favicon.ico requests causing trouble ?
For information, the access point part, and the sensor part of my code have been tested separately and work as expected.
Thanks in advance.
Related
Here is my code in which i want to make esp32 to advertise both as ibeacon and eddystone URL. In start it will act as a Ibeacon but when i write URL text from nrf connect app after connection URL beacon function is called and advertisement data changes to URL format from setBeacon() method.
#include "sys/time.h"
#include "BLEDevice.h"
#include "BLEUtils.h"
#include "BLEServer.h"
#include "BLEBeacon.h"
#include "esp_sleep.h"
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep 4 seconds and then wake up
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
RTC_DATA_ATTR static uint32_t bootcount;
uint16_t beconUUID = 0xFEAA;
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
BLEAdvertising *pAdvertising;
struct timeval now;
const char turnIbeacon ='IBeacon';
const char turnURL ='URL';
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
void setBeacon2() {
Serial.println("Function2 is called");
BLEBeacon oBeacon = BLEBeacon();
oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!)
oBeacon.setProximityUUID(BLEUUID(beconUUID));
oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16);
oBeacon.setMinor(bootcount & 0xFFFF);
oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04
std::string strServiceData = "";
strServiceData += (char)26; // Len
strServiceData += (char)0xFF; // Type
strServiceData += oBeacon.getData();
oAdvertisementData.addData(strServiceData);
pAdvertising->setAdvertisementData(oAdvertisementData);
pAdvertising->setScanResponseData(oScanResponseData);
}
void setBeacon() {
char beacon_data[22];
Serial.println("Function is called");
beacon_data[0] = 0x10; // Eddystone Frame Type (Eddystone-URL)
beacon_data[1] = 0x20; // Beacons TX power at 0m
beacon_data[2] = 0x03; // URL Scheme 'https://'
beacon_data[3] = 'y'; // URL add 1
beacon_data[4] = 'o'; // URL add 2
beacon_data[5] = 'u'; // URL add 3
beacon_data[6] = 't'; // URL add 4
beacon_data[7] = 'u'; // URL add 5
beacon_data[8] = 'b'; // URL add 6
beacon_data[9] = 'e'; // URL add 7
beacon_data[10] = '.'; // URL add 8
beacon_data[11] = 'c'; // URL add 9
beacon_data[12] = 'o'; // URL add 10
beacon_data[13] = 'm'; // URL add 11
oAdvertisementData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data, 14));
}
class MyCallbacks: public BLECharacteristicCallbacks
{
void onWrite(BLECharacteristic *pCharacteristic)
{
std::string value = pCharacteristic->getValue();
if (value.length() > 0)
{
Serial.println("*********");
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
{
Serial.print(value[i]);
// if(value[i] == turnON)
// {
//setBeacon();
// }
//
/* If received Character is 0, then turn OFF the LED */
if(value[i] == turnIbeacon)
{
setBeacon();
}
}
Serial.println();
Serial.println("*********");
}
}
};
void setup()
{
Serial.begin(115200);
gettimeofday(&now, NULL);
Serial.printf("start ESP32 %d\n",bootcount++);
Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last);
last = now.tv_sec;
BLEDevice::init("ESP32-BLE-Server");
BLEServer *pServer = BLEDevice::createServer();
pAdvertising = BLEDevice::getAdvertising();
BLEDevice::startAdvertising();
setBeacon2();
BLEService *pService = pServer->createService(beconUUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop()
{
delay(2000);
}
In initial run i was able to write something on esp32 using nrf connect app but now that write sign is not showing. And after connect when start i re-scan my device will disappears.
Note: Everything is running perfectly when i am uploading individual code of ibeacon and eddystone url on esp32
I try to build a chrono laser with two ESP32 and two LDR.
I have two ESP32:
One do a ACCESS Point with a LDR
Second connect to the AP, have the algorithm of the laser chrono and a second LDR
In the second one, i try also to have a WebServer so i can connect with my phone and restart the chrono.
I try to do multitasking but everytime my ESP32 restart :
18:10:42.625 -> E (5775) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
18:10:42.672 -> E (5775) task_wdt: - IDLE0 (CPU 0)
18:10:42.672 -> E (5775) task_wdt: Tasks currently running:
18:10:42.672 -> E (5775) task_wdt: CPU 0: Task1
18:10:42.672 -> E (5775) task_wdt: CPU 1: Task2
18:10:42.672 -> E (5775) task_wdt: Aborting.
18:10:42.672 -> abort() was called at PC 0x400d9033 on core 0
You can find my code here:
https://create.arduino.cc/editor/marcoberle/c4762636-36c6-4551-92f2-935021a001c9/preview
My code :
// Load Wi-Fi library
#include <WiFi.h>
#include <HTTPClient.h>
//Declaration of PIN
const int departReceiver = 4; // the number of the pin
const int LED_BUILTIN = 2;
// Replace with your network credentials
const char* ssid = "ESP32-ARRIVE";
const char* password = "123456789";
// Set web server port number to 80
WiFiServer server(80);
WiFiClient client = server.available();
//Your IP address or domain name with URL path
const char* serverNameArrive = "http://192.168.4.1/arrive";
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output26State = "off";
String output27State = "off";
// Assign output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
TaskHandle_t Task1;
TaskHandle_t Task2;
void setup() {
Serial.begin(115200);
pinMode(departReceiver, INPUT);
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
client = server.available();
//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
6, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
7, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
delay(500);
}
//CHRONO VAR
unsigned int departStatus = 0;
unsigned int arriveStatus = 0;
unsigned int start_time = 0;
unsigned int stop_time = 0;
unsigned int total_time = 0;
const unsigned int OFF = 0;
const unsigned int ON = 1;
String httpGETRequest(const char* serverName) {
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "--";
if (httpResponseCode > 0) {
payload = http.getString();
}
else {
}
// Free resources
http.end();
return payload;
}
String ChronoLog = "";
int reset = 0;
void Chrono () {
if (reset == 0) {
reset = 1;
ChronoLog = "<p>ALGO START</p>";
Serial.println("ALGO START");
while (departStatus == OFF) {
departStatus = digitalRead(departReceiver);
}
ChronoLog += "<p>Borne de début prête</p>";
Serial.println("Borne de début prête");
while (arriveStatus == OFF) {
arriveStatus = httpGETRequest(serverNameArrive).toInt();
}
ChronoLog += "<p>Borne de fin prête</p>";
ChronoLog += "<p>En attente du courreur</p>";
Serial.println("Borne de fin prête");
Serial.println("En attente du courreur");
while (departStatus == ON) {
departStatus = digitalRead(departReceiver);
}
ChronoLog += "<p>Courreur en place</p>";
Serial.println("Courreur en place");
digitalWrite(LED_BUILTIN, HIGH);
while (departStatus == OFF) {
departStatus = digitalRead(departReceiver);
}
ChronoLog += "<p>Chrono lancé</p>";
Serial.println("Chrono lancé");
start_time = millis();
while (arriveStatus == ON) {
arriveStatus = httpGETRequest(serverNameArrive).toInt();
}
stop_time = millis();
total_time = stop_time - start_time;
ChronoLog += "<p>Courru en </p>";
float timeSe = total_time * 0.001;
//ChronoLog += "<p><b>" + String(timeSe) + "</b></p>" ;
Serial.println("Courru en");
Serial.println(timeSe);
}
}
void ResetChrono () {
reset = 0;
departStatus = 0;
arriveStatus = 0;
start_time = 0;
stop_time = 0;
total_time = 0;
}
void Webserver() {
// Listen for incoming clients
client = server.available();
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
//RESET CHRONO
if (header.indexOf("GET /RESET") >= 0) {
ResetChrono();
}
// turns the GPIOs on and off
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
//Chrono(client);
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>CHRONO</h1>");
client.println(ChronoLog);
client.println("<p><button class=\"button\">RESET</button></p>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State == "off") {
client.println("<p><button class=\"button\">ON</button></p>");
} else {
client.println("<p><button class=\"button button2\">OFF</button></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State == "off") {
client.println("<p><button class=\"button\">ON</button></p>");
} else {
client.println("<p><button class=\"button button2\">OFF</button></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
//Task1code: blinks an LED every 1000 ms
void Task1code( void * pvParameters ){
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
for(;;){
Chrono();
}
}
//Task2code: blinks an LED every 700 ms
void Task2code( void * Parameters ){
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
for(;;){
Webserver();
}
}
void loop() {
}
Do you have any suggestion to optimize my code or a solution to not have the problem anymore ?
You're getting watchdog timer resets. That means your code has been running for too long without resetting a timer that's used to recover from crashes and infinite loops. This timer is handled automatically by the underlying code in the Arduino Core.
Your Chrono and Webserver tasks simply won't work the way you've written them. They never yield the processor. The ESP32 does not do multitasking the way Linux or Windows does. Multitasking on the ESP32 is non-preemptive. A task runs until it says "okay, I've done enough, someone else can run now". These two tasks never do that, so the watchdog timer goes off. Even if the watchdog timer didn't go off, this wouldn't work because nothing else would get the chance to run.
At the least you need to yield the processor in each of the tasks:
for(;;){
Chrono();
}
should be:
for(;;){
Chrono();
delay(1);
}
and
for(;;){
Webserver();
}
should be
for(;;){
Webserver();
delay(1);
}
Your code also has a lot of places where you do things like this:
while (departStatus == OFF) {
departStatus = digitalRead(departReceiver);
}
If departStatus stays OFF for too long, this will trigger the watchdog timer.
Any time you have a loop like this you need to make sure that the underlying system has a change to run once in a while.
Rewrite every single while loop that you have that's like that to call delay(1); - that will give the Arduino Core a chance to reset the watchdog timer.
Like so:
while (departStatus == OFF) {
departStatus = digitalRead(departReceiver);
delay(1);
}
I try a method i found on the web, and it's works.
Add libraries :
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
Create a method:
void feedthedog () {
TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed = 1;
TIMERG0.wdt_wprotect = 0;
}
Add execution of the method in every while
while (departStatus == OFF) {
feedthedog();
departStatus = digitalRead(departReceiver);
}
I can wait, refresh page on the webserver, i don't get error :
19:38:40.321 -> Task1 running on core 0
19:38:40.321 -> ALGO START
19:39:20.937 -> Borne de début prête
19:39:39.093 -> Borne de fin prête
19:39:39.093 -> En attente du courreur
19:39:39.140 -> Courreur en place
19:39:51.840 -> Chrono lancé
19:39:58.804 -> Courru en
19:39:58.804 -> 6.99
19:39:58.804 -> Tâche fini
I put my update code here:
https://create.arduino.cc/editor/marcoberle/cb88dff8-d56c-422d-9f2e-4011ea2b80f4/preview
I am trying to turn a char into a string so I can extract the values I am interested in, however it just appears empty.
The variable I am interested in is content.
I am performing a get and it returns a JSON object. And want to extract the sunrise and sunset values.
#include <SPI.h>
#include <Ethernet2.h>
#include <String.h>
#include <stdlib.h>
EthernetClient client;
const char* server = "api.sunrise-sunset.org"; // server's address
const char* resource = "/json?lat=53.440&lng=0.200&date=today"; // http resource
const unsigned long BAUD_RATE = 9600; // serial connection speed
const unsigned long HTTP_TIMEOUT = 10000; // max respone time from server
const size_t MAX_CONTENT_SIZE = 512; // max size of the HTTP response
// ARDUINO entry point #1: runs once when you press reset or power the board
void setup() {
initSerial();
initEthernet();
}
// ARDUINO entry point #2: runs over and over again forever
void loop() {
if (connect(server)) {
if (sendRequest(server, resource) && skipResponseHeaders()) {
char response[MAX_CONTENT_SIZE];
String str(response);
Serial.println(str);
char* field;
char* sunset;
char* sunrise;
field = strtok(response,"{,");
while (field != NULL)
{
field = strtok (NULL, ",}");
if(field != NULL)
{
if(strstr(field, "sunrise") != NULL)
{
int length = strlen(field);
sunrise = new char[length + 1];
strncpy(sunrise, field, length + 1); // +1 to copy a terminating null as well
}
if(strstr(field, "sunset") != NULL)
{
int length = strlen(field);
sunset = new char[length + 1];
strncpy(sunset, field, length + 1); // +1 to copy a terminating null as well
}
}
}
//Serial.println("SUNRISE DATA: %s\n\n", sunrise);
//Serial.println("SUNSET DATA: %s\n\n", sunset);
free(sunrise); // do not forget to free the memory if not needed anymore
free(sunset); // do not forget to free the memory if not needed anymore
}
disconnect();
}
wait();
}
// Initialize Serial port
void initSerial() {
Serial.begin(BAUD_RATE);
while (!Serial) {
; // wait for serial port to initialize
}
Serial.println("Serial ready");
}
// Initialize Ethernet library
void initEthernet() {
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = { 192,168,0,202 };
if (!Ethernet.begin(mac)) {
Serial.println("Failed to configure Ethernet");
return;
}
Serial.println("Ethernet ready");
delay(1000);
}
// Open connection to the HTTP server
bool connect(const char* hostName) {
Serial.print("Connect to ");
Serial.println(hostName);
bool ok = client.connect(hostName, 80);
Serial.println(ok ? "Connected" : "Connection Failed!");
return ok;
}
// Send the HTTP GET request to the server
bool sendRequest(const char* host, const char* resource) {
Serial.print("GET ");
Serial.println(resource);
client.print("GET ");
client.print(resource);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
return true;
}
// Skip HTTP headers so that we are at the beginning of the response's body
bool skipResponseHeaders() {
// HTTP headers end with an empty line
char endOfHeaders[] = "\r\n\r\n";
client.setTimeout(HTTP_TIMEOUT);
bool ok = client.find(endOfHeaders);
if (!ok) {
Serial.println("No response or invalid response!");
}
return ok;
}
void disconnect() {
Serial.println("Disconnect");
client.stop();
}
// Pause for a 1 minute
void wait() {
Serial.println("Wait 60 seconds");
delay(60000);
}
I think there is a misunderstanding from your side. Certainly you want to process the response of the server and according to your code, this is char response[MAX_CONTENT_SIZE] where the response is stored.
Now this already is a string, more or less. An array of characters, chars. Definiton from here.
Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
You can extract the relevant parts from it straight away.
Your response should look like something like this, according to sunrise-sunset.org/api. Note that I just copied the data into an array for testing purposes.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CONTENT_SIZE 512
char response[MAX_CONTENT_SIZE] = \
"{\
\"results\":\
{\
\"sunrise\":\"7:27:02 AM\",\
\"sunset\":\"5:05:55 PM\",\
\"solar_noon\":\"12:16:28 PM\",\
\"day_length\":\"9:38:53\",\
\"civil_twilight_begin\":\"6:58:14 AM\",\
\"civil_twilight_end\":\"5:34:43 PM\",\
\"nautical_twilight_begin\":\"6:25:47 AM\",\
\"nautical_twilight_end\":\"6:07:10 PM\",\
\"astronomical_twilight_begin\":\"5:54:14 AM\",\
\"astronomical_twilight_end\":\"6:38:43 PM\"\
},\
\"status\":\"OK\"\
}";
You can easily process it using strtok function from string.h. Using a {, delimiter first will separate "result":{ from "sunrise .... Then you can use a }, delimiter.
With strstr you can check for "sunrise" and "sunset" field, and if you find them you can copy their value into a new string with strncpy.
int main()
{
char* field;
char* sunset;
char* sunrise;
field = strtok(response,"{,");
while (field != NULL)
{
field = strtok (NULL, ",}");
if(field != NULL)
{
if(strstr(field, "sunrise") != NULL)
{
int length = strlen(field);
sunrise = malloc(length * sizeof(char) + 1); // +1 for terminating null character '\0'
strncpy(sunrise, field, length + 1); // +1 to copy a terminating null as well
}
if(strstr(field, "sunset") != NULL)
{
int length = strlen(field);
sunset = malloc(length * sizeof(char) + 1); // +1 for terminating null character '\0'
strncpy(sunset, field, length + 1); // +1 to copy a terminating null as well
}
}
}
printf("SUNRISE DATA: %s\n\n", sunrise);
printf("SUNSET DATA: %s\n\n", sunset);
free(sunrise); // do not forget to free the memory if not needed anymore
free(sunset); // do not forget to free the memory if not needed anymore
return 0;
}
The output of this program is:
SUNRISE DATA: "sunrise":"7:27:02 AM"
SUNSET DATA: "sunset":"5:05:55 PM"
You can further process these strings with strtok again if you like. This is just an example code, you can use it to implement your solution.
I am relatively new to arduino and its programming though I have been plodding along and getting results. I have added a shortened version of my sketch which connects to my webserver and obtains the current temp for a particular city. I will then use that temp to set my heater also controlled by my arduino. The problem I am facing is I cant get my head around how to return to the main loop after I've read the data on webserver from the connectandread function. The sketch stops/halts after receiving the data(temp). Eventually I will be calling the function every 2 hours. So all in all I need to call the function then after it runs return to the main loop. I hope I've explained clearly enough.
byte server[] = { 192,168,0,2 }; //ip Address of the server you will connect to
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/index1.php HTTP/1.0";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
int convertedtemp;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
String responseString;
void setup() {
Ethernet.begin(mac);
delay( 1000 );
// Start the I2C interface
Wire.begin();
}
void ReadDS3231() {
int second,minute,hour,date,month,year,temperature;
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();
lcd.setCursor(0, 1);
}
void loop() {
connectAndRead();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h0 = dht0.readHumidity();
int h1 = dht1.readHumidity();
int t0 = dht0.readTemperature();
int t1 = dht1.readTemperature();
ReadDS3231(); delay(3000);
}
String connectAndRead() {
//connect to the server
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.print("GET ");
client.println(location);
client.println();
//Connected - Read the page
return readPage(); //go and read the output
} else {
return "connection failed";
}
}
String readPage() {
//read the page, and capture & return everything between '<' and '>'
stringPos = 0;
memset( &inString, 0, 32 ); //clear inString memory
while (true) {
if (client.available()) {
char c = client.read();
if (c == '<' ) { //'<' is our begining character
startRead = true; //Ready to start reading the part
} else if (startRead) {
if (c != '>') { //'>' is our ending character
inString[stringPos] = c;
stringPos ++;
} else {
//got what we need here! We can disconnect now
startRead = false;
//Serial.print(inString);
//convertedtemp = atoi(inString).c_str());
int val = atoi(inString);
//int convertedhumid = atoi(getValuesFromKey(responseString, "relative_humidity").c_str());
Serial.println(val);
client.stop();
client.flush();
Serial.println("disconnecting.");
//return inString;
//return ;
}
}
}
}
}
Well, at least the version of the sketch you have posted have an infinite loop in it... (while(true)).
Try un-commenting the line with return inString;.
Also unrelated, since you read data from external source, you need to stop reading after you have received 31 characters even if you have not received an ">". Otherwise, a bug or malicious code on the server can corrupt your memory.
I’ve a project for control a robot in arduino via android.
the trouble is when I press a button for pin 6 (Maju), its turn ON.
But when I press it again its always ON, cant turn OFF.
Here's my code:
int kananMaju;
int kiriMundur;
int kananMundur;
int kiriMaju;
#define Kanan A4
#define Kiri 13
#define Maju 6
#define Mundur A5
#define STBY 12
#define PWMA 11 //left
#define PWMB 10 //right
#define AIN2 8
#define AIN1 7
#define BIN1 3
#define BIN2 2
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(4,5); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
void setup()
{
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
pinMode(STBY,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(PWMA,OUTPUT);
digitalWrite(STBY, HIGH); //standby
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, HIGH);
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, HIGH);
analogWrite(PWMB, 100); //490 Hz
analogWrite(PWMA, 100); //490 Hz
pinMode(Maju,OUTPUT);
digitalWrite(Maju,LOW);
sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
sendCommand("AT+CWMODE=1\r\n",1000,DEBUG); // configure as access point
sendCommand("AT+CWJAP=\"PanduGanteng\",\"akhirpekan666\"\r\n",3000,DEBUG);
delay(10000);
sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendCommand("AT+CIPSERVER=1,8080\r\n",1000,DEBUG); // turn on server on port 80
Serial.println("Server Ready");
}
void loop()
{
if(esp8266.available()) // check if the esp is sending a message
{
if(esp8266.find("+IPD,"))
{
delay(1000); // wait for the serial buffer to fill up (read all the serial data)
// get the connection id so that we can then disconnect
int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
esp8266.find("pin="); // advance cursor to "pin="
int pinNumber = (esp8266.read()-48); // get first number i.e. if the pin 13 then the 1st number is 1
int secondNumber = (esp8266.read()-48);
if(secondNumber>=0 && secondNumber<=9)
{
pinNumber*=10;
pinNumber +=secondNumber; // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
}
digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin
// build string that is send back to device that is requesting pin toggle
String content;
content = "Pin ";
content += pinNumber;
content += " is ";
if(digitalRead(Maju))
{
content += "ON";
kiriMaju;{
digitalWrite(STBY,HIGH);
analogWrite(PWMA, 100);
digitalWrite(AIN1,HIGH);
digitalWrite(AIN2,LOW);
}
kananMaju;{
digitalWrite(STBY,HIGH);
analogWrite(PWMB, 100);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
}
}
else
{
content += "OFF";
}
sendHTTPResponse(connectionId,content);
// make close command
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendCommand(closeCommand,1000,DEBUG); // close connection
}
}
}
/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
int dataSize = command.length();
char data[dataSize];
command.toCharArray(data,dataSize);
esp8266.write(data,dataSize); // send the read character to the esp8266
if(debug)
{
Serial.println("\r\n====== HTTP Response From Arduino ======");
Serial.write(data,dataSize);
Serial.println("\r\n========================================");
}
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
/*
* Name: sendHTTPResponse
* Description: Function that sends HTTP 200, HTML UTF-8 response
*/
void sendHTTPResponse(int connectionId, String content)
{
// build HTTP response
String httpResponse;
String httpHeader;
// HTTP Header
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader +="Connection: close\r\n\r\n";
httpResponse = httpHeader + content + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
sendCIPData(connectionId,httpResponse);
}
/*
* Name: sendCIPDATA
* Description: sends a CIPSEND=<connectionId>,<data> command
*
*/
void sendCIPData(int connectionId, String data)
{
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=data.length();
cipSend +="\r\n";
sendCommand(cipSend,1000,DEBUG);
sendData(data,1000,DEBUG);
}
/*
* Name: sendCommand
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendCommand(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
I want to when I press button for pin 6 is ON, and when I press it again its OFF. But it's always turned ON, where's my fault?
Maju should be an input not an output
And you need to toggle a state
pinMode(Maju,INPUT);
static boolean function get_state()
{
static boolean previous = LOW;
static boolean state = LOW;
boolean current = digitalRead(Maju);
if (current == HIGH && current != previous) {
state = (state == LOW) ? HIGH : LOW;
}
previous = current;
return state;
}