WeMOS: AnalogRead A0 + UDP send + 7 segmen

From OnnoWiki
Jump to navigation Jump to search
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <TM1650.h>

TM1650 d;

/* WiFi network name and password */
const char * ssid = "HUAWEI-1A73";
const char * pwd = "52408495";

// IP address to send UDP data to.
// it can be ip address of the server or 
// a network broadcast address
// here is broadcast address
const char * udpAddress = "192.168.8.102";
const int udpPort = 8888;

//create UDP instance
WiFiUDP udp;

void setup(){
  Wire.begin(); //Join the bus as master
  Serial.begin(115200);
  
  //Connect to the WiFi network
  WiFi.begin(ssid, pwd);
  Serial.println("");   

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  //This initializes udp and transfer buffer
  udp.begin(udpPort);
  d.init();  
} 

void loop() {
  int sensorReading = analogRead(A0); 
  char buffer[5] = "    ";
  
  udp.beginPacket(udpAddress, udpPort);
  Serial.println(sensorReading);
  d.displayOn();
  d.setBrightnessGradually(TM1650_MAX_BRIGHT);
  d.displayString(itoa(sensorReading,buffer,10));
  udp.printf("%4d\n", sensorReading);
  udp.endPacket();
  delay(500);
}


Pranala Menarik