-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
127 lines (113 loc) · 3.33 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* ESP8266 Norlinux Library.c
*
* Created: 02-01-2018
* Author : Norlinux
* Based in Source Code Piotr Styczy?ski
* http://www.microelectronic.pt
* http://maquina.96.lt
* https://www.facebook.com/MundoDosMicrocontroladores/
* Released under GPLv3.
* Please refer to LICENSE file for licensing information.
* which can be found at http://www.gnu.org/licenses/gpl.txt
*/
#define F_CPU 8000000UL /* Define CPU Frequency e.g. here its Ext. 8MHz */
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h> /* for sei() */
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdio.h> /* Include standard IO library */
#include <string.h> /* Include string library */
#include <stdlib.h> /* Include standard library */
#include "USART_V1.h" /* Include USART header file */
#include "lcd_lib.h" /* Include HD44780 header file */
#include "ESP8266.h" /* Include ESP8266 header file */
#include "Timer2.h"
#define Wait() _delay_ms(1800);
//Strings stored in AVR Flash memory
const uint8_t Arranque[] PROGMEM="ESP8266 DEMO ...\0";
char msgBox[18]={};
/* Wifi access point ussid and password (to connect to) */
char* USSID = "NAME-USSID";
char* PASSWORD = "pass-USSID";
/* ESP working structure and result enumeration */
ESP_Result espResp;
int main(void)
{
//char segundos=0;
//uint8_t esp_Result;
char msgData[160];
char hostString[68]={"Connection: keep-alive\r\nCache-Control: max-age=0\r\n\r\n\r\n"};
char Manobras=0,Energia=0;
/*Start Timer2*/
TimerInit();
/* Start LCD 16x2 */
LCDinit();
LCDStringtoLCD(Arranque,0,0);
if ((espResp = ESP_Begin(115200)) == espOK) {
LCDprintXY("ESP INIT OK...",0,0);
Wait();
}
else {sprintf(msgBox,"ESP INIT ERR:%d",espResp);
LCDprintXY(msgBox,0,0);
Wait();
}
if ((espResp =ESP_WIFIMode(ESP_Mode_STA,Single))== espOK)
{
LCDprintXY("STATION OK...",0,0); Wait();
}
else {sprintf(msgBox,"STATION ERRO:%d",espResp);
LCDprintXY(msgBox,0,0);
Wait();
}
if (ESP_WIFIConnect(USSID,PASSWORD)==espOK)
{
sprintf(msgBox,"WIFI %s",USSID);
LCDprintXY(msgBox,0,0);
Wait();
}else{
LCDprintXY("ERRO WIFI RESET",0,0);
Wait();
if (ESP_Restart()==espOK)
{
sprintf(msgBox,"WIFI %s",USSID);
LCDprintXY(msgBox,0,0);
Wait();
}else{
loop:
LCDprintXY("SOMETHING WRONG...",0,0);
Wait();
LCDprintXY("REMOVE ESP8266...",0,0);
Wait();
LCDprintXY("CHECK USSID....",0,0);
Wait();
LCDprintXY("CHECK PASSWORD..",0,0);
Wait();
goto loop;
}
}
while (1)
{
LCDclr();
LCDprintXY("BEGIN SEND DATA",0,0);
// Connect to host
Wait();
uart_flush();
Manobras=1+(rand() % 40); // For aleatory numbers
Energia=1+(rand() % 30); // For aleatory numbers
if (WIFI_Client_Connection("TCP", "maquina.96.lt", 80)==espOK)
{
sprintf(msgData,"GET /gravar.php?man=%d&energy=%d HTTP/1.1\r\nHost: maquina.96.lt\r\n%s",Manobras,Energia,hostString);
WIFI_Send(msgData);
}
else
{
LCDclr();
LCDprintXY("ERROR OPEN URL",0,0);
Wait();
}
// Check data in http://maquina.96.lt/default.php
while(timeout_event(25));
}
}