I will connect to WiFi and transfer value of latitude and longitude and course with GPS shield.
Also, I will display some information on OLED LCD screen. There’s problem here.
I noticed the problem after I had used u8g.firstPage(); and do ~ while with httpRequest(); together.
If I use them at the same time, all values that are on the screen will be zero after HTTP is requested.
So, I removed u8g.firstPage(); and do ~ while section and tried again. I saw it went well.
How can I go through this?? I would like to use them at the same time.
Any help would be great!! thanks!
Here is my all source code.
#include <SPI.h>
#include <WiFi.h>
#include <TinyGPS.h>
#include "U8glib.h"
TinyGPS gps;
float flat, flon, fspeed, fcourse;
char ssid[] = "G2_3711";
char pass[] = "12345678";
//my network key Index number(needed only for WEP, usually it's 0)
int keyIndex = 0;
int status = WL_IDLE_STATUS;
char server[] = "kic0326.dothome.co.kr";
WiFiClient client;
// OLED DISPLAY VALUES
unsigned int cnt = 0, steps = 0, second = 0, minute = 0;
double cal = 0.0;
String p = "GET /dbconfig.php?latitude=";
String p2 = "&longitude=";
String p3 = "&course=";
String p4 = " HTTP/1.1";
void setup()
{
Serial.begin(115200);
Serial1.begin(9600);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.println("Connected to wifi");
Serial.println("\nStarting connection to server...");
}
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
void loop()
{
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
for (unsigned long start = millis(); millis() - start < 20;)
{
while (Serial1.available())
{
char c = Serial1.read();
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c))
newData = true;
}
}
if(newData)
{
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
fspeed = gps.f_speed_kmph();
fcourse = gps.f_course();
Serial.print("********************************************** ");
Serial.print("LAT=");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
Serial.print(" LON=");
Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
Serial.print(" SPEED=");
Serial.print(fspeed);
Serial.print(" Course=");
Serial.print(fcourse);
Serial.println(" **********************************************");
}
gps.stats(&chars, &sentences, &failed);
Serial.print(" CHARS=");
Serial.print(chars);
Serial.print(" SENTENCES=");
Serial.print(sentences);
Serial.print(" CSUM ERR=");
Serial.println(failed);
if (chars == 0){
Serial.println("** No characters received from GPS: check wiring **");
}
cnt++;
if(fspeed <= 130.0)
cal = 0.00175 * (( 0.1 * fspeed + 3.5) / 3.5) * 60 * cnt/25;
else if(fspeed > 130.0)
cal = 0.00175 * (( 0.2 * fspeed + 3.5) / 3.5) * 60 * cnt/25;
if(cnt % 25 == 0)
second++;
if(second % 60 == 0)
second = 0;
if(cnt % 1500 == 0)
minute++;
if(minute % 60 == 0)
minute = 0;
Serial.println();
Serial.print("Time : ");
Serial.print(minute);
Serial.print(" : ");
Serial.println(second);
Serial.print("Steps : ");
Serial.println(steps);
Serial.print("Speed : ");
Serial.print(fspeed);
Serial.println(" km/h");
Serial.print("Cal : ");
Serial.print(cal);
Serial.println();
//******************************** Problem ****************************************
u8g.firstPage();
do{
printText();
}while( u8g.nextPage() );
if(cnt % 400 == 0){
httpRequest();
}
//******************************** Problem ****************************************
}
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
client.connect(server, 80);
Serial.println("connected to server");
// Make a HTTP request:
client.print(p);
client.print(flat, 6);
client.print(p2);
client.print(flon, 6);
client.print(p3);
client.print(fcourse, 6);
client.println(p4);
client.println("Host: kic0326.dothome.co.kr");
client.println("Connection: close");
client.println();
}
void printText() {
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(1,10);
u8g.print("Time : ");
u8g.print(minute);
u8g.print(" : ");
u8g.print(second);
u8g.setPrintPos(1,30);
u8g.print("Steps: ");
u8g.print(steps);
u8g.setPrintPos(1,45);
u8g.print("Speed: ");
u8g.print(fspeed);
u8g.print(" km/h");
u8g.setPrintPos(1,60);
u8g.print("Cal : ");
u8g.print(cal, 2);
}