Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This sketch sends data via HTTP GET requests to data.sparkfun.com service.
- You need to get streamId and privateKey at data.sparkfun.com and paste them
- below. Or just customize this script to talk to other HTTP servers.
- */
- #include <ESP8266WiFi.h>
- #include <LiquidCrystal.h>
- #include <PubSubClient.h>
- const char* ssid = "embarcados";
- const char* password = "embarcados";
- const char* host = "10.30.152.13";
- const char* mqtt_server = "10.30.152.111";
- const char* mqtt_user = "embarcados";
- const char* mqtt_pass = "embarcados";
- char* command;
- char vetor[30];
- char* temp;
- char* umid;
- int intense = 0;
- IPAddress ip;
- typedef struct
- {
- int ip[4];
- float temp,umid;
- } estrutura;
- WiFiClient client; //Para servidor vai ser WiFiServer...
- PubSubClient pubsubclient(client);
- void setup() {
- Serial.begin(115200);
- delay(10);
- pinMode(2,OUTPUT); //Pino para o servo motor
- pinMode(4, OUTPUT); // led
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- setup_wifi();
- pubsubclient.setServer(mqtt_server,1883);
- pubsubclient.setCallback(callback);
- display_data(0,0,ip);
- }
- int value = 0;
- int i,ldr=0;
- estrutura dados;
- void loop() {
- delay(5000);
- ++value;
- ldr = analogRead(12);
- Serial.print(ldr);
- Serial.println("adc");
- intense = (le_adc()*100/1023);
- Serial.print(intense);
- /* teste led fade
- Serial.println("10 % PWM");
- analogWrite(4,102);
- delay(2000);
- TESTE LED FADE
- Serial.println("20 % PWM");
- analogWrite(4,205);
- delay(2000);
- Serial.println("40 % PWM");
- analogWrite(4,410);
- delay(2000);
- Serial.println("70 % PWM");
- analogWrite(4,714);
- delay(2000);
- Serial.println("100 % PWM");
- analogWrite(4,1024);
- delay(2000);
- */
- Serial.print("connecting to ");
- Serial.println(host);
- // Use WiFiClient class to create TCP connections
- const int httpPort = 32000;
- if (!client.connect(host, httpPort)) {
- Serial.println("connection failed");
- return;
- }
- ip = WiFi.localIP();
- display_data(temp,umid,ip);
- for(i=0; i<4;i++){
- dados.ip[i] = ip[i];
- }
- Serial.print(intense);
- sprintf(vetor, "%dL",intense);
- pubsubclient.publish("ldr_espqt_guilherme",vetor);
- pubsubclient.subscribe("servo_esp_guilherme");
- pubsubclient.subscribe("temp_esp_guilherme");
- pubsubclient.subscribe("umid_esp_guilherme");
- pubsubclient.subscribe("led_intense_guilherme");
- // This will send the request to the server
- //client.print("Ola servidor"); //Manda dados etc para o servidor
- unsigned long timeout = millis();
- while (client.available() == 0) {
- if (millis() - timeout > 5000) {
- Serial.println(">>> Client Timeout !");
- client.stop();
- return;
- }
- }
- int i;
- for(i=0; i<20;i++){
- digitalWrite(2,HIGH);
- delay(1);
- digitalWrite(2,LOW);
- delay(19);
- }
- // Read all the lines of the reply from server and print them to Serial
- while (client.available()) {
- String line = client.readStringUntil('.');
- Serial.print(line); //Resposta do servidor
- }
- Serial.println();
- Serial.println("closing connection");
- }
- int le_adc(){
- return analogRead(0);
- }
- void display_data(char* temp, char* umidade,IPAddress ip){
- const int rs = 0, en = 15, d4 = 13, d5 = 12 , d6 = 14, d7 = 16; // temque mudar
- LiquidCrystal lcd(rs,en,d4,d5,d6,d7); // config
- lcd.begin(16,2);
- lcd.setCursor(0,0); // zero 1
- lcd.print("IP:");
- lcd.print(ip.toString());
- lcd.setCursor(0,1);
- lcd.print("temp ");
- lcd.print(temp);
- lcd.print("um ");
- lcd.print(umidade);
- }
- void setup_wifi(){
- /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
- would try to act as both a client and an access-point and could cause
- network-issues with your other WiFi-devices on your WiFi-network. */
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- ip = WiFi.localIP();
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- if(topic[strlen(topic)-1] == 'T'){
- memcpy(topic,temp,sizeof(topic));
- }
- if(topic[strlen(topic)-1] == 'U'){
- memcpy(topic,umid,sizeof(topic));
- }
- else memcpy(topic,command,sizeof(topic));
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- memcpy(&command, &payload, sizeof(payload));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement